forked from effector/effector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
babel.config.js
250 lines (238 loc) · 5.82 KB
/
babel.config.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
const {resolve: resolvePath, join} = require('path')
module.exports = api => {
api && api.cache && api.cache.never && api.cache.never()
// const env = api.cache(() => process.env.NODE_ENV)
return generateConfig(meta, babelConfig)
}
function generateConfig(meta, config = babelConfig) {
const result = {}
for (const key in config) {
const value = config[key]
result[key] = typeof value === 'function' ? value(meta) : value
}
return result
}
module.exports.generateConfig = generateConfig
const meta = {
isBuild: !!process.env.IS_BUILD,
isTest: process.env.NODE_ENV === 'test',
isCompat: false,
isEsm: false,
}
const isBrowserstackDomTest = !!process.env.DOM
const typesTestsPath = join('types', '__tests__')
const domTestsPath = join('forest', '__tests__')
const domSSRTestsPath = join('forest', '__tests__', 'ssr')
const jsdomTestPlugin = resolvePath(
__dirname,
'tools',
'remoteDeviceTestRunner',
'jsdomTestPlugin.js',
)
const aliases = {
'effector/fixtures': resolveFromSources('fixtures'),
'effector-react/scope': {
esm: 'effector-react/scope.mjs',
compat: null,
build: null,
test: null,
default: null,
},
'effector-react': {
esm: 'effector-react/effector-react.mjs',
compat: 'effector-react/compat',
build: null,
test: null,
default: resolveFromSources('../npm/effector-react'),
},
'effector-vue': resolveFromSources('vue'),
Builder: resolveFromSources('../tools/builder'),
effector: {
esm: 'effector/effector.mjs',
compat: 'effector/compat',
build: null,
test: null,
default: resolveFromSources('../npm/effector'),
},
}
const babelPlugin = resolvePath(__dirname, 'src', 'babel', 'babel-plugin.js')
const locationPlugin = resolvePath(
__dirname,
'src',
'types',
'src',
'locationPlugin.js',
)
const babelConfig = {
presets: [
['@babel/preset-react', {useBuiltIns: true}],
[
'@babel/preset-env',
{
loose: true,
useBuiltIns: 'entry',
corejs: 3,
modules: false,
shippedProposals: true,
targets: {
node: '10',
browsers: [
'last 2 Chrome versions',
'last 2 Firefox versions',
'last 2 Safari versions',
'last 1 Edge versions',
],
},
},
],
],
plugins(meta) {
const alias = parseAliases(meta, aliases)
if (meta.replaceVueNext) {
alias['vue-next'] = 'vue'
}
const result = [
'@babel/plugin-proposal-export-namespace-from',
'@babel/plugin-proposal-optional-chaining',
'@babel/plugin-proposal-nullish-coalescing-operator',
['@babel/plugin-proposal-class-properties', {loose: true}],
[
'babel-plugin-module-resolver',
{
alias,
loglevel: 'silent',
},
],
]
if (meta.isTest) {
result.push('@babel/plugin-transform-modules-commonjs')
}
if (meta.isBuild) {
const constToLetPlugin = resolvePath(
__dirname,
'src',
'babel',
'constToLet.js',
)
result.unshift(constToLetPlugin)
}
if (meta.replaceVueReactivity) {
const replaceVuePlugin = resolvePath(
__dirname,
'src',
'babel',
'vueImports.js',
)
result.unshift(replaceVuePlugin)
}
return result
},
overrides: [
{
test(filename) {
return (
!isBrowserstackDomTest &&
filename &&
filename.includes(domTestsPath) &&
!filename.includes(domSSRTestsPath)
)
},
plugins: [jsdomTestPlugin],
},
{
test(filename) {
return filename && filename.includes(typesTestsPath)
},
plugins: [locationPlugin],
},
{
test(filename) {
return (
filename &&
filename.includes('__tests__') &&
!filename.includes('compat') &&
!filename.includes('observable')
)
},
plugins: [
[
babelPlugin,
{
addLoc: true,
factories: ['src/effector/__tests__/fork/factory'],
},
],
],
},
{
test(filename) {
return filename && filename.includes('multiPass')
},
plugins: [
[
babelPlugin,
{
addLoc: true,
},
'effector-logger',
],
],
},
{
test(filename) {
return (
filename && (filename.endsWith('.tsx') || filename.endsWith('.ts'))
)
},
presets: [
[
'@babel/preset-typescript',
{
isTSX: true,
allExtensions: true,
},
],
],
},
{
test(filename) {
return (
filename && !(filename.endsWith('.tsx') || filename.endsWith('.ts'))
)
},
presets: ['@babel/preset-flow'],
},
],
sourceMaps: true,
}
function parseAliases(meta, obj) {
const result = {}
for (const key in obj) {
const value = obj[key]
if (typeof value === 'function') {
const name = value(meta)
if (name === undefined || name === null) continue
result[key] = name
} else if (typeof value === 'object' && value !== null) {
const name = applyPaths(value)
if (name === undefined || name === null) continue
result[key] = name
} else {
const name = value
if (name === undefined || name === null) continue
result[key] = name
}
}
return result
function applyPaths(paths) {
if (meta.isEsm) return paths.esm
if (meta.isCompat) return paths.compat
if (meta.isBuild) return paths.build
if (meta.isTest) return paths.test
return paths.default
}
}
function resolveFromSources(path) {
return resolvePath(__dirname, 'src', path)
}
module.exports.getAliases = (metadata = meta) => parseAliases(metadata, aliases)