Skip to content

Commit 8cedc8d

Browse files
authored
fix(gatsby): always pass stage option to babel-preset-gatsby (#30047)
1 parent 0f4de9d commit 8cedc8d

4 files changed

Lines changed: 59 additions & 0 deletions

File tree

packages/gatsby/src/redux/__tests__/__snapshots__/babelrc.ts.snap

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`Babelrc actions/reducer adds stage option to babel-preset-gatsby defined with userland babelrc 1`] = `
4+
Array [
5+
Array [
6+
Array [
7+
"/path/to/module/babel-preset-gatsby",
8+
Object {
9+
"stage": "develop",
10+
},
11+
],
12+
Object {
13+
"type": "preset",
14+
},
15+
],
16+
]
17+
`;
18+
319
exports[`Babelrc actions/reducer allows adding a new plugin 1`] = `
420
Object {
521
"stages": Object {

packages/gatsby/src/redux/__tests__/babelrc.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { babelrcReducer } from "../reducers/babelrc"
33
import {
44
prepareOptions,
55
mergeConfigItemOptions,
6+
addRequiredPresetOptions,
67
} from "../../utils/babel-loader-helpers"
78

89
describe(`Babelrc actions/reducer`, () => {
@@ -83,6 +84,20 @@ describe(`Babelrc actions/reducer`, () => {
8384
expect(babel.createConfigItem.mock.calls).toMatchSnapshot()
8485
})
8586

87+
it(`adds stage option to babel-preset-gatsby defined with userland babelrc`, () => {
88+
const fakeResolver = (moduleName): string => `/path/to/module/${moduleName}`
89+
const babel = { createConfigItem: jest.fn() }
90+
const presets: any = [
91+
{
92+
file: { resolved: fakeResolver(`babel-preset-gatsby`) },
93+
},
94+
]
95+
96+
addRequiredPresetOptions(babel, presets, { stage: `develop` }, fakeResolver)
97+
98+
expect(babel.createConfigItem.mock.calls).toMatchSnapshot()
99+
})
100+
86101
it(`allows setting options`, () => {
87102
const action = actions.setBabelOptions(
88103
{ options: { sourceMaps: `inline` } },

packages/gatsby/src/utils/babel-loader-helpers.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,29 @@ const prepareOptions = (babel, options = {}, resolve = require.resolve) => {
104104
]
105105
}
106106

107+
const addRequiredPresetOptions = (
108+
babel,
109+
presets,
110+
options = {},
111+
resolve = require.resolve
112+
) => {
113+
// Always pass `stage` option to babel-preset-gatsby
114+
// (even if defined in custom babelrc)
115+
const gatsbyPresetResolved = resolve(`babel-preset-gatsby`)
116+
const index = presets.findIndex(p => p.file.resolved === gatsbyPresetResolved)
117+
118+
if (index !== -1) {
119+
presets[index] = babel.createConfigItem(
120+
[
121+
gatsbyPresetResolved,
122+
{ ...presets[index].options, stage: options.stage },
123+
],
124+
{ type: `preset` }
125+
)
126+
}
127+
return presets
128+
}
129+
107130
const mergeConfigItemOptions = ({ items, itemToMerge, type, babel }) => {
108131
const index = _.findIndex(
109132
items,
@@ -133,3 +156,4 @@ exports.getCustomOptions = getCustomOptions
133156
// Export helper functions for testing
134157
exports.prepareOptions = prepareOptions
135158
exports.mergeConfigItemOptions = mergeConfigItemOptions
159+
exports.addRequiredPresetOptions = addRequiredPresetOptions

packages/gatsby/src/utils/babel-loader.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const {
44
prepareOptions,
55
getCustomOptions,
66
mergeConfigItemOptions,
7+
addRequiredPresetOptions,
78
} = require(`./babel-loader-helpers`)
89
const { getBrowsersList } = require(`./browserslist`)
910

@@ -76,6 +77,9 @@ module.exports = babelLoader.custom(babel => {
7677
plugins: [...options.plugins, ...requiredPlugins],
7778
presets: [...options.presets, ...requiredPresets],
7879
}
80+
// User-defined preset likely contains `babel-preset-gatsby`
81+
// Make sure to pass required dynamic options (e.g. `stage` to it):
82+
addRequiredPresetOptions(babel, options.presets, customOptions)
7983
}
8084

8185
// Merge in presets/plugins added from gatsby plugins.

0 commit comments

Comments
 (0)