-
Notifications
You must be signed in to change notification settings - Fork 0
/
babel.config.js
298 lines (254 loc) · 9.71 KB
/
babel.config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
const _ = require('lodash');
/**
* If Babel finds other .babelrc files while transpiling files in
* subfolder, it will merge the configuration together. This can lead
* to undefined behaviors thats why we use 'babel.config.js'
* @note -> Can still extend other .bablerc's files - check the docs
* @docs -> https://babeljs.io/docs/en/next/babelconfigjs
*/
const babelConfig = function babelConfig (api) {
// Babel cache
// @docs: https://babeljs.io/docs/en/next/config-files#apicache
api.cache.invalidate(() => process.env.NODE_ENV);
// Node/Babel env
// @docs: https://babeljs.io/docs/en/next/config-files#apienv
const BABEL_ENV = _.isString(api)
? api || 'production'
: api.env() || 'production';
const IS_DEV = BABEL_ENV === 'development';
// Build Type 🠲 browser || node
const BUILD_TYPE = process.env.BUILD_TYPE || 'node';
/**
* Enviroment specific presets/plugins
* @note: depending on BUILD_TYPE merges specific config
* @type {Object}
*/
const env = {
/**
* Browser Env
*/
browser: {
presets: [
// A Babel preset for each environment.
// @docs: https://babeljs.io/docs/en/next/babel-preset-env.html
['@babel/preset-env', {
modules: 'umd',
shippedProposals: true,
useBuiltIns: 'usage',
corejs: 3,
targets: {
browsers: [
'>0.2%',
'last 1 version',
'not dead',
'not ie <= 11',
'not op_mini all',
],
}
}],
]
},
/**
* Node Env
*/
node: {
presets: [
// A Babel preset for each environment.
// @docs: https://babeljs.io/docs/en/next/babel-preset-env.html
['@babel/preset-env', {
modules: false,
shippedProposals: true,
useBuiltIns: 'usage',
corejs: 3,
targets: {
node: '11.3.0',
}
}],
]
},
};
/**
* Config Babel env plugins/presets
* @todo: Maybe throw warning if not found?
*/
let envPresets = _.get(env, `${BUILD_TYPE}.presets`) || [];
let envPlugins = _.get(env, `${BUILD_TYPE}.plugins`) || [];
/**
* Common babel presets
* @type {Array}
*/
const presetsCommon = [];
/**
* Common babel plugins
* @type Array<String | Array>
*/
const pluginsCommon = [
// Allow parsing of import()
// @docs: https://babeljs.io/docs/en/next/babel-plugin-syntax-dynamic-import.html
'@babel/plugin-syntax-dynamic-import',
// Allow parsing of import.meta
// @docs: https://babeljs.io/docs/en/next/babel-plugin-syntax-import-meta.html
'@babel/plugin-syntax-import-meta',
// Escape U+2028 LINE SEPARATOR and U+2029 PARAGRAPH SEPARATOR in JS strings
// @docs: https://babeljs.io/docs/en/next/babel-plugin-proposal-json-strings.html
'@babel/plugin-proposal-json-strings',
// Compile class and object decorators to ES5
// @note: Must be declared before '@babel/plugin-proposal-class-properties'
// @docs: https://babeljs.io/docs/en/next/babel-plugin-proposal-decorators.html
['@babel/plugin-proposal-decorators', {
legacy: true
}],
// Plugin transforms static class properties as well as properties
// declared with the property initializer syntax
// @docs: https://babeljs.io/docs/en/next/babel-plugin-proposal-class-properties.html
['@babel/plugin-proposal-class-properties', {
loose: true
}],
// Compile function bind operator to ES5
// @docs: https://babeljs.io/docs/en/next/babel-plugin-proposal-function-bind.html
'@babel/plugin-proposal-function-bind',
// Compile optional catch bindings
// @docs: https://babeljs.io/docs/en/next/babel-plugin-proposal-optional-catch-binding.html
'@babel/plugin-proposal-optional-catch-binding',
// Transform optional chaining operators into a series of nil checks
// @note: Might want to replace with the package 'idx'
// @docs: https://babeljs.io/docs/en/next/babel-plugin-proposal-optional-chaining.html
'@babel/plugin-proposal-optional-chaining',
// Allow parsing of async generator functions
// @docs: https://babeljs.io/docs/en/next/babel-plugin-syntax-async-generators.html
'@babel/plugin-syntax-async-generators',
// Allow parsing of do expressions
// @docs: https://babeljs.io/docs/en/next/babel-plugin-syntax-do-expressions.html
'@babel/plugin-syntax-do-expressions',
// Allow parsing of object rest/spread
// @docs: https://babeljs.io/docs/en/next/babel-plugin-syntax-object-rest-spread.html
'@babel/plugin-syntax-object-rest-spread',
// Allow parsing of optional catch bindings
// @docs: https://babeljs.io/docs/en/next/babel-plugin-syntax-optional-catch-binding.html
'@babel/plugin-syntax-optional-catch-binding',
// Apply ES2015 function.name semantics to all functions
// @docs: https://babeljs.io/docs/en/next/babel-plugin-transform-function-name.html
'@babel/plugin-transform-function-name',
// Compile ES2015 shorthand properties to ES5
// @docs: https://babeljs.io/docs/en/next/babel-plugin-transform-shorthand-properties.html
'@babel/plugin-transform-shorthand-properties',
// Turn async functions into ES2015 generators
// @docs: https://babeljs.io/docs/en/next/babel-plugin-transform-async-to-generator.html
'@babel/plugin-transform-async-to-generator',
// Allow parsing of the flow syntax
// @docs: https://babeljs.io/docs/en/next/babel-plugin-syntax-flow.html
'@babel/plugin-syntax-flow',
// Turn flow type annotations into comments
// @docs: https://babeljs.io/docs/en/next/babel-plugin-transform-flow-comments.html
'@babel/plugin-transform-flow-comments',
// Transforms ES2015 modules to CommonJS
// @docs: https://babeljs.io/docs/en/next/babel-plugin-transform-modules-commonjs.html
['@babel/plugin-transform-modules-commonjs', {
// Do not lazy-initialize local `./foo` imports, but lazy-init `foo` dependencies.
lazy: true,
}],
// Cherry-picks lodash modules - works with `lodash-webpack-plugin`.
// This plugin also works with `recompose` and a few other libs
// @docs: https://github.com/lodash/babel-plugin-lodash
['lodash', {
id: [
'lodash',
]
}],
// Plugin can add a new resolver for your modules when compiling your code
// using Babel. Allows you to add new 'root' directories that contain your
// modules. It also allows for custom alias for directories,
// specific files, or even other npm modules.
// @docs: https://github.com/tleunen/babel-plugin-module-resolver#readme
['module-resolver', {
root: ['.'],
alias: {
lib : './lib',
util : './lib/util',
}
}],
// This plugin allows all the helpers to reference the module babel-runtime
// to avoid duplication across your compiled output.
// @note: Unlike the production version this version make debugging a bit easier
// @docs: https://babeljs.io/docs/en/babel-plugin-transform-runtime
['@babel/plugin-transform-runtime', {
helpers: true,
regenerator: true,
corejs: 3,
useESModules: false,
}],
];
/**
* Development env plugins/presets
*/
if (IS_DEV) {
// development presets (none)
envPresets = _.concat(envPresets, []);
// development plugins
envPlugins = _.concat(envPlugins, []);
}
/**
* Production env plugins/presets
*/
if (!IS_DEV) {
// production presets
envPresets = _.concat(envPresets, []);
// to be minified
if (process.env.BABEL_MINIFY) {
envPresets = _.concat(envPresets, [
// An ES6+ aware minifier based on the Babel toolchain
// @note:
// + Current pipeline
// - ES2015+ code -> BabelMinify -> Minified ES2015+ Code
// - Might want to enable Webpack uglify for multiple passes
// - @ref: https://github.com/babel/minify/issues/61
// @docs: https://babeljs.io/docs/en/babel-preset-minify
['minify', {
keepClassName: true,
keepFnName: true,
mangle: false,
mergeVars: false,
removeUndefined: false,
tdz: true,
}],
]);
}
// production plugins
envPlugins = _.concat(envPlugins, [
// Compile ES2015 destructuring to ES5
// @docs: https://babeljs.io/docs/en/next/babel-plugin-transform-destructuring.html
'@babel/plugin-transform-destructuring',
// Compile ES2015 default and rest parameters to ES5
// @docs: https://babeljs.io/docs/en/next/babel-plugin-transform-parameters.html
'@babel/plugin-transform-parameters',
// Compile ES5 property mutator shorthand syntax to Object.defineProperty
// @docs: https://babeljs.io/docs/en/next/babel-plugin-transform-property-mutators.html
'@babel/plugin-transform-property-mutators',
// Compile ES2015 template literals to ES5
// @docs: https://babeljs.io/docs/en/next/babel-plugin-transform-template-literals.html
'@babel/plugin-transform-template-literals',
// Removes flow types
// @docs: https://babeljs.io/docs/en/babel-plugin-transform-flow-strip-types
'@babel/plugin-transform-flow-strip-types',
// Inline's environment variables
// @docs: https://github.com/babel/minify/tree/master/packages/babel-plugin-transform-inline-environment-variables
['transform-inline-environment-variables', {
include: [
'NODE_ENV',
'WEBPACK_MODE'
]
}],
]);
}
/**
* Config based on env and return
*/
const presets = _.concat(presetsCommon, envPresets);
const plugins = _.concat(pluginsCommon, envPlugins);
// ->
return {
presets,
plugins,
};
};
module.exports = babelConfig;