1 | "use strict";
|
2 |
|
3 | const path = require(`path`);
|
4 |
|
5 | const _ = require(`lodash`);
|
6 |
|
7 | const 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 |
|
24 | const getCustomOptions = stage => {
|
25 | const pluginBabelConfig = loadCachedConfig();
|
26 | return pluginBabelConfig.stages[stage].options;
|
27 | };
|
28 |
|
29 | const prepareOptions = (babel, options = {}, resolve = require.resolve) => {
|
30 | let pluginBabelConfig = loadCachedConfig();
|
31 | const {
|
32 | stage
|
33 | } = options;
|
34 |
|
35 | const requiredPlugins = [babel.createConfigItem([resolve(`babel-plugin-remove-graphql-queries`)], {
|
36 | type: `plugin`
|
37 | })];
|
38 | const requiredPresets = [];
|
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 | }
|
51 |
|
52 |
|
53 | const fallbackPresets = [];
|
54 | fallbackPresets.push(babel.createConfigItem([resolve(`babel-preset-gatsby`), {
|
55 | stage
|
56 | }], {
|
57 | type: `preset`
|
58 | }));
|
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 |
|
77 | const mergeConfigItemOptions = ({
|
78 | items,
|
79 | itemToMerge,
|
80 | type,
|
81 | babel
|
82 | }) => {
|
83 | const index = _.findIndex(items, i => i.file.resolved === itemToMerge.file.resolved);
|
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 |
|
97 | exports.getCustomOptions = getCustomOptions;
|
98 |
|
99 | exports.prepareOptions = prepareOptions;
|
100 | exports.mergeConfigItemOptions = mergeConfigItemOptions;
|
101 |
|
\ | No newline at end of file |