From 6f850b01966d65f3eaeb799c37fadad36f3b916d Mon Sep 17 00:00:00 2001 From: Black1358 <122863429+Black1358@users.noreply.github.com> Date: Mon, 20 Jan 2025 17:48:12 +0300 Subject: [PATCH 1/5] feat: upgrade resolve-alias test in svelte-package --- packages/package/test/fixtures/resolve-alias/expected/not.d.ts | 1 + packages/package/test/fixtures/resolve-alias/expected/not.js | 1 + packages/package/test/fixtures/resolve-alias/src/lib/not.ts | 1 + 3 files changed, 3 insertions(+) create mode 100644 packages/package/test/fixtures/resolve-alias/expected/not.d.ts create mode 100644 packages/package/test/fixtures/resolve-alias/expected/not.js create mode 100644 packages/package/test/fixtures/resolve-alias/src/lib/not.ts diff --git a/packages/package/test/fixtures/resolve-alias/expected/not.d.ts b/packages/package/test/fixtures/resolve-alias/expected/not.d.ts new file mode 100644 index 000000000000..24bce3e70b9b --- /dev/null +++ b/packages/package/test/fixtures/resolve-alias/expected/not.d.ts @@ -0,0 +1 @@ +export declare const notImportFromLib: () => string; diff --git a/packages/package/test/fixtures/resolve-alias/expected/not.js b/packages/package/test/fixtures/resolve-alias/expected/not.js new file mode 100644 index 000000000000..a887dba63d18 --- /dev/null +++ b/packages/package/test/fixtures/resolve-alias/expected/not.js @@ -0,0 +1 @@ +export const notImportFromLib = () => `from '$lib/Test.svelte''`; diff --git a/packages/package/test/fixtures/resolve-alias/src/lib/not.ts b/packages/package/test/fixtures/resolve-alias/src/lib/not.ts new file mode 100644 index 000000000000..a887dba63d18 --- /dev/null +++ b/packages/package/test/fixtures/resolve-alias/src/lib/not.ts @@ -0,0 +1 @@ +export const notImportFromLib = () => `from '$lib/Test.svelte''`; From 47a3a462768563b7fe9fd3ae96f3b62878afdfd3 Mon Sep 17 00:00:00 2001 From: Black1358 <122863429+Black1358@users.noreply.github.com> Date: Mon, 20 Jan 2025 19:19:32 +0300 Subject: [PATCH 2/5] fix: svelte-package resolving alias --- packages/package/src/utils.js | 25 ++++++++++++++++--- .../fixtures/resolve-alias/expected/not.js | 2 +- .../resolve-alias/expected/sub/bar.d.ts | 5 ++-- .../resolve-alias/expected/sub/bar.js | 1 + .../fixtures/resolve-alias/src/lib/not.ts | 2 +- .../fixtures/resolve-alias/src/lib/sub/bar.ts | 1 + 6 files changed, 28 insertions(+), 8 deletions(-) diff --git a/packages/package/src/utils.js b/packages/package/src/utils.js index 0c5f25736620..7262a32ba9b5 100644 --- a/packages/package/src/utils.js +++ b/packages/package/src/utils.js @@ -17,10 +17,9 @@ const is_svelte_5_plus = Number(VERSION.split('.')[0]) >= 5; export function resolve_aliases(input, file, content, aliases) { /** * @param {string} match - * @param {string} _ * @param {string} import_path */ - const replace_import_path = (match, _, import_path) => { + const replace_import_path = (match, import_path) => { for (const [alias, value] of Object.entries(aliases)) { if (!import_path.startsWith(alias)) continue; @@ -33,8 +32,26 @@ export function resolve_aliases(input, file, content, aliases) { return match; }; - content = content.replace(/from\s+('|")([^"';,]+?)\1/g, replace_import_path); - content = content.replace(/import\s*\(\s*('|")([^"';,]+?)\1\s*\)/g, replace_import_path); + // import/export ... from ... + content = content.replace( + /\b(import|export)\s+([\w*\s{},]*)\s+from\s+(['"])([^'";]+)\3/g, + (_, keyword, specifier, quote, import_path) => + replace_import_path( + `${keyword} ${specifier} from ${quote}${import_path}${quote}`, + import_path + ) + ); + + // import(...) + content = content.replace(/\bimport\s*\(\s*(['"])([^'";]+)\1\s*\)/g, (_, quote, import_path) => + replace_import_path(`import(${quote}${import_path}${quote})`, import_path) + ); + + // import '...' + content = content.replace(/\bimport\s+(['"])([^'";]+)\1/g, (_, quote, import_path) => + replace_import_path(`import ${quote}${import_path}${quote}`, import_path) + ); + return content; } diff --git a/packages/package/test/fixtures/resolve-alias/expected/not.js b/packages/package/test/fixtures/resolve-alias/expected/not.js index a887dba63d18..ebb522bcef22 100644 --- a/packages/package/test/fixtures/resolve-alias/expected/not.js +++ b/packages/package/test/fixtures/resolve-alias/expected/not.js @@ -1 +1 @@ -export const notImportFromLib = () => `from '$lib/Test.svelte''`; +export const notImportFromLib = () => " from '$lib/Test.svelte'"; diff --git a/packages/package/test/fixtures/resolve-alias/expected/sub/bar.d.ts b/packages/package/test/fixtures/resolve-alias/expected/sub/bar.d.ts index 9f1a49e9dfd6..009dfdc8d3dd 100644 --- a/packages/package/test/fixtures/resolve-alias/expected/sub/bar.d.ts +++ b/packages/package/test/fixtures/resolve-alias/expected/sub/bar.d.ts @@ -1,2 +1,3 @@ -export declare const bar1: import('./foo').Foo; -export declare const bar2: import('../baz').Baz; +export declare const dynamic: () => Promise; +export declare const bar1: import("./foo").Foo; +export declare const bar2: import("../baz").Baz; diff --git a/packages/package/test/fixtures/resolve-alias/expected/sub/bar.js b/packages/package/test/fixtures/resolve-alias/expected/sub/bar.js index 17941b746387..1402159c02e4 100644 --- a/packages/package/test/fixtures/resolve-alias/expected/sub/bar.js +++ b/packages/package/test/fixtures/resolve-alias/expected/sub/bar.js @@ -1,4 +1,5 @@ import { baz } from '../baz'; import { foo } from './foo'; +export const dynamic = () => import("../Test.svelte"); export const bar1 = foo; export const bar2 = baz; diff --git a/packages/package/test/fixtures/resolve-alias/src/lib/not.ts b/packages/package/test/fixtures/resolve-alias/src/lib/not.ts index a887dba63d18..ebb522bcef22 100644 --- a/packages/package/test/fixtures/resolve-alias/src/lib/not.ts +++ b/packages/package/test/fixtures/resolve-alias/src/lib/not.ts @@ -1 +1 @@ -export const notImportFromLib = () => `from '$lib/Test.svelte''`; +export const notImportFromLib = () => " from '$lib/Test.svelte'"; diff --git a/packages/package/test/fixtures/resolve-alias/src/lib/sub/bar.ts b/packages/package/test/fixtures/resolve-alias/src/lib/sub/bar.ts index 335e08c551d3..c1d073daa6d0 100644 --- a/packages/package/test/fixtures/resolve-alias/src/lib/sub/bar.ts +++ b/packages/package/test/fixtures/resolve-alias/src/lib/sub/bar.ts @@ -1,5 +1,6 @@ import { baz } from '$lib/baz'; import { foo } from '$lib/sub/foo'; +export const dynamic = () => import('$lib/Test.svelte'); export const bar1 = foo; export const bar2 = baz; From ed4752de39c848d7f897d800179ccbb611ec8cc4 Mon Sep 17 00:00:00 2001 From: Black1358 <122863429+Black1358@users.noreply.github.com> Date: Mon, 20 Jan 2025 19:31:56 +0300 Subject: [PATCH 3/5] create changeset --- .changeset/many-gorillas-shout.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/many-gorillas-shout.md diff --git a/.changeset/many-gorillas-shout.md b/.changeset/many-gorillas-shout.md new file mode 100644 index 000000000000..8e69fd639dc2 --- /dev/null +++ b/.changeset/many-gorillas-shout.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/package': minor +--- + +fix resolving alias From fd1869540a319f68ca3be9e7507b17c09fc193c3 Mon Sep 17 00:00:00 2001 From: Black1358 <122863429+Black1358@users.noreply.github.com> Date: Mon, 20 Jan 2025 20:03:32 +0300 Subject: [PATCH 4/5] chore: formating --- .../test/fixtures/resolve-alias/expected/sub/bar.d.ts | 6 +++--- .../package/test/fixtures/resolve-alias/expected/sub/bar.js | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/package/test/fixtures/resolve-alias/expected/sub/bar.d.ts b/packages/package/test/fixtures/resolve-alias/expected/sub/bar.d.ts index 009dfdc8d3dd..fac8707fc2ad 100644 --- a/packages/package/test/fixtures/resolve-alias/expected/sub/bar.d.ts +++ b/packages/package/test/fixtures/resolve-alias/expected/sub/bar.d.ts @@ -1,3 +1,3 @@ -export declare const dynamic: () => Promise; -export declare const bar1: import("./foo").Foo; -export declare const bar2: import("../baz").Baz; +export declare const dynamic: () => Promise; +export declare const bar1: import('./foo').Foo; +export declare const bar2: import('../baz').Baz; diff --git a/packages/package/test/fixtures/resolve-alias/expected/sub/bar.js b/packages/package/test/fixtures/resolve-alias/expected/sub/bar.js index 1402159c02e4..6c2ec176b703 100644 --- a/packages/package/test/fixtures/resolve-alias/expected/sub/bar.js +++ b/packages/package/test/fixtures/resolve-alias/expected/sub/bar.js @@ -1,5 +1,5 @@ import { baz } from '../baz'; import { foo } from './foo'; -export const dynamic = () => import("../Test.svelte"); +export const dynamic = () => import('../Test.svelte'); export const bar1 = foo; export const bar2 = baz; From 5bd67d97d0e952415fc4ccc1382b59903fad26bd Mon Sep 17 00:00:00 2001 From: Simon H <5968653+dummdidumm@users.noreply.github.com> Date: Mon, 20 Jan 2025 20:07:17 +0100 Subject: [PATCH 5/5] Update .changeset/many-gorillas-shout.md --- .changeset/many-gorillas-shout.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.changeset/many-gorillas-shout.md b/.changeset/many-gorillas-shout.md index 8e69fd639dc2..50f23660ae7f 100644 --- a/.changeset/many-gorillas-shout.md +++ b/.changeset/many-gorillas-shout.md @@ -1,5 +1,5 @@ --- -'@sveltejs/package': minor +'@sveltejs/package': patch --- -fix resolving alias +fix: resolve aliases more robustly