Skip to content
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

feat: refactored to support node/browser targets using babel #1423

Closed
wants to merge 6 commits into from
Closed

feat: refactored to support node/browser targets using babel #1423

wants to merge 6 commits into from

Conversation

niftylettuce
Copy link
Collaborator

this should resolve tons of issues, but namely these ones:

#1418, #1365, #1421, #1390, #1340, #1276, #1269

@niftylettuce
Copy link
Collaborator Author

@kornelski I also suggest that we use np and release to publish new versions moving forward

@niftylettuce
Copy link
Collaborator Author

There's an issue right now with eslint-config-xo-lass requiring Node v8.3+, I am fixing this, one moment.

niftylettuce added a commit to lassjs/eslint-config-xo-lass that referenced this pull request Oct 1, 2018
@niftylettuce
Copy link
Collaborator Author

As an aside, if you use np, be sure to use it with np --no-yarn since yarn publish does not have OTP (two-factor auth) support yet.

@niftylettuce
Copy link
Collaborator Author

Tests should pass now, I think.

@niftylettuce
Copy link
Collaborator Author

I doubt that we can use this package, but I made it anyways https://github.com/niftylettuce/spdy-or-http2. Not sure of the differences or if there'd be a conflict. The only reason why tests fail is because http2 is not available in Node v6.x. The package I linked spdy-or-http2 may fix this issue, but again, I'm uncertain. In the meanwhile I've removed Node v6.x from being supported.

@codecov-io
Copy link

codecov-io commented Oct 2, 2018

Codecov Report

❗ No coverage uploaded for pull request base (master@a0e6a75). Click here to learn what that means.
The diff coverage is 92.3%.

Impacted file tree graph

@@            Coverage Diff            @@
##             master    #1423   +/-   ##
=========================================
  Coverage          ?   94.64%           
=========================================
  Files             ?       10           
  Lines             ?      766           
  Branches          ?        0           
=========================================
  Hits              ?      725           
  Misses            ?       41           
  Partials          ?        0
Impacted Files Coverage Δ
src/node/parsers/text.js 100% <ø> (ø)
src/node/parsers/index.js 100% <ø> (ø)
src/node/parsers/image.js 100% <ø> (ø)
src/node/parsers/urlencoded.js 90% <ø> (ø)
src/node/unzip.js 100% <100%> (ø)
src/node/parsers/json.js 100% <100%> (ø)
src/node/response.js 95.45% <50%> (ø)
src/node/agent.js 90.47% <87.5%> (ø)
src/node/request.js 93.87% <91.66%> (ø)
src/node/http2wrapper.js 96.61% <96.66%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a0e6a75...bf8b9a2. Read the comment docs.

@niftylettuce
Copy link
Collaborator Author

✅ all tests are now passing btw

@niftylettuce
Copy link
Collaborator Author

I also had updated the docs too, you can view a preview at https://github.com/niftylettuce/superagent.

@kornelski
Copy link
Contributor

That's a huge PR with many unrelated changes. It'll take me a while to review it.

@niftylettuce
Copy link
Collaborator Author

Thanks @kornelski, if I can answer any questions please let me know. This is blocking for me right now and I published @ladjs/superagent@5.0.0 in the meanwhile (you could also use this package for testing or to see what the NPM node_modules folder would look like, e.g. mkdir test && npm init && npm install @ladjs/superagent && cd node_modules/@ladjs/superagent && ls).

I didn't change any core code other than running it through the linter / xo / prettier with --fix command. I also just tested it in the browser myself using the jsdelivr package and it works great https://jsfiddle.net/t82ysrh7/4/.

Currently there are a ton of code quality issues with the existing codebase, such as using != vs !== and not using Object.hasOwnProperty.call. I did not want to make these changes without first getting this PR resolved. If you look at my branch you'll notice an extensive list of eslint rules that I've turned off due so that the linting tests will pass for now. See https://github.com/niftylettuce/superagent/blob/master/package.json#L205-L250 for this list.

The reason the PR is so huge is because I used https://lass.js.org, and therefore moved everything from lib to src folder (src folder gets built to lib using Babel with .babelrc Node/Browser targets thanks to @babel/preset-env.

@larron
Copy link

larron commented Dec 7, 2018

@niftylettuce Thanks for this, I was also blocked on the same issue with having ES6 slip into a final build. Your package @ladjs/superagent works perfectly.

@kornelski
Copy link
Contributor

Hi!

Sorry, there are too many unrelated changes. I can't accept this PR. I don't have time to review 20000 lines of changes.

It doesn't matter that the changes are just from a code formatter, I can't accept code changes I haven't reviewed, and basically everything everywhere has changed.

