1 | # vue-frag-plugin <a href="https://npm.im/vue-frag-plugin"><img src="https://badgen.net/npm/v/vue-frag-plugin"></a> <a href="https://npm.im/vue-frag-plugin"><img src="https://badgen.net/npm/dm/vue-frag-plugin"></a> <a href="https://packagephobia.now.sh/result?p=vue-frag-plugin"><img src="https://packagephobia.now.sh/badge?p=vue-frag-plugin"></a>
|
2 |
|
3 | Webpack/Rollup/Vite plugin to use multiple root nodes in Vue 2 [Single-file Components (SFCs)](https://vuejs.org/v2/guide/single-file-components.html). Powered by [`vue-frag`](https://github.com/privatenumber/vue-frag).
|
4 |
|
5 |
|
6 | ```vue
|
7 | <template>
|
8 | <span>
|
9 | Hello
|
10 | </span>
|
11 | <span>
|
12 | Multiple root nodes
|
13 | </span>
|
14 | </template>
|
15 | ```
|
16 |
|
17 | <sub>Support this project by ⭐️ starring and sharing it. [Follow me](https://github.com/privatenumber) to see what other cool projects I'm working on! ❤️</sub>
|
18 |
|
19 | ## 🚀 Install
|
20 | ```sh
|
21 | npm i -D vue-frag-plugin vue-frag
|
22 | ```
|
23 |
|
24 | ## 🙋♂️ Why?
|
25 | [`vue-frag`](https://github.com/privatenumber/vue-frag) is a directive that lets you use Fragments in Vue.js 2 components, but you have to manually register it and insert it as the root node.
|
26 |
|
27 | `vue-frag-plugin` is a build-time plugin that automates this process, injecting vue-frag where necessary. You will be able to use multiple root nodes seamlessly in your SFCs, bringing the developer experience much closer to Vue 3.
|
28 |
|
29 | ## 🚦 Quick setup
|
30 |
|
31 | ### Webpack
|
32 | Add `vue-frag-plugin/loader` before `vue-loader` in `webpack.config.js`.
|
33 |
|
34 | <details>
|
35 | <summary>Example <code>webpack.config.js</code></summary>
|
36 | <br>
|
37 |
|
38 | ```diff
|
39 | module.exports = {
|
40 | ...,
|
41 |
|
42 | module: {
|
43 | rules: [
|
44 | ...,
|
45 |
|
46 | // Update the vue-loader rule to insert `vue-frag-plugin/loader` before it
|
47 | {
|
48 | test: /\.vue$/,
|
49 | - loader: 'vue-loader',
|
50 | + use: [
|
51 | + 'vue-loader',
|
52 | + 'vue-frag-plugin/loader'
|
53 | + ]
|
54 | }
|
55 | ]
|
56 | }
|
57 | }
|
58 | ```
|
59 | </details>
|
60 |
|
61 |
|
62 | ### Rollup / Vite
|
63 | 1. Import `vueFrag` from `vue-frag-plugin`
|
64 | 2. Add it to `plugins` before the Vue plugin in `rollup.config.js` or `vite.config.js`
|
65 |
|
66 | <details>
|
67 | <summary>Example <code>rollup.config.js</code></summary>
|
68 | <br>
|
69 |
|
70 | ```diff
|
71 | import { definePlugin } from 'rollup
|
72 | import vue from 'rollup-plugin-vue'
|
73 | + import { vueFrag } from 'vue-frag-plugin'
|
74 |
|
75 | export default definePlugin({
|
76 | ...,
|
77 |
|
78 | plugins: [
|
79 | + vueFrag(), // Important this goes before `vue()`
|
80 | vue()
|
81 | ],
|
82 |
|
83 | ...
|
84 | })
|
85 | ```
|
86 | </details>
|
87 |
|
88 | <details>
|
89 | <summary>Example <code>vite.config.js</code></summary>
|
90 | <br>
|
91 |
|
92 | ```diff
|
93 | import { definePlugin } from 'vite'
|
94 | import { createVuePlugin } from 'vite-plugin-vue2'
|
95 | + import { vueFrag } from 'vue-frag-plugin'
|
96 |
|
97 | export default definePlugin({
|
98 | ...,
|
99 |
|
100 | plugins: [
|
101 | + vueFrag(), // Important this goes before `createVuePlugin()`
|
102 | createVuePlugin()
|
103 | ],
|
104 |
|
105 | ...
|
106 | })
|
107 | ```
|
108 | </details>
|
109 |
|
110 | ## 💞 Related
|
111 | - [unplugin-vue2-script-setup](https://github.com/antfu/unplugin-vue2-script-setup) - Build-time plugin to use `<script setup>` in Vue 2 SFC
|