From 8c77949ac07d847f548458ccfaa8fa8d83ab0d7e Mon Sep 17 00:00:00 2001 From: Danny Gleckler Date: Fri, 18 Oct 2024 15:22:56 -0400 Subject: [PATCH 1/6] Normalize paths at runtime to match glob results --- .../src/dynamic-import-to-glob.js | 9 ++ packages/dynamic-import-vars/src/index.js | 18 ++- ...llup-plugin-dynamic-import-vars.test.js.md | 125 ++++++++++++------ ...up-plugin-dynamic-import-vars.test.js.snap | Bin 1006 -> 1152 bytes .../test/src/dynamic-import-to-glob.test.mjs | 19 ++- 5 files changed, 124 insertions(+), 47 deletions(-) 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..004c0394c 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,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, ''); diff --git a/packages/dynamic-import-vars/src/index.js b/packages/dynamic-import-vars/src/index.js index 3351a1582..0526aafb7 100644 --- a/packages/dynamic-import-vars/src/index.js +++ b/packages/dynamic-import-vars/src/index.js @@ -7,7 +7,16 @@ 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(6) + .replace(/\n {2}/g, '\n '); function dynamicImportVariables({ include, exclude, warnOnError, errorWhenNoFilesFound } = {}) { const filter = createFilter(include, exclude); @@ -72,13 +81,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` @@ -122,4 +132,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..2363f7a4d 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,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))␊ );␊ })␊ }␊ @@ -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))␊ );␊ })␊ }␊ @@ -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))␊ );␊ })␊ }␊ @@ -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))␊ );␊ })␊ }␊ @@ -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))␊ );␊ })␊ }␊ @@ -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))␊ );␊ })␊ }␊ @@ -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))␊ );␊ })␊ }␊ @@ -234,25 +297,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 8ee67b5d83ef2e95272bfbecccf13a9140b5b0e2..d793029ac0e0b26c5557d2f6145fb0ee62b8a004 100644 GIT binary patch literal 1152 zcmV-`1b_QMRzVBIM)Eq4>Luwh#%tJAtw;6g?ud`NU3+K^o| z002A>Bu3&8f$$-?9x$#LMbLdbo#aZ4LlU6nZFyVnmgn^vV0xxKg+Q6~U=>`~gP1U> zrq)`Iw>|gC<8ANBCtIFvO%1o)4B56f#@})yXKW;#V}ME#IfM`iWxNNjH)Uzt$`Mwr zjdmGq-2hNWOb30iPUA6(5BhZC3-yCezq$vTcl@KB0~DYV83Vo6jYJj4b_ z+`~YReFU)dFh`Wh(Ik645v(Y94!L4of2j8lsz zVc5ubb5BYkt*hVheJ+kTj8hfp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#fJ<%~0qz9hh9?{52Bv?)@*~kRx&7zi^)ml=d zQhdqFQ$ldlCOV{^y;~Z^uoR8x`n-1?)4Z^3Ti^c8cZm;bGxsLxEiL@ zPl~_6RCEiuZIAvh!PdiAMl+fjmtcj7UE3PzfxcCSgv?ZY^UH58YR0=ysXW!q$NG#n zAI8GvJf`V>qv{J(+|uB+2;aqVw*k6EG?;cHWIk}A!Nr8aKh}us{L1tpKNm7)FqZ&1<@S<05II`QUCw| 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..dd477023e 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' +].forEach((p) => { + test(`normalizePath - ${p}`, (t) => { + const normalPath = normalizePath(p); + t.is(posix.normalize(p), normalPath); + }); +}); From 9b6d4bcb8e16e6b178313525e423daa84c7b19bb Mon Sep 17 00:00:00 2001 From: Danny Gleckler Date: Fri, 18 Oct 2024 15:28:59 -0400 Subject: [PATCH 2/6] Adjust after prettier --- packages/dynamic-import-vars/src/index.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/packages/dynamic-import-vars/src/index.js b/packages/dynamic-import-vars/src/index.js index 0526aafb7..daad3e8d9 100644 --- a/packages/dynamic-import-vars/src/index.js +++ b/packages/dynamic-import-vars/src/index.js @@ -13,10 +13,7 @@ import { normalizePath } from './dynamic-import-to-glob'; -const normalizePathString = normalizePath - .toString() - .substring(6) - .replace(/\n {2}/g, '\n '); +const normalizePathString = normalizePath.toString().substring(10); function dynamicImportVariables({ include, exclude, warnOnError, errorWhenNoFilesFound } = {}) { const filter = createFilter(include, exclude); From c5aff1924766300c56bf5ba682bbdbf3cde3b95b Mon Sep 17 00:00:00 2001 From: Danny Gleckler Date: Fri, 18 Oct 2024 16:11:34 -0400 Subject: [PATCH 3/6] Handle nesting better --- .../src/dynamic-import-to-glob.js | 2 +- ...llup-plugin-dynamic-import-vars.test.js.md | 22 +++++++++--------- ...up-plugin-dynamic-import-vars.test.js.snap | Bin 1152 -> 1163 bytes .../test/src/dynamic-import-to-glob.test.mjs | 3 ++- 4 files changed, 14 insertions(+), 13 deletions(-) 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 004c0394c..90dfbb26d 100644 --- a/packages/dynamic-import-vars/src/dynamic-import-to-glob.js +++ b/packages/dynamic-import-vars/src/dynamic-import-to-glob.js @@ -131,6 +131,6 @@ export const normalizePath = (p) => // remove any ./ inside the path .replace(/\/\.\//g, '/') // remove dir/ + ../ pairs - .replace(/([^/.][^/]*\/)(([^/.][^/]*\/)(\.\.\/))?(\.\.\/)/g, '') + .replace(/([^/.][^/]*\/)(([^/.][^/]*\/)(([^/.][^/]*\/)(\.\.\/))*?(\.\.\/))*?(\.\.\/)/g, '') // remove unnecessary leading ./ .replace(/^(\.\/)+(?=\.\.\/)/g, ''); 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 2363f7a4d..2a103535f 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 @@ -13,7 +13,7 @@ Generated by [AVA](https://avajs.dev). // remove any ./ inside the path␊ .replace(/\\/\\.\\//g, '/')␊ // remove dir/ + ../ pairs␊ - .replace(/([^/.][^/]*\\/)(([^/.][^/]*\\/)(\\.\\.\\/))?(\\.\\.\\/)/g, '')␊ + .replace(/([^/.][^/]*\\/)(([^/.][^/]*\\/)(([^/.][^/]*\\/)(\\.\\.\\/))*?(\\.\\.\\/))*?(\\.\\.\\/)/g, '')␊ // remove unnecessary leading ./␊ .replace(/^(\\.\\/)+(?=\\.\\.\\/)/g, '');␊ switch (normalPath) {␊ @@ -43,7 +43,7 @@ Generated by [AVA](https://avajs.dev). // remove any ./ inside the path␊ .replace(/\\/\\.\\//g, '/')␊ // remove dir/ + ../ pairs␊ - .replace(/([^/.][^/]*\\/)(([^/.][^/]*\\/)(\\.\\.\\/))?(\\.\\.\\/)/g, '')␊ + .replace(/([^/.][^/]*\\/)(([^/.][^/]*\\/)(([^/.][^/]*\\/)(\\.\\.\\/))*?(\\.\\.\\/))*?(\\.\\.\\/)/g, '')␊ // remove unnecessary leading ./␊ .replace(/^(\\.\\/)+(?=\\.\\.\\/)/g, '');␊ switch (normalPath) {␊ @@ -51,7 +51,7 @@ Generated by [AVA](https://avajs.dev). 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-qW3pa8f7.js');␊ + case './sub-dir/fixture-upwards-path.js': return import('./fixture-upwards-path-gQ8U7UtJ.js');␊ default: return new Promise(function(resolve, reject) {␊ (typeof queueMicrotask === 'function' ? queueMicrotask : setTimeout)(␊ reject.bind(null, new Error("Unknown variable dynamic import: " + normalPath))␊ @@ -76,7 +76,7 @@ Generated by [AVA](https://avajs.dev). // remove any ./ inside the path␊ .replace(/\\/\\.\\//g, '/')␊ // remove dir/ + ../ pairs␊ - .replace(/([^/.][^/]*\\/)(([^/.][^/]*\\/)(\\.\\.\\/))?(\\.\\.\\/)/g, '')␊ + .replace(/([^/.][^/]*\\/)(([^/.][^/]*\\/)(([^/.][^/]*\\/)(\\.\\.\\/))*?(\\.\\.\\/))*?(\\.\\.\\/)/g, '')␊ // remove unnecessary leading ./␊ .replace(/^(\\.\\/)+(?=\\.\\.\\/)/g, '');␊ switch (normalPath) {␊ @@ -106,7 +106,7 @@ Generated by [AVA](https://avajs.dev). // remove any ./ inside the path␊ .replace(/\\/\\.\\//g, '/')␊ // remove dir/ + ../ pairs␊ - .replace(/([^/.][^/]*\\/)(([^/.][^/]*\\/)(\\.\\.\\/))?(\\.\\.\\/)/g, '')␊ + .replace(/([^/.][^/]*\\/)(([^/.][^/]*\\/)(([^/.][^/]*\\/)(\\.\\.\\/))*?(\\.\\.\\/))*?(\\.\\.\\/)/g, '')␊ // remove unnecessary leading ./␊ .replace(/^(\\.\\/)+(?=\\.\\.\\/)/g, '');␊ switch (normalPath) {␊ @@ -138,7 +138,7 @@ Generated by [AVA](https://avajs.dev). // remove any ./ inside the path␊ .replace(/\\/\\.\\//g, '/')␊ // remove dir/ + ../ pairs␊ - .replace(/([^/.][^/]*\\/)(([^/.][^/]*\\/)(\\.\\.\\/))?(\\.\\.\\/)/g, '')␊ + .replace(/([^/.][^/]*\\/)(([^/.][^/]*\\/)(([^/.][^/]*\\/)(\\.\\.\\/))*?(\\.\\.\\/))*?(\\.\\.\\/)/g, '')␊ // remove unnecessary leading ./␊ .replace(/^(\\.\\/)+(?=\\.\\.\\/)/g, '');␊ switch (normalPath) {␊ @@ -168,7 +168,7 @@ Generated by [AVA](https://avajs.dev). // remove any ./ inside the path␊ .replace(/\\/\\.\\//g, '/')␊ // remove dir/ + ../ pairs␊ - .replace(/([^/.][^/]*\\/)(([^/.][^/]*\\/)(\\.\\.\\/))?(\\.\\.\\/)/g, '')␊ + .replace(/([^/.][^/]*\\/)(([^/.][^/]*\\/)(([^/.][^/]*\\/)(\\.\\.\\/))*?(\\.\\.\\/))*?(\\.\\.\\/)/g, '')␊ // remove unnecessary leading ./␊ .replace(/^(\\.\\/)+(?=\\.\\.\\/)/g, '');␊ switch (normalPath) {␊ @@ -176,7 +176,7 @@ Generated by [AVA](https://avajs.dev). 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-qW3pa8f7.js');␊ + case './sub-dir/fixture-upwards-path.js': return import('./fixture-upwards-path-gQ8U7UtJ.js');␊ default: return new Promise(function(resolve, reject) {␊ (typeof queueMicrotask === 'function' ? queueMicrotask : setTimeout)(␊ reject.bind(null, new Error("Unknown variable dynamic import: " + normalPath))␊ @@ -190,7 +190,7 @@ Generated by [AVA](https://avajs.dev). // remove any ./ inside the path␊ .replace(/\\/\\.\\//g, '/')␊ // remove dir/ + ../ pairs␊ - .replace(/([^/.][^/]*\\/)(([^/.][^/]*\\/)(\\.\\.\\/))?(\\.\\.\\/)/g, '')␊ + .replace(/([^/.][^/]*\\/)(([^/.][^/]*\\/)(([^/.][^/]*\\/)(\\.\\.\\/))*?(\\.\\.\\/))*?(\\.\\.\\/)/g, '')␊ // remove unnecessary leading ./␊ .replace(/^(\\.\\/)+(?=\\.\\.\\/)/g, '');␊ switch (normalPath) {␊ @@ -209,7 +209,7 @@ Generated by [AVA](https://avajs.dev). // remove any ./ inside the path␊ .replace(/\\/\\.\\//g, '/')␊ // remove dir/ + ../ pairs␊ - .replace(/([^/.][^/]*\\/)(([^/.][^/]*\\/)(\\.\\.\\/))?(\\.\\.\\/)/g, '')␊ + .replace(/([^/.][^/]*\\/)(([^/.][^/]*\\/)(([^/.][^/]*\\/)(\\.\\.\\/))*?(\\.\\.\\/))*?(\\.\\.\\/)/g, '')␊ // remove unnecessary leading ./␊ .replace(/^(\\.\\/)+(?=\\.\\.\\/)/g, '');␊ switch (normalPath) {␊ @@ -277,7 +277,7 @@ Generated by [AVA](https://avajs.dev). // remove any ./ inside the path␊ .replace(/\\/\\.\\//g, '/')␊ // remove dir/ + ../ pairs␊ - .replace(/([^/.][^/]*\\/)(([^/.][^/]*\\/)(\\.\\.\\/))?(\\.\\.\\/)/g, '')␊ + .replace(/([^/.][^/]*\\/)(([^/.][^/]*\\/)(([^/.][^/]*\\/)(\\.\\.\\/))*?(\\.\\.\\/))*?(\\.\\.\\/)/g, '')␊ // remove unnecessary leading ./␊ .replace(/^(\\.\\/)+(?=\\.\\.\\/)/g, '');␊ switch (normalPath) {␊ 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 d793029ac0e0b26c5557d2f6145fb0ee62b8a004..35bbebf7c101227935159def83d6db0a8bcf0e09 100644 GIT binary patch literal 1163 zcmV;61a$jBRzV(7mNJeVfCN@+fL8`O`Qlt+T zF5=i4l2_8$yjJQx=$4t=>@9gXt5m5sZjx+#|GpT@a5t863 zQ3Gg0dQ$@ca2yaA@&g3IMqoQYnWR2~8sKRoTVNCr4-IF_*|N7Br{4hG(ak9Yp9%+7 zz_uNT2o-Xwukmc#v7bNNcAnqfa!lj=%Pl({$TV-SSA5OJKQnyH7}d* zcBUq=ZDa^5#(KL9wx$8dL#n(17^kTkClJ6R5<$0}ko$3fmQRI6CKtqN+PhL;1%WCS z3~06z#%XI}HLV_v**713)=67tW6i^C&BJV2`xyD&X!D?e>0^(?fja5O@DQE~9#V?>C z$gvmUB4_<3Q19 z%w~C(&e#7a(fRm#P3M}<=h3+W)16$n^XBPVP3wbw{|EBj-Ny@K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&t7XSKBs*SJcO-#`;t&TJvjU=#lWb!Z`r}cFzV~YVQR|O?kN&8O z%!D}jnnZXpfv|F%YlPj2Tazn>3^CnJcVRUnV|xC>Pv$aZ5(6#dutGaLD;1xp)(&r@ z_(&nNGv@yc+Lh)wVdqnJhKF&W@2#HeA z5hC|vhJbx-Z`m@dsbS22y1Wd=)qUiaHM(v?`->AYiV)pU^el7a2U4>E#k6%Jwby4z z^^O$LT_!Kom2WNBmG21As4_aK-kVgQ;--e6Pk0Z<+8uPCXi$BTgcP{Y;A1g^o{{yN zf0b1IpG&2%5T|L*jKXcJvzS!T|?Cm{5c+3-RpfXY+6}Oqco#p5O{cUinS(;Nz*|84#sVW;>`jsn!1lsUKGL S!|EORFUa3({1Pb~oe 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 dd477023e..b6d304602 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 @@ -296,7 +296,8 @@ test('escapes []', (t) => { './../فو/bar/../../foo.js', '../foo/../foo.js', './../foo/../bar/.././../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); From 943df50c947041cca9c89b11a1c636e03dce8515 Mon Sep 17 00:00:00 2001 From: Danny Gleckler Date: Fri, 18 Oct 2024 16:20:44 -0400 Subject: [PATCH 4/6] Fix lint --- .../dynamic-import-vars/test/src/dynamic-import-to-glob.test.mjs | 1 - 1 file changed, 1 deletion(-) 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 b6d304602..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 @@ -288,7 +288,6 @@ test('escapes []', (t) => { t.is(glob, './*/\\[foo\\].js'); }); - [ './../foo/./../foo.js', './../../a/foo.js', From 16b1fd7e63e75e40e89d3bf7265a5d6b22bd0f81 Mon Sep 17 00:00:00 2001 From: Danny Gleckler Date: Fri, 18 Oct 2024 17:11:16 -0400 Subject: [PATCH 5/6] Account for minification --- .../src/dynamic-import-to-glob.js | 19 +- packages/dynamic-import-vars/src/index.js | 4 +- ...llup-plugin-dynamic-import-vars.test.js.md | 166 +++++++++++------- ...up-plugin-dynamic-import-vars.test.js.snap | Bin 1163 -> 1195 bytes 4 files changed, 114 insertions(+), 75 deletions(-) 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 90dfbb26d..2697d27ee 100644 --- a/packages/dynamic-import-vars/src/dynamic-import-to-glob.js +++ b/packages/dynamic-import-vars/src/dynamic-import-to-glob.js @@ -126,11 +126,14 @@ 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, ''); +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 daad3e8d9..d13549897 100644 --- a/packages/dynamic-import-vars/src/index.js +++ b/packages/dynamic-import-vars/src/index.js @@ -13,7 +13,7 @@ import { normalizePath } from './dynamic-import-to-glob'; -const normalizePathString = normalizePath.toString().substring(10); +const normalizePathString = normalizePath.toString().replace(/\n/g, '\n '); function dynamicImportVariables({ include, exclude, warnOnError, errorWhenNoFilesFound } = {}) { const filter = createFilter(include, exclude); @@ -78,7 +78,7 @@ function dynamicImportVariables({ include, exclude, warnOnError, errorWhenNoFile // will turn these into chunks automatically ms.prepend( `function __variableDynamicImportRuntime${dynamicImportIndex}__(path) { - const normalPath = path${normalizePathString}; + path = (${normalizePathString})(path); switch (normalPath) { ${paths .map((p) => ` case '${p}': return import('${p}'${importArg ? `, ${importArg}` : ''});`) 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 2a103535f..496447938 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,13 +9,17 @@ Generated by [AVA](https://avajs.dev). > Snapshot 1 `function __variableDynamicImportRuntime0__(path) {␊ - const normalPath = path␊ - // remove any ./ inside the path␊ - .replace(/\\/\\.\\//g, '/')␊ - // remove dir/ + ../ pairs␊ - .replace(/([^/.][^/]*\\/)(([^/.][^/]*\\/)(([^/.][^/]*\\/)(\\.\\.\\/))*?(\\.\\.\\/))*?(\\.\\.\\/)/g, '')␊ - // remove unnecessary leading ./␊ - .replace(/^(\\.\\/)+(?=\\.\\.\\/)/g, '');␊ + path = (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');␊ @@ -39,19 +43,23 @@ Generated by [AVA](https://avajs.dev). > Snapshot 1 `function __variableDynamicImportRuntime0__(path) {␊ - const normalPath = path␊ - // remove any ./ inside the path␊ - .replace(/\\/\\.\\//g, '/')␊ - // remove dir/ + ../ pairs␊ - .replace(/([^/.][^/]*\\/)(([^/.][^/]*\\/)(([^/.][^/]*\\/)(\\.\\.\\/))*?(\\.\\.\\/))*?(\\.\\.\\/)/g, '')␊ - // remove unnecessary leading ./␊ - .replace(/^(\\.\\/)+(?=\\.\\.\\/)/g, '');␊ + path = (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-gQ8U7UtJ.js');␊ + case './sub-dir/fixture-upwards-path.js': return import('./fixture-upwards-path-3kYiIOy5.js');␊ default: return new Promise(function(resolve, reject) {␊ (typeof queueMicrotask === 'function' ? queueMicrotask : setTimeout)(␊ reject.bind(null, new Error("Unknown variable dynamic import: " + normalPath))␊ @@ -72,13 +80,17 @@ Generated by [AVA](https://avajs.dev). > Snapshot 1 `function __variableDynamicImportRuntime0__(path) {␊ - const normalPath = path␊ - // remove any ./ inside the path␊ - .replace(/\\/\\.\\//g, '/')␊ - // remove dir/ + ../ pairs␊ - .replace(/([^/.][^/]*\\/)(([^/.][^/]*\\/)(([^/.][^/]*\\/)(\\.\\.\\/))*?(\\.\\.\\/))*?(\\.\\.\\/)/g, '')␊ - // remove unnecessary leading ./␊ - .replace(/^(\\.\\/)+(?=\\.\\.\\/)/g, '');␊ + path = (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');␊ @@ -102,13 +114,17 @@ Generated by [AVA](https://avajs.dev). > Snapshot 1 `function __variableDynamicImportRuntime0__(path) {␊ - const normalPath = path␊ - // remove any ./ inside the path␊ - .replace(/\\/\\.\\//g, '/')␊ - // remove dir/ + ../ pairs␊ - .replace(/([^/.][^/]*\\/)(([^/.][^/]*\\/)(([^/.][^/]*\\/)(\\.\\.\\/))*?(\\.\\.\\/))*?(\\.\\.\\/)/g, '')␊ - // remove unnecessary leading ./␊ - .replace(/^(\\.\\/)+(?=\\.\\.\\/)/g, '');␊ + path = (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');␊ @@ -134,13 +150,17 @@ Generated by [AVA](https://avajs.dev). > Snapshot 1 `function __variableDynamicImportRuntime0__(path) {␊ - const normalPath = path␊ - // remove any ./ inside the path␊ - .replace(/\\/\\.\\//g, '/')␊ - // remove dir/ + ../ pairs␊ - .replace(/([^/.][^/]*\\/)(([^/.][^/]*\\/)(([^/.][^/]*\\/)(\\.\\.\\/))*?(\\.\\.\\/))*?(\\.\\.\\/)/g, '')␊ - // remove unnecessary leading ./␊ - .replace(/^(\\.\\/)+(?=\\.\\.\\/)/g, '');␊ + path = (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');␊ @@ -164,19 +184,23 @@ Generated by [AVA](https://avajs.dev). > Snapshot 1 `function __variableDynamicImportRuntime2__(path) {␊ - const normalPath = path␊ - // remove any ./ inside the path␊ - .replace(/\\/\\.\\//g, '/')␊ - // remove dir/ + ../ pairs␊ - .replace(/([^/.][^/]*\\/)(([^/.][^/]*\\/)(([^/.][^/]*\\/)(\\.\\.\\/))*?(\\.\\.\\/))*?(\\.\\.\\/)/g, '')␊ - // remove unnecessary leading ./␊ - .replace(/^(\\.\\/)+(?=\\.\\.\\/)/g, '');␊ + path = (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-gQ8U7UtJ.js');␊ + case './sub-dir/fixture-upwards-path.js': return import('./fixture-upwards-path-3kYiIOy5.js');␊ default: return new Promise(function(resolve, reject) {␊ (typeof queueMicrotask === 'function' ? queueMicrotask : setTimeout)(␊ reject.bind(null, new Error("Unknown variable dynamic import: " + normalPath))␊ @@ -186,13 +210,17 @@ Generated by [AVA](https://avajs.dev). }␊ ␊ function __variableDynamicImportRuntime1__(path) {␊ - const normalPath = path␊ - // remove any ./ inside the path␊ - .replace(/\\/\\.\\//g, '/')␊ - // remove dir/ + ../ pairs␊ - .replace(/([^/.][^/]*\\/)(([^/.][^/]*\\/)(([^/.][^/]*\\/)(\\.\\.\\/))*?(\\.\\.\\/))*?(\\.\\.\\/)/g, '')␊ - // remove unnecessary leading ./␊ - .replace(/^(\\.\\/)+(?=\\.\\.\\/)/g, '');␊ + path = (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');␊ @@ -205,13 +233,17 @@ Generated by [AVA](https://avajs.dev). }␊ ␊ function __variableDynamicImportRuntime0__(path) {␊ - const normalPath = path␊ - // remove any ./ inside the path␊ - .replace(/\\/\\.\\//g, '/')␊ - // remove dir/ + ../ pairs␊ - .replace(/([^/.][^/]*\\/)(([^/.][^/]*\\/)(([^/.][^/]*\\/)(\\.\\.\\/))*?(\\.\\.\\/))*?(\\.\\.\\/)/g, '')␊ - // remove unnecessary leading ./␊ - .replace(/^(\\.\\/)+(?=\\.\\.\\/)/g, '');␊ + path = (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');␊ @@ -273,13 +305,17 @@ Generated by [AVA](https://avajs.dev). > Snapshot 1 `function __variableDynamicImportRuntime0__(path) {␊ - const normalPath = path␊ - // remove any ./ inside the path␊ - .replace(/\\/\\.\\//g, '/')␊ - // remove dir/ + ../ pairs␊ - .replace(/([^/.][^/]*\\/)(([^/.][^/]*\\/)(([^/.][^/]*\\/)(\\.\\.\\/))*?(\\.\\.\\/))*?(\\.\\.\\/)/g, '')␊ - // remove unnecessary leading ./␊ - .replace(/^(\\.\\/)+(?=\\.\\.\\/)/g, '');␊ + path = (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');␊ 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 35bbebf7c101227935159def83d6db0a8bcf0e09..cd8622916dc35f28875b2f2193f8ca6680f2c075 100644 GIT binary patch literal 1195 zcmV;c1XTM$RzVVhS~} z*iDL|*&mAt00000000B+TFY+RMih1G6a|QEx+}W62m+9dMKV&7qOqYG2~wq5q&512 zgE+Q_yObk|LR?z$;RN-}9vmh3n|Vo)1#q?t4K zp?i4lP%pb~z`b4h^Dn5V3UEhK)(a4PDyU>6l6|h^PcMC+DDrgipT&ib7Sh+14;McE z;MHO`W}c#)!PeG35tO(AJ{&R9vj7CDSTHcM0YEf*2XGt^81j7t!iHcwK$)aIg6d-iKAC6>i~{1J;cPe? z_J-s18lXG6S%KtJ;lL8uwgVBNLRMfH&o&+V`Lj*u`OOW-G)^CG*y)C*d2_YmGP9%7 zjF>U>P)Z_(5Fqg>>w)c57_(_)NK3|Qy8_f)(Et)N4|;kn52*6`VC11*o8$t(BN9Qk zosj!+fR;~%Ma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;)8{CeuD}}**2hUP0BS%K$N`T7 z9~f8QV&1|(QzvZxQz{ewUMi)9SWSIq79JR-uoofCtYYfnEGRua>L@O(K0f;>{7kG? zc6mBPWf}FLoXv-YJnnmh0S>(&_7QqCz_aDl=0Pz{*9Hrggn1w@{Th7u{#3mOL`oFt d3@S>$`adr9bF6-jUH$*K{0nS3MAx|{006(GCsP0b From b60fe9ef4eec9cc4c449b4c202c8b4fc1ab94da1 Mon Sep 17 00:00:00 2001 From: Danny Gleckler Date: Fri, 18 Oct 2024 17:59:56 -0400 Subject: [PATCH 6/6] Fix variable name --- packages/dynamic-import-vars/src/index.js | 2 +- ...llup-plugin-dynamic-import-vars.test.js.md | 22 +++++++++--------- ...up-plugin-dynamic-import-vars.test.js.snap | Bin 1195 -> 1204 bytes 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/packages/dynamic-import-vars/src/index.js b/packages/dynamic-import-vars/src/index.js index d13549897..c7152cfd8 100644 --- a/packages/dynamic-import-vars/src/index.js +++ b/packages/dynamic-import-vars/src/index.js @@ -78,7 +78,7 @@ function dynamicImportVariables({ include, exclude, warnOnError, errorWhenNoFile // will turn these into chunks automatically ms.prepend( `function __variableDynamicImportRuntime${dynamicImportIndex}__(path) { - path = (${normalizePathString})(path); + const normalPath = (${normalizePathString})(path); switch (normalPath) { ${paths .map((p) => ` case '${p}': return import('${p}'${importArg ? `, ${importArg}` : ''});`) 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 496447938..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,7 +9,7 @@ Generated by [AVA](https://avajs.dev). > Snapshot 1 `function __variableDynamicImportRuntime0__(path) {␊ - path = (function normalizePath(p) {␊ + const normalPath = (function normalizePath(p) {␊ return (␊ p␊ // remove any ./ inside the path␊ @@ -43,7 +43,7 @@ Generated by [AVA](https://avajs.dev). > Snapshot 1 `function __variableDynamicImportRuntime0__(path) {␊ - path = (function normalizePath(p) {␊ + const normalPath = (function normalizePath(p) {␊ return (␊ p␊ // remove any ./ inside the path␊ @@ -59,7 +59,7 @@ Generated by [AVA](https://avajs.dev). 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-3kYiIOy5.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: " + normalPath))␊ @@ -80,7 +80,7 @@ Generated by [AVA](https://avajs.dev). > Snapshot 1 `function __variableDynamicImportRuntime0__(path) {␊ - path = (function normalizePath(p) {␊ + const normalPath = (function normalizePath(p) {␊ return (␊ p␊ // remove any ./ inside the path␊ @@ -114,7 +114,7 @@ Generated by [AVA](https://avajs.dev). > Snapshot 1 `function __variableDynamicImportRuntime0__(path) {␊ - path = (function normalizePath(p) {␊ + const normalPath = (function normalizePath(p) {␊ return (␊ p␊ // remove any ./ inside the path␊ @@ -150,7 +150,7 @@ Generated by [AVA](https://avajs.dev). > Snapshot 1 `function __variableDynamicImportRuntime0__(path) {␊ - path = (function normalizePath(p) {␊ + const normalPath = (function normalizePath(p) {␊ return (␊ p␊ // remove any ./ inside the path␊ @@ -184,7 +184,7 @@ Generated by [AVA](https://avajs.dev). > Snapshot 1 `function __variableDynamicImportRuntime2__(path) {␊ - path = (function normalizePath(p) {␊ + const normalPath = (function normalizePath(p) {␊ return (␊ p␊ // remove any ./ inside the path␊ @@ -200,7 +200,7 @@ Generated by [AVA](https://avajs.dev). 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-3kYiIOy5.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: " + normalPath))␊ @@ -210,7 +210,7 @@ Generated by [AVA](https://avajs.dev). }␊ ␊ function __variableDynamicImportRuntime1__(path) {␊ - path = (function normalizePath(p) {␊ + const normalPath = (function normalizePath(p) {␊ return (␊ p␊ // remove any ./ inside the path␊ @@ -233,7 +233,7 @@ Generated by [AVA](https://avajs.dev). }␊ ␊ function __variableDynamicImportRuntime0__(path) {␊ - path = (function normalizePath(p) {␊ + const normalPath = (function normalizePath(p) {␊ return (␊ p␊ // remove any ./ inside the path␊ @@ -305,7 +305,7 @@ Generated by [AVA](https://avajs.dev). > Snapshot 1 `function __variableDynamicImportRuntime0__(path) {␊ - path = (function normalizePath(p) {␊ + const normalPath = (function normalizePath(p) {␊ return (␊ p␊ // remove any ./ inside the path␊ 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 cd8622916dc35f28875b2f2193f8ca6680f2c075..1ab24197b226ce656081aeb72d63d176b0a47b5f 100644 GIT binary patch literal 1204 zcmV;l1WWrtRzV#JiiDMnqKCgaZx0<4h`GT!bW8 zC8`TeFh)Z8*Yq`}SBxkh0U%Jtf`O4W0HVP=fa8F`knbT7)(6`G$|Us>R2Lzga@5fl z7zM;b!&!IM?RCfL)IfK1vqZ_K!hu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