diff --git a/.babelrc.js b/.babelrc.js index 0caaf19bf0..9a6311a2c5 100644 --- a/.babelrc.js +++ b/.babelrc.js @@ -1,48 +1,69 @@ "use strict"; -const env = process.env.BABEL_ENV || process.env.NODE_ENV; -const envOpts = { - loose: true, +module.exports = function(api) { + const env = api.env(); + + const includeCoverage = process.env.BABEL_COVERAGE === "true"; + + const envOpts = { + loose: true, + modules: false, + exclude: ["transform-typeof-symbol"], + }; + + let convertESM = true; + + switch (env) { + // Configs used during bundling builds. + case "babylon": + case "standalone": + convertESM = false; + break; + case "production": + // Config during builds before publish. + break; + case "development": + envOpts.debug = true; + envOpts.targets = { + node: "current", + }; + break; + case "test": + envOpts.targets = { + node: "current", + }; + break; + } + + const config = { + comments: false, + presets: [["@babel/env", envOpts]], + plugins: [ + // TODO: Use @babel/preset-flow when + // https://github.com/babel/babel/issues/7233 is fixed + "@babel/plugin-transform-flow-strip-types", + ["@babel/proposal-class-properties", { loose: true }], + "@babel/proposal-export-namespace-from", + "@babel/proposal-numeric-separator", + ["@babel/proposal-object-rest-spread", { useBuiltIns: true }], + convertESM ? "@babel/transform-modules-commonjs" : null, + ].filter(Boolean), + overrides: [ + { + test: "packages/babylon", + plugins: [ + "babel-plugin-transform-charcodes", + ["@babel/transform-for-of", { assumeArray: true }], + ], + }, + ], + }; + + // we need to do this as long as we do not test everything from source + if (includeCoverage) { + config.auxiliaryCommentBefore = "istanbul ignore next"; + config.plugins.push("babel-plugin-istanbul"); + } + + return config; }; - -switch (env) { - case "development": - envOpts.debug = true; - // fall-through - case "test": - case "cov": - envOpts.targets = { - node: "current", - }; -} - -const config = { - comments: false, - presets: [["@babel/env", envOpts]], - plugins: [ - // TODO: Use @babel/preset-flow when - // https://github.com/babel/babel/issues/7233 is fixed - "@babel/plugin-transform-flow-strip-types", - ["@babel/proposal-class-properties", { loose: true }], - "@babel/proposal-export-namespace-from", - "@babel/proposal-numeric-separator", - ["@babel/proposal-object-rest-spread", { useBuiltIns: true }], - ], - overrides: [ - { - test: "packages/babylon", - plugins: [ - "babel-plugin-transform-charcodes", - ["@babel/transform-for-of", { assumeArray: true }], - ], - }, - ], -}; - -// we need to do this as long as we do not test everything from source -if (env === "cov") { - config.auxiliaryCommentBefore = "istanbul ignore next"; - config.plugins.push("babel-plugin-istanbul"); -} - -module.exports = config; diff --git a/.babelrc.rollup.js b/.babelrc.rollup.js deleted file mode 100644 index e8897bf436..0000000000 --- a/.babelrc.rollup.js +++ /dev/null @@ -1,16 +0,0 @@ -"use strict"; - -const cloneDeep = require("lodash/cloneDeep"); -const babelrc = require("./.babelrc.js"); - -const config = cloneDeep(babelrc); - -const presetEnv = config.presets.find(preset => preset[0] === "@babel/env"); - -if (!presetEnv) { - throw new Error("Error while extracting @preset/env from .babelrc.js"); -} - -presetEnv[1].modules = false; - -module.exports = config; diff --git a/Gulpfile.js b/Gulpfile.js index 5fd5071fa7..e1cff38668 100644 --- a/Gulpfile.js +++ b/Gulpfile.js @@ -98,8 +98,9 @@ function buildRollup(packages) { format: "cjs", plugins: [ rollupBabel({ + envName: "babylon", babelrc: false, - extends: "./.babelrc.rollup.js", + extends: "./.babelrc.js", }), rollupNodeResolve(), ], diff --git a/Makefile b/Makefile index 94e80e1e2f..7566640405 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ MAKEFLAGS = -j1 FLOW_COMMIT = 622bbc4f07acb77eb1109830c70815f827401d90 TEST262_COMMIT = 52f70e2f637731aae92a9c9a2d831310c3ab2e1e -export NODE_ENV = test +export BABEL_ENV = test # Fix color output until TravisCI fixes https://github.com/travis-ci/travis-ci/issues/7967 export FORCE_COLOR = true @@ -22,7 +22,7 @@ build: clean node scripts/generators/typescript.js > ./packages/babel-types/lib/index.d.ts # generate docs node scripts/generators/docs.js > ./packages/babel-types/README.md -ifneq ("$(BABEL_ENV)", "cov") +ifneq ("$(BABEL_COVERAGE)", "true") make build-standalone make build-preset-env-standalone endif @@ -82,7 +82,7 @@ test-ci: test-ci-coverage: SHELL:=/bin/bash test-ci-coverage: - BABEL_ENV=cov make bootstrap + BABEL_COVERAGE=true BABEL_ENV=test make bootstrap TEST_TYPE=cov ./scripts/test-cov.sh bash <(curl -s https://codecov.io/bash) -f coverage/coverage-final.json diff --git a/package.json b/package.json index 651b1b3334..deb3894cfe 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,7 @@ "devDependencies": { "@babel/cli": "7.0.0-beta.41", "@babel/core": "7.0.0-beta.41", + "@babel/plugin-transform-modules-commonjs": "7.0.0-beta.41", "@babel/preset-env": "7.0.0-beta.41", "@babel/preset-flow": "7.0.0-beta.41", "@babel/preset-stage-0": "7.0.0-beta.41", diff --git a/scripts/gulp-tasks.js b/scripts/gulp-tasks.js index 972474f433..d49bb9a1d1 100644 --- a/scripts/gulp-tasks.js +++ b/scripts/gulp-tasks.js @@ -39,25 +39,18 @@ function webpackBuild(opts) { test: /\.js$/, loader: "babel-loader", options: { + // Use the bundled config so that module syntax is passed through + // for Webpack. + envName: "standalone", + // Some of the node_modules may have their own "babel" section in // their project.json (or a ".babelrc" file). We need to ignore // those as we're using our own Babel options. babelrc: false, - presets: [ - [ - "@babel/env", - { - loose: true, - exclude: ["transform-typeof-symbol"], - }, - ], - ], - overrides: [ - { - exclude: /node_modules/, - presets: [["@babel/stage-0", { loose: true }]], - }, - ], + + // We explicitly load the `.babelrc.js` file since searching is + // turned off, but we still want to use the main config. + extends: "./.babelrc.js", }, }, ],