Skip to content

Commit 0f0feac

Browse files
DSchaupieh
authored andcommitted
fix(gatsby): correctly pick up browserslist overrides (#9669)
* fix: start ironing out browserslist issues * chore: make linter happy * chore: change to expected shape * refactor: move browserslist functionality into gatsby core
1 parent fc38707 commit 0f0feac

File tree

8 files changed

+97
-26
lines changed

8 files changed

+97
-26
lines changed

packages/babel-preset-gatsby/src/__tests__/index.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,9 @@ it(`Specifies proper presets and plugins for build-html stage`, () => {
101101
})
102102

103103
it(`Allows to configure browser targets`, () => {
104+
const targets = `last 1 version`
104105
const { presets } = preset(null, {
105-
targets: { browsers: [`last 1 version`] },
106+
targets,
106107
})
107108

108109
expect(presets[0]).toEqual([
@@ -111,9 +112,7 @@ it(`Allows to configure browser targets`, () => {
111112
loose: true,
112113
modules: false,
113114
useBuiltIns: `usage`,
114-
targets: {
115-
browsers: [`last 1 version`],
116-
},
115+
targets,
117116
},
118117
])
119118
})
Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const path = require(`path`)
22

3-
const r = m => require.resolve(m)
3+
const resolve = m => require.resolve(m)
44

55
const loadCachedConfig = () => {
66
let pluginBabelConfig = {}
@@ -13,7 +13,7 @@ const loadCachedConfig = () => {
1313
return pluginBabelConfig
1414
}
1515

16-
function preset(context, options = {}) {
16+
module.exports = function preset(_, options = {}) {
1717
let { targets = null } = options
1818

1919
const pluginBabelConfig = loadCachedConfig()
@@ -25,16 +25,14 @@ function preset(context, options = {}) {
2525
node: `current`,
2626
}
2727
} else {
28-
targets = {
29-
browsers: pluginBabelConfig.browserslist,
30-
}
28+
targets = pluginBabelConfig.browserslist
3129
}
3230
}
3331

3432
return {
3533
presets: [
3634
[
37-
r(`@babel/preset-env`),
35+
resolve(`@babel/preset-env`),
3836
{
3937
loose: true,
4038
modules: stage === `test` ? `commonjs` : false,
@@ -43,7 +41,7 @@ function preset(context, options = {}) {
4341
},
4442
],
4543
[
46-
r(`@babel/preset-react`),
44+
resolve(`@babel/preset-react`),
4745
{
4846
useBuiltIns: true,
4947
pragma: `React.createElement`,
@@ -53,15 +51,15 @@ function preset(context, options = {}) {
5351
],
5452
plugins: [
5553
[
56-
r(`@babel/plugin-proposal-class-properties`),
54+
resolve(`@babel/plugin-proposal-class-properties`),
5755
{
5856
loose: true,
5957
},
6058
],
61-
r(`babel-plugin-macros`),
62-
r(`@babel/plugin-syntax-dynamic-import`),
59+
resolve(`babel-plugin-macros`),
60+
resolve(`@babel/plugin-syntax-dynamic-import`),
6361
[
64-
r(`@babel/plugin-transform-runtime`),
62+
resolve(`@babel/plugin-transform-runtime`),
6563
{
6664
helpers: true,
6765
regenerator: true,
@@ -70,5 +68,3 @@ function preset(context, options = {}) {
7068
],
7169
}
7270
}
73-
74-
module.exports = preset

packages/gatsby/src/bootstrap/index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const Promise = require(`bluebird`)
1212

1313
const apiRunnerNode = require(`../utils/api-runner-node`)
1414
const mergeGatsbyConfig = require(`../utils/merge-gatsby-config`)
15+
const getBrowserslist = require(`../utils/browserslist`)
1516
const { graphql } = require(`graphql`)
1617
const { store, emitter } = require(`../redux`)
1718
const loadPlugins = require(`./load-plugins`)
@@ -56,10 +57,13 @@ module.exports = async (args: BootstrapArgs) => {
5657
// and invokes Gatsby API based on actions.
5758
require(`../redux/plugin-runner`)
5859

60+
const directory = slash(args.directory)
61+
5962
const program = {
6063
...args,
64+
browserslist: getBrowserslist(directory),
6165
// Fix program directory path for windows env.
62-
directory: slash(args.directory),
66+
directory,
6367
}
6468

6569
store.dispatch({

packages/gatsby/src/commands/build.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ function reportFailure(msg, err: Error) {
1717
type BuildArgs = {
1818
directory: string,
1919
sitePackageJson: object,
20-
browserslist: string[],
2120
prefixPaths: boolean,
2221
noUglify: boolean,
2322
openTracingConfigFile: string,

packages/gatsby/src/internal-plugins/load-babel-config/gatsby-node.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,15 @@ exports.onPreBootstrap = async ({ store }) => {
2121
await apiRunnerNode(`onCreateBabelConfig`, {
2222
stage: `build-html`,
2323
})
24-
const babelrcState = store.getState().babelrc
25-
babelrcState.browserslist = browserslist
26-
const babelState = JSON.stringify(babelrcState.stages, null, 4)
24+
25+
const babelState = JSON.stringify(
26+
{
27+
...store.getState().babelrc,
28+
browserslist,
29+
},
30+
null,
31+
2
32+
)
33+
2734
await fs.writeFile(directoryPath(`.cache/babelState.json`), babelState)
2835
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
jest.mock(`browserslist/node`, () => {
2+
return {
3+
findConfig: jest.fn(),
4+
}
5+
})
6+
const path = require(`path`)
7+
const getBrowsersList = require(`../browserslist`)
8+
const { findConfig: mockedFindConfig } = require(`browserslist/node`)
9+
10+
const BASE = path.resolve(`.`)
11+
12+
describe(`browserslist`, () => {
13+
it(`prefers returned browserslist results`, () => {
14+
const defaults = [`IE 11`]
15+
mockedFindConfig.mockReturnValueOnce({
16+
defaults,
17+
})
18+
19+
const list = getBrowsersList(BASE)
20+
21+
expect(list).toEqual(defaults)
22+
})
23+
24+
it(`falls back to defaults`, () => {
25+
mockedFindConfig.mockReturnValueOnce(undefined)
26+
27+
const list = getBrowsersList(BASE)
28+
29+
expect(list).toEqual([`>0.25%`, `not dead`])
30+
})
31+
})

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ const path = require(`path`)
22
const _ = require(`lodash`)
33

44
const loadCachedConfig = () => {
5-
let pluginBabelConfig = { test: { plugins: [], presets: [] } }
5+
let pluginBabelConfig = {
6+
stages: {
7+
test: { plugins: [], presets: [] },
8+
},
9+
}
610
if (process.env.NODE_ENV !== `test`) {
711
pluginBabelConfig = require(path.join(
812
process.cwd(),
@@ -15,7 +19,7 @@ const loadCachedConfig = () => {
1519
const getCustomOptions = () => {
1620
const pluginBabelConfig = loadCachedConfig()
1721
const stage = process.env.GATSBY_BUILD_STAGE || `test`
18-
return pluginBabelConfig[stage].options
22+
return pluginBabelConfig.stages[stage].options
1923
}
2024

2125
const prepareOptions = (babel, resolve = require.resolve) => {
@@ -59,14 +63,14 @@ const prepareOptions = (babel, resolve = require.resolve) => {
5963
// Go through babel state and create config items for presets/plugins from.
6064
const reduxPlugins = []
6165
const reduxPresets = []
62-
pluginBabelConfig[stage].plugins.forEach(plugin => {
66+
pluginBabelConfig.stages[stage].plugins.forEach(plugin => {
6367
reduxPlugins.push(
6468
babel.createConfigItem([resolve(plugin.name), plugin.options], {
6569
type: `plugin`,
6670
})
6771
)
6872
})
69-
pluginBabelConfig[stage].presets.forEach(preset => {
73+
pluginBabelConfig.stages[stage].presets.forEach(preset => {
7074
reduxPresets.push(
7175
babel.createConfigItem([resolve(preset.name), preset.options], {
7276
type: `preset`,
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const path = require(`path`)
2+
const browserslist = require(`browserslist/node`)
3+
4+
function installedGatsbyVersion(directory) {
5+
try {
6+
const { version } = require(path.join(
7+
directory,
8+
`node_modules`,
9+
`gatsby`,
10+
`package.json`
11+
))
12+
return parseInt(version.split(`.`)[0], 10)
13+
} catch (e) {
14+
return undefined
15+
}
16+
}
17+
18+
module.exports = function getBrowsersList(directory) {
19+
const fallback =
20+
installedGatsbyVersion(directory) === 1
21+
? [`>1%`, `last 2 versions`, `IE >= 9`]
22+
: [`>0.25%`, `not dead`]
23+
24+
const config = browserslist.findConfig(directory)
25+
26+
if (config && config.defaults) {
27+
return config.defaults
28+
}
29+
30+
return fallback
31+
}

0 commit comments

Comments
 (0)