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(dynamic-import-vars): normalize paths at runtime #1794

Closed
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
9 changes: 9 additions & 0 deletions packages/dynamic-import-vars/src/dynamic-import-to-glob.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,12 @@ export function dynamicImportToGlob(node, sourceString) {

return glob;
}

export const normalizePath = (p) =>
p
// remove any ./ inside the path
.replace(/\/\.\//g, '/')
// remove dir/ + ../ pairs
.replace(/([^/.][^/]*\/)(([^/.][^/]*\/)(\.\.\/))?(\.\.\/)/g, '')
// remove unnecessary leading ./
.replace(/^(\.\/)+(?=\.\.\/)/g, '');
15 changes: 11 additions & 4 deletions packages/dynamic-import-vars/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ import { generate } from 'astring';

import { createFilter } from '@rollup/pluginutils';

import { dynamicImportToGlob, VariableDynamicImportError } from './dynamic-import-to-glob';
import {
dynamicImportToGlob,
VariableDynamicImportError,
normalizePath
} from './dynamic-import-to-glob';

const normalizePathString = normalizePath.toString().substring(10);

function dynamicImportVariables({ include, exclude, warnOnError, errorWhenNoFilesFound } = {}) {
const filter = createFilter(include, exclude);
Expand Down Expand Up @@ -72,13 +78,14 @@ function dynamicImportVariables({ include, exclude, warnOnError, errorWhenNoFile
// will turn these into chunks automatically
ms.prepend(
`function __variableDynamicImportRuntime${dynamicImportIndex}__(path) {
switch (path) {
const normalPath = path${normalizePathString};
switch (normalPath) {
${paths
.map((p) => ` case '${p}': return import('${p}'${importArg ? `, ${importArg}` : ''});`)
.join('\n')}
${` default: return new Promise(function(resolve, reject) {
(typeof queueMicrotask === 'function' ? queueMicrotask : setTimeout)(
reject.bind(null, new Error("Unknown variable dynamic import: " + path))
reject.bind(null, new Error("Unknown variable dynamic import: " + normalPath))
);
})\n`} }
}\n\n`
Expand Down Expand Up @@ -122,4 +129,4 @@ ${` default: return new Promise(function(resolve, reject) {
}

export default dynamicImportVariables;
export { dynamicImportToGlob, VariableDynamicImportError };
export { dynamicImportToGlob, VariableDynamicImportError, normalizePath };
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,19 @@ Generated by [AVA](https://avajs.dev).
> Snapshot 1

`function __variableDynamicImportRuntime0__(path) {␊
switch (path) {␊
const normalPath = path␊
// remove any ./ inside the path␊
.replace(/\\/\\.\\//g, '/')␊
// remove dir/ + ../ pairs␊
.replace(/([^/.][^/]*\\/)(([^/.][^/]*\\/)(\\.\\.\\/))?(\\.\\.\\/)/g, '')␊
// remove unnecessary leading ./␊
.replace(/^(\\.\\/)+(?=\\.\\.\\/)/g, '');␊
switch (normalPath) {␊
case './module-dir-a/module-a-1.js': return import('./module-a-1-fiunS6HF.js');␊
case './module-dir-a/module-a-2.js': return import('./module-a-2-qepdcyXv.js');␊
default: return new Promise(function(resolve, reject) {␊
(typeof queueMicrotask === 'function' ? queueMicrotask : setTimeout)(␊
reject.bind(null, new Error("Unknown variable dynamic import: " + path))␊
reject.bind(null, new Error("Unknown variable dynamic import: " + normalPath))␊
);␊
})␊
}␊
Expand All @@ -32,15 +39,22 @@ Generated by [AVA](https://avajs.dev).
> Snapshot 1

`function __variableDynamicImportRuntime0__(path) {␊
switch (path) {␊
const normalPath = path␊
// remove any ./ inside the path␊
.replace(/\\/\\.\\//g, '/')␊
// remove dir/ + ../ pairs␊
.replace(/([^/.][^/]*\\/)(([^/.][^/]*\\/)(\\.\\.\\/))?(\\.\\.\\/)/g, '')␊
// remove unnecessary leading ./␊
.replace(/^(\\.\\/)+(?=\\.\\.\\/)/g, '');␊
switch (normalPath) {␊
case './module-dir-a/module-a-1.js': return import('./module-a-1-fiunS6HF.js');␊
case './module-dir-a/module-a-2.js': return import('./module-a-2-qepdcyXv.js');␊
case './module-dir-b/module-b-1.js': return import('./module-b-1-3qdzaV4G.js');␊
case './module-dir-b/module-b-2.js': return import('./module-b-2-_8gpxG1i.js');␊
case './sub-dir/fixture-upwards-path.js': return import('./fixture-upwards-path-jrfqlYMu.js');␊
case './sub-dir/fixture-upwards-path.js': return import('./fixture-upwards-path-qW3pa8f7.js');␊
default: return new Promise(function(resolve, reject) {␊
(typeof queueMicrotask === 'function' ? queueMicrotask : setTimeout)(␊
reject.bind(null, new Error("Unknown variable dynamic import: " + path))␊
reject.bind(null, new Error("Unknown variable dynamic import: " + normalPath))␊
);␊
})␊
}␊
Expand All @@ -58,12 +72,19 @@ Generated by [AVA](https://avajs.dev).
> Snapshot 1

`function __variableDynamicImportRuntime0__(path) {␊
switch (path) {␊
const normalPath = path␊
// remove any ./ inside the path␊
.replace(/\\/\\.\\//g, '/')␊
// remove dir/ + ../ pairs␊
.replace(/([^/.][^/]*\\/)(([^/.][^/]*\\/)(\\.\\.\\/))?(\\.\\.\\/)/g, '')␊
// remove unnecessary leading ./␊
.replace(/^(\\.\\/)+(?=\\.\\.\\/)/g, '');␊
switch (normalPath) {␊
case '../module-dir-a/module-a-1.js': return import('./module-a-1-fiunS6HF.js');␊
case '../module-dir-a/module-a-2.js': return import('./module-a-2-qepdcyXv.js');␊
default: return new Promise(function(resolve, reject) {␊
(typeof queueMicrotask === 'function' ? queueMicrotask : setTimeout)(␊
reject.bind(null, new Error("Unknown variable dynamic import: " + path))␊
reject.bind(null, new Error("Unknown variable dynamic import: " + normalPath))␊
);␊
})␊
}␊
Expand All @@ -81,14 +102,21 @@ Generated by [AVA](https://avajs.dev).
> Snapshot 1

`function __variableDynamicImportRuntime0__(path) {␊
switch (path) {␊
const normalPath = path␊
// remove any ./ inside the path␊
.replace(/\\/\\.\\//g, '/')␊
// remove dir/ + ../ pairs␊
.replace(/([^/.][^/]*\\/)(([^/.][^/]*\\/)(\\.\\.\\/))?(\\.\\.\\/)/g, '')␊
// remove unnecessary leading ./␊
.replace(/^(\\.\\/)+(?=\\.\\.\\/)/g, '');␊
switch (normalPath) {␊
case './module-dir-a/module-a-1.js': return import('./module-a-1-fiunS6HF.js');␊
case './module-dir-a/module-a-2.js': return import('./module-a-2-qepdcyXv.js');␊
case './module-dir-b/module-b-1.js': return import('./module-b-1-3qdzaV4G.js');␊
case './module-dir-b/module-b-2.js': return import('./module-b-2-_8gpxG1i.js');␊
default: return new Promise(function(resolve, reject) {␊
(typeof queueMicrotask === 'function' ? queueMicrotask : setTimeout)(␊
reject.bind(null, new Error("Unknown variable dynamic import: " + path))␊
reject.bind(null, new Error("Unknown variable dynamic import: " + normalPath))␊
);␊
})␊
}␊
Expand All @@ -106,12 +134,19 @@ Generated by [AVA](https://avajs.dev).
> Snapshot 1

`function __variableDynamicImportRuntime0__(path) {␊
switch (path) {␊
const normalPath = path␊
// remove any ./ inside the path␊
.replace(/\\/\\.\\//g, '/')␊
// remove dir/ + ../ pairs␊
.replace(/([^/.][^/]*\\/)(([^/.][^/]*\\/)(\\.\\.\\/))?(\\.\\.\\/)/g, '')␊
// remove unnecessary leading ./␊
.replace(/^(\\.\\/)+(?=\\.\\.\\/)/g, '');␊
switch (normalPath) {␊
case './root-module-a.js': return import('./root-module-a-lF5i40jF.js');␊
case './root-module-b.js': return import('./root-module-b-F1yin99u.js');␊
default: return new Promise(function(resolve, reject) {␊
(typeof queueMicrotask === 'function' ? queueMicrotask : setTimeout)(␊
reject.bind(null, new Error("Unknown variable dynamic import: " + path))␊
reject.bind(null, new Error("Unknown variable dynamic import: " + normalPath))␊
);␊
})␊
}␊
Expand All @@ -129,39 +164,60 @@ Generated by [AVA](https://avajs.dev).
> Snapshot 1

`function __variableDynamicImportRuntime2__(path) {␊
switch (path) {␊
const normalPath = path␊
// remove any ./ inside the path␊
.replace(/\\/\\.\\//g, '/')␊
// remove dir/ + ../ pairs␊
.replace(/([^/.][^/]*\\/)(([^/.][^/]*\\/)(\\.\\.\\/))?(\\.\\.\\/)/g, '')␊
// remove unnecessary leading ./␊
.replace(/^(\\.\\/)+(?=\\.\\.\\/)/g, '');␊
switch (normalPath) {␊
case './module-dir-a/module-a-1.js': return import('./module-a-1-fiunS6HF.js');␊
case './module-dir-a/module-a-2.js': return import('./module-a-2-qepdcyXv.js');␊
case './module-dir-b/module-b-1.js': return import('./module-b-1-3qdzaV4G.js');␊
case './module-dir-b/module-b-2.js': return import('./module-b-2-_8gpxG1i.js');␊
case './sub-dir/fixture-upwards-path.js': return import('./fixture-upwards-path-jrfqlYMu.js');␊
case './sub-dir/fixture-upwards-path.js': return import('./fixture-upwards-path-qW3pa8f7.js');␊
default: return new Promise(function(resolve, reject) {␊
(typeof queueMicrotask === 'function' ? queueMicrotask : setTimeout)(␊
reject.bind(null, new Error("Unknown variable dynamic import: " + path))␊
reject.bind(null, new Error("Unknown variable dynamic import: " + normalPath))␊
);␊
})␊
}␊
}␊
function __variableDynamicImportRuntime1__(path) {␊
switch (path) {␊
const normalPath = path␊
// remove any ./ inside the path␊
.replace(/\\/\\.\\//g, '/')␊
// remove dir/ + ../ pairs␊
.replace(/([^/.][^/]*\\/)(([^/.][^/]*\\/)(\\.\\.\\/))?(\\.\\.\\/)/g, '')␊
// remove unnecessary leading ./␊
.replace(/^(\\.\\/)+(?=\\.\\.\\/)/g, '');␊
switch (normalPath) {␊
case './module-dir-a/module-a-1.js': return import('./module-a-1-fiunS6HF.js');␊
case './module-dir-a/module-a-2.js': return import('./module-a-2-qepdcyXv.js');␊
default: return new Promise(function(resolve, reject) {␊
(typeof queueMicrotask === 'function' ? queueMicrotask : setTimeout)(␊
reject.bind(null, new Error("Unknown variable dynamic import: " + path))␊
reject.bind(null, new Error("Unknown variable dynamic import: " + normalPath))␊
);␊
})␊
}␊
}␊
function __variableDynamicImportRuntime0__(path) {␊
switch (path) {␊
const normalPath = path␊
// remove any ./ inside the path␊
.replace(/\\/\\.\\//g, '/')␊
// remove dir/ + ../ pairs␊
.replace(/([^/.][^/]*\\/)(([^/.][^/]*\\/)(\\.\\.\\/))?(\\.\\.\\/)/g, '')␊
// remove unnecessary leading ./␊
.replace(/^(\\.\\/)+(?=\\.\\.\\/)/g, '');␊
switch (normalPath) {␊
case './module-dir-a/module-a-1.js': return import('./module-a-1-fiunS6HF.js');␊
case './module-dir-a/module-a-2.js': return import('./module-a-2-qepdcyXv.js');␊
default: return new Promise(function(resolve, reject) {␊
(typeof queueMicrotask === 'function' ? queueMicrotask : setTimeout)(␊
reject.bind(null, new Error("Unknown variable dynamic import: " + path))␊
reject.bind(null, new Error("Unknown variable dynamic import: " + normalPath))␊
);␊
})␊
}␊
Expand Down Expand Up @@ -217,12 +273,19 @@ Generated by [AVA](https://avajs.dev).
> Snapshot 1

`function __variableDynamicImportRuntime0__(path) {␊
switch (path) {␊
const normalPath = path␊
// remove any ./ inside the path␊
.replace(/\\/\\.\\//g, '/')␊
// remove dir/ + ../ pairs␊
.replace(/([^/.][^/]*\\/)(([^/.][^/]*\\/)(\\.\\.\\/))?(\\.\\.\\/)/g, '')␊
// remove unnecessary leading ./␊
.replace(/^(\\.\\/)+(?=\\.\\.\\/)/g, '');␊
switch (normalPath) {␊
case './module-dir-a/module-a-1.js': return import('./module-a-1-fiunS6HF.js');␊
case './module-dir-a/module-a-2.js': return import('./module-a-2-qepdcyXv.js');␊
default: return new Promise(function(resolve, reject) {␊
(typeof queueMicrotask === 'function' ? queueMicrotask : setTimeout)(␊
reject.bind(null, new Error("Unknown variable dynamic import: " + path))␊
reject.bind(null, new Error("Unknown variable dynamic import: " + normalPath))␊
);␊
})␊
}␊
Expand All @@ -234,25 +297,3 @@ Generated by [AVA](https://avajs.dev).
export { importModule };␊
`

## no files in dir
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do I just add this back manually?


> Snapshot 1

`function __variableDynamicImportRuntime0__(path) {␊
switch (path) {␊
default: return new Promise(function(resolve, reject) {␊
(typeof queueMicrotask === 'function' ? queueMicrotask : setTimeout)(␊
reject.bind(null, new Error("Unknown variable dynamic import: " + path))␊
);␊
})␊
}␊
}␊
function importModule(name) {␊
return __variableDynamicImportRuntime0__(\`./module-dir-c/${name}.js\`);␊
}␊
export { importModule };␊
`
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

import { parse } from 'acorn';
import test from 'ava';
import { posix } from 'node:path';

import { dynamicImportToGlob, VariableDynamicImportError } from 'current-package';
import { dynamicImportToGlob, normalizePath, VariableDynamicImportError } from 'current-package';

test('template literal with variable filename', (t) => {
const ast = parse('import(`./foo/${bar}.js`);', {
Expand Down Expand Up @@ -286,3 +287,19 @@ test('escapes []', (t) => {
const glob = dynamicImportToGlob(ast.body[0].expression.source);
t.is(glob, './*/\\[foo\\].js');
});


[
'./../foo/./../foo.js',
'./../../a/foo.js',
'./föö/../bar/.././../foo.js',
'./../فو/bar/../../foo.js',
'../foo/../foo.js',
'./../foo/../bar/.././../foo.js',
'./../foo/bar/.././../foo.js'
].forEach((p) => {
test(`normalizePath - ${p}`, (t) => {
const normalPath = normalizePath(p);
t.is(posix.normalize(p), normalPath);
});
});
Loading