-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
build/distribute as UMD #616
Conversation
package.json
Outdated
}, | ||
"files": [ | ||
"LICENSE", | ||
"fetch.js" | ||
"dist/whatwg-fetch.js" |
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.
If it's desired we could build to ./fetch.js
instead of putting things inside dist
(and naming it something else). I did it this way as it's more traditional in the community, but otherwise your call.
webpack.config.js
Outdated
output: { | ||
filename: 'whatwg-fetch.js', | ||
path: path.resolve(__dirname, 'dist'), | ||
library: 'WHATWGFetch', |
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.
Wasn't sure what to call the global object. Any preferences?
53ca60e
to
a5ec273
Compare
src/index.js
Outdated
@@ -0,0 +1,476 @@ | |||
'use strict'; |
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.
The way github is displaying this isn't great (it doesn't realize the file move/rename and changes). Here is the separate commit with whitespace ignored (?w=1
flag) a5ec273?w=1#diff-1fdf421c05c1140f6d71444ea2b27638
Thank you for working on this! What do you think about skipping webpack dependency altogether (it might be an overkill), and wrap current (function(root, factory) {
if (typeof define === 'function' && define.amd) {
define([], function(){ return factory({}, root) })
} else if (typeof module === 'object' && module.exports) {
factory(module.exports, root)
} else {
factory({}, root)
}
}(typeof self !== 'undefined' ? self : this, function(exports, global) {
// current fetch.js contents
if (!global.fetch) {
global.fetch = fetch
// ...
}
exports.fetch = fetch
exports.Request = Request
// ...
return exports
})) |
18f8d4a
to
a05392c
Compare
@mislav based on our PM conversations I've updated the PR to have the UMD preamble inline. I also added a simple test to verify the exports are there in CJS. ✌️ |
fetch.js
Outdated
(function(root, factory) { | ||
/* jshint strict:false */ | ||
/* globals define, module */ | ||
if (typeof define === 'function' && define.amd) { |
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.
I initially tried to use/* jshint ignore: start */ (and end) but it didn't work in 2.8.0 jshint (maybe jshint/jshint#2411). After upgrading to jshint 2.9.0 it works, but then it says factory is unused, and everything I tried to ignore that caused even more issues, and 2.9.0 also detects other undefined variables like Promise/Symbol. So this seemed to be the solution with the least amount of changes.
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.
Great start!
fetch.js
Outdated
searchParams: 'URLSearchParams' in self, | ||
iterable: 'Symbol' in self && 'iterator' in Symbol, | ||
blob: 'FileReader' in self && 'Blob' in self && (function() { | ||
searchParams: 'URLSearchParams' in global, |
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.
To keep the diff to a minimum, could global
simply continue to be called self
like before?
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.
done
script/test
Outdated
@@ -7,3 +7,5 @@ if [ -n "$SAUCE_BROWSER" ]; then | |||
else | |||
./script/phantomjs | |||
fi | |||
|
|||
./node_modules/mocha/bin/mocha test/cjs.js |
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.
Why is this separate file needed?
These tests could be in test.js
. That file already jumps through hoops to exercise both native and polyfilled fetch, and this module approach could potentially clean it up.
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.
A separate file was used as it is testing whether the CJS exports are available and the easiest and foolproof way seemed to be to do it inside a real CJS environment. test.js is evaluated in the context of a browser, where no require
exists, and I did not see any other tests running inside a CJS env. Sorry about that, am I missing an easier solution? I could make an ajax request then eval it inside a fake CJS environment stub but this seemed harder and more brittle. lmk what you had in mind.
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.
@mislav any thoughts? 🕺
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.
@mislav hey there. Anything I can do to move this forward? I'm truly sorry to be that nagging person 😞I realize OSS is mostly volunteer and super appreciate what you've done for everyone! We're approaching a company deadline for getting in dependencies and we really need abort polyfill support for fetch so it's important for us to get this in so we can then get #592 in as well. I'm happy to do any leg work, just need to know what to do 😃
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.
bump
Hey let us know if we can help here! This is a much needed feature! |
fyi https://www.npmjs.com/package/fetch-ponyfill already provides a module for this |
This comment has been minimized.
This comment has been minimized.
@mislav anything I can do to help get this, or something similar landed? I would hate to have rely on a fork long-term. |
First of all, loading our UMD dist file in Node doesn't work due to usage of `self` within `fetch.js`. Second, we don't need to test exports, since we trust Rollup doing its job.
Prior to this change, `test.js` had to be loaded before `fetch.js` so it can nulify the native implementation of `window.fetch` to force testing of the polyfill. Now that we have exports from the UMD file, we can abandon this hack and simply access the exports to juggle between native and polyfilled versions of the implementation.
Hi @jayphelps, I've just pushed my proposed changes to this branch. Thank you for waiting. Everything was OK with the state of the PR as you've left it. The static UMD wrapper looked good. However, on our side, some new requirements to how we package our open source libraries have crept in. Specifically, we want our packages to support a I have made a call to have Because jshint was giving me trouble through this transition, I've switched to eslint & prettier like we also do in our other open source projects. This resulted in a lot of apparent code changes that are mostly just prettier adjusting whitespace. This makes the diff of this PR look huge, but I think the transition is worth it in the long run. The diff is best viewable while ignoring whitespace changes. I would appreciate a review. Thank you! 🙏 |
Good call! |
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.
This looks good to me :)
LGTM |
A somewhat related offshoot concern I still have is that the current proposed abort support in #592 won't polyfill The approach we took in yetch was that the default import/export of the project didn't polyfill anything so it could be used as a library without side effects. If they wanted to instead polyfill you use |
Ponyfills like yetch are always the preferred option
…On Tue, 22 May 2018, 23:18 Jay Phelps, ***@***.***> wrote:
A somewhat related offshoot concern I still have is that the current
proposed abort support in #592 <#592>
won't polyfill window.fetch if window.fetch is already assigned but
doesn't itself support aborting. My gut says that would be non-obvious to
people and potentially cause an issue where aborting works as expected in
one browser but doesn't in another. With this PR adding modules, is there
potential to reconsider #592 <#592
behavior or alternatively provide a way to opt-in to it always polyfilling
if something isn't 100% supported? e.g. it would override window.fetch if
any only if it doesn't support abort signals. If that as the default
behavior isn't acceptable, perhaps an opt-in module? import
'whatwg-fetch/polyfill-with-abort';
The approach we took in yetch was that the default import/export of the
project didn't polyfill anything so it could be used as a library without
side effects. If they wanted to instead polyfill you use import import
'yetch/polyfill';. My intent isn't to advertise yetch, just using it as a
reference.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#616 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AAZQTMGVBhIssTR0Q-aiiSPbPibbNItdks5t1I6lgaJpZM4TBFB4>
.
|
@jayphelps I do not disagree - but I think this is a good first step. Let's wait for this patch to land and then look into those subsequent issues. |
There's definitely value in the ponyfill approach, especially in the world where using modules is becoming more and more standard. However, you have to consider the existing I'm not ready to pull the plug on this behavior and make the polyfill an opt-in just yet, even through a major version bump, especially because this library didn't provide exports in the past. So, my plan is to first provide the exports (while keeping the implicit polyfill functionality), have our users get used to that, and possibly switch to the ponyfill approach months down the line through another major version bump. Thanks for your input! |
Github/fetch has merged PR for UMD distribution: JakeChampion/fetch#616 Also abort support has been added: JakeChampion/fetch#592 We can use Github fetch instead of yetch now. But AbortableFetch is still not supported, see comment in test.
Adapted from JakeChampion/fetch#616
I still need to adjust tests--still trying to figure out if I can change things while still keeping the existing makefile setup, is that the preference?Ready!This was requested before #592 can be merged.