UNPKG

2.5 kBJavaScriptView Raw
1import { dirname } from 'path';
2
3export default function(context, opts = {}) {
4 const nodeEnv = process.env.NODE_ENV;
5 const {
6 useBuiltIns = false,
7 loose = false,
8 targets = { browsers: ['last 2 versions'] },
9 env = {},
10 } = opts;
11 const transformRuntime =
12 'transformRuntime' in opts
13 ? opts.transformRuntime
14 : {
15 absoluteRuntime: dirname(require.resolve('../package')),
16 };
17 const exclude = [
18 'transform-typeof-symbol',
19 'transform-unicode-regex',
20 'transform-sticky-regex',
21 'transform-new-target',
22 'transform-modules-umd',
23 'transform-modules-systemjs',
24 'transform-modules-amd',
25 'transform-literals',
26 ];
27
28 const plugins = [
29 require.resolve('babel-plugin-react-require'),
30 require.resolve('@babel/plugin-syntax-dynamic-import'),
31 [require.resolve('@babel/plugin-proposal-object-rest-spread'), { loose, useBuiltIns }],
32 require.resolve('@babel/plugin-proposal-optional-catch-binding'),
33 require.resolve('@babel/plugin-proposal-async-generator-functions'),
34
35 // 下面两个的顺序的配置都不能动
36 [require.resolve('@babel/plugin-proposal-decorators'), { legacy: true }],
37 [require.resolve('@babel/plugin-proposal-class-properties'), { loose: true }],
38
39 require.resolve('@babel/plugin-proposal-export-namespace-from'),
40 require.resolve('@babel/plugin-proposal-export-default-from'),
41 [require.resolve('@babel/plugin-proposal-nullish-coalescing-operator'), { loose }],
42 [require.resolve('@babel/plugin-proposal-optional-chaining'), { loose }],
43 [
44 require.resolve('@babel/plugin-proposal-pipeline-operator'),
45 {
46 proposal: 'minimal',
47 },
48 ],
49 require.resolve('@babel/plugin-proposal-do-expressions'),
50 require.resolve('@babel/plugin-proposal-function-bind'),
51 require.resolve('babel-plugin-macros'),
52 [require.resolve('@babel/plugin-transform-destructuring'), { loose: false }],
53 ];
54
55 if (nodeEnv !== 'test' && transformRuntime) {
56 plugins.push([require.resolve('@babel/plugin-transform-runtime'), transformRuntime]);
57 }
58 if (nodeEnv === 'production') {
59 plugins.push(require.resolve('babel-plugin-transform-react-remove-prop-types'));
60 }
61
62 return {
63 presets: [
64 [
65 require.resolve('@babel/preset-env'),
66 {
67 targets,
68 loose,
69 modules: 'commonjs',
70 exclude,
71 ...env,
72 },
73 ],
74 require.resolve('@babel/preset-react'),
75 ],
76 plugins,
77 };
78}