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): Remove default support for non ESM browsers #36522

Merged
merged 8 commits into from
Sep 7, 2022
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
9 changes: 8 additions & 1 deletion packages/gatsby/src/utils/__tests__/browserslist.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,14 @@ describe(`browserslist`, () => {

const list = getBrowsersList(BASE)

expect(list).toEqual([`>0.25%`, `not dead`])
if (_CFLAGS_.GATSBY_MAJOR === `5`) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure we'll ever get to this point in the tests. Given that CFLAGS are environmental set by the compiler, they won't exist in the tests. I could be wrong, but either way, we won't ever be testing both cases.

Can we add a _CFLAGS_ mock to the setup/teardown? This might need some tweaking, but something like:

let originalCflags
beforeEach(() => {
  originalCflags = _CFLAGS_
  
  # TODO set up mock vars, set them in tests, etc
  _CFLAGS_ = {...}
})

afterEach(() => {
  _CFLAGS_ = originalCflags
})

Copy link
Contributor Author

@marvinjude marvinjude Sep 6, 2022

Choose a reason for hiding this comment

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

We do pass in CFLAGS flags to test with babel-preset-gatsby-package.

My thinking is that we'll eventually remove the else block since we'll never hit that when the compile flags becomes 5

cc - @wardpeet

Copy link
Contributor

Choose a reason for hiding this comment

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

Ahh ok. I won't push too hard on it because the logic is simple, but I'd prefer to have full logic coverage even before the Gatsby 5 launch.

expect(list).toEqual([
`>0.25% and supports es6-module`,
`not dead and supports es6-module`,
])
} else {
expect(list).toEqual([`>0.25%`, `not dead`])
}
})

it(`hasES6ModuleSupport returns true if all browsers support es6 modules`, () => {
Expand Down
13 changes: 10 additions & 3 deletions packages/gatsby/src/utils/browserslist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,17 @@ const installedGatsbyVersion = (directory: string): number | undefined => {
}

export const getBrowsersList = (directory: string): Array<string> => {
const fallbackV1 = [`>1%`, `last 2 versions`, `IE >= 9`]
let fallbackOthers = [`>0.25%`, `not dead`]

if (_CFLAGS_.GATSBY_MAJOR === `5`) {
fallbackOthers = fallbackOthers.map(
fallback => fallback + ` and supports es6-module`
)
}

const fallback =
installedGatsbyVersion(directory) === 1
? [`>1%`, `last 2 versions`, `IE >= 9`]
: [`>0.25%`, `not dead`]
installedGatsbyVersion(directory) === 1 ? fallbackV1 : fallbackOthers

const config = browserslist.findConfig(directory)

Expand Down