Skip to content

Commit 1a9f2c9

Browse files
hirisovLukeTowersbennothommo
authored
Add an example of using Vue in a plugin (#102)
Co-authored-by: Luke Towers <github@luketowers.ca> Co-authored-by: Ben Thomson <git@alfreido.com>
1 parent 8fbd046 commit 1a9f2c9

1 file changed

Lines changed: 75 additions & 1 deletion

File tree

console-asset-compilation.md

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
- [Registering theme packages](#registering-themes)
99
- [Mix configuration](#mix-configuration)
1010
- [Examples](#examples)
11+
- [Tailwind CSS](#examples-tailwind)
12+
- [Vue JS](#examples-vue)
1113
- [Commands](#commands)
1214
- [Install Node dependencies](#mix-install)
1315
- [Update Node dependencies](#mix-update)
@@ -118,6 +120,7 @@ When the `winter.mix.js` file is evaluated, regardless of where you ran `mix:com
118120

119121
Here are some examples of installing common frontend libraries for use with the asset compilation.
120122

123+
<a name="examples-tailwind"></a>
121124
### Tailwind CSS
122125

123126
For themes that wish to use Tailwind CSS, include the `tailwindcss`, `postcss` and `autoprefixer` dependencies in your `package.json` file.
@@ -149,6 +152,77 @@ In the example above, we have a base CSS file that contains the Tailwind styling
149152

150153
Your theme will now be ready for Tailwind CSS development.
151154

155+
<a name="examples-vue"></a>
156+
### Vue
157+
158+
If you want to use [Vue 3](https://vuejs.org/) in your project, either in the backend or in a component or theme, you can follow these steps.
159+
160+
First, define Vue as a dependency in your plugin's `package.json`:
161+
162+
```
163+
"name": "myauthor-myplugin",
164+
"private": true,
165+
"version": "1.0.0",
166+
"dependencies": {
167+
"vue": "^3.2.41"
168+
}
169+
```
170+
171+
Run `php artisan mix:install` to install Vue and the dependencies that Vue requires.
172+
173+
Then, add a `winter.mix.js` configuration file to your plugin directory. This will build a specific entry point file, in this case `assets/src/js/myplugin.js` and create a built version of your Vue app as `assets/dist/js/myplugin.js`.
174+
175+
```js
176+
const mix = require('laravel-mix');
177+
mix.setPublicPath(__dirname);
178+
mix
179+
// compile javascript assets for plugin
180+
.js('assets/src/js/myplugin.js', 'assets/dist/js').vue({ version: 3 })
181+
```
182+
183+
Next you can create your Vue source files in your plugin's assets directory. Mix supports rendering of [single-file components](https://vuejs.org/guide/scaling-up/sfc.html), allowing you to define the template, scripting and styling all in one file.
184+
185+
```js
186+
// assets/src/js/myplugin.js
187+
188+
import { createApp } from 'vue'
189+
import Welcome from './components/Welcome'
190+
191+
const myPlugin = createApp({})
192+
193+
myPlugin.component('welcome', Welcome)
194+
195+
myPlugin.mount('[data-vue-app="myPlugin"]')
196+
```
197+
198+
```js
199+
// assets/src/js/components/Welcome.vue
200+
201+
<template>
202+
<h1>{{ title }}</h1>
203+
</template>
204+
205+
<script>
206+
export default {
207+
setup: () => ({
208+
title: 'Vue 3 in Laravel'
209+
})
210+
}
211+
</script>
212+
```
213+
214+
Now if you comple your assets in the project root with `php artisan mix:compile`, Mix will create your compiled and built Vue component as a JS file.
215+
216+
Next in the your controller's template file (eg. controllers/myvuecontroller/index.php) you can include the generated `myplugin.js` file, and render the content in the div with the `data-vue-app="myPlugin"` attribute:
217+
218+
```html
219+
<div data-vue-app="myPlugin">
220+
<welcome/>
221+
</div>
222+
223+
<script src="/plugins/foo/bar/assets/dist/js/myplugin.js"></script>
224+
```
225+
152226
<a name="commands"></a>
153227
## Commands
154228

@@ -245,4 +319,4 @@ This can be useful for running arbitrary scripts that augment the capabilities o
245319

246320
By default, all package scripts are run in "development" mode. If you wish to run a script in "production" mode, add the `-f` or `--production` flag to the command.
247321

248-
If you wish to pass extra arguments or options to your script, you can add `--` to the end of the command. Any arguments or options added after the `--` argument are interpreted as arguments and options to be sent to the script itself.
322+
If you wish to pass extra arguments or options to your script, you can add `--` to the end of the command. Any arguments or options added after the `--` argument are interpreted as arguments and options to be sent to the script itself.

0 commit comments

Comments
 (0)