Skip to content

Commit 2ca09ce

Browse files
authored
fix: svelte-package resolve alias (#13351)
1 parent f5103c6 commit 2ca09ce

File tree

8 files changed

+32
-4
lines changed

8 files changed

+32
-4
lines changed

.changeset/many-gorillas-shout.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 aliases more robustly

packages/package/src/utils.js

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,9 @@ const is_svelte_5_plus = Number(VERSION.split('.')[0]) >= 5;
1717
export function resolve_aliases(input, file, content, aliases) {
1818
/**
1919
* @param {string} match
20-
* @param {string} _
2120
* @param {string} import_path
2221
*/
23-
const replace_import_path = (match, _, import_path) => {
22+
const replace_import_path = (match, import_path) => {
2423
for (const [alias, value] of Object.entries(aliases)) {
2524
if (!import_path.startsWith(alias)) continue;
2625

@@ -33,8 +32,26 @@ export function resolve_aliases(input, file, content, aliases) {
3332
return match;
3433
};
3534

36-
content = content.replace(/from\s+('|")([^"';,]+?)\1/g, replace_import_path);
37-
content = content.replace(/import\s*\(\s*('|")([^"';,]+?)\1\s*\)/g, replace_import_path);
35+
// import/export ... from ...
36+
content = content.replace(
37+
/\b(import|export)\s+([\w*\s{},]*)\s+from\s+(['"])([^'";]+)\3/g,
38+
(_, keyword, specifier, quote, import_path) =>
39+
replace_import_path(
40+
`${keyword} ${specifier} from ${quote}${import_path}${quote}`,
41+
import_path
42+
)
43+
);
44+
45+
// import(...)
46+
content = content.replace(/\bimport\s*\(\s*(['"])([^'";]+)\1\s*\)/g, (_, quote, import_path) =>
47+
replace_import_path(`import(${quote}${import_path}${quote})`, import_path)
48+
);
49+
50+
// import '...'
51+
content = content.replace(/\bimport\s+(['"])([^'";]+)\1/g, (_, quote, import_path) =>
52+
replace_import_path(`import ${quote}${import_path}${quote}`, import_path)
53+
);
54+
3855
return content;
3956
}
4057

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export declare const notImportFromLib: () => string;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const notImportFromLib = () => " from '$lib/Test.svelte'";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
export declare const dynamic: () => Promise<typeof import('../Test.svelte')>;
12
export declare const bar1: import('./foo').Foo;
23
export declare const bar2: import('../baz').Baz;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { baz } from '../baz';
22
import { foo } from './foo';
3+
export const dynamic = () => import('../Test.svelte');
34
export const bar1 = foo;
45
export const bar2 = baz;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const notImportFromLib = () => " from '$lib/Test.svelte'";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { baz } from '$lib/baz';
22
import { foo } from '$lib/sub/foo';
33

4+
export const dynamic = () => import('$lib/Test.svelte');
45
export const bar1 = foo;
56
export const bar2 = baz;

0 commit comments

Comments
 (0)