UNPKG

2.78 kBJavaScriptView Raw
1"use strict";
2
3const path = require(`path`);
4
5const _ = require(`lodash`);
6
7const loadCachedConfig = () => {
8 let pluginBabelConfig = {
9 stages: {
10 test: {
11 plugins: [],
12 presets: []
13 }
14 }
15 };
16
17 if (process.env.NODE_ENV !== `test`) {
18 pluginBabelConfig = require(path.join(process.cwd(), `./.cache/babelState.json`));
19 }
20
21 return pluginBabelConfig;
22};
23
24const getCustomOptions = stage => {
25 const pluginBabelConfig = loadCachedConfig();
26 return pluginBabelConfig.stages[stage].options;
27};
28
29const prepareOptions = (babel, options = {}, resolve = require.resolve) => {
30 let pluginBabelConfig = loadCachedConfig();
31 const {
32 stage
33 } = options; // Required plugins/presets
34
35 const requiredPlugins = [babel.createConfigItem([resolve(`babel-plugin-remove-graphql-queries`)], {
36 type: `plugin`
37 })];
38 const requiredPresets = []; // Stage specific plugins to add
39
40 if (stage === `build-html` || stage === `develop-html`) {
41 requiredPlugins.push(babel.createConfigItem([resolve(`babel-plugin-dynamic-import-node`)], {
42 type: `plugin`
43 }));
44 }
45
46 if (stage === `develop`) {
47 requiredPlugins.push(babel.createConfigItem([resolve(`react-hot-loader/babel`)], {
48 type: `plugin`
49 }));
50 } // Fallback preset
51
52
53 const fallbackPresets = [];
54 fallbackPresets.push(babel.createConfigItem([resolve(`babel-preset-gatsby`), {
55 stage
56 }], {
57 type: `preset`
58 })); // Go through babel state and create config items for presets/plugins from.
59
60 const reduxPlugins = [];
61 const reduxPresets = [];
62 pluginBabelConfig.stages[stage].plugins.forEach(plugin => {
63 reduxPlugins.push(babel.createConfigItem([resolve(plugin.name), plugin.options], {
64 name: plugin.name,
65 type: `plugin`
66 }));
67 });
68 pluginBabelConfig.stages[stage].presets.forEach(preset => {
69 reduxPresets.push(babel.createConfigItem([resolve(preset.name), preset.options], {
70 name: preset.name,
71 type: `preset`
72 }));
73 });
74 return [reduxPresets, reduxPlugins, requiredPresets, requiredPlugins, fallbackPresets];
75};
76
77const mergeConfigItemOptions = ({
78 items,
79 itemToMerge,
80 type,
81 babel
82}) => {
83 const index = _.findIndex(items, i => i.file.resolved === itemToMerge.file.resolved); // If this exist, merge the options, otherwise, add it to the array
84
85
86 if (index !== -1) {
87 items[index] = babel.createConfigItem([itemToMerge.file.resolved, _.merge({}, items[index].options, itemToMerge.options)], {
88 type
89 });
90 } else {
91 items.push(itemToMerge);
92 }
93
94 return items;
95};
96
97exports.getCustomOptions = getCustomOptions; // Export helper functions for testing
98
99exports.prepareOptions = prepareOptions;
100exports.mergeConfigItemOptions = mergeConfigItemOptions;
101//# sourceMappingURL=babel-loader-helpers.js.map
\No newline at end of file