I'm still keen to improve compilation for browsers, but just that. Please leave out changes of quoting style or moving commas and semicolons around.

@kornelski kornelski closed this Jan 2, 2019
@niftylettuce
Copy link
Collaborator Author

This will also fix #1437

I will release this soon, thank you folks for adding me as a contributor!

Copy link

@MatthewHerbst MatthewHerbst left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your work on this, especially making a browser compatible version.

Instead of switching to Yarn, please just start committing the package-lock.json from npm

["@babel/env", {
"targets": {
"node": "8.8.1",
"browsers": [ "> 1%", "last 2 versions", "ie >= 9" ]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: is there a way to use the .browserlistrc file here so we don't have duplicated config?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is not

package-lock.json

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: while not a change you made, package-lock.json should absolutely be being committed to the codebase.

@@ -5,3 +5,5 @@ examples
lib-cov
coverage.html
bower.json
.nyc_output
coverage

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I don't think this file isn't being used because we already specify the files directive in package.json.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had a scenario which happened multiple times (and I reported to NPM) where despite files and being in .gitignore, NPM tarball contained these folders, so it was just a safe guard.

@@ -0,0 +1,3 @@
CONTRIBUTING.md

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this repo use Remark?

Copy link
Collaborator Author

@niftylettuce niftylettuce Jan 11, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is now using it (via my PR), I have to update the contributors block still, it's pretty cool - gets read from contributors in package.json and automatically added to README.md in the # Contributors section.

@@ -58,9 +58,9 @@ exports.unzip = (req, res) => {
// override `on` to capture data listeners
const _on = res.on;
res.on = function(type, fn) {
if ('data' == type || 'end' == type) {
if (type == 'data' || type == 'end') {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: use ===

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left most of the core logic as-is, this was from prior code. If you see I turned a TON of ESLint rules off so the tests would pass... there were so many existing issues with the old codebase.

this.serverError = type == 5;
this.error = type == 4 || type == 5 ? this.toError() : false;

// sugar

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: use ===. use Number.parseInt(status, 10) if status is a string

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as #1423 (comment), what we have to do is fix this everywhere by turning off the rules one by one and fixing lint errors. I didn't want to do a daunting commit with tons of changes everywhere, my first step was getting browser support done.

"type": "git",
"url": "https://github.com/visionmedia/superagent"
},
"scripts": {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: these should be near the top of the file

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I use fixpack which organizes them differently, I think there might be a way to configure the order somehow with fixpack. Is there a better alternative than using fixpack?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like what I'm looking for should be the default behavior: From the fixpack docs:

It will re-write your package.json file as follows:

  • name first
  • description second
  • version third
  • author fourth
  • all other keys in alphabetical order
  • dependencies and devDependencies sorted alphabetically
  • newline at the end of the file

It seems you could create a .fixpackrc file to manage the configuration.

Personally I just manage package.json files manually (save for npm install runs)

"test-coverage": "npm run build && npm run lint && npm run nyc"
},
"unpkg": "dist/superagent.min.js",
"xo": {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like an ESLint config. Can we use a .eslintrc file instead please? This really clutters this file

return new exports.Request('GET', method).end(url);
}

// url first
if (1 == arguments.length) {
if (arguments.length == 1) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: use ===

try { return new ActiveXObject('Msxml2.XMLHTTP'); } catch(e) {}
if (
root.XMLHttpRequest &&
(!root.location || root.location.protocol != 'file:' || !root.ActiveXObject)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: use !==

@niftylettuce
Copy link
Collaborator Author

niftylettuce commented Jan 11, 2019 via email

@fmal
Copy link

fmal commented Mar 25, 2019

@niftylettuce what's the status of this? will 5.0 be released soon?

@niftylettuce
Copy link
Collaborator Author

Will try to get this released today.

@niftylettuce
Copy link
Collaborator Author

v5.0.0 was just released to NPM and an updated README is now available here on GitHub (see https://github.com/visionmedia/superagent#superagent).

What's new

  • Browser version is now only 19KB (that's 60% in file size savings from v4.x which was 48KB)!
  • Codebase refactored using xo, prettier, remark, and more
  • Browser compatibility linting via eslint-plugin-compat
  • Browserslist integration (current targets are > 1%, last 2 versions, ie 9)

How to install

npm install superagent@latest

Or if you're using yarn:

yarn add superagent@latest

Polyfills

Please see the Required Browser Features section in the README if you plan to support IE 9 and/or IE 10 and use superagent.

@niftylettuce
Copy link
Collaborator Author

btw @MatthewHerbst I fixed all those weird syntax and equality issues from the old version, e.g. == vs ===

@MatthewHerbst
Copy link

@niftylettuce thanks so much for your hard work on this! Excited to upgrade at my company today 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants