Vue.js/Guidelines

The following guidelines apply to usage of Vue.js inside of MediaWiki

Get Vue.js from ResourceLoaderEdit

Developers should rely on the version of Vue that is already provided via ResourceLoader rather than bundling or vendoring their own copy of the library. Currently MW ships Vue 3 with the Compatibility Build included.

Developers of skins or extensions which list "vue" as a dependency in extension.json or skin.json can require the library as they would any other RL module.

const Vue = require( 'vue' );

Developers of gadgets, or of any feature where Vue is to be loaded conditionally, can load the Vue module on the client-side:

mw.loader.using( [ 'vue' ] ).then( function ( require ) {
    const Vue = require( 'vue' );
} );

Use Single-file componentsEdit

TODO: ensure this list is up to date

MediaWiki supports Vue single-file components (aka .vue files) within any ResourceLoader module that is using the packageFiles feature. However, MediaWiki's support for single-file components is incomplete, and has the following limitations:

  • <script setup> is not supported
  • Advanced style features, such as <style scoped>, <style module>, and v-bind in styles are not supported
  • Src Imports are not supported (but support for this feature could potentially be added, if developers find it useful)
  • Pre-processors, such as <template lang="pug"> or <script lang="ts"> (TypeScript) are not supported. For styles, only <style lang="less"> is supported.
  • LESS styles are processed using MediaWiki's LESS processor, which uses an old version of LESS. On the other hand, this means all the features supported in other LESS files in MediaWiki are supported here too.
  • ES6 import and export are not supported. Instead of import, use require(); instead of export default { ... }; use module.exports = { ... };. Other ES6 syntax is supported.
  • ES2016 and newer syntax is not supported. Notable examples of unsupported syntax include the spread operator in object literals ({ foo, ...bar }), async/await, and Object.values()/Object.entries().
  • Self-closing tags for components (e.g. <my-component />) are not supported. Instead, you have to use open and close tags (e.g. <my-component></my-component>).
  • Component tags using PascalCase (e.g. <MyComponent>) are not supported. Instead, you have to use kebab-case for component names (e.g. <my-component>).
  • .vue files are only supported in package modules (modules that use packageFiles)

If you try to use an unsupported SFC feature, ResourceLoader will throw an error at runtime. Unsupported JS or template syntax results in a lint error, so make sure you set up linting for your code to catch these, as well as other pitfalls and style guideline violations.

Initialize with createMwApp()Edit

To mount your component to the DOM, use Vue.createMwApp(). This function works the same as Vue.createApp(), but adds shared MediaWiki-specific plugins for internationalization and error logging. The code mounting your component should be in a simple init file that requires the top-level component (plus any stores and plugins) and mounts it to a placeholder div. Other logic and UI code should live in other files (for example, the top-level component) as much as possible. Conventionally, the top-level component file is called App.vue and the init file is called init.js, unless there are multiple entry points that each have their own top-level component.

A simple example of an init file would look like this:

var Vue = require( 'vue' ),
    App = require( './App.vue' );

// Assuming there's a <div id="my-component-placeholder"> on the page
Vue.createMwApp( App ).mount( '#my-component-placeholder' );

A more complex init file that uses a Vuex store and a custom plugin would look like this:

var Vue = require( 'vue' ),
    App = require( './App.vue' ),
    store = require( './store/index.js' ),
    fooPlugin = require( './plugins/foo.js' );

Vue.createMwApp( App )
    .use( store )
    .use( fooPlugin )
    // Assuming there's a <div id="my-component-placeholder"> on the page
    .mount( '#my-component-placeholder' );

Write Vue3-compatible codeEdit

MediaWiki uses the compatibility build of Vue 3. This means that Vue will try to provide compatibility with Vue 2 code by default, which breaks certain Vue 3 features (in particular v-model on components, and setting attributes to false). To avoid this breakage, all new code written for Vue 3 (and old code that has been fully migrated to Vue 3) must set compatConfig: { MODE: 3 } and compilerOptions: { whitespace: 'condense' } in the component definition of every component, like this:

// @vue/component
module.exports = exports = {
    // Enable Vue 3 mode with compatConfig and compilerOptions
    compatConfig: {
        MODE: 3
    },
    compilerOptions: {
        whitespace: 'condense'
    },
    // The rest of your component definition goes here:
    components: {
        // ...
    },
    data() {
        return {
            // ...
        };
    },
    methods: {
        // ...
    }
    // etc.
};

Once all old code written for Vue 2 has been migrated to Vue 3, MediaWiki will migrate to the non-compatibility build of Vue 3. Once that has happened, these compatConfig settings will no longer be necessary.

Don't rely on build tools or server-side renderingEdit

In the wider Vue.js community, developers make frequent use of Node.js build tools like Vite, Rollup, Nuxt.js, etc. MediaWiki is a PHP application, and currently no Node.js service is available for use in production in WMF projects. For the time being, developers working with Vue in MediaWiki must limit themselves to client-side functionality only. The Design Systems Team is currently exploring ways to support a front-end build step or server-side component rendering, but this remains experimental.

Use other MW-provided libraries when necessaryEdit

Pinia is available in MediaWiki. Pinia is the new official state management library for use with Vue 3 and successor to Vuex 4. Pinia is recommended for new projects; for older Vuex-based projects, MediaWiki also makes Vuex 4 available.

Follow MediaWiki coding conventionsEdit

See MediaWiki's Vue coding conventions, which are largely based on the official Vue Style Guide. We use ESLint to enforce these conventions; see here for how to set up ESLint in your repository.