Skip to content

Commit 944e7d3

Browse files
authored
fix(dynamic-import-vars): escape special glob characters (#1636)
1 parent 2c58b01 commit 944e7d3

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

packages/dynamic-import-vars/src/dynamic-import-to-glob.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
import path from 'path';
22

3+
import fastGlob from 'fast-glob';
4+
35
export class VariableDynamicImportError extends Error {}
46

57
/* eslint-disable-next-line no-template-curly-in-string */
68
const example = 'For example: import(`./foo/${bar}.js`).';
79

810
function sanitizeString(str) {
11+
if (str === '') return str;
912
if (str.includes('*')) {
1013
throw new VariableDynamicImportError('A dynamic import cannot contain * characters.');
1114
}
12-
return str;
15+
return fastGlob.escapePath(str);
1316
}
1417

1518
function templateLiteralToGlob(node) {

packages/dynamic-import-vars/test/src/dynamic-import-to-glob.test.mjs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,3 +268,21 @@ test('throws when dynamic import imports does not contain a file extension', (t)
268268
);
269269
t.true(error instanceof VariableDynamicImportError);
270270
});
271+
272+
test('escapes ()', (t) => {
273+
const ast = parse('import(`./${foo}/(foo).js`);', {
274+
sourceType: 'module'
275+
});
276+
277+
const glob = dynamicImportToGlob(ast.body[0].expression.source);
278+
t.is(glob, './*/\\(foo\\).js');
279+
});
280+
281+
test('escapes []', (t) => {
282+
const ast = parse('import(`./${foo}/[foo].js`);', {
283+
sourceType: 'module'
284+
});
285+
286+
const glob = dynamicImportToGlob(ast.body[0].expression.source);
287+
t.is(glob, './*/\\[foo\\].js');
288+
});

0 commit comments

Comments
 (0)