Skip to content

Apply useESModules option to @babel/preset-env #5973

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 40 additions & 38 deletions packages/babel-preset-react-app/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,47 +8,49 @@

const path = require('path');

const validateBoolOption = (name, value, defaultValue) => {
if (typeof value === 'undefined') {
value = defaultValue;
}

if (typeof value !== 'boolean') {
throw new Error(`Preset react-app: '${name}' option must be a boolean.`);
const assert = (assertion, message) => {
if (!assertion) {
throw new Error(message);
}
};

return value;
const assertBool = (value, name) => {
assert(
typeof value === 'boolean',
`Preset react-app: '${name}' option must be a boolean.`
);
};

module.exports = function(api, opts, env) {
if (!opts) {
opts = {};
}
const modules = ['amd', 'umd', 'systemjs', 'commonjs', 'cjs', 'auto', false];

module.exports = function(api, opts, env) {
var isEnvDevelopment = env === 'development';
var isEnvProduction = env === 'production';
var isEnvTest = env === 'test';

var useESModules = validateBoolOption(
'useESModules',
opts.useESModules,
isEnvDevelopment || isEnvProduction
);
var isFlowEnabled = validateBoolOption('flow', opts.flow, true);
var isTypeScriptEnabled = validateBoolOption(
'typescript',
opts.typescript,
true
);
var areHelpersEnabled = validateBoolOption('helpers', opts.helpers, true);
var useAbsoluteRuntime = validateBoolOption(
'absoluteRuntime',
opts.absoluteRuntime,
true
const defaults = {
modules: false,
useESModules: isEnvDevelopment || isEnvProduction,
absoluteRuntime: true,
typescript: true,
helpers: true,
flow: true,
};

opts = Object.assign({}, defaults, opts || {});

assert(
modules.includes(opts.modules),
`Preset react-app: 'modules' option must be one of: ${modules.join(', ')}.`
);
assertBool(opts.useESModules, 'useESModules');
assertBool(opts.absoluteRuntime, 'absoluteRuntime');
assertBool(opts.typescript, 'typescript');
assertBool(opts.helpers, 'helpers');
assertBool(opts.flow, 'flow');

var absoluteRuntimePath = undefined;
if (useAbsoluteRuntime) {
if (opts.absoluteRuntime) {
absoluteRuntimePath = path.dirname(
require.resolve('@babel/runtime/package.json')
);
Expand Down Expand Up @@ -90,8 +92,8 @@ module.exports = function(api, opts, env) {
// If users import all core-js they're probably not concerned with
// bundle size. We shouldn't rely on magic to try and shrink it.
useBuiltIns: false,
// Do not transform modules to CJS
modules: false,
// Do not transform modules to CJS by default
modules: opts.modules,
// Exclude transforms that make all code slower
exclude: ['transform-typeof-symbol'],
},
Expand All @@ -107,7 +109,7 @@ module.exports = function(api, opts, env) {
useBuiltIns: true,
},
],
isTypeScriptEnabled && [require('@babel/preset-typescript').default],
opts.typescript && [require('@babel/preset-typescript').default],
].filter(Boolean),
plugins: [
// Strip flow types before any other transform, emulating the behavior
Expand All @@ -116,7 +118,7 @@ module.exports = function(api, opts, env) {
// We will conditionally enable this plugin below in overrides as it clashes with
// @babel/plugin-proposal-decorators when using TypeScript.
// https://github.com/facebook/create-react-app/issues/5741
isFlowEnabled && [
opts.flow && [
require('@babel/plugin-transform-flow-strip-types').default,
false,
],
Expand All @@ -128,7 +130,7 @@ module.exports = function(api, opts, env) {
// don't work without it: https://github.com/babel/babel/issues/7215
require('@babel/plugin-transform-destructuring').default,
// Turn on legacy decorators for TypeScript files
isTypeScriptEnabled && [
opts.typescript && [
require('@babel/plugin-proposal-decorators').default,
false,
],
Expand Down Expand Up @@ -156,12 +158,12 @@ module.exports = function(api, opts, env) {
require('@babel/plugin-transform-runtime').default,
{
corejs: false,
helpers: areHelpersEnabled,
helpers: opts.helpers,
regenerator: true,
// https://babeljs.io/docs/en/babel-plugin-transform-runtime#useesmodules
// We should turn this on once the lowest version of Node LTS
// supports ES Modules.
useESModules,
useESModules: opts.useESModules,
// Undocumented option that lets us encapsulate our runtime, ensuring
// the correct version is used
// https://github.com/babel/babel/blob/090c364a90fe73d36a30707fc612ce037bdbbb24/packages/babel-plugin-transform-runtime/src/index.js#L35-L42
Expand All @@ -182,11 +184,11 @@ module.exports = function(api, opts, env) {
require('babel-plugin-dynamic-import-node'),
].filter(Boolean),
overrides: [
isFlowEnabled && {
opts.flow && {
exclude: /\.tsx?$/,
plugins: [require('@babel/plugin-transform-flow-strip-types').default],
},
isTypeScriptEnabled && {
opts.typescript && {
test: /\.tsx?$/,
plugins: [
[
Expand Down