Skip to content

Commit

Permalink
chore: upgrade core-js to version 3 (#25595)
Browse files Browse the repository at this point in the history
  • Loading branch information
wardpeet committed Jul 9, 2020
1 parent 80dab3f commit 46e7988
Show file tree
Hide file tree
Showing 20 changed files with 551 additions and 376 deletions.
Expand Up @@ -67,7 +67,7 @@ Array [
Array [
"@babel/preset-env",
Object {
"corejs": 2,
"corejs": 3,
"debug": false,
"loose": true,
"modules": "commonjs",
Expand All @@ -90,7 +90,7 @@ Array [
Array [
"@babel/preset-env",
Object {
"corejs": 2,
"corejs": 3,
"debug": true,
"loose": true,
"modules": "commonjs",
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-preset-gatsby-package/index.js
Expand Up @@ -14,7 +14,7 @@ function preset(context, options = {}) {
}

const nodeConfig = {
corejs: 2,
corejs: 3,
useBuiltIns: `entry`,
targets: {
node: nodeVersion,
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-preset-gatsby-package/package.json
Expand Up @@ -18,7 +18,7 @@
"@babel/preset-flow": "^7.10.1",
"@babel/preset-react": "^7.10.1",
"babel-plugin-dynamic-import-node": "^2.3.3",
"core-js": "^2.6.11"
"core-js": "^3.6.5"
},
"peerDependencies": {
"@babel/core": "^7.0.0"
Expand Down
3 changes: 2 additions & 1 deletion packages/babel-preset-gatsby/package.json
Expand Up @@ -24,7 +24,8 @@
"gatsby-core-utils": "^1.3.11"
},
"peerDependencies": {
"@babel/core": "^7.0.0"
"@babel/core": "^7.0.0",
"core-js": "^3.0.0"
},
"license": "MIT",
"main": "index.js",
Expand Down
Expand Up @@ -19,7 +19,7 @@ Object {
Array [
"<PROJECT_ROOT>/node_modules/@babel/preset-env/lib/index.js",
Object {
"corejs": 2,
"corejs": 3,
"exclude": Array [
"transform-typeof-symbol",
],
Expand Down
Expand Up @@ -45,7 +45,7 @@ Object {
Array [
"<PROJECT_ROOT>/node_modules/@babel/preset-env/lib/index.js",
Object {
"corejs": 2,
"corejs": 3,
"exclude": Array [
"transform-typeof-symbol",
],
Expand Down Expand Up @@ -120,7 +120,7 @@ Object {
Array [
"<PROJECT_ROOT>/node_modules/@babel/preset-env/lib/index.js",
Object {
"corejs": 2,
"corejs": 3,
"exclude": Array [
"transform-typeof-symbol",
],
Expand Down Expand Up @@ -187,7 +187,7 @@ Object {
Array [
"<PROJECT_ROOT>/node_modules/@babel/preset-env/lib/index.js",
Object {
"corejs": 2,
"corejs": 3,
"exclude": Array [
"transform-typeof-symbol",
],
Expand Down Expand Up @@ -254,7 +254,7 @@ Object {
Array [
"<PROJECT_ROOT>/node_modules/@babel/preset-env/lib/index.js",
Object {
"corejs": 2,
"corejs": 3,
"exclude": Array [
"transform-typeof-symbol",
],
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-preset-gatsby/src/__tests__/index.js
Expand Up @@ -24,7 +24,7 @@ describe(`babel-preset-gatsby`, () => {
expect.stringContaining(path.join(`@babel`, `preset-env`)),
{
exclude: [`transform-typeof-symbol`],
corejs: 2,
corejs: 3,
loose: true,
modules: false,
useBuiltIns: `usage`,
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-preset-gatsby/src/dependencies.ts
Expand Up @@ -32,7 +32,7 @@ export default (_?: unknown, options: IPresetOptions = {}) => {
{
// Allow importing core-js in entrypoint and use browserlist to select polyfills
useBuiltIns: `usage`,
corejs: 2,
corejs: 3,
modules: false,
// Exclude transforms that make all code slower (https://github.com/facebook/create-react-app/pull/5278)
exclude: [`transform-typeof-symbol`],
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-preset-gatsby/src/index.js
Expand Up @@ -51,7 +51,7 @@ module.exports = function preset(_, options = {}) {
[
resolve(`@babel/preset-env`),
{
corejs: 2,
corejs: 3,
loose: true,
modules: stage === `test` ? `commonjs` : false,
useBuiltIns: `usage`,
Expand Down
1 change: 0 additions & 1 deletion packages/gatsby-cli/package.json
Expand Up @@ -11,7 +11,6 @@
},
"dependencies": {
"@babel/code-frame": "^7.10.3",
"@babel/runtime": "^7.10.3",
"@hapi/joi": "^15.1.1",
"@types/common-tags": "^1.8.0",
"better-opn": "^1.0.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby-cli/src/index.ts
Expand Up @@ -45,7 +45,7 @@ if (semver.prerelease(version)) {
report.warn(
report.stripIndent(`
You are currently using a prerelease version of Node (${version}), which is not supported.
You can use this for testing, but we do not recommend it in production.
You can use this for testing, but we do not recommend it in production.
Before reporting any bugs, please test with a supported version of Node (>=${MIN_NODE_VERSION}).`)
)
}
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby-graphiql-explorer/package.json
Expand Up @@ -39,7 +39,7 @@
"@babel/preset-react": "^7.10.1",
"babel-loader": "^8.1.0",
"babel-preset-gatsby-package": "^0.5.1",
"core-js": "^2.6.11",
"core-js": "^3.6.5",
"cross-env": "^5.2.1",
"css-loader": "^1.0.1",
"graphiql": "^0.17.5",
Expand Down
Expand Up @@ -23,7 +23,7 @@ module.exports = {
[
`@babel/preset-env`,
{
corejs: 2,
corejs: 3,
loose: true,
modules: `commonjs`,
useBuiltIns: `usage`,
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby-plugin-mdx/package.json
Expand Up @@ -27,7 +27,7 @@
"@babel/types": "^7.10.3",
"camelcase-css": "^2.0.1",
"change-case": "^3.1.0",
"core-js": "2",
"core-js": "^3.6.5",
"dataloader": "^1.4.0",
"debug": "^4.1.1",
"escape-string-regexp": "^1.0.5",
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby-plugin-mdx/utils/gen-mdx.js
Expand Up @@ -155,7 +155,7 @@ ${code}`
require(`@babel/preset-env`),
{
useBuiltIns: `entry`,
corejs: 2,
corejs: 3,
modules: false,
},
],
Expand Down
4 changes: 2 additions & 2 deletions packages/gatsby/package.json
Expand Up @@ -13,7 +13,6 @@
"@babel/code-frame": "^7.10.3",
"@babel/core": "^7.10.3",
"@babel/parser": "^7.10.3",
"@babel/polyfill": "^7.8.7",
"@babel/runtime": "^7.10.3",
"@babel/traverse": "^7.10.3",
"@hapi/joi": "^15.1.1",
Expand Down Expand Up @@ -46,7 +45,7 @@
"compression": "^1.7.4",
"convert-hrtime": "^3.0.0",
"copyfiles": "^2.3.0",
"core-js": "^2.6.11",
"core-js": "^3.6.5",
"cors": "^2.8.5",
"css-loader": "^1.0.1",
"date-fns": "^2.14.0",
Expand Down Expand Up @@ -167,6 +166,7 @@
"babel-preset-gatsby-package": "^0.5.1",
"cross-env": "^5.2.1",
"documentation": "^12.3.0",
"enhanced-resolve": "^4.2.0",
"eslint-plugin-jsx-a11y": "^6.3.1",
"react": "^16.12.0",
"react-dom": "^16.12.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/gatsby/src/utils/webpack.config.js
Expand Up @@ -5,6 +5,7 @@ const fs = require(`fs-extra`)
const path = require(`path`)
const dotenv = require(`dotenv`)
const PnpWebpackPlugin = require(`pnp-webpack-plugin`)
const { CoreJSResolver } = require(`./webpack/corejs-resolver`)
const { store } = require(`../redux`)
const { actions } = require(`../redux/actions`)
const { getPublicPath } = require(`./get-public-path`)
Expand Down Expand Up @@ -390,7 +391,6 @@ module.exports = async (
"@babel/runtime": path.dirname(
require.resolve(`@babel/runtime/package.json`)
),
"core-js": path.dirname(require.resolve(`core-js/package.json`)),
// TODO: Remove entire block when we make fast-refresh the default
...(process.env.GATSBY_HOT_LOADER !== `fast-refresh`
? {
Expand All @@ -417,6 +417,7 @@ module.exports = async (
PnpWebpackPlugin.bind(directoryPath(`public`), module),
// Transparently resolve packages via PnP when needed; noop otherwise
PnpWebpackPlugin,
new CoreJSResolver(),
],
}

Expand Down Expand Up @@ -616,7 +617,6 @@ module.exports = async (
`@reach/router/lib/history`,
`@reach/router`,
`common-tags`,
/^core-js\//,
`crypto`,
`debug`,
`fs`,
Expand Down
75 changes: 75 additions & 0 deletions packages/gatsby/src/utils/webpack/__tests__/corejs-resolver.ts
@@ -0,0 +1,75 @@
import { slash } from "gatsby-core-utils"
import { CoreJSResolver } from "../corejs-resolver"

function executeResolve(
resolver: CoreJSResolver,
request: { request: string },
doResolveMock: unknown
): Promise<string> {
return new Promise((resolve, reject) => {
const webpackResolver = {
doResolve: doResolveMock,
ensureHook: (hook: string): string => hook,
getHook: (): Record<string, unknown> => {
return {
tapAsync: (_name: string, fn: Function): void => {
fn(request, null, (err, result) =>
err ? reject(err) : resolve(result)
)
},
}
},
}

resolver.apply(webpackResolver)
})
}

describe(`CoreJSResolver`, () => {
it(`should convert core-js@2 file to core-js@3`, async () => {
const resolver = new CoreJSResolver()

const doResolve = jest.fn((_, request, __, ___, callback) =>
callback(null, slash(request.request))
)

expect(
await executeResolve(
resolver,
{ request: `core-js/modules/es6.array.split.js` },
doResolve
)
).toEqual(expect.stringContaining(`core-js/modules/es.array.split.js`))
})

it(`should convert es6.regexp.replace to it's corejs@3 equivalent`, async () => {
const resolver = new CoreJSResolver()

const doResolve = jest.fn((_, request, __, ___, callback) =>
callback(null, slash(request.request))
)

expect(
await executeResolve(
resolver,
{ request: `core-js/modules/es6.regexp.replace.js` },
doResolve
)
).toEqual(expect.stringContaining(`core-js/modules/es.string.replace.js`))
})

it(`should ignore non corejs requests`, async () => {
const resolver = new CoreJSResolver()

const doResolve = jest.fn()

expect(
await executeResolve(
resolver,
{ request: `gatsby/not/core-js.js` },
doResolve
)
).toBeUndefined()
expect(doResolve).not.toHaveBeenCalled()
})
})

0 comments on commit 46e7988

Please sign in to comment.