Support cjs shorthand for modules option in preset-es2015 & preset-env (#7283)

This commit is contained in:
Mateusz Burzyński
2018-01-30 17:46:21 +01:00
committed by Henry Zhu
parent 252ea5a966
commit 3316a554bf
22 changed files with 111 additions and 4 deletions

View File

@@ -231,7 +231,7 @@ Enable ["loose" transformations](http://2ality.com/2015/12/babel6-loose-mode.htm
### `modules`
`"amd" | "umd" | "systemjs" | "commonjs" | false`, defaults to `"commonjs"`.
`"amd" | "umd" | "systemjs" | "commonjs" | "cjs" | false`, defaults to `"commonjs"`.
Enable transformation of ES6 module syntax to another module type.

View File

@@ -1,6 +1,7 @@
export default {
amd: "transform-modules-amd",
commonjs: "transform-modules-commonjs",
cjs: "transform-modules-commonjs",
systemjs: "transform-modules-systemjs",
umd: "transform-modules-umd",
};

View File

@@ -0,0 +1 @@
export function a () {}

View File

@@ -0,0 +1,8 @@
{
"presets": [
["../../../../lib", {
"modules": "amd",
"loose": true
}]
]
}

View File

@@ -0,0 +1,8 @@
define(["exports"], function (_exports) {
"use strict";
_exports.__esModule = true;
_exports.a = a;
function a() {}
});

View File

@@ -0,0 +1 @@
import a from "a";

View File

@@ -0,0 +1,7 @@
{
"presets": [
["../../../../lib", {
"modules": "cjs"
}]
]
}

View File

@@ -0,0 +1,5 @@
"use strict";
var _a = _interopRequireDefault(require("a"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

View File

@@ -0,0 +1 @@
import a from "a";

View File

@@ -0,0 +1,7 @@
{
"presets": [
["../../../../lib", {
"modules": "commonjs"
}]
]
}

View File

@@ -0,0 +1,5 @@
"use strict";
var _a = _interopRequireDefault(require("a"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

View File

@@ -0,0 +1 @@
import a from "a";

View File

@@ -0,0 +1,7 @@
{
"presets": [
["../../../../lib", {
"modules": "systemjs"
}]
]
}

View File

@@ -0,0 +1,11 @@
System.register(["a"], function (_export, _context) {
"use strict";
var a;
return {
setters: [function (_a) {
a = _a.default;
}],
execute: function () {}
};
});

View File

@@ -0,0 +1 @@
import a from "a";

View File

@@ -0,0 +1,7 @@
{
"presets": [
["../../../../lib", {
"modules": "umd"
}]
]
}

View File

@@ -0,0 +1,19 @@
(function (global, factory) {
if (typeof define === "function" && define.amd) {
define(["a"], factory);
} else if (typeof exports !== "undefined") {
factory(require("a"));
} else {
var mod = {
exports: {}
};
factory(global.a);
global.input = mod.exports;
}
})(this, function (_a) {
"use strict";
_a = _interopRequireDefault(_a);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
});

View File

@@ -44,7 +44,7 @@ Enable "loose" transformations for any plugins in this preset that allow them.
### `modules`
`"amd" | "umd" | "systemjs" | "commonjs" | false`, defaults to `"commonjs"`.
`"amd" | "umd" | "systemjs" | "commonjs" | "cjs" | false`, defaults to `"commonjs"`.
Enable transformation of ES6 module syntax to another module type.

View File

@@ -24,7 +24,7 @@ import transformES2015Instanceof from "@babel/plugin-transform-instanceof";
import transformRegenerator from "@babel/plugin-transform-regenerator";
export default function(api, opts = {}) {
const moduleTypes = ["commonjs", "amd", "umd", "systemjs"];
const moduleTypes = ["commonjs", "cjs", "amd", "umd", "systemjs"];
let loose = false;
let modules = "commonjs";
let spec = false;
@@ -72,7 +72,10 @@ export default function(api, opts = {}) {
transformES2015BlockScoping,
transformES2015TypeofSymbol,
transformES2015Instanceof,
modules === "commonjs" && [transformES2015ModulesCommonJS, optsLoose],
(modules === "commonjs" || modules === "cjs") && [
transformES2015ModulesCommonJS,
optsLoose,
],
modules === "systemjs" && [transformES2015ModulesSystemJS, optsLoose],
modules === "amd" && [transformES2015ModulesAMD, optsLoose],
modules === "umd" && [transformES2015ModulesUMD, optsLoose],

View File

@@ -0,0 +1 @@
export function a () {}

View File

@@ -0,0 +1,5 @@
{
"presets": [
["es2015", { "modules": "cjs" }]
]
}

View File

@@ -0,0 +1,8 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.a = a;
function a() {}