Skip to content

Commit cdc6383

Browse files
authored
fix: better alias resolution (#14413)
* fix: better alias resolution The fix in #13351 solved some issues but introduced others. This reworks the aliasing further to better check against the patterns that come up. Fixes #13532 Fixes #13436 * shut eslint, please?
1 parent 67b5664 commit cdc6383

File tree

6 files changed

+36
-16
lines changed

6 files changed

+36
-16
lines changed

.changeset/pink-pots-read.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: better alias resolution

eslint.config.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ export default [
1616
'**/.custom-out-dir',
1717
'packages/adapter-*/files',
1818
'packages/kit/src/core/config/fixtures/multiple', // dir contains svelte config with multiple extensions tripping eslint
19-
'packages/package/test/fixtures/typescript-svelte-config/expected'
19+
'packages/package/test/fixtures/typescript-svelte-config/expected',
20+
'packages/package/test/errors/**/*',
21+
'packages/package/test/fixtures/**/*'
2022
]
2123
},
2224
{
@@ -43,8 +45,6 @@ export default [
4345
'packages/kit/test/apps/**/*',
4446
'packages/kit/test/build-errors/**/*',
4547
'packages/kit/test/prerendering/**/*',
46-
'packages/package/test/errors/**/*',
47-
'packages/package/test/fixtures/**/*',
4848
'packages/test-redirect-importer/index.js'
4949
]
5050
}

packages/package/src/utils.js

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,39 +17,38 @@ 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} quote
2021
* @param {string} import_path
2122
*/
22-
const replace_import_path = (match, import_path) => {
23+
const replace_import_path = (match, quote, import_path) => {
2324
for (const [alias, value] of Object.entries(aliases)) {
2425
if (!import_path.startsWith(alias)) continue;
2526

2627
const full_path = path.join(input, file);
2728
const full_import_path = path.join(value, import_path.slice(alias.length));
2829
let resolved = posixify(path.relative(path.dirname(full_path), full_import_path));
2930
resolved = resolved.startsWith('.') ? resolved : './' + resolved;
30-
return match.replace(import_path, resolved);
31+
return match.replace(quote + import_path + quote, quote + resolved + quote);
3132
}
3233
return match;
3334
};
3435

3536
// import/export ... from ...
3637
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-
)
38+
/\b(import|export)(?:\s+type)?\s+((?:\p{L}[\p{L}0-9]*\s+)|(?:\{[^}]*\}\s*))from\s*(['"])([^'";]+)\3/gmu,
39+
(match, _keyword, _specifier, quote, import_path) =>
40+
replace_import_path(match, quote, import_path)
4341
);
4442

4543
// 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)
44+
content = content.replace(
45+
/\bimport\s*\(\s*(['"])([^'";]+)\1\s*\)/g,
46+
(match, quote, import_path) => replace_import_path(match, quote, import_path)
4847
);
4948

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

5554
return content;
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import type { Baz } from '../baz';
12
export declare const dynamic: () => Promise<typeof import('../Test.svelte')>;
23
export declare const bar1: import('./foo').Foo;
3-
export declare const bar2: import('../baz').Baz;
4+
export declare const bar2: Baz;
5+
export declare const bar3: Baz;
6+
export declare const bar4: import('./foo').Foo;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import { baz } from '../baz';
22
import { foo } from './foo';
3+
import {
4+
// some comment
5+
foo as $foo,
6+
} from "./foo";
37
export const dynamic = () => import('../Test.svelte');
48
export const bar1 = foo;
59
export const bar2 = baz;
10+
export const bar3 = { baz: "bar" };
11+
export const bar4 = $foo;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
import { baz } from '$lib/baz';
22
import { foo } from '$lib/sub/foo';
3+
import type { Baz } from '$lib/baz';
4+
import {
5+
// some comment
6+
foo as $foo
7+
} from '$lib/sub/foo';
38

49
export const dynamic = () => import('$lib/Test.svelte');
510
export const bar1 = foo;
611
export const bar2 = baz;
12+
export const bar3: Baz = { baz: 'bar' };
13+
export const bar4 = $foo;

0 commit comments

Comments
 (0)