diff --git a/packages/dynamic-import-vars/src/dynamic-import-to-glob.js b/packages/dynamic-import-vars/src/dynamic-import-to-glob.js index de63b90b1..2697d27ee 100644 --- a/packages/dynamic-import-vars/src/dynamic-import-to-glob.js +++ b/packages/dynamic-import-vars/src/dynamic-import-to-glob.js @@ -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, '') + ); +} diff --git a/packages/dynamic-import-vars/src/index.js b/packages/dynamic-import-vars/src/index.js index 3351a1582..c7152cfd8 100644 --- a/packages/dynamic-import-vars/src/index.js +++ b/packages/dynamic-import-vars/src/index.js @@ -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); @@ -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` @@ -122,4 +129,4 @@ ${` default: return new Promise(function(resolve, reject) { } export default dynamicImportVariables; -export { dynamicImportToGlob, VariableDynamicImportError }; +export { dynamicImportToGlob, VariableDynamicImportError, normalizePath }; diff --git a/packages/dynamic-import-vars/test/snapshots/rollup-plugin-dynamic-import-vars.test.js.md b/packages/dynamic-import-vars/test/snapshots/rollup-plugin-dynamic-import-vars.test.js.md index 0889053e5..df1ff0319 100644 --- a/packages/dynamic-import-vars/test/snapshots/rollup-plugin-dynamic-import-vars.test.js.md +++ b/packages/dynamic-import-vars/test/snapshots/rollup-plugin-dynamic-import-vars.test.js.md @@ -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))␊ );␊ })␊ }␊ @@ -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))␊ );␊ })␊ }␊ @@ -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))␊ );␊ })␊ }␊ @@ -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))␊ );␊ })␊ }␊ @@ -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))␊ );␊ })␊ }␊ @@ -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))␊ );␊ })␊ }␊ @@ -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))␊ );␊ })␊ }␊ @@ -234,25 +333,3 @@ Generated by [AVA](https://avajs.dev). ␊ export { importModule };␊ ` - -## no files in dir - -> 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 };␊ - ` diff --git a/packages/dynamic-import-vars/test/snapshots/rollup-plugin-dynamic-import-vars.test.js.snap b/packages/dynamic-import-vars/test/snapshots/rollup-plugin-dynamic-import-vars.test.js.snap index 8ee67b5d8..1ab24197b 100644 Binary files a/packages/dynamic-import-vars/test/snapshots/rollup-plugin-dynamic-import-vars.test.js.snap and b/packages/dynamic-import-vars/test/snapshots/rollup-plugin-dynamic-import-vars.test.js.snap differ diff --git a/packages/dynamic-import-vars/test/src/dynamic-import-to-glob.test.mjs b/packages/dynamic-import-vars/test/src/dynamic-import-to-glob.test.mjs index 17f50124c..1fdc7642e 100644 --- a/packages/dynamic-import-vars/test/src/dynamic-import-to-glob.test.mjs +++ b/packages/dynamic-import-vars/test/src/dynamic-import-to-glob.test.mjs @@ -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`);', { @@ -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', + './foo/bar/baz/../qux/../../../../foo.js' +].forEach((p) => { + test(`normalizePath - ${p}`, (t) => { + const normalPath = normalizePath(p); + t.is(posix.normalize(p), normalPath); + }); +});