|
| 1 | +/* eslint max-statements: ["error", 24] */ |
| 2 | +/* eslint-disable global-require */ |
| 3 | +const helpers = require('@babel/helper-plugin-utils'); |
| 4 | + |
| 5 | +// 1. For nodejs projects, pass `node` option to desired nodejs version |
| 6 | +// 2. If you want old browsers, put browserslist config in your project, |
| 7 | +// so Babel automatically will pick it up |
| 8 | +// 3. If you want modern browsers (esmodules compatible), pass `browsers: true` |
| 9 | + |
| 10 | +module.exports = helpers.declare((api, options) => { |
| 11 | + api.assertVersion(7); |
| 12 | + |
| 13 | + const opts = { |
| 14 | + modules: 'commonjs', |
| 15 | + include: [], |
| 16 | + exclude: [ |
| 17 | + '@babel/plugin-transform-regenerator', |
| 18 | + '@babel/plugin-transform-async-to-generator', |
| 19 | + ], |
| 20 | + ...options, |
| 21 | + }; |
| 22 | + |
| 23 | + const { modules, include, exclude } = opts; |
| 24 | + |
| 25 | + let environmentOptions = { modules, include, exclude }; |
| 26 | + |
| 27 | + if (opts.browsers === true) { |
| 28 | + environmentOptions = { |
| 29 | + ...environmentOptions, |
| 30 | + targets: { esmodules: true }, |
| 31 | + }; |
| 32 | + } |
| 33 | + if ( |
| 34 | + (typeof opts.browsers === 'string' || Array.isArray(opts.browsers)) && |
| 35 | + opts.browsers.length > 0 |
| 36 | + ) { |
| 37 | + environmentOptions = { |
| 38 | + ...environmentOptions, |
| 39 | + targets: { browsers: opts.browsers }, |
| 40 | + }; |
| 41 | + } |
| 42 | + if (typeof opts.node === 'string' && opts.node.length > 0) { |
| 43 | + environmentOptions = { |
| 44 | + ...environmentOptions, |
| 45 | + targets: { node: opts.node }, |
| 46 | + }; |
| 47 | + } |
| 48 | + |
| 49 | + const plugins = [ |
| 50 | + '@babel/plugin-syntax-import-meta', |
| 51 | + ['babel-plugin-add-module-exports', { addDefaultProperty: true }], |
| 52 | + ]; |
| 53 | + const presets = [[require('@babel/preset-env'), environmentOptions]]; |
| 54 | + |
| 55 | + if (opts.typescript) { |
| 56 | + presets.push(require('@babel/preset-typescript')); |
| 57 | + } |
| 58 | + |
| 59 | + const reactPreset = [ |
| 60 | + require('@babel/preset-react'), |
| 61 | + { |
| 62 | + development: api.env('development'), |
| 63 | + }, |
| 64 | + ]; |
| 65 | + const reactPlugin = api.env('production') |
| 66 | + ? [ |
| 67 | + require('babel-plugin-transform-react-remove-prop-types'), |
| 68 | + { |
| 69 | + removeImport: true, |
| 70 | + }, |
| 71 | + ] |
| 72 | + : undefined; |
| 73 | + |
| 74 | + if (opts.react) { |
| 75 | + presets.push(reactPreset); |
| 76 | + |
| 77 | + if (reactPlugin) { |
| 78 | + plugins.push(reactPlugin); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + return { |
| 83 | + plugins, |
| 84 | + presets, |
| 85 | + overrides: opts.isTSX |
| 86 | + ? undefined |
| 87 | + : [ |
| 88 | + { |
| 89 | + test: /\.(js|md|ts)x$/, |
| 90 | + plugins: reactPlugin && [reactPlugin], |
| 91 | + presets: [reactPreset], |
| 92 | + }, |
| 93 | + ], |
| 94 | + }; |
| 95 | +}); |
0 commit comments