Skip to content

Commit

Permalink
feat(gatsby): shim much used polyfills to noOp (#25162)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
wardpeet committed Jul 9, 2020
1 parent cbbed80 commit 669de83
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 6 deletions.
1 change: 1 addition & 0 deletions packages/gatsby/cache-dir/polyfills/fetch.js
@@ -0,0 +1 @@
module.exports = global.fetch
1 change: 1 addition & 0 deletions packages/gatsby/cache-dir/polyfills/no-op.js
@@ -0,0 +1 @@
// empty file
1 change: 1 addition & 0 deletions packages/gatsby/cache-dir/polyfills/object-assign.js
@@ -0,0 +1 @@
module.exports = Object.assign
3 changes: 3 additions & 0 deletions packages/gatsby/cache-dir/polyfills/whatwg-fetch.js
@@ -0,0 +1,3 @@
module.exports = {
fetch: global.fetch,
}
36 changes: 30 additions & 6 deletions packages/gatsby/src/utils/webpack.config.js
Expand Up @@ -13,7 +13,6 @@ const debug = require(`debug`)(`gatsby:webpack-config`)
const report = require(`gatsby-cli/lib/reporter`)
import { withBasePath, withTrailingSlash } from "./path"
import { getGatsbyDependents } from "./gatsby-dependents"

const apiRunnerNode = require(`./api-runner-node`)
import { createWebpackUtils } from "./webpack-utils"
import { hasLocalEslint } from "./local-eslint-config-finder"
Expand Down Expand Up @@ -425,11 +424,36 @@ module.exports = async (
const target =
stage === `build-html` || stage === `develop-html` ? `node` : `web`
if (target === `web`) {
// force to use es modules when importing internals of @reach.router
// for browser bundles
resolve.alias[`@reach/router`] = path.join(
path.dirname(require.resolve(`@reach/router/package.json`)),
`es`
const noOp = directoryPath(`.cache/polyfills/no-op.js`)
const objectAssignStub = directoryPath(
`.cache/polyfills/object-assign.js`
)
const fetchStub = directoryPath(`.cache/polyfills/fetch.js`)
const whatwgFetchStub = directoryPath(`.cache/polyfills/whatwg-fetch.js`)
resolve.alias = Object.assign(
{},
{
// force to use es modules when importing internals of @reach.router
// for browser bundles
"@reach/router": path.join(
path.dirname(require.resolve(`@reach/router/package.json`)),
`es`
),

// These files are already polyfilled so these should return in a no-op
// Stub Package: object.assign & object-assign
"object.assign": objectAssignStub,
"object-assign$": objectAssignStub,
"@babel/runtime/helpers/extends.js$": objectAssignStub,
// Stub package: fetch
unfetch$: fetchStub,
"unfetch/polyfill$": noOp,
"isomorphic-unfetch$": fetchStub,
"whatwg-fetch$": whatwgFetchStub,
// Stub package: url-polyfill
"url-polyfill$": noOp,
},
resolve.alias
)
}

Expand Down

0 comments on commit 669de83

Please sign in to comment.