2018-09-03 14:41:52 +00:00
|
|
|
const env = process.env.NODE_ENV
|
|
|
|
const isProduction = env === 'production'
|
|
|
|
const isDevelopment = env === 'development'
|
|
|
|
const isTest = env === 'test'
|
|
|
|
|
2017-10-15 17:54:57 +00:00
|
|
|
// Resolve styled-jsx plugins
|
|
|
|
function styledJsxOptions (opts) {
|
|
|
|
if (!opts) {
|
|
|
|
return {}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Array.isArray(opts.plugins)) {
|
|
|
|
return opts
|
|
|
|
}
|
|
|
|
|
|
|
|
opts.plugins = opts.plugins.map(plugin => {
|
|
|
|
if (Array.isArray(plugin)) {
|
|
|
|
const [name, options] = plugin
|
|
|
|
return [
|
|
|
|
require.resolve(name),
|
|
|
|
options
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
return require.resolve(plugin)
|
|
|
|
})
|
|
|
|
|
|
|
|
return opts
|
|
|
|
}
|
|
|
|
|
2017-10-07 06:38:59 +00:00
|
|
|
module.exports = (context, opts = {}) => ({
|
2016-12-21 02:01:56 +00:00
|
|
|
presets: [
|
2018-09-03 14:41:52 +00:00
|
|
|
[require('@babel/preset-env').default, {
|
|
|
|
// In the test environment `modules` is often needed to be set to true, babel figures that out by itself using the `'auto'` option
|
|
|
|
// In production/development this option is set to `false` so that webpack can handle import/export with tree-shaking
|
2018-09-07 09:42:10 +00:00
|
|
|
modules: isDevelopment || isProduction ? false : 'auto',
|
2017-10-07 06:38:59 +00:00
|
|
|
...opts['preset-env']
|
2017-02-08 12:05:25 +00:00
|
|
|
}],
|
2018-09-03 14:41:52 +00:00
|
|
|
[require('@babel/preset-react'), {
|
|
|
|
// This adds @babel/plugin-transform-react-jsx-source and
|
|
|
|
// @babel/plugin-transform-react-jsx-self automatically in development
|
|
|
|
development: isDevelopment || isTest,
|
|
|
|
...opts['preset-react']
|
|
|
|
}]
|
2016-12-21 02:01:56 +00:00
|
|
|
],
|
|
|
|
plugins: [
|
2018-04-03 07:34:07 +00:00
|
|
|
require('babel-plugin-react-require'),
|
2018-07-24 09:24:40 +00:00
|
|
|
require('@babel/plugin-syntax-dynamic-import'),
|
|
|
|
require('./plugins/react-loadable-plugin'),
|
2018-06-19 19:48:54 +00:00
|
|
|
[require('@babel/plugin-proposal-class-properties'), opts['class-properties'] || {}],
|
2018-04-03 07:34:07 +00:00
|
|
|
require('@babel/plugin-proposal-object-rest-spread'),
|
2018-08-24 08:27:48 +00:00
|
|
|
[require('@babel/plugin-transform-runtime'), {
|
2018-02-01 11:24:31 +00:00
|
|
|
helpers: false,
|
2018-08-24 08:27:48 +00:00
|
|
|
regenerator: true,
|
|
|
|
...opts['transform-runtime']
|
2018-02-01 11:24:31 +00:00
|
|
|
}],
|
2018-04-03 07:34:07 +00:00
|
|
|
[require('styled-jsx/babel'), styledJsxOptions(opts['styled-jsx'])],
|
2018-04-18 16:18:06 +00:00
|
|
|
process.env.NODE_ENV === 'production' && require('babel-plugin-transform-react-remove-prop-types')
|
|
|
|
].filter(Boolean)
|
2017-10-07 06:38:59 +00:00
|
|
|
})
|