User:JHernandez (WMF)/Draft: Invest in the MobileFrontend & MinervaNeue frontend architecture

= Invest in the MobileFrontend &amp; MinervaNeue frontend architecture =

Summary
The mobile website (specifically MobileFrontend and MinervaNeue codebases) are technologically outdated. This impedes development of new features and maintenance of existing ones. We propose a solution to, and have provided proof of concepts for, iteratively refactoring and revising the existing code in order to achieve the following outputs:


 * Enable compressed asset bundling.
 * Speed up unit tests and increase code coverage.
 * Complete the MinervaNeue migration.
 * Review and refactor components, separation of presentation and logic.

In addition to solving the stated problems, we expect the completion of these focused tasks to yield the following outcomes:


 * UI feature work is more independent of MediaWiki implementation decisions.
 * Greater capacity, and throughput.
 * Increased interest, engagement, and collaboration of internal and external contributors.
 * Decreased developer onboarding time and enhanced understanding of code.
 * Improved data savings and performance.
 * Code sharing (pooled features and talent, reduced bugs and maintenance, gained stability).
 * Significantly faster, better automated testing and fewer regressions.
 * Fewer dependencies on WMF maintained third-party code.

We believe that this initiative is the responsible choice for building on existing work and a stepping stone to future platform evolution.

Background
Working on frontend (UI features) is challenging and painful in a variety of ways given the existing frontend infrastructure and the state of our mobile web frontend code bases.

With the upcoming API focus from the Platform Evolution Program, the rapid innovation by browser vendors (e.g. Service Workers) and the learnings from Extension:Popups (Page previews) and the progressive web app prototypes, we believe we should invest more in our mobile web platform if we want to make future changes to mobile web, adapt to upcoming changes to MediaWiki Core, ease future maintenance, and have a good base for future feature work, whether based on new contributor or reader features.

This project is planned and budgeted in the annual plan, as technical work under the CDP Output 2.7. This document specifies and explains the work and its value, and sets measurable goals and to keep track of progress on the outputs. The project will be worked on by the Readers Web team, where ~2 engineers should focus on this project for continuous amounts of time, leaving other FTEs to work on other goals.

The project is broken up in sub-projects (outputs), that are valuable in isolation, and are part of a step by step plan towards the big end goal. Each sub-project will be tackled sequentially &amp; iteratively and on the production codebases (live websites), to ensure compatibility with the product, avoid divergence and minimise merge conflicts. We will not use big feature branches or require big unveil refactors.

The document follows the annual plan terminology, discussing first Outcomes that we want to accomplish, and then outlining the Outputs (sub-projects) that will be tackled to accomplish those outcomes.

Overview of outcomes
Given our previous work  and experience as a team, and the evolution of the industry and global FLOSS community tooling and ecosystem, we believe we can improve the state of the mobile web UI code bases, with the following outcomes:


 * 1) Improve developer experience (onboarding, maintenance, development)
 * 2) Reduce technical debt and make production improvements
 * 3) Separation of presentation and logic
 * 4) Make Minerva and MobileFrontend better citizens of the MediaWiki ecosystem[a][b]

See the Outcomes section for more detail.

Smoother engineer onboarding and contributions from the current and next generation of technical volunteers and employees.
Our code will meet expectations of newer developers (hires/volunteers) and be more pleasurable to work with by the team as a whole. This is hard to measure but intuitively valuable and likely to increase throughput of changes.

Right now we use bespoke code and don’t use modern tooling. This confuses and slows down the onboarding process for many of our devs.

Measuring success
As mentioned, onboarding is hard to measure. Instead, we will measure some proxies of outcomes that we think would help onboarding:


 * Having better docs and easier install/run steps
 * Lines of configuration for JavaScript file order removed

Data savings and possible performance improvements from reducing the size of our frontend assets (JavaScript and templates mainly).
We will improve bytes shipped with minimal effort by relying on tooling.

By using better tooling we can automate many performance improvements that right now are very difficult e.g. tree shaking/better minification, using templates without a runtime payload/compute cost.

Measuring success
We will monitor the JavaScript bundle size closely and be able to report the change.

Decrease the complexity for new features to be developed and reusing existing components by making our code saner to work with
When the mobile site was built, its component system used an inheritance model with templates and template partials. Projects like React have shown us that the alternative of declarative rendering and a component based composition can make development faster, and libraries such as Hogan are no longer maintained, suggesting their days are over.

Today, we have overlays for all our components - TalkOverlay, EditorOverlay and ImageOverlay. We then have base components such as TalkOverlayBase, EditorOverlayBase and Overlay. As a result it’s very difficult to follow our code and understand how each class operates without looking at ancestor classes. This makes testing and new component creation harder as many of our classes are hard wired for specific but similar workflows. Creating a new overlay usually requires creating templates and JavaScript classes and lots of additional wiring. We envision a future where if a designer wants to create a new Overlay, a developer will find this incredibly easy - e.g. 5 lines of code rather than several files.

