1 | const path = require('path')
|
2 | const merge = require('lodash.merge')
|
3 |
|
4 | const env = process.env.BABEL_ENV || process.env.NODE_ENV
|
5 | const isTest = env === 'test'
|
6 |
|
7 | const validateBoolOption = (name, value, defaultValue) => {
|
8 | if (typeof value === 'undefined') {
|
9 | value = defaultValue
|
10 | }
|
11 |
|
12 | if (typeof value !== 'boolean') {
|
13 | throw new TypeError(`Poi babel preset: '${name}' option must be a boolean.`)
|
14 | }
|
15 |
|
16 | return value
|
17 | }
|
18 |
|
19 | module.exports = (
|
20 | context,
|
21 | {
|
22 | jsx = process.env.POI_JSX || 'react',
|
23 | jsxPragmaFrag,
|
24 | flow,
|
25 | typescript,
|
26 | env,
|
27 | namedImports = process.env.POI_NAMED_IMPORTS
|
28 | } = {}
|
29 | ) => {
|
30 | jsxPragmaFrag = jsxPragmaFrag || 'React.Fragment'
|
31 |
|
32 | if (typeof namedImports === 'string') {
|
33 | namedImports = JSON.parse(namedImports)
|
34 | }
|
35 |
|
36 | const isVueJSX = jsx === 'vue'
|
37 | const isReactJSX = jsx === 'react'
|
38 |
|
39 |
|
40 |
|
41 | const isFlowEnabled = validateBoolOption('flow', flow, true)
|
42 | const isTypeScriptEnabled = validateBoolOption('typescript', typescript, true)
|
43 |
|
44 | const presets = [
|
45 | [
|
46 | require('@babel/preset-env'),
|
47 | Object.assign(
|
48 | {
|
49 | modules: isTest ? 'commonjs' : false,
|
50 | targets: isTest
|
51 | ? {
|
52 | node: 'current'
|
53 | }
|
54 | : {
|
55 | ie: 9
|
56 | }
|
57 | },
|
58 | env
|
59 | )
|
60 | ],
|
61 | !isVueJSX && [
|
62 | require('@babel/preset-react'),
|
63 | {
|
64 | pragma: isReactJSX ? 'React.createElement' : jsx,
|
65 | pragmaFrag: jsxPragmaFrag
|
66 | }
|
67 | ],
|
68 | isTypeScriptEnabled && require('@babel/preset-typescript')
|
69 | ].filter(Boolean)
|
70 |
|
71 | const plugins = [
|
72 |
|
73 |
|
74 | isFlowEnabled && require('@babel/plugin-transform-flow-strip-types'),
|
75 |
|
76 | isVueJSX && require('@babel/plugin-syntax-jsx'),
|
77 | isVueJSX && require('babel-plugin-transform-vue-jsx'),
|
78 |
|
79 | require('@babel/plugin-syntax-dynamic-import'),
|
80 | [
|
81 | require('@babel/plugin-proposal-class-properties'),
|
82 | {
|
83 |
|
84 | loose: true
|
85 | }
|
86 | ],
|
87 | [
|
88 | require('@babel/plugin-proposal-object-rest-spread'),
|
89 | {
|
90 | useBuiltIns: true
|
91 | }
|
92 | ],
|
93 | [
|
94 | require('babel-plugin-assets-named-imports'),
|
95 | {
|
96 | loaderMap: merge(
|
97 | {
|
98 | svg: {
|
99 | ReactComponent: '!@svgr/webpack?-prettier![path]',
|
100 | VueComponent: '!vue-loader!svg-to-vue-component/loader![path]'
|
101 | },
|
102 | md: {
|
103 | ReactComponent: `!babel-loader?${JSON.stringify({
|
104 | babelrc: false,
|
105 | configFile: false,
|
106 | presets: [__filename]
|
107 | })}!@mdx-js/loader![path]`,
|
108 | VueComponent: '!vue-loader!vmark-loader![path]'
|
109 | }
|
110 | },
|
111 | namedImports
|
112 | )
|
113 | }
|
114 | ],
|
115 | require('babel-plugin-macros'),
|
116 | [
|
117 | require('@babel/plugin-transform-runtime'),
|
118 | {
|
119 | helpers: false,
|
120 | regenerator: true,
|
121 | absoluteRuntime: path.dirname(
|
122 | require.resolve('@babel/runtime/package.json')
|
123 | )
|
124 | }
|
125 | ]
|
126 | ].filter(Boolean)
|
127 |
|
128 | return {
|
129 | presets,
|
130 | plugins
|
131 | }
|
132 | }
|