Skip to content
This repository has been archived by the owner on Apr 14, 2023. It is now read-only.

Module browser bundle configuration using webpack #48

Closed
wants to merge 21 commits into from
Closed

Module browser bundle configuration using webpack #48

wants to merge 21 commits into from

Conversation

schrepfler
Copy link
Contributor

@schrepfler schrepfler commented Nov 15, 2016

Only thing I can see is that the bundle is being created but I don't have the skills to really test this (sorry, Java/Scala guy here).
This feature should enable simple CDN-like referencing via https://unpkg.com/ while keeping the publishing to npm as a basis. Related to #47

TODO:

  • Make sure all of the significant new logic is covered by tests
  • Rebase your changes on master so that they can be merged easily
  • Make sure all tests and linter rules pass
  • Update CHANGELOG.md with your change

@apollo-cla
Copy link

@schrepfler: Thank you for submitting a pull request! Before we can merge it, you'll need to sign the Meteor Contributor Agreement here: https://contribute.meteor.com/

@schrepfler
Copy link
Contributor Author

To trigger the build one just launches webpack after which the browser bundle is created. This command can probably be added in package.json, I guess it depends on your build pipeline. Not sure if I need to add something specific so that npm publish publishes the artifact to npm as well?

@schrepfler
Copy link
Contributor Author

At the moment I'm not sure if the generated bundle is ideal for consumption from </script> tags. Would be nice if a webpack expert suggests what's missing and also confirm will npm publish publish the browser bundle automatically or not?

@schrepfler
Copy link
Contributor Author

I think at the moment the bundle is not properly created, don't merge just yet (and if anyone has suggestions feel free to comment on the PR.)

@schrepfler
Copy link
Contributor Author

I think with this final commit, the generated bundle is usable in a browser via a script tag

var client = new subscriptions_transport_ws.Client('ws://localhost:8080/graphql', {timeout: 4000})

and

npm pack 

generates a tgz which contains it so I suppose this is more/less what we want?

@schrepfler
Copy link
Contributor Author

The only thing which actually comes to mind, if subscriptions_transport_ws imports things from graphql-subscriptions and they finish in the bundle, it might be possible to say that these need to be provided as linked scripts from the outside (declare them external in webpack) but at this point I think it's better for you guys to evaluate.

@schrepfler
Copy link
Contributor Author

Any feedback on this would be nice?

@helfer
Copy link
Contributor

helfer commented Dec 13, 2016

@schrepfler sorry, this fell off of my radar for a while. I think we should definitely implement it, but it looks like you're compiling things twice now (once with TS once with webpack). Couldn't webpack just use the output from TS and bundle for the browser after that?

@rricard I think you know this library (and also js in general 😄 ) pretty well. Could you take a quick look at this PR and help @schrepfler where necessary? That would be awesome!

@rricard
Copy link
Contributor

rricard commented Dec 13, 2016

@helfer thanks for directing me to this issue.

@schrepfler I'm going to do a proper review of your PR.

Copy link
Contributor

@rricard rricard left a comment

Choose a reason for hiding this comment

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

What you did works 🎉 (by eyeballing it, we'll need to actually publish it to be sure it really works... Like you I'm concerned about the peer dependency on graphql-subscriptions) so it's pretty cool, although, I have to nitpick on some stuff! Fix that and I'll be able to merge you!

package.json Outdated
@@ -24,7 +26,8 @@
"testonly": "mocha --reporter spec --full-trace ./dist/test/tests.js",
"coverage": "node ./node_modules/istanbul/lib/cli.js cover _mocha -- --full-trace ./dist/test/tests.js",
"postcoverage": "remap-istanbul --input coverage/coverage.raw.json --type lcovonly --output coverage/lcov.info",
"prepublish": "npm run compile"
"start": "webpack --config webpack.config.js",
Copy link
Contributor

Choose a reason for hiding this comment

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

Don't call this script start, something more meaningful: browser-compile

Copy link
Contributor

Choose a reason for hiding this comment

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

webpack.config.js will be called by default so just put "webpack" in the script invocation

package.json Outdated
@@ -24,7 +26,8 @@
"testonly": "mocha --reporter spec --full-trace ./dist/test/tests.js",
"coverage": "node ./node_modules/istanbul/lib/cli.js cover _mocha -- --full-trace ./dist/test/tests.js",
"postcoverage": "remap-istanbul --input coverage/coverage.raw.json --type lcovonly --output coverage/lcov.info",
"prepublish": "npm run compile"
"start": "webpack --config webpack.config.js",
"prepublish": "npm run compile && npm run start"
Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of start, put browser-compile