Measuring success

 * We will be able to tie the MobileFrontend code to the style guide and identify missing components.
 * We will remove most inheritance chains in classes in favor of composition.

Increase the quality of our code, reducing the numbers of bugs that make it to production
Much of our JavaScript has no tests due to the way it is architected and the way in which it has been built (several designers/deadline driven work). A re-org/refactor of our code provides the opportunity to write unit tests as we go. This will help us understand our code; identify edge cases and identify areas we can simplify.

Many features built by volunteers are not production ready (e.g., Categories) - we will improve/simplify/remove redundant code in the process.

Measuring success
We will provide baselines of test code coverage before and after the refactoring.

Enable reuse of the mobile web UIs and workflows outside MediaWiki (apps, existing or new products, other projects in the ecosystem, etc).
Right now, our mobile site is too tightly coupled to MediaWiki JavaScript, the Hogan templating library, jQuery and other large libraries of code. We believe by outlining and abstracting where our dependencies are, we will be capable of breaking free of these dependencies in the future with minimal effort (in case we have to respond to unexpected changes in our ecosystem). This also opens up opportunities to share code with Android and iOS (e.g., the Page Library).

Measuring success
By looking into the % of code (LOC based) that has been migrated to a library and that can be run without MediaWiki.

4. Make Minerva and MobileFrontend better citizens of the MediaWiki ecosystem
This project will make developers’ lives easier; increase awareness of cross-skin/mobile testing by other teams and reduce grumbles about the structure and function of MobileFrontend and Minerva in the MediaWiki ecosystem.

Since we split MobileFrontend from Minerva we have seen increased attention and efforts in testing. For instance, some Foundation teams frequently test against the Minerva skin as part of their QA process now (whereas they didn’t before), and there has been renewed interest in Minerva/MobileFrontend. This move helped people conceptualise the different purposes of these 2 repositories as well as guiding engineers on where code should live. That said, there is still a little confusion with the setup:


 * On the code level with regards to where code should go - where code lives, code review and feature development is a little rougher than it needs to be.
 * Editors/third parties have expressed an interest in using Minerva as their mobile AND desktop skin, which is currently a buggy experience. This work will fix those bugs and make this a viable alternative alongside existing Vector and other skins and improve the experience of the Minerva skin.

Right now, MobileFrontend and Minerva are coupled due to their JavaScript and other UI assets. By turning the JavaScript in MobileFrontend into a library inside Minerva, we will allow people to run MobileFrontend without Minerva with an additional build step (we envision MobileFrontend having an optional install step for people who want to do this). Currently, it is quite painful for our own developers to build features as they have to consider 2 repositories and write patches that depend on each other inside MobileFrontend and Minerva. Having the UI code in Minerva will take much of this pain away and for development of the mobile site within WMF we will only need to work inside one repository. Third parties who want different setups will be able to have them by following specified build steps (e.g. MobileFrontend wikis without Minerva).

Measuring success
It will be possible to install Minerva without MobileFrontend (and vice versa). This will be well documented and the developer community will be notified.

Automate asset bundling
The mobile web projects have very extensive manual configuration for the frontend assets and specifying their dependencies. Just the configuration for MobileFrontend has +1000 lines of code and Minerva has ~500 lines of code.

This current state has a negative  impact on development practices, ResourceLoader performance, on general development, and our ability to change, move and split the code.

In support of

 * Outputs:


 * Speed up unit test execution and increase code coverage
 * Review and refactor components


 * Outcomes:


 * 1. Improve developer experience (onboarding, maintenance, development)
 * 3. Separation of presentation and logic

Proposal
Use a Node.js based bundler (webpack) to compile the JavaScript files into different entry points automatically based only on source code import statements. These bundled files will then be given to ResourceLoader for serving.

This approach is the same one taken in Extension:Popups (Page Previews), which has proven to be an effective strategy.

We will also use UglifyJS for minifying our JavaScript files for production, which should give us approximately 25% savings on file sizes for the mobile website.

Proof of concept
https://gerrit.wikimedia.org/r/#/q/topic:dreamofwebpackification+(status:open+OR+status:merged)

Measuring progress
There are ~80 JavaScript files in MobileFrontend, in 58 modules.

We will go module by module, file by file, migrating the file to the src/ folder like in Extension:Popups, adapting the imports/exports, and moving the QUnit test files to node-qunit ([1],[2]) if possible/easy.

