From cc078e777e8e71d72efab5f70006657de1e6fd1f Mon Sep 17 00:00:00 2001 From: Leo Selig Date: Wed, 29 Mar 2017 11:28:39 +0200 Subject: [PATCH 1/6] Add support for new ES dynamic `import()` - adds support for resolving module options on calls to the stage 3 proposal for `import()` (for details, see draft at https://github.com/tc39/proposal-dynamic-import) - this is very similar to the `Systom.import()`-implementation - we need to add `babel-preset-stage-2` in order to parse the `import()`-calls in our tests --- package.json | 3 +- src/transformers/call.js | 13 +++++++-- src/utils.js | 4 +++ test/dynamicImport.test.js | 58 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 75 insertions(+), 3 deletions(-) create mode 100644 test/dynamicImport.test.js diff --git a/package.json b/package.json index 0411459..e0f5749 100644 --- a/package.json +++ b/package.json @@ -42,6 +42,7 @@ "babel-plugin-transform-es2015-modules-commonjs": "^6.24.0", "babel-plugin-transform-object-rest-spread": "^6.23.0", "babel-preset-env": "^1.2.2", + "babel-preset-stage-2": "^6.22.0", "common-tags": "^1.4.0", "eslint": "^3.18.0", "eslint-config-airbnb-base": "^11.1.2", @@ -55,7 +56,7 @@ "pretest": "npm run lint", "test": "jest --coverage", "test:suite": "jest", - "test:watch": "jest --watch", + "test:watch": "jest --watch test/dynamicImport.test.js", "prepublish": "npm run compile", "release": "standard-version" }, diff --git a/src/transformers/call.js b/src/transformers/call.js index 089cbda..604a763 100644 --- a/src/transformers/call.js +++ b/src/transformers/call.js @@ -1,4 +1,8 @@ -import { matchesPattern, mapPathString } from '../utils'; +import { + matchesPattern, + mapPathString, + isImportCall, +} from '../utils'; const patterns = [ @@ -14,8 +18,13 @@ const patterns = [ export default function transformCall(nodePath, state) { const calleePath = nodePath.get('callee'); + const isNormalCall = patterns.some(pattern => matchesPattern(state.types, calleePath, pattern)); - if (patterns.some(pattern => matchesPattern(state.types, calleePath, pattern))) { + if (isNormalCall) { + mapPathString(nodePath.get('arguments.0'), state); + } + + if (isImportCall(state.types, nodePath)) { mapPathString(nodePath.get('arguments.0'), state); } } diff --git a/src/utils.js b/src/utils.js index e849491..ad00450 100644 --- a/src/utils.js +++ b/src/utils.js @@ -44,3 +44,7 @@ export function mapPathString(nodePath, state) { nodePath.replaceWith(state.types.stringLiteral(modulePath)); } } + +export function isImportCall(types, calleePath) { + return types.isImport(calleePath.node.callee); +} diff --git a/test/dynamicImport.test.js b/test/dynamicImport.test.js new file mode 100644 index 0000000..f6c07dd --- /dev/null +++ b/test/dynamicImport.test.js @@ -0,0 +1,58 @@ +/* eslint-env jest */ +import { transform } from 'babel-core'; // eslint-disable-line import/no-extraneous-dependencies +import plugin from '../src'; + +// According to https://github.com/tc39/proposal-dynamic-import + +describe('import()', () => { + const transformerOpts = { + babelrc: false, + // We need to add the stage-2 preset in order to parse the `import()`-calls + presets: ['stage-2'], + plugins: [ + [plugin, { + root: [ + './test/testproject/src', + ], + alias: { + test: './test/testproject/test', + }, + }], + ], + }; + + it('CFG should resolve the path based on the root config', () => { + const code = 'import("app").then(() => {}).catch(() => {});'; + const result = transform(code, transformerOpts); + + expect(result.code).toBe('import("./test/testproject/src/app").then(() => {}).catch(() => {});'); + }); + + it('should alias the path', () => { + const code = 'import("test/tools").then(() => {}).catch(() => {});'; + const result = transform(code, transformerOpts); + + expect(result.code).toBe('import("./test/testproject/test/tools").then(() => {}).catch(() => {});'); + }); + + it('should not change the path', () => { + const code = 'import("./something").then(() => {}).catch(() => {});'; + const result = transform(code, transformerOpts); + + expect(result.code).toBe('import("./something").then(() => {}).catch(() => {});'); + }); + + it('should handle the first argument not being a string literal', () => { + const code = 'import(path).then(() => {}).catch(() => {});'; + const result = transform(code, transformerOpts); + + expect(result.code).toBe('import(path).then(() => {}).catch(() => {});'); + }); + + it('should handle an empty path', () => { + const code = 'import(\'\').then(() => {}).catch(() => {});'; + const result = transform(code, transformerOpts); + + expect(result.code).toBe('import(\'\').then(() => {}).catch(() => {});'); + }); +}); From 1197c2c03425c365e6410d435f80222e19172e28 Mon Sep 17 00:00:00 2001 From: Leo Selig Date: Tue, 18 Apr 2017 09:23:00 +0200 Subject: [PATCH 2/6] Use explicit babel syntax plugin for test - this replaces the `stage-2` preset that was pulling in way more than needed --- package.json | 2 +- test/dynamicImport.test.js | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index e0f5749..df4fea7 100644 --- a/package.json +++ b/package.json @@ -39,10 +39,10 @@ "babel-cli": "^6.24.0", "babel-core": "^6.24.0", "babel-jest": "^19.0.0", + "babel-plugin-syntax-dynamic-import": "^6.18.0", "babel-plugin-transform-es2015-modules-commonjs": "^6.24.0", "babel-plugin-transform-object-rest-spread": "^6.23.0", "babel-preset-env": "^1.2.2", - "babel-preset-stage-2": "^6.22.0", "common-tags": "^1.4.0", "eslint": "^3.18.0", "eslint-config-airbnb-base": "^11.1.2", diff --git a/test/dynamicImport.test.js b/test/dynamicImport.test.js index f6c07dd..d2ec57a 100644 --- a/test/dynamicImport.test.js +++ b/test/dynamicImport.test.js @@ -7,9 +7,10 @@ import plugin from '../src'; describe('import()', () => { const transformerOpts = { babelrc: false, - // We need to add the stage-2 preset in order to parse the `import()`-calls - presets: ['stage-2'], plugins: [ + // We need to add the corresponding syntax plugin + // in order to parse the `import()`-calls + 'syntax-dynamic-import', [plugin, { root: [ './test/testproject/src', From 1d567522b5cb5fd887b9e7bcd008fd3afb4c4676 Mon Sep 17 00:00:00 2001 From: Leo Selig Date: Tue, 18 Apr 2017 09:23:45 +0200 Subject: [PATCH 3/6] Remove forgotten `test:watch` run script --- package.json | 1 - 1 file changed, 1 deletion(-) diff --git a/package.json b/package.json index df4fea7..e4fd215 100644 --- a/package.json +++ b/package.json @@ -56,7 +56,6 @@ "pretest": "npm run lint", "test": "jest --coverage", "test:suite": "jest", - "test:watch": "jest --watch test/dynamicImport.test.js", "prepublish": "npm run compile", "release": "standard-version" }, From 7e3cd3698de910f5febcd6cbbc8ac44816109791 Mon Sep 17 00:00:00 2001 From: Leo Selig Date: Tue, 18 Apr 2017 09:25:21 +0200 Subject: [PATCH 4/6] Group import-call detection --- src/transformers/call.js | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/transformers/call.js b/src/transformers/call.js index 604a763..9e35518 100644 --- a/src/transformers/call.js +++ b/src/transformers/call.js @@ -20,11 +20,7 @@ export default function transformCall(nodePath, state) { const calleePath = nodePath.get('callee'); const isNormalCall = patterns.some(pattern => matchesPattern(state.types, calleePath, pattern)); - if (isNormalCall) { - mapPathString(nodePath.get('arguments.0'), state); - } - - if (isImportCall(state.types, nodePath)) { + if (isNormalCall || isImportCall(state.types, nodePath)) { mapPathString(nodePath.get('arguments.0'), state); } } From 8bedab3dceff1cc1267bcb1541df0f7afa18564b Mon Sep 17 00:00:00 2001 From: Leo Selig Date: Sun, 23 Apr 2017 18:46:43 +0200 Subject: [PATCH 5/6] Re-add `test:watch` run script --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index e4fd215..a4f9bb5 100644 --- a/package.json +++ b/package.json @@ -56,6 +56,7 @@ "pretest": "npm run lint", "test": "jest --coverage", "test:suite": "jest", + "test:watch": "jest --watch", "prepublish": "npm run compile", "release": "standard-version" }, From ecbff837ac5b747d9ea620308f832e72c9186eed Mon Sep 17 00:00:00 2001 From: Leo Selig Date: Sun, 23 Apr 2017 18:47:02 +0200 Subject: [PATCH 6/6] Clean up minor flaws in tests --- test/dynamicImport.test.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/dynamicImport.test.js b/test/dynamicImport.test.js index d2ec57a..1552a81 100644 --- a/test/dynamicImport.test.js +++ b/test/dynamicImport.test.js @@ -22,7 +22,7 @@ describe('import()', () => { ], }; - it('CFG should resolve the path based on the root config', () => { + it('should resolve the path based on the root config', () => { const code = 'import("app").then(() => {}).catch(() => {});'; const result = transform(code, transformerOpts); @@ -51,9 +51,9 @@ describe('import()', () => { }); it('should handle an empty path', () => { - const code = 'import(\'\').then(() => {}).catch(() => {});'; + const code = 'import("").then(() => {}).catch(() => {});'; const result = transform(code, transformerOpts); - expect(result.code).toBe('import(\'\').then(() => {}).catch(() => {});'); + expect(result.code).toBe('import("").then(() => {}).catch(() => {});'); }); });