Skip to content

Commit 5d18989

Browse files
committed
Add "modules" option
1 parent af0a854 commit 5d18989

File tree

1 file changed

+40
-38
lines changed

1 file changed

+40
-38
lines changed

packages/babel-preset-react-app/create.js

Lines changed: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -8,47 +8,49 @@
88

99
const path = require('path');
1010

11-
const validateBoolOption = (name, value, defaultValue) => {
12-
if (typeof value === 'undefined') {
13-
value = defaultValue;
14-
}
15-
16-
if (typeof value !== 'boolean') {
17-
throw new Error(`Preset react-app: '${name}' option must be a boolean.`);
11+
const assert = (assertion, message) => {
12+
if (!assertion) {
13+
throw new Error(message);
1814
}
15+
};
1916

20-
return value;
17+
const assertBool = (value, name) => {
18+
assert(
19+
typeof value === 'boolean',
20+
`Preset react-app: '${name}' option must be a boolean.`
21+
);
2122
};
2223

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

26+
module.exports = function(api, opts, env) {
2827
var isEnvDevelopment = env === 'development';
2928
var isEnvProduction = env === 'production';
3029
var isEnvTest = env === 'test';
3130

32-
var useESModules = validateBoolOption(
33-
'useESModules',
34-
opts.useESModules,
35-
isEnvDevelopment || isEnvProduction
36-
);
37-
var isFlowEnabled = validateBoolOption('flow', opts.flow, true);
38-
var isTypeScriptEnabled = validateBoolOption(
39-
'typescript',
40-
opts.typescript,
41-
true
42-
);
43-
var areHelpersEnabled = validateBoolOption('helpers', opts.helpers, true);
44-
var useAbsoluteRuntime = validateBoolOption(
45-
'absoluteRuntime',
46-
opts.absoluteRuntime,
47-
true
31+
const defaults = {
32+
modules: false,
33+
useESModules: isEnvDevelopment || isEnvProduction,
34+
absoluteRuntime: true,
35+
typescript: true,
36+
helpers: true,
37+
flow: true,
38+
};
39+
40+
opts = Object.assign({}, defaults, opts || {});
41+
42+
assert(
43+
modules.includes(opts.modules),
44+
`Preset react-app: 'modules' option must be one of: ${modules.join(', ')}.`
4845
);
46+
assertBool(opts.useESModules, 'useESModules');
47+
assertBool(opts.absoluteRuntime, 'absoluteRuntime');
48+
assertBool(opts.typescript, 'typescript');
49+
assertBool(opts.helpers, 'helpers');
50+
assertBool(opts.flow, 'flow');
4951

5052
var absoluteRuntimePath = undefined;
51-
if (useAbsoluteRuntime) {
53+
if (opts.absoluteRuntime) {
5254
absoluteRuntimePath = path.dirname(
5355
require.resolve('@babel/runtime/package.json')
5456
);
@@ -90,8 +92,8 @@ module.exports = function(api, opts, env) {
9092
// If users import all core-js they're probably not concerned with
9193
// bundle size. We shouldn't rely on magic to try and shrink it.
9294
useBuiltIns: false,
93-
// Do not transform modules to CJS
94-
modules: false,
95+
// Do not transform modules to CJS by default
96+
modules: opts.modules,
9597
// Exclude transforms that make all code slower
9698
exclude: ['transform-typeof-symbol'],
9799
},
@@ -107,7 +109,7 @@ module.exports = function(api, opts, env) {
107109
useBuiltIns: true,
108110
},
109111
],
110-
isTypeScriptEnabled && [require('@babel/preset-typescript').default],
112+
opts.typescript && [require('@babel/preset-typescript').default],
111113
].filter(Boolean),
112114
plugins: [
113115
// Strip flow types before any other transform, emulating the behavior
@@ -116,7 +118,7 @@ module.exports = function(api, opts, env) {
116118
// We will conditionally enable this plugin below in overrides as it clashes with
117119
// @babel/plugin-proposal-decorators when using TypeScript.
118120
// https://github.com/facebook/create-react-app/issues/5741
119-
isFlowEnabled && [
121+
opts.flow && [
120122
require('@babel/plugin-transform-flow-strip-types').default,
121123
false,
122124
],
@@ -128,7 +130,7 @@ module.exports = function(api, opts, env) {
128130
// don't work without it: https://github.com/babel/babel/issues/7215
129131
require('@babel/plugin-transform-destructuring').default,
130132
// Turn on legacy decorators for TypeScript files
131-
isTypeScriptEnabled && [
133+
opts.typescript && [
132134
require('@babel/plugin-proposal-decorators').default,
133135
false,
134136
],
@@ -156,12 +158,12 @@ module.exports = function(api, opts, env) {
156158
require('@babel/plugin-transform-runtime').default,
157159
{
158160
corejs: false,
159-
helpers: areHelpersEnabled,
161+
helpers: opts.helpers,
160162
regenerator: true,
161163
// https://babeljs.io/docs/en/babel-plugin-transform-runtime#useesmodules
162164
// We should turn this on once the lowest version of Node LTS
163165
// supports ES Modules.
164-
useESModules,
166+
useESModules: opts.useESModules,
165167
// Undocumented option that lets us encapsulate our runtime, ensuring
166168
// the correct version is used
167169
// https://github.com/babel/babel/blob/090c364a90fe73d36a30707fc612ce037bdbbb24/packages/babel-plugin-transform-runtime/src/index.js#L35-L42
@@ -182,11 +184,11 @@ module.exports = function(api, opts, env) {
182184
require('babel-plugin-dynamic-import-node'),
183185
].filter(Boolean),
184186
overrides: [
185-
isFlowEnabled && {
187+
opts.flow && {
186188
exclude: /\.tsx?$/,
187189
plugins: [require('@babel/plugin-transform-flow-strip-types').default],
188190
},
189-
isTypeScriptEnabled && {
191+
opts.typescript && {
190192
test: /\.tsx?$/,
191193
plugins: [
192194
[

0 commit comments

Comments
 (0)