1 | //
|
2 |
|
3 |
|
4 |
|
5 | const { ArrayUtils, LogicUtils } = require('constellate-dev-utils')
|
6 |
|
7 | // :: Options -> BabelConfig
|
8 | module.exports = function generateConfig(pkg ) {
|
9 | const env = process.env.BABEL_ENV || process.env.NODE_ENV
|
10 | return {
|
11 | babelrc: false,
|
12 |
|
13 | // Handy for sourcemaps generation.
|
14 | sourceRoot: pkg.paths.packageRoot,
|
15 |
|
16 | presets: ArrayUtils.removeNil([
|
17 | [
|
18 | 'env',
|
19 | {
|
20 | targets: {
|
21 | // React parses on ie 9, so we should too
|
22 | ie: 9,
|
23 | // We currently minify with uglify
|
24 | // Remove after https://github.com/mishoo/UglifyJS2/issues/448
|
25 | uglify: true,
|
26 | },
|
27 | // Disable polyfill transforms
|
28 | // useBuiltIns: false,
|
29 | // Do not transform modules to CJS
|
30 | modules: false,
|
31 | },
|
32 | ],
|
33 |
|
34 | // jsx && flow support
|
35 | 'react',
|
36 | ]),
|
37 |
|
38 | plugins: ArrayUtils.removeNil([
|
39 | // const { foo, ...others } = object
|
40 | // object = { foo, ...others }
|
41 | // This plugin uses Object.assign directly.
|
42 | ['transform-object-rest-spread'],
|
43 |
|
44 | // function (
|
45 | // arg1,
|
46 | // arg2,
|
47 | // ) { }
|
48 | 'syntax-trailing-function-commas',
|
49 |
|
50 | // class { handleThing = () => { } }
|
51 | 'transform-class-properties',
|
52 |
|
53 | // Adds syntax support for import(), which webpack can handle
|
54 | 'babel-plugin-syntax-dynamic-import',
|
55 |
|
56 | // Polyfills the runtime needed for async/await and generators.
|
57 | 'babel-plugin-transform-runtime',
|
58 |
|
59 | // Replaces the React.createElement function with one that is
|
60 | // more optimized for production.
|
61 | // NOTE: Relies on Symbol being available.
|
62 | LogicUtils.onlyIf(
|
63 | env === 'production',
|
64 | 'transform-react-inline-elements',
|
65 | ),
|
66 |
|
67 | // Hoists element creation to the top level for subtrees that
|
68 | // are fully static, which reduces call to React.createElement
|
69 | // and the resulting allocations. More importantly, it tells
|
70 | // React that the subtree hasn’t changed so React can completely
|
71 | // skip it when reconciling.
|
72 | LogicUtils.onlyIf(
|
73 | env === 'production',
|
74 | 'transform-react-constant-elements',
|
75 | ),
|
76 |
|
77 | // Removes PropTypes code as it's just dead weight for a production build.
|
78 | LogicUtils.onlyIf(
|
79 | env === 'production',
|
80 | 'babel-plugin-transform-react-remove-prop-types',
|
81 | ),
|
82 |
|
83 | // The following two plugins are currently necessary to make React warnings
|
84 | // include more valuable information. They are included here because they are
|
85 | // currently not enabled in babel-preset-react. See the below threads for more info:
|
86 | // https://github.com/babel/babel/issues/4702
|
87 | // https://github.com/babel/babel/pull/3540#issuecomment-228673661
|
88 | // https://github.com/facebookincubator/create-react-app/issues/989
|
89 |
|
90 | // Adds __self attribute to JSX which React will use for some warnings
|
91 | LogicUtils.onlyIf(
|
92 | env === 'development' || env === 'test',
|
93 | 'transform-react-jsx-self',
|
94 | ),
|
95 |
|
96 | // Adds component stack to warning messages
|
97 | LogicUtils.onlyIf(
|
98 | env === 'development' || env === 'test',
|
99 | 'transform-react-jsx-source',
|
100 | ),
|
101 | ]),
|
102 | }
|
103 | }
|