-
Notifications
You must be signed in to change notification settings - Fork 915
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 (#547)
- Loading branch information
1 parent
31a5f70
commit 23289f4
Showing
17 changed files
with
134 additions
and
19 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.
2 changes: 1 addition & 1 deletion
2
src/rollup-plugin-entrypoint-alias.ts → ...plugins/rollup-plugin-entrypoint-alias.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
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
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
src/rollup-plugin-wrap-install-targets.ts → ...ins/rollup-plugin-wrap-install-targets.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
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
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" | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
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,45 @@ | ||
// 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 nodeFetch = global.fetch.bind(global); | ||
const Headers = global.Headers; | ||
const Request = global.Request; | ||
const Response = global.Response; | ||
|
||
var fetch = /*#__PURE__*/Object.freeze({ | ||
__proto__: null, | ||
'default': nodeFetch, | ||
Headers: Headers, | ||
Request: Request, | ||
Response: Response | ||
}); | ||
|
||
// native patch for: node-fetch, whatwg-fetch | ||
// ref: https://github.com/tc39/proposal-global | ||
var getGlobal$1 = function () { | ||
if (typeof self !== 'undefined') { return self; } | ||
if (typeof window !== 'undefined') { return window; } | ||
if (typeof global$1 !== 'undefined') { return global$1; } | ||
throw new Error('unable to locate global object'); | ||
}; | ||
var global$1 = getGlobal$1(); | ||
var whatwgFetch = global$1.fetch.bind(global$1); | ||
const Headers$1 = global$1.Headers; | ||
const Request$1 = global$1.Request; | ||
const Response$1 = global$1.Response; | ||
|
||
var fetch_ = /*#__PURE__*/Object.freeze({ | ||
__proto__: null, | ||
'default': whatwgFetch, | ||
Headers: Headers$1, | ||
Request: Request$1, | ||
Response: Response$1 | ||
}); | ||
|
||
console.log(fetch, fetch_); |
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" | ||
} | ||
} |
23289f4
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.
Successfully deployed to following URLs: