diff --git a/packages/babel-plugin-transform-es2015-modules-systemjs/package.json b/packages/babel-plugin-transform-es2015-modules-systemjs/package.json index 586893e7a3..6184ea4433 100644 --- a/packages/babel-plugin-transform-es2015-modules-systemjs/package.json +++ b/packages/babel-plugin-transform-es2015-modules-systemjs/package.json @@ -14,6 +14,7 @@ "babel-plugin" ], "devDependencies": { - "babel-helper-plugin-test-runner": "^6.18.0" + "babel-helper-plugin-test-runner": "^6.18.0", + "babel-plugin-syntax-dynamic-import": "^6.18.0" } } diff --git a/packages/babel-plugin-transform-es2015-modules-systemjs/src/index.js b/packages/babel-plugin-transform-es2015-modules-systemjs/src/index.js index a542343bed..93091bac29 100644 --- a/packages/babel-plugin-transform-es2015-modules-systemjs/src/index.js +++ b/packages/babel-plugin-transform-es2015-modules-systemjs/src/index.js @@ -22,6 +22,9 @@ let buildExportAll = template(` } `); + +const TYPE_IMPORT = "Import"; + export default function ({ types: t }) { let IGNORE_REASSIGNMENT_SYMBOL = Symbol(); @@ -69,6 +72,14 @@ export default function ({ types: t }) { return { visitor: { + + CallExpression(path, state) { + if (path.node.callee.type === TYPE_IMPORT) { + let contextIdent = state.contextIdent; + path.replaceWith(t.callExpression(t.memberExpression(contextIdent, t.identifier("import")), path.node.arguments)); + } + }, + ReferencedIdentifier(path, state) { if (path.node.name == "__moduleName" && !path.scope.hasBinding("__moduleName")) { path.replaceWith(t.memberExpression(state.contextIdent, t.identifier("id"))); diff --git a/packages/babel-plugin-transform-es2015-modules-systemjs/test/fixtures/dynamic-import/dynamic-import/actual.js b/packages/babel-plugin-transform-es2015-modules-systemjs/test/fixtures/dynamic-import/dynamic-import/actual.js new file mode 100644 index 0000000000..6e6ea63962 --- /dev/null +++ b/packages/babel-plugin-transform-es2015-modules-systemjs/test/fixtures/dynamic-import/dynamic-import/actual.js @@ -0,0 +1,6 @@ +export function lazyLoadOperation () { + return import('./x') + .then(function (x) { + x.y(); + }); +} diff --git a/packages/babel-plugin-transform-es2015-modules-systemjs/test/fixtures/dynamic-import/dynamic-import/expected.js b/packages/babel-plugin-transform-es2015-modules-systemjs/test/fixtures/dynamic-import/dynamic-import/expected.js new file mode 100644 index 0000000000..de2b2da619 --- /dev/null +++ b/packages/babel-plugin-transform-es2015-modules-systemjs/test/fixtures/dynamic-import/dynamic-import/expected.js @@ -0,0 +1,16 @@ +System.register([], function (_export, _context) { + "use strict"; + + function lazyLoadOperation() { + return _context.import('./x').then(function (x) { + x.y(); + }); + } + + _export('lazyLoadOperation', lazyLoadOperation); + + return { + setters: [], + execute: function () {} + }; +}); diff --git a/packages/babel-plugin-transform-es2015-modules-systemjs/test/fixtures/dynamic-import/options.json b/packages/babel-plugin-transform-es2015-modules-systemjs/test/fixtures/dynamic-import/options.json new file mode 100644 index 0000000000..20631e3163 --- /dev/null +++ b/packages/babel-plugin-transform-es2015-modules-systemjs/test/fixtures/dynamic-import/options.json @@ -0,0 +1,3 @@ +{ + "plugins": ["external-helpers", "syntax-dynamic-import", "transform-es2015-modules-systemjs"] +}