Over the past few weeks I've been working on some major improvements to Flarum's JavaScript tooling and asset pipeline. ??
Webpack
Previously we used a custom Gulp workflow to compile our ES6 source code into something that browsers could understand. This setup worked, but it was pretty unconventional. It also limited our ability to explicitly define a public API for extensions, which would've made backwards compatibility harder going forward.
Now I have almost finished switching us over to Webpack, which is a very popular tool for this kind of thing among the JavaScript community. Adopting common technologies like this is a often good thing because they are tried and tested and a lot of people are familiar with them!
The new Webpack setup is gold-standard. As opposed to converting all of the source code into System.js modules (all accessible to extensions), only the modules we export directly from index.js
will be made public. This means we will be able to explicitly define our new public JavaScript API (one of the things I'll be working on next). In the meantime, backwards compatibility with the old style of API consumption remains. As such, beta 8 will require some minor re-tooling of extension JavaScript code, but no massive changes to the code itself.
Folder Structure
This change has also involved making some changes to the JS folder structure. Now there will only be one npm instance in the package root, and you will only have to run npm run build
to compile both forum and admin JavaScript files. I am still finalising these changes and am aiming to push them soon.
Travis CI
One of the things we've been meaning to do for a while is to set up a bot that runs on Travis CI to automatically re-compile the JavaScript files whenever new code is pushed to master
. This will help us to avoid conflicts when developers run slightly different versions of the JavaScript tooling with slightly different outputs. It will also result in a safer codebase because people will not have the opportunity to hide malicious code in their compiled JS when submitting a pull request.
Asset Manager
Finally, I have been working on some improvements to the asset pipeline, where Flarum collates the JavaScript files from core + extensions and serves them up to the browser. Previously we applied minification to the compiled JavaScript using PHP, but this was slow and made enabling/disabling extensions take a long time. Now, Webpack does this job for us and we will commit the minified output to the GitHub repo so it's ready to be used by Flarum installations straight away!
I have fixed numerous bugs along the way, eliminating some common problems like Flarum showing translation keys instead of the actual English translations, and CSS not recompiling during development when you make some changes to a LESS file.
Additionally, Webpack produces source maps when it compiles the JavaScript. I have added functionality to Flarum's asset manager so it will serve these up as well (if debug mode is on) so that developers can see JavaScript errors in the context of the original, unminified, uncompiled source code. The LESS parser we're using also supports source maps so I was able to enable it for that as well. Very cool!
Up Next
- Finalize the new JS folder structure and apply it to all extensions
- Merge the flarum/core
webpack
branch into master
- Set up Travis CI JavaScript compilation for all extensions
- Clean up asset manager changes and merge into master
- Begin work on the foundation of the new JavaScript Extension API