-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
babel-loader.js
93 lines (85 loc) · 2.5 KB
/
babel-loader.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
const babelLoader = require(`babel-loader`)
const {
prepareOptions,
getCustomOptions,
mergeConfigItemOptions,
} = require(`./babel-loader-helpers`)
/**
* Gatsby's custom loader for webpack & babel
*
* Gatsby allows sites to either use our Babel setup (the default)
* or to add a .babelrc to take control.
*
* Our default setup is defined in the fallbackPlugins/fallbackPresets arrays
* below.
*
* After using either the fallback or user supplied setup, we add on a handful
* of required plugins and finally merge in any presets/plugins supplied
* by Gatsby plugins.
*
* You can find documentation for the custom loader here: https://babeljs.io/docs/en/next/babel-core.html#loadpartialconfig
*/
module.exports = babelLoader.custom(babel => {
const toReturn = {
// Passed the loader options.
customOptions({ stage = `test`, reactRuntime = `classic`, ...options }) {
return {
custom: {
stage,
reactRuntime,
},
loader: {
sourceType: `unambiguous`,
...getCustomOptions(stage),
...options,
},
}
},
// Passed Babel's 'PartialConfig' object.
config(partialConfig, { customOptions }) {
let { options } = partialConfig
const [
reduxPresets,
reduxPlugins,
requiredPresets,
requiredPlugins,
fallbackPresets,
] = prepareOptions(babel, customOptions)
// If there is no filesystem babel config present, add our fallback
// presets/plugins.
if (!partialConfig.hasFilesystemConfig()) {
options = {
...options,
plugins: requiredPlugins,
presets: [...fallbackPresets, ...requiredPresets],
}
} else {
// With a babelrc present, only add our required plugins/presets
options = {
...options,
plugins: [...options.plugins, ...requiredPlugins],
presets: [...options.presets, ...requiredPresets],
}
}
// Merge in presets/plugins added from gatsby plugins.
reduxPresets.forEach(preset => {
options.presets = mergeConfigItemOptions({
items: options.presets,
itemToMerge: preset,
type: `preset`,
babel,
})
})
reduxPlugins.forEach(plugin => {
options.plugins = mergeConfigItemOptions({
items: options.plugins,
itemToMerge: plugin,
type: `plugin`,
babel,
})
})
return options
},
}
return toReturn
})