User:Jdlrobson/Developing with Webpack and ResourceLoader

This is a high-level (in progress) walkthrough of how front-end developers can build software in MediaWiki using Webpack. Webpack is used alongside ResourceLoader in Extension:MobileFrontend and Extension:Popups

For more about how ResourceLoader is designed, its features and why it works the way it does, see ResourceLoader/Features.

For complicated codebases, using Webpack inside MediaWiki can help with a variety of things:
 * Ability to use npm packages
 * Reduces the amount of edits needed to extension/skin.json
 * Allows you to rename files freely and cheaply (many IDE's these days including Visual Code Studio can update import paths for you when you rename file but they cannot automatically update extension.json)
 * Unit test discovery (no need to update a hook every time you add a unit test)
 * Manages dependency trees for you
 * Quicker unit tests
 * Ability to get JavaScript code coverage reports from the command line
 * Less excuses for not writing unit tests - no need for exposing public interfaces and complaining about speed
 * Ability to use ES6 JavaScript
 * Ability to use JavaScript map files.

There is currently one minor trade off we'll hoping to resolve:
 * Built assets have to be committed on every commit (see T199004.)

Porting from an existing RL module to Webpack
Imagine an extremely simplistic extension with 3 files.

In ResourceLoader this might look like this:

If foo, bar and myrlmodule grow in size, this pattern might not be ideal and Webpack may provide a solution for you. Imagine if those files were rewritten as source (src) files.

The problem now becomes, how to compile that code into one single file that can be shipped by ResourceLoader. For this you can use Webpack!

First you'll need to configure Webpack in the extension repo. How to configure Webpack is out of scope for this task, but MobileFrontend and Popup webpack configurations will provide inspiration. The following snippet provides the basics:

After setting up webpack you'll need to define your ResourceLoader module to ship the file built by Webpack. In the example above, mrlmodule will be outputted to distDir (resources/dist/) with the name of the entry point passed through output.filename (myrlmodule.js).

Shipping this in MediaWiki becomes a case of shipping the dist file. Currently this dist file needs to be committed to the code repository (see T199004).

Writing unit tests and getting code coverage reports
Now you have require and module.exports writing unit tests and getting code coverage reports becomes much easier.

Updating your package.json will provide you 2 command line tools that can be run to run unit tests and get code coverage reports. Given your src files export modules and require modules, your tests can simply reference them like so without much modifications to any existing tests. The only problem remains providing the minimal environment (Sinon, QUnit and mediaWiki) for these tests to run. The Reading Web team are working on (and using!) https://github.com/wikimedia/mw-node-qunit to successfully provide this functionality. See also: RFC: Let's stop using QUnit as a mechanism for integration tests for a proposal to make this the standard for all MediaWiki extensions.