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

Webpack all the things #640

Closed
wants to merge 3 commits into from

Conversation

nornagon
Copy link
Contributor

Builds on top of #609. Drops uglify in favor of webpack. Uses code splitting and imports-loader to get everything to play nicely together.

@astorije
Copy link
Member

So, a word of caution: I believe some of the libs in client/js/libs have been modified in the repo (actually, I know some have diverged, but I don't see them in this diff). Did you check the potential differences when removing the files?

@astorije astorije added the Type: Feature Tickets that describe a desired feature or PRs that add them to the project. label Sep 27, 2016
@xPaw
Copy link
Member

xPaw commented Sep 27, 2016

@astorije the removed libs were not modified by us

@nornagon
Copy link
Contributor Author

$ git log --stat client/js/libs/handlebars.js client/js/libs/jquery.js client/js/libs/jquery/jquery-ui.js client/js/libs/jquery/mousetrap.js client/js/libs/notification.js client/js/libs/uri.js

^- only contains large deltas, which suggests that those libraries were only ever added or updated.

Copy link
Member

@astorije astorije left a comment

Choose a reason for hiding this comment

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

A few minor changes and some naive and stupid questions as I have never used webpack, but good stuff overall!

module.exports = {
entry: "./client/js/lounge.js",
resolve: {
extensions: ["", ".js"]
Copy link
Member

Choose a reason for hiding this comment

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

Same question than #640 for the record: why do we need "" here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So it allows importing without the ".js" extension in import.

Copy link
Member

Choose a reason for hiding this comment

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

Aaah, makes sense! Do we want to allow both with and without the extension? Or force one or the other format?

Copy link
Member

Choose a reason for hiding this comment

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

As per our discussion on IRC, could you remove that section altogether and rely on the defaults?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep, looks like that works fine!

@@ -7,6 +7,5 @@ coverage/
# See https://docs.npmjs.com/misc/scripts
client/fonts/
client/js/bundle.js
client/js/libs.min.js.map
Copy link
Member

Choose a reason for hiding this comment

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

No .map anymore? Is that on purpose?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This webpack config doesn't produce source maps, but no reason it couldn't--I'll update it to produce inline source maps (so no need for an extra file).

Copy link
Member

Choose a reason for hiding this comment

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

Do not inline the source map. Thats a lot of useless data to be included.

import "./roundBadgeNumber";
import "./tojson";
import "./tz";
import "./users";
Copy link
Member

Choose a reason for hiding this comment

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

I sure wish there was a way to do import "./*"; but apparently there is no such thing.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nope, quite intentionally :) (That would cause an import cycle, for one thing!)

Copy link
Member

Choose a reason for hiding this comment

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

Well, you could locate the loading file outside of the directory (Ruby-style). But yeah, whatever :-)

import "jquery-ui/ui/widgets/sortable";
import Mousetrap from "mousetrap";
import Handlebars from "handlebars/runtime";
import "notification-polyfill";
Copy link
Member

Choose a reason for hiding this comment

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

Is that polyfill still necessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

Ok maybe let's keep it for now then. Sigh...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Seems like IE still doesn't have notification support, so yeah.


import "./libs/jquery/inputhistory";
import "./libs/jquery/tabcomplete";
import "./libs/jquery/stickyscroll";
Copy link
Member

Choose a reason for hiding this comment

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

I believe all these libs by @erming are on npm, but also that we have monkey-patched these over time. A great contribution would be diff-ing ours with the upstream ones (because, yeah, they have evolved on the upstream project too...), open PRs on @erming's repos, and as they are being merged, move these to use the npm versions :-)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I intend to ultimately remove all of these libs as dependencies.

Copy link
Member

Choose a reason for hiding this comment

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

@astorije These "libraries" were so heavily modified that I would consider them part of Lounge's code and not vendor.

"mocha": "3.0.2",
"mousetrap": "1.6.0",
Copy link
Member

@astorije astorije Sep 30, 2016

Choose a reason for hiding this comment

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

Same comment, version in repo is 1.4.6.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch.

"npm-run-all": "3.1.0",
"stylelint": "7.3.1",
"uglify-js": "2.7.3",
"urijs": "1.18.1",
Copy link
Member

Choose a reason for hiding this comment

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

Version in repo is 1.14.1.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

NPM only has back to 1.16.1 of this lib, so I've rolled back to that.

@@ -1,5 +1,5 @@
var _ = require("lodash");
var package = require("../package.json");
var packageJson = require("../package.json");
Copy link
Member

Choose a reason for hiding this comment

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

#399 did something similar with server code, but went with pkg. I have no preference over which one, but it would be nice to use the same one :-)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will do.

@@ -64,11 +63,16 @@
"eslint": "3.6.0",
"font-awesome": "4.6.3",
"handlebars": "4.0.5",
"imports-loader": "0.6.5",
Copy link
Member

Choose a reason for hiding this comment

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

Not sure I'm getting this, why do we need it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

To make $ appear in libs without calling require.

Copy link
Member

Choose a reason for hiding this comment

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

Good point, didn't think of that. Clever way to do so too, cool!

Copy link
Member

Choose a reason for hiding this comment

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

I think we should explicitly require the needed stuff without automagic.

Copy link
Member

Choose a reason for hiding this comment

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

I don't think this is even possible or reasonable. Unless I am mistaken, I believe @nornagon is talking about real libs (like jquery-ui) and not our "libs" in client/js/libs.

Or maybe there is a different/better way to do so, I don't know. I think this is good enough for a first shot at it and we can improve later.

Copy link
Member

Choose a reason for hiding this comment

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

@nornagon I see you added imports for $ and others, is this still needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

imports-loader is still being used to provide the jQuery global variable to the jquery libs, which I haven't changed in this diff.

@@ -1,5 +1,17 @@
const webpack = require("webpack");
Copy link
Member

Choose a reason for hiding this comment

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

Interesting, why do you need to require webpack in your second commit? It seems like it was not needed for the first one.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because this one uses webpack in its body, to refer to webpack.optimize.CommonsChunkPlugin.

Copy link
Member

Choose a reason for hiding this comment

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

Duh yeah, I missed that (aka reviewing PRs at 1AM 😅).
Too bad we can't use import here though, the irony :-)

@astorije astorije self-assigned this Sep 30, 2016
@astorije
Copy link
Member

Let me know when you're done updating this :-)

@nornagon
Copy link
Contributor Author

Ready for another look!

Copy link
Member

@astorije astorije left a comment

Choose a reason for hiding this comment

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

1 minor comment and 1 very small change and then we should be ok :) (I'll test after that before approving this though).

"istanbul": "0.4.5",
"jquery": "2.1.1",
"jquery-ui": "1.12.1",
Copy link
Member

Choose a reason for hiding this comment

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

Did 1.12.1 work as expected? Did the version bump turn out to fix #494 by any miracle?

module.exports = {
entry: "./client/js/lounge.js",
resolve: {
extensions: ["", ".js"]
Copy link
Member

Choose a reason for hiding this comment

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

As per our discussion on IRC, could you remove that section altogether and rely on the defaults?

"build:handlebars": "handlebars client/views/ -e tpl -f client/js/lounge.templates.js",
"build:webpack": "webpack",
"watch": "webpack -w",
Copy link
Member

Choose a reason for hiding this comment

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

Can we also get a script to automatically start the server and webpack's watch within a single command for development purposes?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added.

@@ -1,5 +1,5 @@
var _ = require("lodash");
var package = require("../package.json");
var pkg = require("../package.json");
Copy link
Member

Choose a reason for hiding this comment

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

You touched client.js for no reason.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This isn't for no reason. package is a reserved word in ES6, and webpack was refusing to compile this file without this rename.

Copy link
Member

Choose a reason for hiding this comment

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

Also, if you add "use strict"; at the top (and feel free to do so on files you changed as it's a really good thing to have), implements, interface, let, package, private, protected, public, static, and yield become reserved keywords.

const webpack = require("webpack");

module.exports = {
entry: {
Copy link
Member

Choose a reason for hiding this comment

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

Just for future proofing, will it be easy to add our postcss stuff into this config, or it will have to be rewritten for an additional export or whatever?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep, should be easy to add. See https://github.com/postcss/postcss-loader.

Copy link
Member

@astorije astorije left a comment

Choose a reason for hiding this comment

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

Awesome job, @nornagon! Alright people, let's :shipit: ! And improve along the way when necessary.

@xPaw xPaw added this to the 2.1.0 milestone Oct 2, 2016
@@ -14,16 +14,16 @@
"scripts": {
"coverage": "istanbul cover node_modules/mocha/bin/_mocha -r test/fixtures/env.js test/**/*.js",
"start": "node index",
"start-dev": "npm run watch & npm run start",
Copy link
Member

Choose a reason for hiding this comment

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

This only runs the webpack watch and blocks, and then never runs the lounge itself.

Copy link
Contributor Author

@nornagon nornagon Oct 9, 2016

Choose a reason for hiding this comment

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

That's not how & works (perhaps you're thinking of &&?)

Copy link
Member

@xPaw xPaw left a comment

Choose a reason for hiding this comment

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

There's a couple of things that need to be addressed:

babel-core has to be added to package.json:

ERROR in Cannot find module 'babel-core'
npm WARN babel-loader@6.2.5 requires a peer of babel-core@^6.0.0 but none was installed.

Vendor file contains inline source maps,and the file is absolutely huge (554kb!!), generate an external .map.

Source map not being generated for bundle.js (make it external too)

inputhistory requires jQuery, but the dependancy is not resolved: Uncaught ReferenceError: jQuery is not defined

start-dev only runs webpack and doesn't start the server.

I think uglifyjs should be kept to generate a lot smaller files (especially vendor). bundle.js drops at least half in size with uglify plugin.

@xPaw
Copy link
Member

xPaw commented Oct 2, 2016

Also, this needs to be rebased on master due to merge of slideout menu (new libs/slideout.js file)

@astorije astorije mentioned this pull request Oct 2, 2016
@astorije
Copy link
Member

astorije commented Oct 6, 2016

@nornagon, any plans to address @xPaw's comments soon? I would love to see this PR merged :))

@nornagon
Copy link
Contributor Author

nornagon commented Oct 9, 2016

Added babel-core. Whoops.


Source maps inlined in vendor.bundle.js are there in the upstream code, it's not webpack that's adding them. The config in this diff wasn't generating source maps at all for our own code; I've tweaked it to generate them and put them in separate files. With this change:

$ du -sh client/js/*bundle*
112K    client/js/bundle.js
300K    client/js/bundle.js.map
560K    client/js/vendor.bundle.js
1.4M    client/js/vendor.bundle.js.map

I experimented with uglify and it did significantly drop the file sizes:

$ du -sh client/js/*bundle.js
 56K    client/js/bundle.js
180K    client/js/vendor.bundle.js

but at the cost of 2x build time:

$ time npm run build:uglify-webpack
[...]
npm run build:uglify-webpack  8.30s user 0.32s system 110% cpu 7.789 total
$ time npm run build:webpack
[...]
npm run build:webpack  4.05s user 0.26s system 114% cpu 3.771 total

so I don't think it makes sense to uglify during dev. Many projects using webpack have a separate config for prod vs. dev, and I'd be happy to work on that, but perhaps it could be in a followup PR?


I can't reproduce your issue with inputhistory. I tried building from a clean repo and I don't see any error. This is the code webpack is generating:

    /*** IMPORTS FROM imports-loader ***/
    var jQuery = __webpack_require__(1);

    /*!
     * inputhistory
     * https://github.com/erming/inputhistory
     * v0.3.1
     */
    (function ($) {
        $.inputhistory = {};
        /* ... */
    })(jQuery);

which seems fine to me. In addition, the functionality (pressing up to get to old messages) works just fine.


I also can't reproduce your issue with start-dev, it works fine for me:

$ npm run start-dev

> thelounge@2.0.0 start-dev /Users/nornagon/Source/lounge
> npm run watch & npm run start


> thelounge@2.0.0 watch /Users/nornagon/Source/lounge
> webpack -w


> thelounge@2.0.0 start /Users/nornagon/Source/lounge
> node index

2016-10-09 22:05:04 [INFO] The Lounge v2.0.0 is now running on http://*:9000/ in public mode
2016-10-09 22:05:04 [INFO] Press ctrl-c to stop

Hash: 2e8283f667df32ce2724
Version: webpack 1.13.2
Time: 3009ms
               Asset    Size  Chunks             Chunk Names
           bundle.js  112 kB       0  [emitted]  app
    vendor.bundle.js  573 kB       1  [emitted]  vendor
       bundle.js.map  228 kB       0  [emitted]  app
vendor.bundle.js.map  688 kB       1  [emitted]  vendor
   [0] multi vendor 88 bytes {1} [built]
    + 50 hidden modules

@nornagon
Copy link
Contributor Author

nornagon commented Oct 9, 2016

Also rebased, and used import instead of side-effecting onto window to import the slideoutMenu function.

@nornagon
Copy link
Contributor Author

nornagon commented Oct 9, 2016

Just realised the start-dev issue might be a windows thing, are you testing on windows @xPaw ?

@xPaw xPaw removed the Meta: Do Not Merge This PR should not be merged. label Oct 22, 2016
var keys = {
backspace: 8,
tab: 9,
up: 38,
down: 40
};

Copy link
Member

Choose a reason for hiding this comment

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

Let's not lint these, I still want them to come from upstream project (so, npm) after backporting our changes. I started on one, need to continue.

@astorije astorije force-pushed the webpack-all-the-things branch 2 times, most recently from f31a45b to 3b0d324 Compare October 23, 2016 05:31
);
module.exports = function(context) {
return window.JSON.stringify(context);
};
Copy link
Member

Choose a reason for hiding this comment

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

This file needs to be renamed to toJSON.js, otherwise the handlebars loader will be unable to resolve it on case-sensitive file systems.

@xPaw
Copy link
Member

xPaw commented Nov 20, 2016

@astorije I've reverted changes to erming's libs so they are untouched by this PR. Also squashed it.

@astorije
Copy link
Member

astorije commented Nov 20, 2016

Also squashed it.

Nooooo, I was still trying to test some things, which are now gone as you've reverted them as part of one of the commits. Can you revert this, please? (git reflog for the win)
EDIT: I checked and I have no kind of access whatsoever to the unsquashed version, so all of what I had been doing that you reverted has been lost. Please revert the squash.

@AlMcKinlay
Copy link
Member

This will need rebased again. What's the plan with this, it would be great to have this, then we can start splitting up our monolithic codebase.

@xPaw
Copy link
Member

xPaw commented Nov 24, 2016

@YaManicKill astorije wanted something in this PR before merge.

@astorije
Copy link
Member

@xPaw, reminder to un-squash this for now please? :)

@xPaw
Copy link
Member

xPaw commented Dec 9, 2016

@astorije sorry I am not able to un-squash this.

@AlMcKinlay
Copy link
Member

One of you should have it on your reflog, and if not, the server should hav eit on the reflog, no?

@astorije
Copy link
Member

Uuuuh so you mean what I spent 1-2 days working on is lost? Please no!
I don't have the repo under which I worked on anymore, I was not expecting it to be squashed.
@xPaw, as @YaManicKill said, can you browse your reflog?

@xPaw
Copy link
Member

xPaw commented Dec 10, 2016

@AlMcKinlay
Copy link
Member

@astorije Are the things you were testing related to this PR, or are we happy to go with this now? Would be good to get this merged asap, so that other people building PRs can build off this rather than the old code.

@AlMcKinlay
Copy link
Member

@nornagon You've still got some lint errors. Could you fix these and rebase? Would be great to get this into a release soon.

@PolarizedIons
Copy link
Contributor

PolarizedIons commented Dec 13, 2016

Trying to run this PR (via ./scripts/run_pr.sh 640, though I did do npm install and npm run build manually just to be sure) and get this in the console when trying to load thelounge:

The page is also stuck on "Loading the app…"

@xPaw xPaw removed the priority label Dec 16, 2016
@xPaw
Copy link
Member

xPaw commented Dec 18, 2016

I've pushed a fixed branch under our repo: https://github.com/thelounge/lounge/tree/pr/640

It's a lot easier to deal with the branch within the repo rather than dealing with the fork.

@xPaw xPaw mentioned this pull request Dec 18, 2016
@xPaw
Copy link
Member

xPaw commented Dec 18, 2016

Closed in favour of fixed #817.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Feature Tickets that describe a desired feature or PRs that add them to the project.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants