Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat] support for other aliases in package command #6350

Merged
merged 5 commits into from
Aug 29, 2022
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/weak-hotels-hang.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/package': patch
---

[feat] Support aliases set through `kit.alias`
1 change: 1 addition & 0 deletions packages/package/src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
20 changes: 12 additions & 8 deletions packages/package/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<script>
import { foo } from './sub/foo';
import { util } from './utils';
export let bar = foo;
util();
</script>
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export declare const util: () => void;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const util = () => { };
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<script lang="ts">
import { foo } from '$lib/sub/foo';
import { util } from '$utils';

export let bar = foo;

util();
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const util = () => {};
12 changes: 11 additions & 1 deletion packages/package/test/fixtures/resolve-alias/svelte.config.js
Original file line number Diff line number Diff line change
@@ -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')
}
}
};
3 changes: 2 additions & 1 deletion packages/package/test/fixtures/resolve-alias/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down