Skip to content

Commit eb91c6a

Browse files
fix: one more alias resolution fix (#14462)
* fix: handle import/export name, { ... } when resolving aliases * enforce valid identifiers Co-authored-by: Simon H <5968653+dummdidumm@users.noreply.github.com> --------- Co-authored-by: Simon H <5968653+dummdidumm@users.noreply.github.com>
1 parent a86e34f commit eb91c6a

File tree

8 files changed

+23
-3
lines changed

8 files changed

+23
-3
lines changed

.changeset/shiny-schools-slide.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: handle `import/export name, { ... }` when resolving aliases

packages/package/src/utils.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,13 @@ export function resolve_aliases(input, file, content, aliases) {
4242
// 4. ( - Start of specifier group
4343
// (?:(?:\*\s+as\s+)?\p{L}[\p{L}0-9]*\s+) - default import/export, e.g. 'name', or named star import/export, e.g. '* as name '
4444
// | (?:\*\s+) - e.g. star import/export, e.g. '* '
45-
// | (?:\{[^}]*\}\s*) - e.g. named imports/exports, e.g. '{ ... }'
45+
// | (?:(?:\p{L}[\p{L}0-9]*\s*,\s*)?\{[^}]*\}\s*) - e.g. named imports/exports with optional default import/export, e.g. 'name, { ... }' or '{ ... }'
4646
// )
4747
// 5. from\s* - Match 'from' with optional whitespace
4848
// 6. (['"]) - Capture quote
4949
// 7. ([^'";]+) - Capture import path
5050
// 8. \3 - Match the same quote as before
51-
/\b(import|export)(?:\s+type)?\s+((?:(?:\*\s+as\s+)?\p{L}[\p{L}0-9]*\s+)|(?:\*\s+)|(?:\{[^}]*\}\s*))from\s*(['"])([^'";]+)\3/gmu,
51+
/\b(import|export)(?:\s+type)?\s+((?:(?:\*\s+as\s+)?\p{L}[\p{L}0-9]*\s+)|(?:\*\s+)|(?:(?:\p{L}[\p{L}0-9]*\s*,\s*)?\{[^}]*\}\s*))from\s*(['"])([^'";]+)\3/gmu,
5252
(match, _keyword, _specifier, quote, import_path) =>
5353
replace_import_path(match, quote, import_path)
5454
);

packages/package/test/fixtures/resolve-alias/expected/baz.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ export interface Baz {
22
baz: string;
33
}
44
export declare const baz: Baz;
5+
export default baz;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export const baz = { baz: 'baz' };
2+
export default baz;

packages/package/test/fixtures/resolve-alias/expected/index.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,8 @@ export * from "./sub/foo";
33
export type * from "./sub/bar";
44
import * as Utils from "./utils/index";
55
export { Utils };
6+
import baz1, { baz } from './baz';
7+
import { type Baz } from './baz';
8+
import baz2, { type Baz as Bz } from './baz';
9+
export { baz, baz1, baz2, type Baz, type Bz };
610
export { X } from '$libre';
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
export { default as Test } from './Test.svelte';
22
export * from "./sub/foo";
33
import * as Utils from "./utils/index";
4-
export { Utils}
4+
export { Utils };
5+
import baz1, { baz } from './baz';
6+
import baz2 from './baz';
7+
export { baz, baz1, baz2 };
58
// @ts-expect-error
69
export { X } from '$libre';

packages/package/test/fixtures/resolve-alias/src/lib/baz.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ export interface Baz {
33
}
44

55
export const baz: Baz = { baz: 'baz' };
6+
7+
export default baz;

packages/package/test/fixtures/resolve-alias/src/lib/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,9 @@ export * from '$lib/sub/foo';
33
export type * from '$lib/sub/bar';
44
import * as Utils from '$lib/utils/index';
55
export { Utils };
6+
import baz1, { baz } from '$lib/baz';
7+
import { type Baz } from '$lib/baz';
8+
import baz2, { type Baz as Bz } from '$lib/baz';
9+
export { baz, baz1, baz2, type Baz, type Bz };
610
// @ts-expect-error
711
export { X } from '$libre';

0 commit comments

Comments
 (0)