Skip to content

Commit

Permalink
fix(gatsby): Multi-environment browserslist configs
Browse files Browse the repository at this point in the history
  • Loading branch information
chrissantamaria committed Jan 11, 2023
1 parent 9762642 commit 2c0f1f0
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 18 deletions.
18 changes: 6 additions & 12 deletions packages/gatsby/src/utils/__tests__/browserslist.js
Expand Up @@ -2,12 +2,12 @@ jest.mock(`browserslist/node`, () => {
const original = jest.requireActual(`browserslist/node`)
return {
...original,
findConfig: jest.fn(),
loadConfig: jest.fn(),
}
})
const path = require(`path`)
import { getBrowsersList, hasES6ModuleSupport } from "../browserslist"
const { findConfig: mockedFindConfig } = require(`browserslist/node`)
const { loadConfig: mockedLoadConfig } = require(`browserslist/node`)

const BASE = path.resolve(`.`)

Expand All @@ -16,17 +16,15 @@ const itWhenV4 = _CFLAGS_.GATSBY_MAJOR !== `5` ? it : it.skip
describe(`browserslist`, () => {
it(`prefers returned browserslist results`, () => {
const defaults = [`IE 11`]
mockedFindConfig.mockReturnValueOnce({
defaults,
})
mockedLoadConfig.mockReturnValueOnce(defaults)

const list = getBrowsersList(BASE)

expect(list).toEqual(defaults)
})

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

const list = getBrowsersList(BASE)

Expand All @@ -42,9 +40,7 @@ describe(`browserslist`, () => {

it(`hasES6ModuleSupport returns true if all browsers support es6 modules`, () => {
const defaults = [`chrome 90`]
mockedFindConfig.mockReturnValueOnce({
defaults,
})
mockedLoadConfig.mockReturnValueOnce(defaults)

expect(hasES6ModuleSupport(BASE)).toBe(true)
})
Expand All @@ -53,9 +49,7 @@ describe(`browserslist`, () => {
`hasES6ModuleSupport returns false if any browser does not support es6 modules`,
() => {
const defaults = [`IE 11`]
mockedFindConfig.mockReturnValueOnce({
defaults,
})
mockedLoadConfig.mockReturnValueOnce(defaults)
getBrowsersList(BASE)

expect(hasES6ModuleSupport(BASE)).toBe(false)
Expand Down
10 changes: 4 additions & 6 deletions packages/gatsby/src/utils/browserslist.ts
Expand Up @@ -30,13 +30,11 @@ export const getBrowsersList = (directory: string): Array<string> => {
const fallback =
installedGatsbyVersion(directory) === 1 ? fallbackV1 : fallbackOthers

const config = browserslist.findConfig(directory)
const config = browserslist.loadConfig({
path: directory,
})

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

return fallback
return config ?? fallback
}

export const hasES6ModuleSupport = (directory: string): boolean => {
Expand Down

0 comments on commit 2c0f1f0

Please sign in to comment.