Skip to content

Commit 669de83

Browse files
authored
feat(gatsby): shim much used polyfills to noOp (#25162)
* feat(gatsby-legacy-polyfills): create polyfill package * feat(babel-preset-gatsby): disable all unnecessary polyfills * feat(gatsby): add polyfill chunk to gatsby * feat(gatsby): shim much used polyfills to noOp
1 parent cbbed80 commit 669de83

File tree

5 files changed

+36
-6
lines changed

5 files changed

+36
-6
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = global.fetch
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// empty file
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = Object.assign
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
fetch: global.fetch,
3+
}

packages/gatsby/src/utils/webpack.config.js

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ const debug = require(`debug`)(`gatsby:webpack-config`)
1313
const report = require(`gatsby-cli/lib/reporter`)
1414
import { withBasePath, withTrailingSlash } from "./path"
1515
import { getGatsbyDependents } from "./gatsby-dependents"
16-
1716
const apiRunnerNode = require(`./api-runner-node`)
1817
import { createWebpackUtils } from "./webpack-utils"
1918
import { hasLocalEslint } from "./local-eslint-config-finder"
@@ -425,11 +424,36 @@ module.exports = async (
425424
const target =
426425
stage === `build-html` || stage === `develop-html` ? `node` : `web`
427426
if (target === `web`) {
428-
// force to use es modules when importing internals of @reach.router
429-
// for browser bundles
430-
resolve.alias[`@reach/router`] = path.join(
431-
path.dirname(require.resolve(`@reach/router/package.json`)),
432-
`es`
427+
const noOp = directoryPath(`.cache/polyfills/no-op.js`)
428+
const objectAssignStub = directoryPath(
429+
`.cache/polyfills/object-assign.js`
430+
)
431+
const fetchStub = directoryPath(`.cache/polyfills/fetch.js`)
432+
const whatwgFetchStub = directoryPath(`.cache/polyfills/whatwg-fetch.js`)
433+
resolve.alias = Object.assign(
434+
{},
435+
{
436+
// force to use es modules when importing internals of @reach.router
437+
// for browser bundles
438+
"@reach/router": path.join(
439+
path.dirname(require.resolve(`@reach/router/package.json`)),
440+
`es`
441+
),
442+
443+
// These files are already polyfilled so these should return in a no-op
444+
// Stub Package: object.assign & object-assign
445+
"object.assign": objectAssignStub,
446+
"object-assign$": objectAssignStub,
447+
"@babel/runtime/helpers/extends.js$": objectAssignStub,
448+
// Stub package: fetch
449+
unfetch$: fetchStub,
450+
"unfetch/polyfill$": noOp,
451+
"isomorphic-unfetch$": fetchStub,
452+
"whatwg-fetch$": whatwgFetchStub,
453+
// Stub package: url-polyfill
454+
"url-polyfill$": noOp,
455+
},
456+
resolve.alias
433457
)
434458
}
435459

0 commit comments

Comments
 (0)