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 2 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
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
21 changes: 15 additions & 6 deletions packages/package/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,31 @@ import { posixify, mkdirp, walk } from './filesystem.js';
export function resolve_lib_alias(file, content, config) {
/**
* @param {string} match
* @param {string} _
* @param {string} import_path
* @param {string} alias
* @param {string} value
*/
const replace_import_path = (match, _, import_path) => {
if (!import_path.startsWith('$lib/')) {
const replace_import_path = (match, import_path, alias, value) => {
if (!import_path.startsWith(alias)) {
return match;
}

const full_path = path.join(config.package.source, file);
const full_import_path = path.join(config.package.source, import_path.slice('$lib/'.length));
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);
};
content = content.replace(/from\s+('|")([^"';,]+?)\1/g, replace_import_path);
content = content.replace(/import\s*\(\s*('|")([^"';,]+?)\1\s*\)/g, replace_import_path);

const aliases = { $lib: path.resolve(config.package.source), ...(config.kit?.alias ?? {}) };
for (const [alias, value] of Object.entries(aliases)) {
content = content.replace(/from\s+('|")([^"';,]+?)\1/g, (match, _, import_path) =>
replace_import_path(match, import_path, alias, value)
);
content = content.replace(/import\s*\(\s*('|")([^"';,]+?)\1\s*\)/g, (match, _, import_path) =>
replace_import_path(match, import_path, alias, value)
);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is propbably super edge-case-y, but this could theoretically replace import paths multiple times. Could we turn that around and do the for loop inside the replace functions, and check if a replacement was done by checking if output != match?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, there was that possibility. I moved the loop inside, but I did not understood what you meant by the last part of checking if output != match.

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