-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
chore: upgrade core-js to version 3 #25158
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
#!/usr/bin/env node | ||
|
||
import "@babel/polyfill" | ||
import os from "os" | ||
import semver from "semver" | ||
import util from "util" | ||
|
@@ -46,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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is prettier |
||
Before reporting any bugs, please test with a supported version of Node (>=${MIN_NODE_VERSION}).`) | ||
) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
packages/gatsby/src/utils/webpack/__tests__/corejs-resolver.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 its 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() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import Resolver from "enhanced-resolve/lib/Resolver" | ||
wardpeet marked this conversation as resolved.
Show resolved
Hide resolved
|
||
import * as path from "path" | ||
blainekasten marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// Core-js uses es6, es7 & web prefixes, which we'll convert to core-js 3 | ||
const coreJs2FileRegex = /\/modules\/(es6|es7|web)\.|\/es6\/|\/es7\// | ||
|
||
// Try to replace core-js2 files to core-js@3 to reduce file size | ||
const replaceMap = [ | ||
[`/es6.`, `/es.`], | ||
[`/es7.`, `/es.`], | ||
[`/es6/`, `/es/`], | ||
[`/es7/`, `/es/`], | ||
[`/es7/`, `/es/`], | ||
[`web.dom.iterable`, `web.dom-collections.iterator.js`], | ||
[`typed.data-view`, `data-view`], | ||
[`regexp.match`, `string.match`], | ||
[`regexp.replace`, `string.replace`], | ||
[`regexp.search`, `string.search`], | ||
[`regexp.split`, `string.split`], | ||
] | ||
|
||
interface IRequest { | ||
request?: string | ||
path: string | ||
} | ||
|
||
/** | ||
* Babel-preset is set to corejs@3 which will add automatic polyfills. If a project has core-js@2 installed in their root or a package got compiled with core-js@2 | ||
* we need to convert it to corejs@3 because core-js@2 isn't available or we might add multiple polyfills for the same problem. | ||
* | ||
* The resolver converts core-js@2 imports to core-js@3 imports to make our bundle as small as possible. | ||
*/ | ||
export class CoreJSResolver { | ||
_coreJSNodeModulesPath: string | ||
_resolver?: Resolver | ||
_target?: string | ||
|
||
constructor() { | ||
// Get the nodemodules directory where core-js of gatsby lives | ||
// it might be inside gatsby/node_modules when multiple core-js versions are loaded | ||
this._coreJSNodeModulesPath = path.dirname( | ||
path.dirname(require.resolve(`core-js`)) | ||
) | ||
} | ||
|
||
resolve( | ||
request: IRequest, | ||
resolveContext: unknown, | ||
callback: (err?: Error | null, result?: unknown) => void | ||
): void { | ||
const innerRequest = request.request || request.path | ||
|
||
// we only care about core-js | ||
if (!innerRequest || !innerRequest.startsWith(`core-js/`)) { | ||
return void callback() | ||
} | ||
|
||
let coreJsRequest = innerRequest | ||
let resolveMessage = `alias core-js@3 to gatsby's core-js package` | ||
|
||
// preset-env adds packages from modules/ so we rewrite them to our gatsby package | ||
if (coreJs2FileRegex.test(coreJsRequest)) { | ||
replaceMap.forEach(([search, replace]) => { | ||
coreJsRequest = coreJsRequest.replace(search, replace) | ||
}) | ||
|
||
resolveMessage = `map core-js@2(${innerRequest}) to corejs@3(${coreJsRequest})` | ||
} | ||
|
||
return void this._resolver.doResolve( | ||
this._target, | ||
{ | ||
...request, | ||
request: path.resolve(this._coreJSNodeModulesPath, coreJsRequest), | ||
}, | ||
resolveMessage, | ||
resolveContext, | ||
(err, result) => { | ||
if (err) { | ||
return callback(err) | ||
} | ||
|
||
// if a rename fails we try to load the original file | ||
// this could error when our mapping isn't complete. I've tested this on a couple of sites | ||
// and couldn't find anything but you never know. | ||
if (result === undefined) { | ||
return callback() | ||
} | ||
|
||
return callback(null, result) | ||
} | ||
) | ||
} | ||
|
||
apply(resolver: Resolver): void { | ||
this._target = resolver.ensureHook(`resolve`) | ||
this._resolver = resolver | ||
|
||
resolver | ||
.getHook(`described-resolve`) | ||
.tapAsync(`CoreJSResolver`, this.resolve.bind(this)) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we don't need this anymore (node 10.13 is pretty good)