Skip to content

Commit

Permalink
fix: svelte-package resolve alias (#13351)
Browse files Browse the repository at this point in the history
  • Loading branch information
Black1358 authored Jan 20, 2025
1 parent f5103c6 commit 2ca09ce
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/many-gorillas-shout.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/package': patch
---

fix: resolve aliases more robustly
25 changes: 21 additions & 4 deletions packages/package/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export declare const notImportFromLib: () => string;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const notImportFromLib = () => " from '$lib/Test.svelte'";
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export declare const dynamic: () => Promise<typeof import('../Test.svelte')>;
export declare const bar1: import('./foo').Foo;
export declare const bar2: import('../baz').Baz;
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const notImportFromLib = () => " from '$lib/Test.svelte'";
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 2ca09ce

Please sign in to comment.