Skip to content

fix: svelte-package resolve alias #13351

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Loading