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 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
12 changes: 12 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,15 @@ export function dynamicImportToGlob(node, sourceString) {

return glob;
}

export function normalizePath(p) {
return (
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().replace(/\n/g, '\n ');

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 = (${normalizePathString})(path);
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,23 @@ Generated by [AVA](https://avajs.dev).
> Snapshot 1

`function __variableDynamicImportRuntime0__(path) {␊
switch (path) {␊
const normalPath = (function normalizePath(p) {␊
return (␊
p␊
// remove any ./ inside the path␊
.replace(/\\/\\.\\//g, '/')␊
// remove dir/ + ../ pairs␊
.replace(/([^/.][^/]*\\/)(([^/.][^/]*\\/)(([^/.][^/]*\\/)(\\.\\.\\/))*?(\\.\\.\\/))*?(\\.\\.\\/)/g, '')␊
// remove unnecessary leading ./␊
.replace(/^(\\.\\/)+(?=\\.\\.\\/)/g, '')␊
);␊
})(path);␊
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 +43,26 @@ Generated by [AVA](https://avajs.dev).
> Snapshot 1

`function __variableDynamicImportRuntime0__(path) {␊
switch (path) {␊
const normalPath = (function normalizePath(p) {␊
return (␊
p␊
// remove any ./ inside the path␊
.replace(/\\/\\.\\//g, '/')␊
// remove dir/ + ../ pairs␊
.replace(/([^/.][^/]*\\/)(([^/.][^/]*\\/)(([^/.][^/]*\\/)(\\.\\.\\/))*?(\\.\\.\\/))*?(\\.\\.\\/)/g, '')␊
// remove unnecessary leading ./␊
.replace(/^(\\.\\/)+(?=\\.\\.\\/)/g, '')␊
);␊
})(path);␊
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-ULPK6OKR.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 +80,23 @@ Generated by [AVA](https://avajs.dev).
> Snapshot 1

`function __variableDynamicImportRuntime0__(path) {␊
switch (path) {␊
const normalPath = (function normalizePath(p) {␊
return (␊
p␊
// remove any ./ inside the path␊
.replace(/\\/\\.\\//g, '/')␊
// remove dir/ + ../ pairs␊
.replace(/([^/.][^/]*\\/)(([^/.][^/]*\\/)(([^/.][^/]*\\/)(\\.\\.\\/))*?(\\.\\.\\/))*?(\\.\\.\\/)/g, '')␊
// remove unnecessary leading ./␊
.replace(/^(\\.\\/)+(?=\\.\\.\\/)/g, '')␊
);␊
})(path);␊
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 +114,25 @@ Generated by [AVA](https://avajs.dev).
> Snapshot 1

`function __variableDynamicImportRuntime0__(path) {␊
switch (path) {␊
const normalPath = (function normalizePath(p) {␊
return (␊
p␊
// remove any ./ inside the path␊
.replace(/\\/\\.\\//g, '/')␊
// remove dir/ + ../ pairs␊
.replace(/([^/.][^/]*\\/)(([^/.][^/]*\\/)(([^/.][^/]*\\/)(\\.\\.\\/))*?(\\.\\.\\/))*?(\\.\\.\\/)/g, '')␊
// remove unnecessary leading ./␊
.replace(/^(\\.\\/)+(?=\\.\\.\\/)/g, '')␊
);␊
})(path);␊
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 +150,23 @@ Generated by [AVA](https://avajs.dev).
> Snapshot 1

`function __variableDynamicImportRuntime0__(path) {␊
switch (path) {␊
const normalPath = (function normalizePath(p) {␊
return (␊
p␊
// remove any ./ inside the path␊
.replace(/\\/\\.\\//g, '/')␊
// remove dir/ + ../ pairs␊
.replace(/([^/.][^/]*\\/)(([^/.][^/]*\\/)(([^/.][^/]*\\/)(\\.\\.\\/))*?(\\.\\.\\/))*?(\\.\\.\\/)/g, '')␊
// remove unnecessary leading ./␊
.replace(/^(\\.\\/)+(?=\\.\\.\\/)/g, '')␊
);␊
})(path);␊
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 +184,72 @@ Generated by [AVA](https://avajs.dev).
> Snapshot 1

`function __variableDynamicImportRuntime2__(path) {␊
switch (path) {␊
const normalPath = (function normalizePath(p) {␊
return (␊
p␊
// remove any ./ inside the path␊
.replace(/\\/\\.\\//g, '/')␊
// remove dir/ + ../ pairs␊
.replace(/([^/.][^/]*\\/)(([^/.][^/]*\\/)(([^/.][^/]*\\/)(\\.\\.\\/))*?(\\.\\.\\/))*?(\\.\\.\\/)/g, '')␊
// remove unnecessary leading ./␊
.replace(/^(\\.\\/)+(?=\\.\\.\\/)/g, '')␊
);␊
})(path);␊
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-ULPK6OKR.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 = (function normalizePath(p) {␊
return (␊
p␊
// remove any ./ inside the path␊
.replace(/\\/\\.\\//g, '/')␊
// remove dir/ + ../ pairs␊
.replace(/([^/.][^/]*\\/)(([^/.][^/]*\\/)(([^/.][^/]*\\/)(\\.\\.\\/))*?(\\.\\.\\/))*?(\\.\\.\\/)/g, '')␊
// remove unnecessary leading ./␊
.replace(/^(\\.\\/)+(?=\\.\\.\\/)/g, '')␊
);␊
})(path);␊
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 = (function normalizePath(p) {␊
return (␊
p␊
// remove any ./ inside the path␊
.replace(/\\/\\.\\//g, '/')␊
// remove dir/ + ../ pairs␊
.replace(/([^/.][^/]*\\/)(([^/.][^/]*\\/)(([^/.][^/]*\\/)(\\.\\.\\/))*?(\\.\\.\\/))*?(\\.\\.\\/)/g, '')␊
// remove unnecessary leading ./␊
.replace(/^(\\.\\/)+(?=\\.\\.\\/)/g, '')␊
);␊
})(path);␊
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 +305,23 @@ Generated by [AVA](https://avajs.dev).
> Snapshot 1

`function __variableDynamicImportRuntime0__(path) {␊
switch (path) {␊
const normalPath = (function normalizePath(p) {␊
return (␊
p␊
// remove any ./ inside the path␊
.replace(/\\/\\.\\//g, '/')␊
// remove dir/ + ../ pairs␊
.replace(/([^/.][^/]*\\/)(([^/.][^/]*\\/)(([^/.][^/]*\\/)(\\.\\.\\/))*?(\\.\\.\\/))*?(\\.\\.\\/)/g, '')␊
// remove unnecessary leading ./␊
.replace(/^(\\.\\/)+(?=\\.\\.\\/)/g, '')␊
);␊
})(path);␊
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 +333,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
@@ -1,9 +1,10 @@
/* eslint-disable import/extensions, import/no-unresolved, no-template-curly-in-string */

import { parse } from 'acorn';
import test from 'ava';

Check warning on line 4 in packages/dynamic-import-vars/test/src/dynamic-import-to-glob.test.mjs

View workflow job for this annotation

GitHub Actions / Node v20

There should be at least one empty line between import groups

Check warning on line 4 in packages/dynamic-import-vars/test/src/dynamic-import-to-glob.test.mjs

View workflow job for this annotation

GitHub Actions / Node v18

There should be at least one empty line between import groups

Check warning on line 4 in packages/dynamic-import-vars/test/src/dynamic-import-to-glob.test.mjs

View workflow job for this annotation

GitHub Actions / Node v20

There should be at least one empty line between import groups

Check warning on line 4 in packages/dynamic-import-vars/test/src/dynamic-import-to-glob.test.mjs

View workflow job for this annotation

GitHub Actions / Node v18

There should be at least one empty line between import groups
import { posix } from 'node:path';

Check warning on line 5 in packages/dynamic-import-vars/test/src/dynamic-import-to-glob.test.mjs

View workflow job for this annotation

GitHub Actions / Node v20

`node:path` import should occur before import of `acorn`

Check warning on line 5 in packages/dynamic-import-vars/test/src/dynamic-import-to-glob.test.mjs

View workflow job for this annotation

GitHub Actions / Node v18

`node:path` import should occur before import of `acorn`

Check warning on line 5 in packages/dynamic-import-vars/test/src/dynamic-import-to-glob.test.mjs

View workflow job for this annotation

GitHub Actions / Node v20

`node:path` import should occur before import of `acorn`

Check warning on line 5 in packages/dynamic-import-vars/test/src/dynamic-import-to-glob.test.mjs

View workflow job for this annotation

GitHub Actions / Node v18

`node:path` import should occur before import of `acorn`

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 @@
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',
'./foo/bar/baz/../qux/../../../../foo.js'
].forEach((p) => {
test(`normalizePath - ${p}`, (t) => {
const normalPath = normalizePath(p);
t.is(posix.normalize(p), normalPath);
});
});
Loading