You may have heard about Webpack, the new JavaScript Module Bundler that’s taken the webdev world by storm that all the cool kids are talking about. They just launched version 2, are being used by massive web companies like Twitter, Facebook, and Paypal, their monthly downloads are about to cross 4 million, and the Ruby On Rails core team has announced that Rails 5.1 will ship with webpack support built in. In other words, it’s the new hotness and has a ton of momentum behind it.
I just checked, and it should be easy enough to swap out in Phoenix according this post by Chris:
What do you think about it? Would you use it over brunch? Why?
However in my and others experiences, webpack has some major issues, but then again so does brunch. Currently I’m using just npm ‘scripts’, I have bucklescript compiling to a directory, rollup.js bundling and optimizing those output files and my other javascript all together into a set of a few files, and for production environments it also passes it all through an uglifier and google closure (probably redundant to have both, but eh), with significantly less code and significantly greater maintainability than even Brunch’s config (especially webpacks, yeesh…)
Actually I introduced it to the Bucklescript author and rollup.js is now not only added to the official tests of Bucklescript (that you can check there if you are curious) but he is also adding more optimizations that make it even easier for rollup.js to process here:
I’m planning more blog entries on how to use bucklescript including using it with rollup, so expect those as I get time.
When i started with Elixir/Phoenix i did not have idea what was Brunch or Webpack, but actually after 1 year i like Brunch, but i think Webpack community is very bigger than Brunch, and documentation for some features is hard to find.
I did not agreed of remove Brunch/Webpack from generator, but switch to Webpack i think is an good idea.
Over the weekend I finally got Webpack working with Phoenix with everything. Fonts/Images/CSS/SASS/NPM modules. Here is the config file: Webpack 2.0 Config File for Phoenix
Note: Currently there is a bug in file-loader@0.11, please use file-loader@0.10.1 instead or this config will not work.
The basic steps are: generate your project (with Brunch, because it creates the correct directory structure), delete brunch-config.js and remove brunch files from package.json. Install Webpack with npm, create a webpack.config.js file, add an npm script to run Webpack on build, and edit the watchers in config/dev.exs. Lastly, add the specific Webpack loaders to compile your assets (ES 2015, css, etc.).
I followed this post by Matthew Lehner to get up and running with Webpack and Phoenix. I also liked this free DailyDrip video. It covers Webpack, Phoenix, and Elm.
It’s just the Webpack file and the rest of the instructions (npm packages to install, change to config/dev.exs) are in the comments. Let me know if you have trouble with it and I’ll try and help.
Looking around online, I see a lot of articles encouraging code-splitting. But I don’t see counterarguments being considered.
I.e. What is the right size to start code-splitting, given the tradeoff between a slower initial load and then fast navigation versus much faster initial load but slower navigation?
I know for myself, I’d prefer the former unless it’s really long.
Also, if I code-split, navigate, and then hit the back button, the bundle for the page I navigated to is not lost and then reloaded when I navigate back, correct?
Code splitting helps You separate what is changing (Your apps) from what is not (Libraries). Eg. separate into bundle and vendor. But caching is not the ultimate way for optimization.
One cool feature of Webpack is lazy loading… For example if You use a router, each bundle will be loaded on demand. The trick is to transform call to import into a function call. This will result in bundle calling bundle0, bundle1 etc. Something like…