forked from effector/effector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
babel.config.js
166 lines (158 loc) · 3.88 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
//@noflow
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) {
const result = {}
for (const key in config) {
const value = config[key]
result[key] = typeof value === 'function' ? value(meta) : value
}
return result
}
}
const meta = {
isBuild: !!process.env.IS_BUILD,
isTest: process.env.NODE_ENV === 'test',
isCompat: false,
}
const typesTestsPath = join('types', '__tests__')
const aliases = {
'effector/fixtures': 'fixtures',
'@effector/forms': 'forms',
'effector-react': 'react',
'effector-vue': 'vue',
Builder: '../tools/builder',
effector({isTest, isBuild, isCompat}) {
if (isCompat) return 'effector/compat'
if (isBuild) return null
if (isTest) return resolveFromSources('./effector')
return 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)
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,
},
],
]
if (meta.isTest) {
result.push('@babel/plugin-transform-modules-commonjs')
}
return result
},
overrides: [
{
test(filename) {
return filename && filename.includes(typesTestsPath)
},
plugins: [locationPlugin],
},
{
test(filename) {
return (
filename &&
filename.includes('__tests__') &&
!filename.includes('redux') &&
!filename.includes('browserstack') &&
!filename.includes('fromObservable')
)
},
plugins: [
[
babelPlugin,
{
exportMetadata: true,
addLoc: true,
},
],
],
},
{
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 {
const name = value
if (name === undefined || name === null) continue
result[key] = resolveFromSources(name)
}
}
return result
}
function resolveFromSources(path) {
return resolvePath(__dirname, 'src', path)
}
module.exports.getAliases = (metadata = meta) => parseAliases(metadata, aliases)