Progress can be measured by looking at the amount of files and lines of code migrated to the src/ folder.

Speed up unit test execution and increase code coverage
Tests are a crucial part in the development flow to verify that the code works as intended and is future-proof against changes of behavior. With the current stack tests take a long time to run (because of the ResourceLoader strain and the overhead of running through MediaWiki). As a result of the cost of running tests and the slow feedback cycle during the development flow, we haven’t kept up with properly testing the code, and this has negative effects in feature stability and resilience and confidence to change the code.

In support of

 * Outputs:


 * Review and refactor components


 * Outcomes:


 * 1. Improve developer experience (onboarding, maintenance, development)
 * 2. Reduce technical debt and make production improvements
 * 3. Separation of presentation and logic

Proposal
Leveraging the Automatic bundling goal, we will follow the Extension:Popups approach and migrate as many unit tests as we easily can to run in the CLI (Node.js based). Tests that make extensive use of MediaWiki or are too tied to ResourceLoader will be considered integration tests and kept in mediawiki-qunit.

Untested files simple enough to test will have new tests added in the process of migration for Automatic bundling.

Proof of concept
https://gerrit.wikimedia.org/r/#/q/topic:dreamofwebpackification+(status:open+OR+status:merged)

Example
https://gerrit.wikimedia.org/r/#/c/406783/

Measuring progress
Progress can be measured by looking at the amount of files migrated to the src/ folder, like in the previous sub-project (automatic bundling), and the amount of test files moved or left from tests/qunit to tests/node-qunit.

Currently, we have low test coverage and are unable to accurately measure test coverage. Test coverage will be measured using istanbul as files are migrated to node-qunit and we’d aim for ~75% coverage.

In support of

 * Outcomes:


 * 3. Separation of presentation and logic
 * 4. Make Minerva and MobileFrontend better citizens of the MediaWiki ecosystem

Proposal
3rd party support is important to the MediaWiki community and causes the team extra maintenance effort (dealing with talk page questions and bug reports). Currently there is an implicit relationship where Minerva requires installing and configuring MobileFrontend, and MobileFrontend requires Minerva. As such, using only MobileFrontend (with Vector for example) or using only the skin Minerva (on desktop) is complicated and causes many issues to MediaWiki administrators.

We will make Minerva an independent skin, that can be easily be used independently like other skins. We will continue to support mobile friendly special pages inside MobileFrontend making it clearer of the purpose of MobileFrontend and function it serves (which currently causes a lot of confusion).

We will make MobileFrontend use a UI library for the mobile friendly Special pages provided. Minerva will also use the same mobile friendly UI library (which could be used by any skins or other projects in the future).

To do so, we will migrate all UI components and assets into a library, which will be stored in the Minerva skin code repository for development convenience, but also published as an independent library.

Going forward, any engineer working on mobile web - Minerva or related features - will work in the Minerva skin repo. MobileFrontend will then effectively live in maintenance mode.

For WMF products, MobileFrontend will use the installed skin Minerva to get the UI library needed.

For 3rd parties who want to use MobileFrontend with another skin that’s not Minerva we will provide an additional install step for them to do so. In exchange for this slight discomfort they will have more control of their user experience. Websites not affiliated with the WMF (e.g. wikitravel) will have more reason not to inherit Wikipedia’s “brand”.

We believe this will reduce the amount of the work the team has to do in the arena of third party support (we get plenty of bug reports), freeing us to focus on products.

Implementation details may change, depending on activity in T107561 (how will MobileFrontend use Minerva’s components/vice versa).

We’ll email wikitech-l and mobile-l to let MediaWiki community members know about this change, before start as a consultation, sending updates via email with the monthly readers email and after finishing presenting the results.

Proof of concept
N/A

Measuring progress
We will monitor the resources/assets in MobileFrontend and how they move to the MinervaNeue repository, (except the special pages sources or other files MobileFrontend specific ones).

In support of

 * Outcomes:


 * 1. Improve developer experience (onboarding, maintenance, development)
 * 2. Reduce technical debt and make production improvements
 * 3. Separation of presentation and logic

Proposal
We will need to re-evaluate what we need/want from this at this point. When the previous projects have been successful and if time allows we will come up with a concrete proposal for next steps. Some goals for this would be:


 * Separation of concerns, separating UI and network and business logic
 * Making our code testable
 * Using composition instead of inheritance in the UI components
 * Declarative UI rendering

Proof of concept
N/A

Measuring progress
We will define this subproject further as we get closer. It is left open ended right now to allow us for the opportunity to choose whatever suits best the foundation’s long term objectives and the team’s workload during the FY. We believe this would help pay down design debt in the long term, and reduce technical debt, and simplify the UI code to ease onboarding and development of new features, amongst other things.