Skip to content

Commit

Permalink
Create @babel/plugin-proposal-dynamic-import (#9552)
Browse files Browse the repository at this point in the history
* Create @babel/plugin-proposal-dynamic-import

* Use airbnb/babel-plugin-dynamic-import-node

Do not duplicate code, which will unavoidably lead
to bugs being fixed in one plugin and not in the other.

* Update error message

* Add error callback to amd interop

* Update babel-plugin-dynamic-import-node
  • Loading branch information
nicolo-ribaudo committed Jun 30, 2019
1 parent a4170b5 commit 38f8bba
Show file tree
Hide file tree
Showing 61 changed files with 406 additions and 45 deletions.
3 changes: 3 additions & 0 deletions packages/babel-plugin-proposal-dynamic-import/.npmignore
@@ -0,0 +1,3 @@
src
test
*.log
19 changes: 19 additions & 0 deletions packages/babel-plugin-proposal-dynamic-import/README.md
@@ -0,0 +1,19 @@
# @babel/plugin-proposal-dynamic-import

> Transform import() expressions
See our website [@babel/plugin-proposal-dynamic-import](https://babeljs.io/docs/en/next/babel-plugin-proposal-dynamic-import.html) for more information.

## Install

Using npm:

```sh
npm install --save-dev @babel/plugin-proposal-dynamic-import
```

or using yarn:

```sh
yarn add @babel/plugin-proposal-dynamic-import --dev
```
25 changes: 25 additions & 0 deletions packages/babel-plugin-proposal-dynamic-import/package.json
@@ -0,0 +1,25 @@
{
"name": "@babel/plugin-proposal-dynamic-import",
"version": "7.4.0",
"description": "Transform import() expressions",
"repository": "https://github.com/babel/babel/tree/master/packages/babel-plugin-proposal-dynamic-import",
"license": "MIT",
"publishConfig": {
"access": "public"
},
"main": "lib/index.js",
"keywords": [
"babel-plugin"
],
"dependencies": {
"@babel/helper-plugin-utils": "^7.0.0",
"@babel/plugin-syntax-dynamic-import": "^7.2.0"
},
"peerDependencies": {
"@babel/core": "^7.0.0-0"
},
"devDependencies": {
"@babel/core": "^7.2.0",
"@babel/helper-plugin-test-runner": "^7.0.0"
}
}
41 changes: 41 additions & 0 deletions packages/babel-plugin-proposal-dynamic-import/src/index.js
@@ -0,0 +1,41 @@
import { declare } from "@babel/helper-plugin-utils";
import syntaxDynamicImport from "@babel/plugin-syntax-dynamic-import";
import { version } from "../package.json";

const SUPPORTED_MODULES = ["commonjs", "amd", "systemjs"];

const MODULES_NOT_FOUND = `\
@babel/plugin-proposal-dynamic-import depends on a modules
transform plugin. Supported plugins are:
- @babel/plugin-transform-modules-commonjs ^7.4.0
- @babel/plugin-transform-modules-amd ^7.4.0
- @babel/plugin-transform-modules-systemjs ^7.4.0
If you are using Webpack or Rollup and thus don't want
Babel to transpile your imports and exports, you can use
the @babel/plugin-syntax-dynamic-import plugin and let your
bundler handle dynamic imports.
`;

export default declare(api => {
api.assertVersion(7);

return {
name: "proposal-dynamic-import",
inherits: syntaxDynamicImport,

pre() {
this.file.set("@babel/plugin-proposal-dynamic-import", version);
},

visitor: {
Program() {
const modules = this.file.get("@babel/plugin-transform-modules-*");

if (!SUPPORTED_MODULES.includes(modules)) {
throw new Error(MODULES_NOT_FOUND);
}
},
},
};
});
@@ -0,0 +1,3 @@
import "a";

import("b");
@@ -0,0 +1,7 @@
{
"plugins": [
"syntax-dynamic-import",
"transform-modules-amd",
"external-helpers"
]
}
@@ -0,0 +1,5 @@
define(["a"], function (_a) {
"use strict";

import("b");
});
@@ -0,0 +1 @@
var modP = import("mod");
@@ -0,0 +1,5 @@
define(["require"], function (_require) {
"use strict";

var modP = new Promise((_resolve, _reject) => _require(["mod"], imported => _resolve(babelHelpers.interopRequireWildcard(imported)), _reject));
});
@@ -0,0 +1 @@
var modP = import("mod");
@@ -0,0 +1,7 @@
{
"plugins": [
"proposal-dynamic-import",
["transform-modules-amd", { "noInterop": true }],
"external-helpers"
]
}
@@ -0,0 +1,3 @@
define(["require"], function (_require) {
var modP = new Promise((_resolve, _reject) => _require(["mod"], imported => _resolve(imported), _reject));
});
@@ -0,0 +1,7 @@
{
"plugins": [
"proposal-dynamic-import",
"transform-modules-amd",
"external-helpers"
]
}
@@ -0,0 +1 @@
var modP = import("mod");
@@ -0,0 +1,3 @@
define(["require"], function (_require) {
var modP = new Promise((_resolve, _reject) => _require(["mod"], imported => _resolve(babelHelpers.interopRequireWildcard(imported)), _reject));
});
@@ -0,0 +1,8 @@
var mod;

import foo from "foo";

import("mod").then(m => mod = m);

export { mod };

@@ -0,0 +1,12 @@
define(["require", "exports", "foo"], function (_require, _exports, _foo) {
"use strict";

Object.defineProperty(_exports, "__esModule", {
value: true
});
_exports.mod = void 0;
_foo = babelHelpers.interopRequireDefault(_foo);
var mod;
_exports.mod = mod;
new Promise((_resolve, _reject) => _require(["mod"], imported => _resolve(babelHelpers.interopRequireWildcard(imported)), _reject)).then(m => _exports.mod = mod = m);
});
@@ -0,0 +1,4 @@
return import("./mod.js").then(({ default: def, named }) => {
expect(def()).toBe("foo");
expect(named()).toBe("bar");
});
@@ -0,0 +1,2 @@
module.exports = () => "foo";
module.exports.named = () => "bar";
@@ -0,0 +1,5 @@
{
"parserOpts": {
"allowReturnOutsideFunction": true
}
}
@@ -0,0 +1,4 @@
return import("./mod.js").then(({ default: def, named }) => {
expect(def()).toBe("foo");
expect(named()).toBe("bar");
});
@@ -0,0 +1,5 @@
module.exports = {
__esModule: true,
default: () => "foo",
named: () => "bar",
};
@@ -0,0 +1,5 @@
{
"parserOpts": {
"allowReturnOutsideFunction": true
}
}
@@ -0,0 +1,3 @@
import "a";

import("b");
@@ -0,0 +1,7 @@
{
"plugins": [
"syntax-dynamic-import",
"transform-modules-commonjs",
"external-helpers"
]
}
@@ -0,0 +1,5 @@
"use strict";

require("a");

import("b");
@@ -0,0 +1 @@
var modP = import("mod");
@@ -0,0 +1,3 @@
"use strict";

var modP = Promise.resolve().then(() => babelHelpers.interopRequireWildcard(require("mod")));
@@ -0,0 +1 @@
var modP = import("mod");
@@ -0,0 +1,7 @@
{
"plugins": [
"proposal-dynamic-import",
["transform-modules-commonjs", { "noInterop": true }],
"external-helpers"
]
}
@@ -0,0 +1 @@
var modP = Promise.resolve().then(() => require("mod"));
@@ -0,0 +1,7 @@
{
"plugins": [
"proposal-dynamic-import",
"transform-modules-commonjs",
"external-helpers"
]
}
@@ -0,0 +1 @@
var modP = import("mod");
@@ -0,0 +1 @@
var modP = Promise.resolve().then(() => babelHelpers.interopRequireWildcard(require("mod")));
@@ -0,0 +1,6 @@
var require = "foo";

(async function () {
var require = "bar";
await import("./mod");
})();
@@ -0,0 +1,6 @@
var _require2 = "foo";

(async function () {
var _require = "bar";
await Promise.resolve().then(() => babelHelpers.interopRequireWildcard(require("./mod")));
})();
@@ -0,0 +1 @@
import(2);
@@ -0,0 +1 @@
Promise.resolve().then(() => babelHelpers.interopRequireWildcard(require(`${2}`)));
@@ -0,0 +1,4 @@
{
"plugins": ["proposal-dynamic-import"],
"throws": "@babel/plugin-proposal-dynamic-import depends on a modules\ntransform plugin. Supported plugins are:"
}
@@ -0,0 +1,5 @@
// TODO: This should throw in Babel 8

import "a";

import("b");
@@ -0,0 +1,7 @@
{
"plugins": [
"syntax-dynamic-import",
"transform-modules-systemjs",
"external-helpers"
]
}
@@ -0,0 +1,11 @@
System.register(["a"], function (_export, _context) {
"use strict";

return {
setters: [function (_a) {}],
execute: function () {
// TODO: This should throw in Babel 8
_context.import("b");
}
};
});
@@ -0,0 +1 @@
var modP = import("mod");
@@ -0,0 +1,11 @@
System.register([], function (_export, _context) {
"use strict";

var modP;
return {
setters: [],
execute: function () {
modP = _context.import("mod");
}
};
});
@@ -0,0 +1,7 @@
{
"plugins": [
"proposal-dynamic-import",
"transform-modules-systemjs",
"external-helpers"
]
}
@@ -0,0 +1 @@
var modP = import("mod");
@@ -0,0 +1,11 @@
System.register([], function (_export, _context) {
"use strict";

var modP;
return {
setters: [],
execute: function () {
modP = _context.import("mod");
}
};
});
3 changes: 3 additions & 0 deletions packages/babel-plugin-proposal-dynamic-import/test/index.js
@@ -0,0 +1,3 @@
import runner from "@babel/helper-plugin-test-runner";

runner(__dirname);
3 changes: 2 additions & 1 deletion packages/babel-plugin-transform-modules-amd/package.json
Expand Up @@ -10,7 +10,8 @@
"main": "lib/index.js",
"dependencies": {
"@babel/helper-module-transforms": "^7.1.0",
"@babel/helper-plugin-utils": "^7.0.0"
"@babel/helper-plugin-utils": "^7.0.0",
"babel-plugin-dynamic-import-node": "^2.3.0"
},
"keywords": [
"babel-plugin"
Expand Down

0 comments on commit 38f8bba

Please sign in to comment.