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

[fix] Handle windows paths and regexp chars in kit.alias #6034

Merged
merged 1 commit into from
Aug 18, 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/mighty-students-develop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

Handle windows paths and regexp chars in kit.alias
16 changes: 14 additions & 2 deletions packages/kit/src/vite/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import fs from 'fs';
import path from 'path';
import { loadConfigFromFile, loadEnv, normalizePath } from 'vite';
import { runtime_directory } from '../core/utils.js';
import { posixify } from '../utils/filesystem.js';

/**
* @param {import('vite').ResolvedConfig} config
Expand Down Expand Up @@ -113,18 +114,22 @@ export function get_aliases(config) {
];

for (let [key, value] of Object.entries(config.alias)) {
value = posixify(value);
if (value.endsWith('/*')) {
value = value.slice(0, -2);
}
if (key.endsWith('/*')) {
// Doing just `{ find: key.slice(0, -2) ,..}` would mean `import .. from "key"` would also be matched, which we don't want
alias.push({
find: new RegExp(`^${key.slice(0, -2)}\\/(.+)$`),
find: new RegExp(`^${escape_for_regexp(key.slice(0, -2))}\\/(.+)$`),
replacement: `${path.resolve(value)}/$1`
});
} else if (key + '/*' in config.alias) {
// key and key/* both exist -> the replacement for key needs to happen _only_ on import .. from "key"
alias.push({ find: new RegExp(`^${key}$`), replacement: path.resolve(value) });
alias.push({
find: new RegExp(`^${escape_for_regexp(key)}$`),
replacement: path.resolve(value)
});
} else {
alias.push({ find: key, replacement: path.resolve(value) });
}
Expand All @@ -133,6 +138,13 @@ export function get_aliases(config) {
return alias;
}

/**
* @param {string} str
*/
function escape_for_regexp(str) {
return str.replace(/[.*+?^${}()|[\]\\]/g, (match) => '\\' + match);
}

/**
* Given an entry point like [cwd]/src/hooks, returns a filename like [cwd]/src/hooks.js or [cwd]/src/hooks/index.js
* @param {string} entry
Expand Down
14 changes: 12 additions & 2 deletions packages/kit/src/vite/utils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,15 @@ test('merge resolve.alias', () => {

test('transform kit.alias to resolve.alias', () => {
const config = validate_config({
kit: { alias: { simpleKey: 'simple/value', key: 'value', 'key/*': 'value/*' } }
kit: {
alias: {
simpleKey: 'simple/value',
key: 'value',
'key/*': 'value/*',
$regexChar: 'windows\\path',
'$regexChar/*': 'windows\\path\\*'
}
}
});

const transformed = get_aliases(config.kit).map((entry) => {
Expand All @@ -227,7 +235,9 @@ test('transform kit.alias to resolve.alias', () => {
{ find: '$lib', replacement: 'src/lib' },
{ find: 'simpleKey', replacement: 'simple/value' },
{ find: /^key$/.toString(), replacement: 'value' },
{ find: /^key\/(.+)$/.toString(), replacement: 'value/$1' }
{ find: /^key\/(.+)$/.toString(), replacement: 'value/$1' },
{ find: /^\$regexChar$/.toString(), replacement: 'windows/path' },
{ find: /^\$regexChar\/(.+)$/.toString(), replacement: 'windows/path/$1' }
]);
});

Expand Down