Skip to content

Commit ad65935

Browse files
leoseligTommy Leunen
authored andcommitted
feat: Add support for new ES dynamic import() (#143)
1 parent c937f79 commit ad65935

4 files changed

Lines changed: 71 additions & 2 deletions

File tree

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
"babel-cli": "^6.24.0",
4040
"babel-core": "^6.24.0",
4141
"babel-jest": "^19.0.0",
42+
"babel-plugin-syntax-dynamic-import": "^6.18.0",
4243
"babel-plugin-transform-es2015-modules-commonjs": "^6.24.0",
4344
"babel-plugin-transform-object-rest-spread": "^6.23.0",
4445
"babel-preset-env": "^1.2.2",

src/transformers/call.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import { matchesPattern, mapPathString } from '../utils';
1+
import {
2+
matchesPattern,
3+
mapPathString,
4+
isImportCall,
5+
} from '../utils';
26

37

48
const patterns = [
@@ -14,8 +18,9 @@ const patterns = [
1418

1519
export default function transformCall(nodePath, state) {
1620
const calleePath = nodePath.get('callee');
21+
const isNormalCall = patterns.some(pattern => matchesPattern(state.types, calleePath, pattern));
1722

18-
if (patterns.some(pattern => matchesPattern(state.types, calleePath, pattern))) {
23+
if (isNormalCall || isImportCall(state.types, nodePath)) {
1924
mapPathString(nodePath.get('arguments.0'), state);
2025
}
2126
}

src/utils.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,7 @@ export function mapPathString(nodePath, state) {
5353
nodePath.replaceWith(state.types.stringLiteral(modulePath));
5454
}
5555
}
56+
57+
export function isImportCall(types, calleePath) {
58+
return types.isImport(calleePath.node.callee);
59+
}

test/dynamicImport.test.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/* eslint-env jest */
2+
import { transform } from 'babel-core'; // eslint-disable-line import/no-extraneous-dependencies
3+
import plugin from '../src';
4+
5+
// According to https://github.com/tc39/proposal-dynamic-import
6+
7+
describe('import()', () => {
8+
const transformerOpts = {
9+
babelrc: false,
10+
plugins: [
11+
// We need to add the corresponding syntax plugin
12+
// in order to parse the `import()`-calls
13+
'syntax-dynamic-import',
14+
[plugin, {
15+
root: [
16+
'./test/testproject/src',
17+
],
18+
alias: {
19+
test: './test/testproject/test',
20+
},
21+
}],
22+
],
23+
};
24+
25+
it('should resolve the path based on the root config', () => {
26+
const code = 'import("app").then(() => {}).catch(() => {});';
27+
const result = transform(code, transformerOpts);
28+
29+
expect(result.code).toBe('import("./test/testproject/src/app").then(() => {}).catch(() => {});');
30+
});
31+
32+
it('should alias the path', () => {
33+
const code = 'import("test/tools").then(() => {}).catch(() => {});';
34+
const result = transform(code, transformerOpts);
35+
36+
expect(result.code).toBe('import("./test/testproject/test/tools").then(() => {}).catch(() => {});');
37+
});
38+
39+
it('should not change the path', () => {
40+
const code = 'import("./something").then(() => {}).catch(() => {});';
41+
const result = transform(code, transformerOpts);
42+
43+
expect(result.code).toBe('import("./something").then(() => {}).catch(() => {});');
44+
});
45+
46+
it('should handle the first argument not being a string literal', () => {
47+
const code = 'import(path).then(() => {}).catch(() => {});';
48+
const result = transform(code, transformerOpts);
49+
50+
expect(result.code).toBe('import(path).then(() => {}).catch(() => {});');
51+
});
52+
53+
it('should handle an empty path', () => {
54+
const code = 'import("").then(() => {}).catch(() => {});';
55+
const result = transform(code, transformerOpts);
56+
57+
expect(result.code).toBe('import("").then(() => {}).catch(() => {});');
58+
});
59+
});

0 commit comments

Comments
 (0)