-
Notifications
You must be signed in to change notification settings - Fork 922
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
handle node-fetch, whatwg-fetch Node polyfills
- Loading branch information
1 parent
0a09b40
commit 57e8967
Showing
16 changed files
with
101 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import {Plugin} from 'rollup'; | ||
|
||
const FETCH_POLYFILL = ` | ||
// native patch for: node-fetch, whatwg-fetch | ||
// ref: https://github.com/tc39/proposal-global | ||
var getGlobal = function () { | ||
if (typeof self !== 'undefined') { return self; } | ||
if (typeof window !== 'undefined') { return window; } | ||
if (typeof global !== 'undefined') { return global; } | ||
throw new Error('unable to locate global object'); | ||
} | ||
var global = getGlobal(); | ||
export default global.fetch.bind(global); | ||
export const Headers = global.Headers; | ||
export const Request = global.Request; | ||
export const Response = global.Response; | ||
`; | ||
|
||
/** | ||
* rollup-plugin-catch-fetch | ||
* | ||
* How it works: NPM packages will sometimes contain Node.js-specific polyfills | ||
* for the native browser Fetch API. Since this makes no sense in an ESM web | ||
* project, we can replace these expensive polyfills with native references to | ||
* the fetch API. | ||
* | ||
* This still allows you to polyfill fetch in older browsers, if you desire. | ||
*/ | ||
export function rollupPluginCatchFetch(): Plugin { | ||
return { | ||
name: 'snowpack:fetch-handler', | ||
resolveId(id) { | ||
if (id !== 'node-fetch' && id !== 'whatwg-fetch') { | ||
return null; | ||
} | ||
return id; | ||
}, | ||
load(id) { | ||
if (id !== 'node-fetch' && id !== 'whatwg-fetch') { | ||
return null; | ||
} | ||
return FETCH_POLYFILL; | ||
}, | ||
}; | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 0 additions & 4 deletions
4
test/integration/config-external/mock-test-package/entrypoint.js
This file was deleted.
Oops, something went wrong.
5 changes: 0 additions & 5 deletions
5
test/integration/config-external/mock-test-package/package.json
This file was deleted.
Oops, something went wrong.
5 changes: 5 additions & 0 deletions
5
test/integration/dep-node-fetch/expected-install/import-map.json
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,5 @@ | ||
{ | ||
"imports": { | ||
"mock-test-package-a": "./mock-test-package-a.js" | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
test/integration/dep-node-fetch/expected-install/mock-test-package-a.js
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,16 @@ | ||
// native patch for: node-fetch, whatwg-fetch | ||
// ref: https://github.com/tc39/proposal-global | ||
var getGlobal = function () { | ||
if (typeof self !== 'undefined') { return self; } | ||
if (typeof window !== 'undefined') { return window; } | ||
if (typeof global !== 'undefined') { return global; } | ||
throw new Error('unable to locate global object'); | ||
}; | ||
var global = getGlobal(); | ||
var entrypoint = global.fetch.bind(global); | ||
const Headers = global.Headers; | ||
const Request = global.Request; | ||
const Response = global.Response; | ||
|
||
export default entrypoint; | ||
export { Headers, Request, Response }; |
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,3 @@ | ||
✔ snowpack install complete. | ||
⦿ web_modules/ size gzip brotli | ||
└─ mock-test-package-a.js XXXX KB XXXX KB XXXX KB |
3 changes: 3 additions & 0 deletions
3
test/integration/dep-node-fetch/node_modules/mock-test-package-a/entrypoint.js
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
9 changes: 9 additions & 0 deletions
9
test/integration/dep-node-fetch/node_modules/mock-test-package-a/package.json
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,12 @@ | ||
{ | ||
"description": "Handle specific node.js fetch polyfill packages", | ||
"scripts": { | ||
"TEST": "node ../../../pkg/dist-node/index.bin.js" | ||
}, | ||
"snowpack": { | ||
"install": ["mock-test-package-a"] | ||
}, | ||
"dependencies": { | ||
"mock-test-package-a": "^1.0.0" | ||
} | ||
} |