-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
Copy pathutils.js
106 lines (101 loc) · 2.21 KB
/
utils.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
const _ = require(`lodash`)
/**
* Convert a babelrc from a .babelrc file or package.json
* to actions to add it to the store.
*/
const actionifyBabelrc = (babelrc, actions) => {
const { presets, plugins, ...options } = babelrc
if (presets && _.isArray(presets)) {
presets.forEach(p => {
let name
let options = {}
if (_.isArray(p)) {
name = p[0]
options = p[1]
} else {
name = p
}
actions.setBabelPreset({
name,
options,
})
})
}
if (plugins && _.isArray(plugins)) {
plugins.forEach(p => {
let name
let options = {}
if (_.isArray(p)) {
name = p[0]
options = p[1]
} else {
name = p
}
actions.setBabelPlugin({
name,
options,
})
})
}
if (_.isObject(options) && !_.isEmpty(options)) {
actions.setBabelOptions({ options })
}
}
const addDefaultPluginsPresets = (
actions,
{ stage = ``, browserslist = {} }
) => {
let targets
if (stage === `build-html`) {
targets = { node: `current` }
} else {
targets = { browsers: browserslist }
}
// Presets
actions.setBabelPreset({
name: `@babel/preset-env`,
stage,
options: {
loose: true,
modules: false,
useBuiltIns: `usage`,
targets,
},
})
actions.setBabelPreset({
name: `@babel/preset-react`,
stage,
options: {
useBuiltIns: true,
pragma: `React.createElement`,
development: stage === `develop`,
},
})
actions.setBabelPreset({ name: `@babel/preset-flow`, stage })
// Plugins
actions.setBabelPlugin({
name: `@babel/plugin-proposal-class-properties`,
stage,
options: { loose: true },
})
actions.setBabelPlugin({
name: `@babel/plugin-syntax-dynamic-import`,
stage,
})
actions.setBabelPlugin({
name: `babel-plugin-macros`,
stage,
})
// Polyfills the runtime needed for async/await and generators
actions.setBabelPlugin({
name: `@babel/plugin-transform-runtime`,
stage,
options: {
helpers: true,
regenerator: true,
polyfill: false,
},
})
}
exports.addDefaultPluginsPresets = addDefaultPluginsPresets
exports.actionifyBabelrc = actionifyBabelrc