UNPKG

2.89 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
5var _objectWithoutPropertiesLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutPropertiesLoose"));
6
7const babelLoader = require(`babel-loader`);
8
9const {
10 prepareOptions,
11 getCustomOptions,
12 mergeConfigItemOptions
13} = require(`./babel-loader-helpers`);
14/**
15 * Gatsby's custom loader for webpack & babel
16 *
17 * Gatsby allows sites to either use our Babel setup (the default)
18 * or to add a .babelrc to take control.
19 *
20 * Our default setup is defined in the fallbackPlugins/fallbackPresets arrays
21 * below.
22 *
23 * After using either the fallback or user supplied setup, we add on a handful
24 * of required plugins and finally merge in any presets/plugins supplied
25 * by Gatsby plugins.
26 *
27 * You can find documentation for the custom loader here: https://babeljs.io/docs/en/next/babel-core.html#loadpartialconfig
28 */
29
30
31module.exports = babelLoader.custom(babel => {
32 const toReturn = {
33 // Passed the loader options.
34 customOptions(_ref) {
35 let {
36 stage = `test`
37 } = _ref,
38 options = (0, _objectWithoutPropertiesLoose2.default)(_ref, ["stage"]);
39 return {
40 custom: {
41 stage
42 },
43 loader: Object.assign({
44 cacheDirectory: true,
45 sourceType: `unambiguous`
46 }, getCustomOptions(stage), {}, options)
47 };
48 },
49
50 // Passed Babel's 'PartialConfig' object.
51 config(partialConfig, {
52 customOptions
53 }) {
54 let {
55 options
56 } = partialConfig;
57 const [reduxPresets, reduxPlugins, requiredPresets, requiredPlugins, fallbackPresets] = prepareOptions(babel, customOptions); // If there is no filesystem babel config present, add our fallback
58 // presets/plugins.
59
60 if (!partialConfig.hasFilesystemConfig()) {
61 options = Object.assign({}, options, {
62 plugins: requiredPlugins,
63 presets: [...fallbackPresets, ...requiredPresets]
64 });
65 } else {
66 // With a babelrc present, only add our required plugins/presets
67 options = Object.assign({}, options, {
68 plugins: [...options.plugins, ...requiredPlugins],
69 presets: [...options.presets, ...requiredPresets]
70 });
71 } // Merge in presets/plugins added from gatsby plugins.
72
73
74 reduxPresets.forEach(preset => {
75 options.presets = mergeConfigItemOptions({
76 items: options.presets,
77 itemToMerge: preset,
78 type: `preset`,
79 babel
80 });
81 });
82 reduxPlugins.forEach(plugin => {
83 options.plugins = mergeConfigItemOptions({
84 items: options.plugins,
85 itemToMerge: plugin,
86 type: `plugin`,
87 babel
88 });
89 });
90 return options;
91 }
92
93 };
94 return toReturn;
95});
96//# sourceMappingURL=babel-loader.js.map
\No newline at end of file