-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlib.js
68 lines (56 loc) · 2.39 KB
/
lib.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
'use strict';
const moduleTransformations = require('@babel/preset-env/lib/module-transformations').default;
const addDynamicImportSupport = require('./lib/dynamic-import');
const addReactSupport = require('./lib/react');
const addLodashSupport = require('./lib/lodash');
module.exports = (context, options) => {
options = Object.assign({
react: false,
lodash: true,
dynamicImport: true,
modules: process.env.BABEL_ENV === 'es' ? false : 'commonjs', // Usually set to `commonjs` or `false`
namedDefaultExport: null,
}, options);
if (options.modules !== 'commonjs' && options.modules !== 'cjs' && options.namedDefaultExport) {
throw new Error('The `namedDefaultExport` option can only be enabled when `modules` is commonjs');
}
// Set `namedDefaultExport` default value based on `modules`
if (options.namedDefaultExport == null) {
options.namedDefaultExport = options.modules === 'commonjs';
}
const config = {
sourceType: 'unambiguous',
presets: [],
plugins: [],
};
// Activate modules transformation
if (moduleTransformations[options.modules]) {
config.plugins.push(`@babel/plugin-${moduleTransformations[options.modules]}`);
}
// The plugins below activate stage 3 features that babel hasn't added to the stage 3 preset yet
config.plugins.push(
// Allows class { handleClick = () => { } static propTypes = { foo: PropTypes.string } }
require.resolve('@babel/plugin-proposal-class-properties'),
// Allows obj?.x?.y?.z
require.resolve('@babel/plugin-proposal-optional-chaining'),
// Allows object.foo ?? 'bar'
require.resolve('@babel/plugin-proposal-nullish-coalescing-operator')
);
// Adds dynamic import support
if (options.dynamicImport) {
addDynamicImportSupport(config, options.modules);
}
// Add react support without doing any development or production transformations
if (options.react) {
addReactSupport(config, null);
}
// Cherry-pick lodash modules for smaller bundles
if (options.lodash) {
addLodashSupport(config, options.lodash);
}
// Add `module.exports = default;`, see https://github.com/59naga/babel-plugin-add-module-exports
if (options.namedDefaultExport) {
config.plugins.push('babel-plugin-add-module-exports');
}
return config;
};