Skip to content
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/pink-pots-read.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/package': patch
---

fix: better alias resolution
6 changes: 3 additions & 3 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ export default [
'**/.custom-out-dir',
'packages/adapter-*/files',
'packages/kit/src/core/config/fixtures/multiple', // dir contains svelte config with multiple extensions tripping eslint
'packages/package/test/fixtures/typescript-svelte-config/expected'
'packages/package/test/fixtures/typescript-svelte-config/expected',
'packages/package/test/errors/**/*',
'packages/package/test/fixtures/**/*'
]
},
{
Expand All @@ -43,8 +45,6 @@ export default [
'packages/kit/test/apps/**/*',
'packages/kit/test/build-errors/**/*',
'packages/kit/test/prerendering/**/*',
'packages/package/test/errors/**/*',
'packages/package/test/fixtures/**/*',
'packages/test-redirect-importer/index.js'
]
}
Expand Down
23 changes: 11 additions & 12 deletions packages/package/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,39 +17,38 @@ const is_svelte_5_plus = Number(VERSION.split('.')[0]) >= 5;
export function resolve_aliases(input, file, content, aliases) {
/**
* @param {string} match
* @param {string} quote
* @param {string} import_path
*/
const replace_import_path = (match, import_path) => {
const replace_import_path = (match, quote, import_path) => {
for (const [alias, value] of Object.entries(aliases)) {
if (!import_path.startsWith(alias)) continue;

const full_path = path.join(input, file);
const full_import_path = path.join(value, import_path.slice(alias.length));
let resolved = posixify(path.relative(path.dirname(full_path), full_import_path));
resolved = resolved.startsWith('.') ? resolved : './' + resolved;
return match.replace(import_path, resolved);
return match.replace(quote + import_path + quote, quote + resolved + quote);
}
return match;
};

// 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
)
/\b(import|export)(?:\s+type)?\s+((?:\p{L}[\p{L}0-9]*\s+)|(?:\{[^}]*\}\s*))from\s*(['"])([^'";]+)\3/gmu,
(match, _keyword, _specifier, quote, import_path) =>
replace_import_path(match, 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)
content = content.replace(
/\bimport\s*\(\s*(['"])([^'";]+)\1\s*\)/g,
(match, quote, import_path) => replace_import_path(match, quote, import_path)
);

// import '...'
content = content.replace(/\bimport\s+(['"])([^'";]+)\1/g, (_, quote, import_path) =>
replace_import_path(`import ${quote}${import_path}${quote}`, import_path)
content = content.replace(/\bimport\s+(['"])([^'";]+)\1/g, (match, quote, import_path) =>
replace_import_path(match, quote, import_path)
);

return content;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import type { Baz } from '../baz';
export declare const dynamic: () => Promise<typeof import('../Test.svelte')>;
export declare const bar1: import('./foo').Foo;
export declare const bar2: import('../baz').Baz;
export declare const bar2: Baz;
export declare const bar3: Baz;
export declare const bar4: import('./foo').Foo;
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { baz } from '../baz';
import { foo } from './foo';
import {
// some comment
foo as $foo,
} from "./foo";
export const dynamic = () => import('../Test.svelte');
export const bar1 = foo;
export const bar2 = baz;
export const bar3 = { baz: "bar" };
export const bar4 = $foo;
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { baz } from '$lib/baz';
import { foo } from '$lib/sub/foo';
import type { Baz } from '$lib/baz';
import {
// some comment
foo as $foo
} from '$lib/sub/foo';

export const dynamic = () => import('$lib/Test.svelte');
export const bar1 = foo;
export const bar2 = baz;
export const bar3: Baz = { baz: 'bar' };
export const bar4 = $foo;
Loading