Enabling Vue.js (``vue-loader``)
================================
.. admonition:: Screencast
:class: 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``:
.. code-block:: diff
// webpack.config.js
// ...
Encore
// ...
.addEntry('main', './assets/main.js')
+ .enableVueLoader()
;
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.
Runtime Compiler Build
----------------------
By default, Encore uses a Vue "build" that allows you to compile templates at
runtime. This means that you *can* do either of these:
.. code-block:: javascript
new Vue({
template: '
{{ hi }}
'
})
new Vue({
el: '#app', // where in your DOM contains the Vue template
});
If you do *not* need this functionality (e.g. you use single file components),
then you can tell Encore to create a *smaller* and CSP-compliant build:
.. code-block:: javascript
// webpack.config.js
// ...
Encore
// ...
.enableVueLoader(() => {}, { runtimeCompilerBuild: false })
;
You can also silence the recommendation by passing ``runtimeCompilerBuild: true``.
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:
.. code-block:: terminal
$ 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 :doc:`/frontend/encore/dev-server` for more details.
JSX Support
-----------
You can enable `JSX with Vue.js`_ by configuring the second parameter of the
``.enableVueLoader()`` method:
.. code-block:: diff
// webpack.config.js
// ...
Encore
// ...
.addEntry('main', './assets/main.js')
- .enableVueLoader()
+ .enableVueLoader(() => {}, {
+ useJsx: true
+ })
;
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 ``