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/small-ghosts-fail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/package': patch
---

fix: handle `import/export * (as ...)` when resolving aliases
5 changes: 5 additions & 0 deletions .changeset/witty-ties-brush.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/package': patch
---

fix: prevent false-positive alias replacement
17 changes: 15 additions & 2 deletions packages/package/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export function resolve_aliases(input, file, content, aliases) {
*/
const replace_import_path = (match, quote, import_path) => {
for (const [alias, value] of Object.entries(aliases)) {
if (!import_path.startsWith(alias)) continue;
if (import_path !== alias && !import_path.startsWith(alias + '/')) continue;

const full_path = path.join(input, file);
const full_import_path = path.join(value, import_path.slice(alias.length));
Expand All @@ -35,7 +35,20 @@ export function resolve_aliases(input, file, content, aliases) {

// import/export ... from ...
content = content.replace(
/\b(import|export)(?:\s+type)?\s+((?:\p{L}[\p{L}0-9]*\s+)|(?:\{[^}]*\}\s*))from\s*(['"])([^'";]+)\3/gmu,
// Regex parts for import/export ... from ... statements:
// 1. \b(import|export) - Match 'import' or 'export' at a word boundary
// 2. (?:\s+type)? - Optionally match ' type'
// 3. \s+ - At least one whitespace
// 4. ( - Start of specifier group
// (?:(?:\*\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 '
// | (?:\*\s+) - e.g. star import/export, e.g. '* '
// | (?:\{[^}]*\}\s*) - e.g. named imports/exports, e.g. '{ ... }'
// )
// 5. from\s* - Match 'from' with optional whitespace
// 6. (['"]) - Capture quote
// 7. ([^'";]+) - Capture import path
// 8. \3 - Match the same quote as before
/\b(import|export)(?:\s+type)?\s+((?:(?:\*\s+as\s+)?\p{L}[\p{L}0-9]*\s+)|(?:\*\s+)|(?:\{[^}]*\}\s*))from\s*(['"])([^'";]+)\3/gmu,
(match, _keyword, _specifier, quote, import_path) =>
replace_import_path(match, quote, import_path)
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
export { default as Test } from './Test.svelte';
export * from "./sub/foo";
export type * from "./sub/bar";
import * as Utils from "./utils/index";
export { Utils };
export { X } from '$libre';
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
export { default as Test } from './Test.svelte';
export * from "./sub/foo";
import * as Utils from "./utils/index";
export { Utils}
// @ts-expect-error
export { X } from '$libre';
6 changes: 6 additions & 0 deletions packages/package/test/fixtures/resolve-alias/src/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
export { default as Test } from '$lib/Test.svelte';
export * from '$lib/sub/foo';
export type * from '$lib/sub/bar';
import * as Utils from '$lib/utils/index';
export { Utils };
// @ts-expect-error
export { X } from '$libre';
Loading