Enabling Vue.js (vue-loader)

Enabling Vue.js (vue-loader)

Screencast

Do you prefer video tutorials? Check out the Vue screencast series.

Want to use Vue.js? No problem! First enable it in webpack.config.js:

  1. // webpack.config.js
  2. // ...
  3. Encore
  4. // ...
  5. .addEntry('main', './assets/main.js')
  6. + .enableVueLoader()
  7. ;

Then restart Encore. When you do, it will give you a command you can run to install any missing dependencies. After running that command and restarting Encore, you’re done!

Any .vue files that you require will be processed correctly. You can also configure the vue-loader options by passing an options callback to enableVueLoader(). See the Encore’s index.js file for detailed documentation.

Hot Module Replacement (HMR)

The vue-loader supports hot module replacement: just update your code and watch your Vue.js app update without a browser refresh! To activate it, use the dev-server with the --hot option:

  1. $ yarn encore dev-server --hot

That’s it! Change one of your .vue files and watch your browser update. But note: this does not currently work for style changes in a .vue file. Seeing updated styles still requires a page refresh.

See Using webpack-dev-server and HMR for more details.

JSX Support

You can enable JSX with Vue.js by configuring the second parameter of the .enableVueLoader() method:

  1. // webpack.config.js
  2. // ...
  3. Encore
  4. // ...
  5. .addEntry('main', './assets/main.js')
  6. - .enableVueLoader()
  7. + .enableVueLoader(() => {}, {
  8. + useJsx: true
  9. + })
  10. ;

Next, run or restart Encore. When you do, you will see an error message helping you install any missing dependencies. After running that command and restarting Encore, you’re done!

Your .jsx files will now be transformed through @vue/babel-preset-jsx.

Using styles

You can’t use <style> in .jsx files. As a workaround, you can import .css, .scss, etc. files manually:

  1. // App.jsx
  2. import './App.css'
  3. export default {
  4. name: 'App',
  5. render() {
  6. return (
  7. <div>
  8. ...
  9. </div>
  10. )
  11. }
  12. }

Note

Importing styles this way makes them global. See the next section for scoping them to your component.

Using Scoped Styles

You can’t use Scoped Styles (<style scoped>) either in .jsx files. As a workaround, you can use CSS Modules by suffixing import paths with ?module:

  1. // Component.jsx
  2. import styles from './Component.css?module' // suffix with "?module"
  3. export default {
  4. name: 'Component',
  5. render() {
  6. return (
  7. <div>
  8. <h1 class={styles.title}>
  9. Hello World
  10. </h1>
  11. </div>
  12. )
  13. }
  14. }
  1. /* Component.css */
  2. .title {
  3. color: red
  4. }

The output will be something like <h1 class="title_a3dKp">Hello World</h1>.

Using images

You can’t use <img src="./image.png"> in .jsx files. As a workaround, you can import them with require() function:

  1. export default {
  2. name: 'Component',
  3. render() {
  4. return (
  5. <div>
  6. <img src={require("./image.png")}/>
  7. </div>
  8. )
  9. }
  10. }

Using Vue inside Twig templates

Twig templates can instantiate a Vue.js app in the same way as any other JavaScript code. However, given that both Twig and Vue.js use the same delimiters for variables, you should configure the delimiters Vue.js option to change the default variable delimiters.

If you set for example delimiters: ['${', '}$'], then you can use the following in your Twig templates:

  1. {{ twig_variable }} {# renders a Twig variable #}
  2. ${ vuejs_variable }$ {# renders a Vue.js variable #}

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.