output: {
path: __dirname + '/browser',
filename: 'bundle.js',
library: 'subscriptions_transport_ws'
Copy link
Contributor

Choose a reason for hiding this comment

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

Usually exported libs are in pascal case: 'SubscriptionsTransportWs', please do that!

@@ -0,0 +1,18 @@
module.exports = {
context: __dirname + "/src",
Copy link
Contributor

Choose a reason for hiding this comment

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

Use path.join instead of a concatenation

context: __dirname + "/src",
entry: './index.ts',
output: {
path: __dirname + '/browser',
Copy link
Contributor

Choose a reason for hiding this comment

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

Use path.join instead of a concatenation

library: 'subscriptions_transport_ws'
},
resolve: {
extensions: ['', '.webpack.js', '.web.js', '.ts', '.js']
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not just ['.ts', '.js', '.json'] ?

@@ -2,7 +2,7 @@

### vNEXT

- ...
- Webpack configuration to export the generated code into a browser compatible bundle.
Copy link
Contributor

Choose a reason for hiding this comment

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

Nitpick: reference this PR number in the changelog

@rricard
Copy link
Contributor

rricard commented Dec 13, 2016

@helfer I'm concerned about the dependency on graphql-subscriptions wouldn't it be better to put it as a peer dependency? Normally, the browser bundle shouldn't touch it but even with that, having it as a dependency in node is not ideal... And having it as a peer would let us shim it in the webpack config.

@rricard
Copy link
Contributor

rricard commented Dec 13, 2016

@schrepfler On our side we should only expose the client module, so your entry point should only be: src/client.ts, that way we avoid the hassle with graphql-subscriptions

@helfer
Copy link
Contributor

helfer commented Dec 13, 2016

@rricard thanks a lot for tuning in here! ❤️ We can definitely (and probably should) make graphql-subscriptions a peer dependency.

package.json Outdated
@@ -10,8 +10,10 @@
"dependencies": {
"backo2": "^1.0.2",
"graphql-subscriptions": "^0.2.0",
Copy link
Contributor

Choose a reason for hiding this comment

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

Ok, so while we're here, can you move this one in devDependencies and peerDependencies.

@schrepfler
Copy link
Contributor Author

schrepfler commented Dec 24, 2016

Went over the suggestions, only problem was with ['.ts', '.js', '.json'] I had to add '' to the list.

@schrepfler
Copy link
Contributor Author

Ah, I also bumped a bit the libraries, but notably typed-graphql and graphql to get in line with the rest of the apollo stack. Anyhow, merry xMas!

@schrepfler
Copy link
Contributor Author

Would the addition of graphql-subscriptions to peerDependencies mean that we would also need to create a browser bundle for it as well (and the other deps?)

@Akryum
Copy link

Akryum commented Dec 29, 2016

@rricard If a module is never imported in the code, webpack will not include it in the bundle. Declaring it as a peer dependency has no effect regarding webpack, but the externals config does (although not needed here, since graphql-subscriptions is never imported in the client code).

The only difference is that users of this package will have to manually install graphql-subscriptions if they want to use the server part of the package.

Copy link
Contributor

@rricard rricard left a comment

Choose a reason for hiding this comment

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

Seems better, I still need to test it before shipping it though...

@schrepfler
Copy link
Contributor Author

@rricard can you please review the reversal of the change and comment on the runtime vs devDependencies wrt json and ts-loaders?

Copy link
Contributor

@rricard rricard left a comment

Choose a reason for hiding this comment

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

Seems good to me, I just need to check a few things first but once it's done, let's ship it!

@schrepfler
Copy link
Contributor Author

Shall we try to ship it and then try to catch up with the rest of the apollo js stack? 😀🚀🛫🍻

@Urigo
Copy link
Contributor

Urigo commented Feb 2, 2017

Maybe we can use the changes from this PR: #53 ? the package now compiles and create two entry points (one for client.ts and one for server.ts), and the client file does not have an external dependencies.
@rricard , maybe we can use this to avoid two compilations?

@schrepfler
Copy link
Contributor Author

Hi @Urigo, not sure if I understand, are you saying #53 can deprecate this PR?
The rationale of #48 is more to create a browser bundle which can be directly used via unpkg.com once the library gets published onto npm.

@Urigo
Copy link
Contributor

Urigo commented Feb 2, 2017

@schrepfler I meant that instead of creating a new Webpack config file and compile the package for browser, we might be able to do the same bundle process from the compiled files, since they're no longer coupled.
Also, can you please rebase this PR? thanks.

@schrepfler
Copy link
Contributor Author

Not sure what's going on, the rebase went into a loop, so tried to do the merge manually.

@schrepfler
Copy link
Contributor Author

Somehow the new versions popped back into package.json. Hopefully it's good now.

@Urigo
Copy link
Contributor

Urigo commented Feb 3, 2017

@schrepfler , thanks for the rebase. What about using the compiled client file, and then just bundling it?

@schrepfler
Copy link
Contributor Author

I think this is the case, the only thing webpack is bundling is the client.ts? https://github.com/apollographql/subscriptions-transport-ws/pull/48/files#diff-11e9f7f953edc64ba14b0cc350ae7b9d

@schrepfler
Copy link
Contributor Author

schrepfler commented Feb 3, 2017

Also, please consider this has been submitted on the 15th November 2016 and JS is not my main skill, can we wrap it up? I don't mind if you use mine bundle or if you have your bundle.
All it has to provide is:

  • the package.json browser element pointing to the right bundle and
  • that bundle needs to be consumable directly in a browser context (so I guess no requires.) This bundle needs to target the browser, not node.
  • bonus: also export the helper fn? (I didn't do anything around that, personally I'd move it into the client bundle)

@Urigo
Copy link
Contributor

Urigo commented Feb 5, 2017

@schrepfler sorry it took a long time, the other PR was pretty big and I was working on it while implementing subscriptions on one of our internal apps.
So about your last comment, the first and last points should be done with the new version.
So now what's left is just to expose the compiled client code without require.
You can update this PR or create another one

@schrepfler
Copy link
Contributor Author

I think this is the case, I wrap client.ts which is then exposed as SubscriptionsTransportWs in the global scope if added to the page. I think

@schrepfler
Copy link
Contributor Author

I just realised you're the same guy from the JSJabber podcast episode I just listened, nice one! :D

@schrepfler
Copy link
Contributor Author

Any feedback in order to understand further course of actions?

@schrepfler
Copy link
Contributor Author

I'll close this PR and start over.

@schrepfler schrepfler closed this Feb 19, 2017
@Urigo
Copy link
Contributor

Urigo commented Feb 21, 2017

thanks again @schrepfler , linking the merged one if someone will stumble upon this one - #77

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

Successfully merging this pull request may close these issues.

6 participants