Skip to content

Commit

Permalink
fix: multiline imports are not working
Browse files Browse the repository at this point in the history
  • Loading branch information
theKashey committed Jun 22, 2020
1 parent 6399672 commit ab2da95
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
31 changes: 31 additions & 0 deletions __tests__/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,37 @@ describe('scanForImports', () => {
]);
});

it('should match support multiline imports', () => {
const imports = {};
remapImports(
[
{
file: 'a',
content: `
blabla;import(
/* webpackChunkName: "chunk-a" */
"./a.js"
);
something else
import(
// ts-ignore
"./b.js"
);
`,
},
],
root,
root,
(a, b) => a + b,
imports,
() => true
);
expect(Object.values(imports)).toEqual([
`[() => import(/* webpackChunkName: \"chunk-a\" */'${rel}/a.js'), 'chunk-a', '${rel}/a.js', false] /* from .a */`,
`[() => import('${rel}/b.js'), '', '${rel}/b.js', false] /* from .a */`,
]);
});

it('should remove webpackPrefetch and webpackPreload', () => {
const imports = {};
remapImports(
Expand Down
14 changes: 13 additions & 1 deletion src/scanners/scanForImports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,19 @@ const RESOLVE_EXTENSIONS = ['.js', '.jsx', '.ts', '.tsx', '.mjs'];

const trimImport = (str: string) => str.replace(/['"]/g, '');

const getImports = getMatchString(`(['"]?[\\w-/.@]+['"]?)\\)`, 1);
const getImportMatch = getMatchString(`(['"]?[\\w-/.@]+['"]?)\\)`, 1);
const getImports = (str: string) =>
getImportMatch(
str
// remove comments
.replace(/\/\*([^\*]*)\*\//gi, '')
.replace(/\/\/(.*)/gi, '')
// remove new lines
.replace(/\n/gi, '')
// remove spaces?
.replace(/[\s]+\)/i, ')')
);

const getComment = getMatchString(/\/\*.*\*\// as any, 0);

const getChunkName = getMatchString('webpackChunkName: "([^"]*)"', 1);
Expand Down

0 comments on commit ab2da95

Please sign in to comment.