UNPKG

4.29 kBJavaScriptView Raw
1const { ifAnyDep, fromRoot, getPkgSrcDir } = require('@goldwasserexchange/read-pkg-up-helpers');
2const transformImports = require('./transformImports');
3const getBabelESTarget = require('./getBabelESTarget');
4
5const babelESTarget = getBabelESTarget();
6
7const plugins = [
8 [
9 require.resolve('babel-plugin-root-import'),
10 {
11 rootPathSuffix: fromRoot(getPkgSrcDir() || './src'),
12 rootPathPrefix: '#',
13 },
14 ],
15 // class { handleClick = () => { } }
16 require.resolve('@babel/plugin-proposal-class-properties'),
17 require.resolve('@babel/plugin-transform-exponentiation-operator'),
18 // The following two plugins use Object.assign directly, instead of Babel's
19 // extends helper. Note that this assumes `Object.assign` is available.
20 // { ...todo, completed: true }
21 [
22 require.resolve('@babel/plugin-proposal-object-rest-spread'),
23 {
24 loose: true,
25 useBuiltIns: true,
26 },
27 ],
28 ifAnyDep(
29 'react-universal-component',
30 require.resolve('babel-plugin-universal-import')
31 ),
32 // Transforms JSX
33 ifAnyDep(
34 'react',
35 [
36 require.resolve('@babel/plugin-transform-react-jsx'),
37 {
38 useBuiltIns: true,
39 },
40 ]
41 ),
42 // Polyfills the runtime needed for async/await and generators
43 [
44 require.resolve('@babel/plugin-transform-runtime'),
45 {
46 helpers: false,
47 regenerator: true,
48 },
49 ],
50].filter(Boolean);
51
52const getPlugins = (env, target) => [
53 ...plugins,
54 // The following two plugins are currently necessary to make React warnings
55 // include more valuable information. They are included here because they are
56 // currently not enabled in babel-preset-react. See the below threads for more info:
57 // https://github.com/babel/babel/issues/4702
58 // https://github.com/babel/babel/pull/3540#issuecomment-228673661
59 // https://github.com/facebookincubator/create-react-app/issues/989
60 ...(
61 (env === 'development' || env === 'test')
62 ? [
63 // Adds component stack to warning messages
64 ifAnyDep(
65 'react',
66 require.resolve('@babel/plugin-transform-react-jsx-source')
67 ),
68 // Adds __self attribute to JSX which React will use for some warnings
69 ifAnyDep(
70 'react',
71 require.resolve('@babel/plugin-transform-react-jsx-self')
72 ),
73 ]
74 : []
75 ),
76 ...(
77 (target === 'node' || (target === 'browser' && env === 'test'))
78 ? [
79 // Compiles import() to a deferred require()
80 require.resolve('babel-plugin-dynamic-import-node'),
81 ]
82 : [
83 // function* () { yield 42; yield 43; }
84 [
85 require.resolve('@babel/plugin-transform-regenerator'),
86 {
87 // Async functions are converted to generators by babel-preset-env
88 async: false,
89 },
90 ],
91 // Adds syntax support for import()
92 require.resolve('@babel/plugin-syntax-dynamic-import'),
93 ]
94 ),
95 ...(
96 env === 'production'
97 ? [
98 ifAnyDep(
99 'recharts',
100 require.resolve('babel-plugin-recharts')
101 ),
102 ifAnyDep(
103 'lodash',
104 [
105 require.resolve('babel-plugin-lodash'),
106 {
107 id: [
108 'lodash',
109 'recompose',
110 ],
111 },
112 ]
113 ),
114 ifAnyDep(
115 'date-fns',
116 require.resolve('babel-plugin-date-fns')
117 ),
118 transformImports(),
119 ifAnyDep(
120 'prop-types',
121 require.resolve('babel-plugin-transform-react-remove-prop-types')
122 ),
123 ifAnyDep(
124 'ramda',
125 [
126 require.resolve('babel-plugin-ramda'),
127 {
128 useES: babelESTarget === 'es',
129 },
130 ]
131 ),
132 ifAnyDep(
133 'react',
134 require.resolve('@babel/plugin-transform-react-constant-elements')
135 ),
136 ifAnyDep(
137 'react',
138 require.resolve('@babel/plugin-transform-react-inline-elements')
139 ),
140 ifAnyDep(
141 'react-loadable',
142 require.resolve('react-loadable/babel')
143 ),
144 require.resolve('babel-plugin-minify-dead-code-elimination'),
145 ].filter(Boolean)
146 : []
147 ),
148].filter(Boolean);
149
150module.exports = getPlugins;