-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
112 lines (102 loc) · 3.44 KB
/
index.js
File metadata and controls
112 lines (102 loc) · 3.44 KB
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
const { isWx, isHermesJs } = require('./utils')
function getPresets(options, targets) {
return [
[
require.resolve('@babel/preset-env'),
{
modules: false,
shippedProposals: true,
exclude: ['@babel/plugin-transform-regenerator'],
}
],
[
require.resolve('@babel/preset-react'),
{
runtime: 'automatic'
}
]
]
}
function getPlugins(options, targets) {
const plugins = []
if (isHermesJs(targets)) {
plugins.push([
require.resolve('@babel/plugin-transform-runtime'),
{
corejs: false,
helpers: true,
regenerator: true,
version: '^7.10.4'
}
])
if (options.reanimated) {
// react-native-reanimated
plugins.push('react-native-reanimated/plugin')
}
} else {
plugins.push([
require.resolve('@babel/plugin-transform-runtime'),
{
corejs: 3,
version: '^7.10.4'
}
])
}
plugins.push(require.resolve('@mpxjs/babel-plugin-inject-page-events'))
return plugins
}
function getOverrides(options, targets) {
const overrides = []
if (isWx(targets) && options.skyline && options.skyline.worklet && Array.isArray(options.skyline.worklet.include)) {
// 微信小程序 skyline worklet 函数转义
overrides.push({
// skyline worklet babel
include: options.skyline.worklet.include || [],
plugins: [
[require.resolve('@babel/plugin-transform-arrow-functions')],
[require.resolve('@babel/plugin-transform-shorthand-properties')],
[require.resolve('@babel/plugin-proposal-class-properties')],
[require.resolve('@babel/plugin-proposal-optional-chaining')],
[require.resolve('@babel/plugin-proposal-nullish-coalescing-operator')],
require.resolve('babel-plugin-worklet')
]
})
}
return overrides
}
function getTestConfig(options) {
return {
presets: [
[
require.resolve('@babel/preset-env'),
{
shippedProposals: true
}
]
],
plugins: [require.resolve('babel-plugin-transform-require-context')]
}
}
module.exports = function (api, options) {
api.cache(true)
// reanimated 默认为 true
options.reanimated = options.reanimated === undefined ? true : options.reanimated
// 默认为 false
options.skyline = options.skyline ? { worklet: false } : options.skyline
const targets = options.mode || process.env.MPX_CURRENT_TARGET_MODE
if (!targets) {
throw new Error('[@mpxjs/babel-preset-mpx] Missing target mode, please set "mode" option or "MPX_CURRENT_TARGET_MODE" environment variable.')
}
if (['wx', 'ali', 'swan', 'qq', 'tt', 'web', 'dd', 'qa', 'jd', 'android', 'ios', 'harmony'].indexOf(targets) === -1) {
throw new Error(`[@mpxjs/babel-preset-mpx] Invalid target mode: ${targets}.`)
}
return {
presets: getPresets(options, targets),
plugins: getPlugins(options, targets),
overrides: getOverrides(options, targets),
sourceType: 'unambiguous',
env: {
test: getTestConfig()
}
}
}