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: exclude external imports #1193

Merged
merged 5 commits into from Aug 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 18 additions & 1 deletion packages/dynamic-import-vars/src/dynamic-import-to-glob.js
Expand Up @@ -61,11 +61,28 @@ function expressionToGlob(node) {
}
}

const defaultProtocol = 'file:';
const ignoredProtocols = ['data:', 'http:', 'https:'];

function shouldIgnore(glob) {
const containsAsterisk = glob.includes('*');

const globURL = new URL(glob, defaultProtocol);

const containsIgnoredProtocol = ignoredProtocols.some(
(ignoredProtocol) => ignoredProtocol === globURL.protocol
);

return !containsAsterisk || containsIgnoredProtocol;
}

export function dynamicImportToGlob(node, sourceString) {
let glob = expressionToGlob(node);
if (!glob.includes('*') || glob.startsWith('data:')) {

if (shouldIgnore(glob)) {
return null;
}

glob = glob.replace(/\*\*/g, '*');

if (glob.startsWith('*')) {
Expand Down
Expand Up @@ -17,6 +17,24 @@ test('template literal with variable filename', (t) => {
t.is(glob, './foo/*.js');
});

test('external', (t) => {
const ast = CustomParser.parse('import(`https://some.cdn.com/package/${version}/index.js`);', {
sourceType: 'module'
});

const glob = dynamicImportToGlob(ast.body[0].expression.arguments[0]);
t.is(glob, null);
});

test('external - leaves bare module specifiers starting with https in tact', (t) => {
const ast = CustomParser.parse('import("http_utils");', {
sourceType: 'module'
});

const glob = dynamicImportToGlob(ast.body[0].expression.arguments[0]);
t.is(glob, null);
});

test('data uri', (t) => {
const ast = CustomParser.parse('import(`data:${bar}`);', {
sourceType: 'module'
Expand Down