Skip to content

Commit

Permalink
Account for minification
Browse files Browse the repository at this point in the history
  • Loading branch information
bearfriend committed Oct 18, 2024
1 parent 943df50 commit 16b1fd7
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 75 deletions.
19 changes: 11 additions & 8 deletions packages/dynamic-import-vars/src/dynamic-import-to-glob.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, '')
);
}
4 changes: 2 additions & 2 deletions packages/dynamic-import-vars/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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}` : ''});`)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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');␊
Expand All @@ -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))␊
Expand All @@ -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');␊
Expand All @@ -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');␊
Expand All @@ -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');␊
Expand All @@ -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))␊
Expand All @@ -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');␊
Expand All @@ -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');␊
Expand Down Expand Up @@ -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');␊
Expand Down
Binary file not shown.

0 comments on commit 16b1fd7

Please sign in to comment.