Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add relative path prefix to bare internal specifiers #236

Merged
merged 5 commits into from
Jul 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## 0.30.1

- fix conversion of absolute specifiers to include `./` if bare
([#236](https://github.com/feltcoop/gro/pull/236))
- upgrade to esbuild@0.12.15
([#235](https://github.com/feltcoop/gro/pull/235))

Expand Down
37 changes: 26 additions & 11 deletions src/build/postprocess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,9 @@ export const postprocess = (
const is_external_import = is_external_module(specifier);
const is_external_imported_by_external = source.id === EXTERNALS_SOURCE_ID;
const is_external = is_external_import || is_external_imported_by_external;
let mapped_specifier = is_external
? to_build_extension(specifier)
: hack_to_build_extension_with_possibly_extensionless_specifier(specifier);
let mapped_specifier: string;
if (is_external) {
mapped_specifier = to_build_extension(specifier);
if (is_external_import) {
// handle regular externals
if (browser) {
Expand Down Expand Up @@ -89,14 +88,9 @@ export const postprocess = (
}
} else {
// internal import
// support absolute import patterns -- TODO modularize
if (mapped_specifier.startsWith(MODULE_PATH_LIB_PREFIX)) {
mapped_specifier = relative(source.dir, paths.source + mapped_specifier.substring(1));
final_specifier = relative(source.dir, paths.source + final_specifier.substring(1));
} else if (mapped_specifier.startsWith(MODULE_PATH_SRC_PREFIX)) {
mapped_specifier = relative(source.dir, paths.source + mapped_specifier.substring(3));
final_specifier = relative(source.dir, paths.source + final_specifier.substring(3));
}
final_specifier = to_relative_specifier(final_specifier, source.dir, paths.source);
mapped_specifier =
hack_to_build_extension_with_possibly_extensionless_specifier(final_specifier);
build_id = join(build.dir, mapped_specifier);
}
if (dependencies_by_build_id === null) dependencies_by_build_id = new Map();
Expand Down Expand Up @@ -161,6 +155,27 @@ export const postprocess = (
}
};

// Maps absolute `$lib/` and `src/` imports to relative specifiers.
const to_relative_specifier = (specifier: string, dir: string, source_dir: string): string => {
if (specifier.startsWith(MODULE_PATH_LIB_PREFIX)) {
specifier = to_relative_specifier_trimmed_by(1, specifier, dir, source_dir);
} else if (specifier.startsWith(MODULE_PATH_SRC_PREFIX)) {
specifier = to_relative_specifier_trimmed_by(3, specifier, dir, source_dir);
}
return specifier;
};

const to_relative_specifier_trimmed_by = (
chars_to_trim: number,
specifier: string,
dir: string,
source_dir: string,
): string => {
specifier = relative(dir, source_dir + specifier.substring(chars_to_trim));
if (specifier[0] !== '.') specifier = './' + specifier;
return specifier;
};

const TYPE_IMPORT_MATCHER = /^import type [\s\S]*? from '(.+)';$/gm;

const parse_type_imports = (content: string): string[] =>
Expand Down