This repository has been archived by the owner on Dec 6, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 255
/
preset.js
132 lines (121 loc) · 3.33 KB
/
preset.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
const path = require('path')
const merge = require('lodash.merge')
const env = process.env.BABEL_ENV || process.env.NODE_ENV
const isTest = env === 'test'
const validateBoolOption = (name, value, defaultValue) => {
if (typeof value === 'undefined') {
value = defaultValue
}
if (typeof value !== 'boolean') {
throw new TypeError(`Poi babel preset: '${name}' option must be a boolean.`)
}
return value
}
module.exports = (
context,
{
jsx = process.env.POI_JSX || 'react',
jsxPragmaFrag,
flow,
typescript,
env,
namedImports = process.env.POI_NAMED_IMPORTS
} = {}
) => {
jsxPragmaFrag = jsxPragmaFrag || 'React.Fragment'
if (typeof namedImports === 'string') {
namedImports = JSON.parse(namedImports)
}
const isVueJSX = jsx === 'vue'
const isReactJSX = jsx === 'react'
// Enable flow and typescript by default at the same time
// typescript transforms will only be applied to .ts .tsx files
const isFlowEnabled = validateBoolOption('flow', flow, true)
const isTypeScriptEnabled = validateBoolOption('typescript', typescript, true)
const presets = [
[
require('@babel/preset-env'),
Object.assign(
{
modules: isTest ? 'commonjs' : false,
targets: isTest
? {
node: 'current'
}
: {
ie: 9
}
},
env
)
],
!isVueJSX && [
require('@babel/preset-react'),
{
pragma: isReactJSX ? 'React.createElement' : jsx,
pragmaFrag: jsxPragmaFrag
}
],
isTypeScriptEnabled && require('@babel/preset-typescript')
].filter(Boolean)
const plugins = [
// Strip flow types before any other transform, emulating the behavior
// order as-if the browser supported all of the succeeding features
isFlowEnabled && require('@babel/plugin-transform-flow-strip-types'),
// JSX config
isVueJSX && require('@babel/plugin-syntax-jsx'),
isVueJSX && require('babel-plugin-transform-vue-jsx'),
// stage-3 features
require('@babel/plugin-syntax-dynamic-import'),
[
require('@babel/plugin-proposal-class-properties'),
{
// Enable loose mode to use assignment instead of defineProperty
loose: true
}
],
[
require('@babel/plugin-proposal-object-rest-spread'),
{
useBuiltIns: true
}
],
[
require('babel-plugin-assets-named-imports'),
{
loaderMap: merge(
{
svg: {
ReactComponent: '!@svgr/webpack?-prettier![path]',
VueComponent: '!vue-loader!svg-to-vue-component/loader![path]'
},
md: {
ReactComponent: `!babel-loader?${JSON.stringify({
babelrc: false,
configFile: false,
presets: [__filename]
})}!@mdx-js/loader![path]`,
VueComponent: '!vue-loader!vmark-loader![path]'
}
},
namedImports
)
}
],
require('babel-plugin-macros'),
[
require('@babel/plugin-transform-runtime'),
{
helpers: false,
regenerator: true,
absoluteRuntime: path.dirname(
require.resolve('@babel/runtime/package.json')
)
}
]
].filter(Boolean)
return {
presets,
plugins
}
}