-
Notifications
You must be signed in to change notification settings - Fork 94
/
index.js
52 lines (45 loc) · 1.29 KB
/
index.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
/**
* Babel webpack block.
*
* @see https://github.com/babel/babel-loader
*/
module.exports = babel
/**
* @param {object} [options]
* @param {bool} [options.cacheDirectory] Use cache directory. Defaults to true.
* @param {string[]} [options.plugins] Babel plugins to use.
* @param {string[]} [options.presets] Babel presets to use.
* @return {Function}
*/
function babel(options = {}) {
options = Object.assign(
{
cacheDirectory: true
},
options
)
const setter = context => prevConfig => {
context.babel = context.babel || {}
// Merge babel config into the one stored in context
context.babel = Object.assign(
{},
context.babel,
options,
options.plugins ? { plugins: (context.babel.plugins || []).concat(options.plugins) } : {},
options.presets ? { presets: (context.babel.presets || []).concat(options.presets) } : {}
)
return prevConfig
}
return Object.assign(setter, { post: postConfig })
}
function postConfig(context, util) {
const ruleConfig = Object.assign(
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: [{ loader: 'babel-loader', options: context.babel }]
},
context.match
)
return util.addLoader(ruleConfig)
}