-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: upgrade core-js to version 3 (#25158)
* chore: upgrade to core-js@3 * add tests to core-js resolver * update babel-preset tests
- Loading branch information
Showing
16 changed files
with
207 additions
and
30 deletions.
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
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" | ||
import * as path from "path" | ||
|
||
// 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.