Skip to content

Commit c1653ef

Browse files
authored
fix: resolve more alias issues (#14514)
* fix: resolve more alias issues - split up regexes to make them easier to reason about - fix some more cases - turn slow e2e test into unit test which can test more combinations easily Fixes #14455 * also fixes #14480 * remove
1 parent 9c33b91 commit c1653ef

File tree

27 files changed

+127
-192
lines changed

27 files changed

+127
-192
lines changed

.changeset/sour-bananas-tease.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/package': patch
3+
---
4+
5+
fix: resolve more alias issues

packages/package/src/utils.js

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,12 @@ export function resolve_aliases(input, file, content, aliases) {
2222
*/
2323
const replace_import_path = (match, quote, import_path) => {
2424
for (const [alias, value] of Object.entries(aliases)) {
25-
if (import_path !== alias && !import_path.startsWith(alias + '/')) continue;
25+
if (
26+
import_path !== alias &&
27+
!import_path.startsWith(alias + (alias.endsWith('/') ? '' : '/'))
28+
) {
29+
continue;
30+
}
2631

2732
const full_path = path.join(input, file);
2833
const full_import_path = path.join(value, import_path.slice(alias.length));
@@ -33,24 +38,22 @@ export function resolve_aliases(input, file, content, aliases) {
3338
return match;
3439
};
3540

36-
// import/export ... from ...
41+
// import/export (type) (xxx | xxx,) { ... } from ...
3742
content = content.replace(
38-
// Regex parts for import/export ... from ... statements:
39-
// 1. \b(import|export) - Match 'import' or 'export' at a word boundary
40-
// 2. (?:\s+type)? - Optionally match ' type'
41-
// 3. \s+ - At least one whitespace
42-
// 4. ( - Start of specifier group
43-
// (?:(?:\*\s+as\s+)?\p{L}[\p{L}0-9]*\s+) - default import/export, e.g. 'name', or named star import/export, e.g. '* as name '
44-
// | (?:\*\s+) - e.g. star import/export, e.g. '* '
45-
// | (?:(?:\p{L}[\p{L}0-9]*\s*,\s*)?\{[^}]*\}\s*) - e.g. named imports/exports with optional default import/export, e.g. 'name, { ... }' or '{ ... }'
46-
// )
47-
// 5. from\s* - Match 'from' with optional whitespace
48-
// 6. (['"]) - Capture quote
49-
// 7. ([^'";]+) - Capture import path
50-
// 8. \3 - Match the same quote as before
51-
/\b(import|export)(?:\s+type)?\s+((?:(?:\*\s+as\s+)?\p{L}[\p{L}0-9]*\s+)|(?:\*\s+)|(?:(?:\p{L}[\p{L}0-9]*\s*,\s*)?\{[^}]*\}\s*))from\s*(['"])([^'";]+)\3/gmu,
52-
(match, _keyword, _specifier, quote, import_path) =>
53-
replace_import_path(match, quote, import_path)
43+
/\b(?:import|export)(?:\s+type)?(?:(?:\s+\p{L}[\p{L}0-9]*\s+)|(?:(?:\s+\p{L}[\p{L}0-9]*\s*,\s*)?\s*\{[^}]*\}\s*))from\s*(['"])([^'";]+)\1/gmu,
44+
(match, quote, import_path) => replace_import_path(match, quote, import_path)
45+
);
46+
47+
// import/export (type) * as xxx from ...
48+
content = content.replace(
49+
/\b(?:import|export)(?:\s+type)?\s*\*\s*as\s+\p{L}[\p{L}0-9]*\s+from\s*(['"])([^'";]+)\1/gmu,
50+
(match, quote, import_path) => replace_import_path(match, quote, import_path)
51+
);
52+
53+
// export (type) * from ...
54+
content = content.replace(
55+
/\b(?:export)(?:\s+type)?\s*\*\s*from\s*(['"])([^'";]+)\1/gmu,
56+
(match, quote, import_path) => replace_import_path(match, quote, import_path)
5457
);
5558

5659
// import(...)

packages/package/test/fixtures/resolve-alias/expected/Test.svelte

Lines changed: 0 additions & 8 deletions
This file was deleted.

packages/package/test/fixtures/resolve-alias/expected/Test.svelte.d.ts

Lines changed: 0 additions & 20 deletions
This file was deleted.

packages/package/test/fixtures/resolve-alias/expected/baz.d.ts

Lines changed: 0 additions & 5 deletions
This file was deleted.

packages/package/test/fixtures/resolve-alias/expected/baz.js

Lines changed: 0 additions & 2 deletions
This file was deleted.

packages/package/test/fixtures/resolve-alias/expected/index.d.ts

Lines changed: 0 additions & 10 deletions
This file was deleted.

packages/package/test/fixtures/resolve-alias/expected/index.js

Lines changed: 0 additions & 9 deletions
This file was deleted.

packages/package/test/fixtures/resolve-alias/expected/not.d.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

packages/package/test/fixtures/resolve-alias/expected/not.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)