UNPKG

4.2 kBJavaScriptView Raw
1/**
2 * Copyright (c) 2015-present, Facebook, Inc.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 *
7 * @format
8 */
9
10'use strict';
11
12const defaultPlugins = [
13 [require('@babel/plugin-transform-block-scoping')],
14 // the flow strip types plugin must go BEFORE class properties!
15 // there'll be a test case that fails if you don't.
16 [require('@babel/plugin-transform-flow-strip-types')],
17 [
18 require('@babel/plugin-proposal-class-properties'),
19 // use `this.foo = bar` instead of `this.defineProperty('foo', ...)`
20 {loose: true},
21 ],
22 [require('@babel/plugin-transform-computed-properties')],
23 [require('@babel/plugin-transform-destructuring')],
24 [require('@babel/plugin-transform-function-name')],
25 [require('@babel/plugin-transform-literals')],
26 [require('@babel/plugin-transform-parameters')],
27 [require('@babel/plugin-transform-shorthand-properties')],
28 [require('@babel/plugin-transform-react-jsx')],
29 [require('@babel/plugin-transform-regenerator')],
30 [require('@babel/plugin-transform-sticky-regex')],
31 [require('@babel/plugin-transform-unicode-regex')],
32 [
33 require('@babel/plugin-transform-modules-commonjs'),
34 {
35 strict: false,
36 strictMode: false, // prevent "use strict" injections
37 allowTopLevelThis: true, // dont rewrite global `this` -> `undefined`
38 },
39 ],
40];
41
42const es2015ArrowFunctions = [
43 require('@babel/plugin-transform-arrow-functions'),
44];
45const es2015Classes = [require('@babel/plugin-transform-classes')];
46const es2015ForOf = [require('@babel/plugin-transform-for-of'), {loose: true}];
47const es2015Spread = [require('@babel/plugin-transform-spread')];
48const es2015TemplateLiterals = [
49 require('@babel/plugin-transform-template-literals'),
50 {loose: true}, // dont 'a'.concat('b'), just use 'a'+'b'
51];
52const exponentiationOperator = [
53 require('@babel/plugin-transform-exponentiation-operator'),
54];
55const objectAssign = [require('@babel/plugin-transform-object-assign')];
56const objectRestSpread = [require('@babel/plugin-proposal-object-rest-spread')];
57const optionalChaining = [require('@babel/plugin-proposal-optional-chaining')];
58const reactDisplayName = [
59 require('@babel/plugin-transform-react-display-name'),
60];
61const reactJsxSource = [require('@babel/plugin-transform-react-jsx-source')];
62const symbolMember = [require('../transforms/transform-symbol-member')];
63
64const getPreset = (src, options) => {
65 const isNull = src === null || src === undefined;
66 const hasClass = isNull || src.indexOf('class') !== -1;
67 const hasForOf =
68 isNull || (src.indexOf('for') !== -1 && src.indexOf('of') !== -1);
69
70 const extraPlugins = [];
71
72 if (hasClass) {
73 extraPlugins.push(es2015Classes);
74 }
75 if (isNull || src.indexOf('=>') !== -1) {
76 extraPlugins.push(es2015ArrowFunctions);
77 }
78 if (isNull || hasClass || src.indexOf('...') !== -1) {
79 extraPlugins.push(es2015Spread);
80 extraPlugins.push(objectRestSpread);
81 }
82 if (isNull || src.indexOf('`') !== -1) {
83 extraPlugins.push(es2015TemplateLiterals);
84 }
85 if (isNull || src.indexOf('**') !== -1) {
86 extraPlugins.push(exponentiationOperator);
87 }
88 if (isNull || src.indexOf('Object.assign') !== -1) {
89 extraPlugins.push(objectAssign);
90 }
91 if (hasForOf) {
92 extraPlugins.push(es2015ForOf);
93 }
94 if (hasForOf || src.indexOf('Symbol') !== -1) {
95 extraPlugins.push(symbolMember);
96 }
97 if (
98 isNull ||
99 src.indexOf('React.createClass') !== -1 ||
100 src.indexOf('createReactClass') !== -1
101 ) {
102 extraPlugins.push(reactDisplayName);
103 }
104 if (isNull || src.indexOf('?.') !== -1) {
105 extraPlugins.push(optionalChaining);
106 }
107
108 if (options && options.dev) {
109 extraPlugins.push(reactJsxSource);
110 }
111
112 return {
113 comments: false,
114 compact: true,
115 plugins: defaultPlugins.concat(extraPlugins),
116 };
117};
118
119const base = getPreset(null);
120const devTools = getPreset(null, {dev: true});
121
122module.exports = options => {
123 if (options.withDevTools == null) {
124 const env = process.env.BABEL_ENV || process.env.NODE_ENV;
125 if (!env || env === 'development') {
126 return devTools;
127 }
128 }
129 return base;
130};
131
132module.exports.getPreset = getPreset;