Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(gatsby): correctly pick up browserslist overrides #9669

Merged
merged 6 commits into from
Nov 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions packages/babel-preset-gatsby/src/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,9 @@ it(`Specifies proper presets and plugins for build-html stage`, () => {
})

it(`Allows to configure browser targets`, () => {
const targets = `last 1 version`
const { presets } = preset(null, {
targets: { browsers: [`last 1 version`] },
targets,
})

expect(presets[0]).toEqual([
Expand All @@ -111,9 +112,7 @@ it(`Allows to configure browser targets`, () => {
loose: true,
modules: false,
useBuiltIns: `usage`,
targets: {
browsers: [`last 1 version`],
},
targets,
},
])
})
22 changes: 9 additions & 13 deletions packages/babel-preset-gatsby/src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const path = require(`path`)

const r = m => require.resolve(m)
const resolve = m => require.resolve(m)

const loadCachedConfig = () => {
let pluginBabelConfig = {}
Expand All @@ -13,7 +13,7 @@ const loadCachedConfig = () => {
return pluginBabelConfig
}

function preset(context, options = {}) {
module.exports = function preset(_, options = {}) {
let { targets = null } = options

const pluginBabelConfig = loadCachedConfig()
Expand All @@ -25,16 +25,14 @@ function preset(context, options = {}) {
node: `current`,
}
} else {
targets = {
browsers: pluginBabelConfig.browserslist,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: this will be removed in later version in favor of just setting "targets" to a query directly.

https://babeljs.io/docs/en/babel-preset-env#targetsbrowsers

}
targets = pluginBabelConfig.browserslist
}
}

return {
presets: [
[
r(`@babel/preset-env`),
resolve(`@babel/preset-env`),
{
loose: true,
modules: stage === `test` ? `commonjs` : false,
Expand All @@ -43,7 +41,7 @@ function preset(context, options = {}) {
},
],
[
r(`@babel/preset-react`),
resolve(`@babel/preset-react`),
{
useBuiltIns: true,
pragma: `React.createElement`,
Expand All @@ -53,15 +51,15 @@ function preset(context, options = {}) {
],
plugins: [
[
r(`@babel/plugin-proposal-class-properties`),
resolve(`@babel/plugin-proposal-class-properties`),
{
loose: true,
},
],
r(`babel-plugin-macros`),
r(`@babel/plugin-syntax-dynamic-import`),
resolve(`babel-plugin-macros`),
resolve(`@babel/plugin-syntax-dynamic-import`),
[
r(`@babel/plugin-transform-runtime`),
resolve(`@babel/plugin-transform-runtime`),
{
helpers: true,
regenerator: true,
Expand All @@ -70,5 +68,3 @@ function preset(context, options = {}) {
],
}
}

module.exports = preset
6 changes: 5 additions & 1 deletion packages/gatsby/src/bootstrap/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const Promise = require(`bluebird`)

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

const directory = slash(args.directory)

const program = {
...args,
browserslist: getBrowserslist(directory),
// Fix program directory path for windows env.
directory: slash(args.directory),
directory,
}

store.dispatch({
Expand Down
1 change: 0 additions & 1 deletion packages/gatsby/src/commands/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ function reportFailure(msg, err: Error) {
type BuildArgs = {
directory: string,
sitePackageJson: object,
browserslist: string[],
prefixPaths: boolean,
noUglify: boolean,
openTracingConfigFile: string,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,15 @@ exports.onPreBootstrap = async ({ store }) => {
await apiRunnerNode(`onCreateBabelConfig`, {
stage: `build-html`,
})
const babelrcState = store.getState().babelrc
babelrcState.browserslist = browserslist
const babelState = JSON.stringify(babelrcState.stages, null, 4)

const babelState = JSON.stringify(
{
...store.getState().babelrc,
browserslist,
},
null,
2
)

await fs.writeFile(directoryPath(`.cache/babelState.json`), babelState)
}
31 changes: 31 additions & 0 deletions packages/gatsby/src/utils/__tests__/browserslist.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
jest.mock(`browserslist/node`, () => {
return {
findConfig: jest.fn(),
}
})
const path = require(`path`)
const getBrowsersList = require(`../browserslist`)
const { findConfig: mockedFindConfig } = require(`browserslist/node`)

const BASE = path.resolve(`.`)

describe(`browserslist`, () => {
it(`prefers returned browserslist results`, () => {
const defaults = [`IE 11`]
mockedFindConfig.mockReturnValueOnce({
defaults,
})

const list = getBrowsersList(BASE)

expect(list).toEqual(defaults)
})

it(`falls back to defaults`, () => {
mockedFindConfig.mockReturnValueOnce(undefined)

const list = getBrowsersList(BASE)

expect(list).toEqual([`>0.25%`, `not dead`])
})
})
12 changes: 8 additions & 4 deletions packages/gatsby/src/utils/babel-loader-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ const path = require(`path`)
const _ = require(`lodash`)

const loadCachedConfig = () => {
let pluginBabelConfig = { test: { plugins: [], presets: [] } }
let pluginBabelConfig = {
stages: {
test: { plugins: [], presets: [] },
},
}
if (process.env.NODE_ENV !== `test`) {
pluginBabelConfig = require(path.join(
process.cwd(),
Expand All @@ -15,7 +19,7 @@ const loadCachedConfig = () => {
const getCustomOptions = () => {
const pluginBabelConfig = loadCachedConfig()
const stage = process.env.GATSBY_BUILD_STAGE || `test`
return pluginBabelConfig[stage].options
return pluginBabelConfig.stages[stage].options
}

const prepareOptions = (babel, resolve = require.resolve) => {
Expand Down Expand Up @@ -59,14 +63,14 @@ const prepareOptions = (babel, resolve = require.resolve) => {
// Go through babel state and create config items for presets/plugins from.
const reduxPlugins = []
const reduxPresets = []
pluginBabelConfig[stage].plugins.forEach(plugin => {
pluginBabelConfig.stages[stage].plugins.forEach(plugin => {
reduxPlugins.push(
babel.createConfigItem([resolve(plugin.name), plugin.options], {
type: `plugin`,
})
)
})
pluginBabelConfig[stage].presets.forEach(preset => {
pluginBabelConfig.stages[stage].presets.forEach(preset => {
reduxPresets.push(
babel.createConfigItem([resolve(preset.name), preset.options], {
type: `preset`,
Expand Down
31 changes: 31 additions & 0 deletions packages/gatsby/src/utils/browserslist.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const path = require(`path`)
const browserslist = require(`browserslist/node`)

function installedGatsbyVersion(directory) {
try {
const { version } = require(path.join(
directory,
`node_modules`,
`gatsby`,
`package.json`
))
return parseInt(version.split(`.`)[0], 10)
} catch (e) {
return undefined
}
}

module.exports = function getBrowsersList(directory) {
const fallback =
installedGatsbyVersion(directory) === 1
? [`>1%`, `last 2 versions`, `IE >= 9`]
: [`>0.25%`, `not dead`]

const config = browserslist.findConfig(directory)

if (config && config.defaults) {
return config.defaults
}

return fallback
}