Skip to content

Commit 780ea38

Browse files
authored
fix: more alias resolution fixes (#14433)
* fix: handle `import/export * (as ...)` when resolving aliases #14413 (comment) * while we're here
1 parent fc7bce6 commit 780ea38

File tree

6 files changed

+41
-2
lines changed

6 files changed

+41
-2
lines changed

.changeset/small-ghosts-fail.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: handle `import/export * (as ...)` when resolving aliases

.changeset/witty-ties-brush.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: prevent false-positive alias replacement

packages/package/src/utils.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ 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.startsWith(alias)) continue;
25+
if (import_path !== alias && !import_path.startsWith(alias + '/')) continue;
2626

2727
const full_path = path.join(input, file);
2828
const full_import_path = path.join(value, import_path.slice(alias.length));
@@ -35,7 +35,20 @@ export function resolve_aliases(input, file, content, aliases) {
3535

3636
// import/export ... from ...
3737
content = content.replace(
38-
/\b(import|export)(?:\s+type)?\s+((?:\p{L}[\p{L}0-9]*\s+)|(?:\{[^}]*\}\s*))from\s*(['"])([^'";]+)\3/gmu,
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+
// | (?:\{[^}]*\}\s*) - e.g. named imports/exports, e.g. '{ ... }'
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+)|(?:\{[^}]*\}\s*))from\s*(['"])([^'";]+)\3/gmu,
3952
(match, _keyword, _specifier, quote, import_path) =>
4053
replace_import_path(match, quote, import_path)
4154
);
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
11
export { default as Test } from './Test.svelte';
2+
export * from "./sub/foo";
3+
export type * from "./sub/bar";
4+
import * as Utils from "./utils/index";
5+
export { Utils };
6+
export { X } from '$libre';
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
11
export { default as Test } from './Test.svelte';
2+
export * from "./sub/foo";
3+
import * as Utils from "./utils/index";
4+
export { Utils}
5+
// @ts-expect-error
6+
export { X } from '$libre';
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,7 @@
11
export { default as Test } from '$lib/Test.svelte';
2+
export * from '$lib/sub/foo';
3+
export type * from '$lib/sub/bar';
4+
import * as Utils from '$lib/utils/index';
5+
export { Utils };
6+
// @ts-expect-error
7+
export { X } from '$libre';

0 commit comments

Comments
 (0)