diff --git a/.changeset/weak-hotels-hang.md b/.changeset/weak-hotels-hang.md new file mode 100644 index 000000000000..88b11475b6eb --- /dev/null +++ b/.changeset/weak-hotels-hang.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/package': patch +--- + +[feat] Support aliases set through `kit.alias` diff --git a/packages/package/src/config.js b/packages/package/src/config.js index 3518957ed609..b094dee11e06 100644 --- a/packages/package/src/config.js +++ b/packages/package/src/config.js @@ -26,6 +26,7 @@ export async function load_config({ cwd = process.cwd() } = {}) { function process_config(config, { cwd = process.cwd() } = {}) { return { extensions: config.extensions ?? ['.svelte'], + kit: config.kit, package: { source: path.resolve(cwd, config.kit?.files?.lib ?? config.package?.source ?? 'src/lib'), dir: config.package?.dir ?? 'package', diff --git a/packages/package/src/utils.js b/packages/package/src/utils.js index c2a8fc82030c..97c3b03dbf3c 100644 --- a/packages/package/src/utils.js +++ b/packages/package/src/utils.js @@ -16,22 +16,26 @@ import { posixify, mkdirp, walk } from './filesystem.js'; * @returns {string} */ export function resolve_lib_alias(file, content, config) { + const aliases = { $lib: path.resolve(config.package.source), ...(config.kit?.alias ?? {}) }; + /** * @param {string} match * @param {string} _ * @param {string} import_path */ const replace_import_path = (match, _, import_path) => { - if (!import_path.startsWith('$lib/')) { - return match; + for (const [alias, value] of Object.entries(aliases)) { + if (!import_path.startsWith(alias)) continue; + + const full_path = path.join(config.package.source, 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); } - - const full_path = path.join(config.package.source, file); - const full_import_path = path.join(config.package.source, import_path.slice('$lib/'.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; }; + content = content.replace(/from\s+('|")([^"';,]+?)\1/g, replace_import_path); content = content.replace(/import\s*\(\s*('|")([^"';,]+?)\1\s*\)/g, replace_import_path); return content; diff --git a/packages/package/test/fixtures/resolve-alias/expected/Test.svelte b/packages/package/test/fixtures/resolve-alias/expected/Test.svelte index 7e6c336bdbfb..d6dbdc91b465 100644 --- a/packages/package/test/fixtures/resolve-alias/expected/Test.svelte +++ b/packages/package/test/fixtures/resolve-alias/expected/Test.svelte @@ -1,4 +1,6 @@ diff --git a/packages/package/test/fixtures/resolve-alias/expected/package.json b/packages/package/test/fixtures/resolve-alias/expected/package.json index bb3d7d208502..b0cea47c9a8c 100644 --- a/packages/package/test/fixtures/resolve-alias/expected/package.json +++ b/packages/package/test/fixtures/resolve-alias/expected/package.json @@ -7,10 +7,11 @@ "exports": { "./package.json": "./package.json", "./Test.svelte": "./Test.svelte", + "./utils": "./utils/index.js", ".": "./index.js", "./baz": "./baz.js", "./sub/bar": "./sub/bar.js", "./sub/foo": "./sub/foo.js" }, "svelte": "./index.js" -} \ No newline at end of file +} diff --git a/packages/package/test/fixtures/resolve-alias/expected/utils/index.d.ts b/packages/package/test/fixtures/resolve-alias/expected/utils/index.d.ts new file mode 100644 index 000000000000..5f6cf235961a --- /dev/null +++ b/packages/package/test/fixtures/resolve-alias/expected/utils/index.d.ts @@ -0,0 +1 @@ +export declare const util: () => void; diff --git a/packages/package/test/fixtures/resolve-alias/expected/utils/index.js b/packages/package/test/fixtures/resolve-alias/expected/utils/index.js new file mode 100644 index 000000000000..bf331fdbac9d --- /dev/null +++ b/packages/package/test/fixtures/resolve-alias/expected/utils/index.js @@ -0,0 +1 @@ +export const util = () => { }; diff --git a/packages/package/test/fixtures/resolve-alias/src/lib/Test.svelte b/packages/package/test/fixtures/resolve-alias/src/lib/Test.svelte index 6ba757b638c1..77589dd2aaed 100644 --- a/packages/package/test/fixtures/resolve-alias/src/lib/Test.svelte +++ b/packages/package/test/fixtures/resolve-alias/src/lib/Test.svelte @@ -1,5 +1,8 @@ diff --git a/packages/package/test/fixtures/resolve-alias/src/lib/utils/index.ts b/packages/package/test/fixtures/resolve-alias/src/lib/utils/index.ts new file mode 100644 index 000000000000..6853f8f8f1b1 --- /dev/null +++ b/packages/package/test/fixtures/resolve-alias/src/lib/utils/index.ts @@ -0,0 +1 @@ +export const util = () => {}; diff --git a/packages/package/test/fixtures/resolve-alias/svelte.config.js b/packages/package/test/fixtures/resolve-alias/svelte.config.js index 6bd253bdf27d..776bb17b41de 100644 --- a/packages/package/test/fixtures/resolve-alias/svelte.config.js +++ b/packages/package/test/fixtures/resolve-alias/svelte.config.js @@ -1,5 +1,15 @@ import preprocess from 'svelte-preprocess'; +import { fileURLToPath } from 'url'; +import path from 'path'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.join(__filename, '..'); export default { - preprocess: preprocess() + preprocess: preprocess(), + kit: { + alias: { + $utils: path.resolve(__dirname, './src/lib/utils') + } + } }; diff --git a/packages/package/test/fixtures/resolve-alias/tsconfig.json b/packages/package/test/fixtures/resolve-alias/tsconfig.json index 5d4bb5ea3fc1..70b71dd2fa3d 100644 --- a/packages/package/test/fixtures/resolve-alias/tsconfig.json +++ b/packages/package/test/fixtures/resolve-alias/tsconfig.json @@ -5,7 +5,8 @@ "checkJs": true, "baseUrl": ".", "paths": { - "$lib/*": ["./src/lib/*"] + "$lib/*": ["./src/lib/*"], + "$utils/*": ["./src/lib/utils/*"] } }, "include": ["src/**/*.d.ts", "src/**/*.js", "src/**/*.svelte"]