11# vue3-class-component
22
3+ [ ![ npm package] ( https://img.shields.io/npm/v/@haixing_hu/vue3-class-component.svg )] ( https://npmjs.com/package/@haixing_hu/vue3-class-component )
34[ ![ License] ( https://img.shields.io/badge/License-Apache-blue.svg )] ( https://www.apache.org/licenses/LICENSE-2.0 )
45[ ![ 中文文档] ( https://img.shields.io/badge/文档-中文版-blue.svg )] ( README.zh_CN.md )
56[ ![ CircleCI] ( https://dl.circleci.com/status-badge/img/gh/Haixing-Hu/vue3-class-component/tree/master.svg?style=shield )] ( https://dl.circleci.com/status-badge/redirect/gh/Haixing-Hu/vue3-class-component/tree/master )
@@ -9,14 +10,16 @@ This library allows you to create your [Vue] components using the class-style sy
910It draws heavy inspiration from [ vue-class-component] , with a few notable differences:
1011
1112- It supports [ Vue] v3.x.x (currently v3.3.4).
12- - It 's written in pure JavaScript rather than TypeScript, distinguishing it
13- from [ vue-facing-decorator ] .
13+ - Unlike [ vue-facing-decorator ] , it 's written in pure JavaScript rather than TypeScript,
14+ eliminating the need for TypeScript configuration .
1415- It adopts the most recent (as of May 2023) [ stage 3 proposal of JavaScript decorators]
1516 and [ stage 3 proposal of JavaScript decorator metadata] .
1617- It offers a set of commonly used decorators for class-style Vue components, such as
17- ` @Prop ` , ` @Watch ` , ` @Provide ` , and ` @Inject ` (for more details, see the
18+ ` @Prop ` , ` @Watch ` , ` @Provide ` , and ` @Inject ` (for more details, refer to the
1819 [ Predefined Decorators] ( #predefined-decorators ) section). In essence, it
1920 combines the functionality of [ vue-class-component] and [ vue-property-decorator] .
21+ - Offer both UMD and ESM bundling formats, providing support for both [ webpack] and [ vite] .
22+ For more details, refer to the [ Configuration] ( #configuration ) section.
2023- It has undergone extensive unit testing, achieving a remarkable 100% code coverage.
2124- The code has undergone a complete rewrite, and the functionality of decorators has been redesigned for improved coherence and efficiency.
2225
@@ -50,28 +53,105 @@ This library uses the most recent (currently May 2023)
5053[ Babel] with [ @babel/plugin-transform-class-properties ] and the
5154[ @babel/plugin-proposal-decorators ] plugins.
5255
53- A possible [ Babel] configuration file ` babel.config.json ` is as follows:
54- ``` json
55- {
56- "presets" : [
57- " @babel/preset-env"
58- ],
59- "plugins" : [
60- " @babel/plugin-transform-runtime" ,
61- [" @babel/plugin-proposal-decorators" , { "version" : " 2023-05" }],
62- " @babel/plugin-transform-class-properties"
63- ]
64- }
65- ```
66-
67- ** IMPORTANT NOTE:** To support the [ stage 3 proposal of JavaScript decorator metadata] ,
56+ ** NOTE:** To support the [ stage 3 proposal of JavaScript decorator metadata] ,
6857the version of the [ Babel] plugin [ @babel/plugin-proposal-decorators ] must be
6958at least ` 7.23.0 ` .
7059
71- To learn how to create a new Vue.js project and utilize [ vue3-class-component] , please consult the following:
72-
73- - Demo project created by [ vue-cli] and [ webpack] : [ vue3-class-component-demo-webpack]
74- - Demo project created by [ create-vue] and [ vite] : [ vue3-class-component-demo-vite]
60+ ### <span id =" webpack " >Bundling with [ webpack] </span >
61+
62+ 1 . Install the required dependencies:
63+ 2 . Configure [ Babel] by using the [ @babel/plugin-transform-class-properties ]
64+ and [ @babel/plugin-proposal-decorators ] plugins. A possible [ Babel]
65+ configuration file ` babelrc.json ` is as follows:
66+ ``` json
67+ {
68+ "presets" : [
69+ " @babel/preset-env"
70+ ],
71+ "plugins" : [
72+ " @babel/plugin-transform-runtime" ,
73+ [" @babel/plugin-proposal-decorators" , { "version" : " 2023-05" }],
74+ " @babel/plugin-transform-class-properties"
75+ ]
76+ }
77+ ```
78+
79+ For detailed configuration instructions, you can refer to:
80+ - A sample project created with [vue-cli] and [webpack]: [vue3-class-component-demo-webpack]
81+
82+ ### <span id="vite">Bundling with [vite]</span>
83+
84+ 1 . Install the required dependencies:
85+ ```shell
86+ yarn add @haixing_hu/vue3-class-component
87+ yarn add --dev @babel/core @babel/runtime @babel/preset-env
88+ yarn add --dev @babel/plugin-proposal-decorators @babel/plugin-transform-class-properties @babel/plugin-transform-runtime
89+ ```
90+ 2 . Configure [Babel] by using [@babel/plugin-transform-class-properties] and
91+ [@babel/plugin-proposal-decorators ] plugins. A possible [Babel] configuration
92+ file `babelrc.json` is as follows:
93+ ```json
94+ {
95+ "presets" : [
96+ [" @babel/preset-env" , { "modules" : false }]
97+ ],
98+ "plugins" : [
99+ " @babel/plugin-transform-runtime" ,
100+ [" @babel/plugin-proposal-decorators" , { "version" : " 2023-05" }],
101+ " @babel/plugin-transform-class-properties"
102+ ]
103+ }
104+ ```
105+ **Note:** When bundling with [vite], make sure to set the `modules` parameter
106+ of `@babel/preset-env` to `false`.
107+ 3 . Configure [vite] by modifying the `vite.config.js` file to add support for
108+ [Babel ]. A possible `vite.config.js` file is as follows:
109+ ```js
110+ import { fileURLToPath, URL } from 'node:url';
111+ import { defineConfig } from 'vite';
112+ import vue from '@vitejs/plugin-vue';
113+ import * as babel from '@babel/core';
114+
115+ // A very simple Vite plugin support babel transpilation
116+ const babelPlugin = {
117+ name: 'plugin-babel',
118+ transform: (src, id) => {
119+ if (/\.(jsx?|vue)$/.test(id)) { // the pattern of the file to handle
120+ return babel.transform(src, {
121+ filename: id,
122+ babelrc: true,
123+ });
124+ }
125+ },
126+ };
127+ // https://vitejs.dev/config/
128+ export default defineConfig({
129+ plugins: [
130+ vue({
131+ script: {
132+ babelParserPlugins: ['decorators'], // must enable decorators support
133+ },
134+ }),
135+ babelPlugin, // must be after the vue plugin
136+ ],
137+ resolve: {
138+ alias: {
139+ '@': fileURLToPath(new URL('./src', import.meta.url)),
140+ },
141+ },
142+ });
143+ ```
144+ **Note:** In the above configuration file, we've implemented a simple [Vite]
145+ plugin to transpile the code processed by the [vite-plugin-vue] plugin using
146+ [Babel ]. Although there's a [vite-plugin-babel] plugin that claims to add
147+ [Babel ] support to [vite], we found it doesn't correctly handle [vue] Single
148+ File Components (SFCs). After closely examining its source code, we
149+ determined that to achieve correct transpilation, we need to apply [Babel]
150+ after [vite-plugin-vue] processes the source code. Therefore, the very
151+ simple plugin function above suffices for our needs.
152+
153+ For detailed configuration instructions, you can refer to:
154+ - A sample project created with [create-vue] and [vite]: [vue3-class-component-demo-vite]
75155
76156## <span id="usage-example">Usage Example</span>
77157
@@ -716,4 +796,6 @@ or setter of the component class.
716796[working with reactivity]: https://vuejs.org/guide/components/provide-inject#working-with-reactivity
717797[vue3-class-component]: https://github.com/Haixing-Hu/vue3-class-component
718798[vue3-class-component-demo-webpack]: https://github.com/Haixing-Hu/vue3-class-component-demo-webpack
719- [vue3-class-component-demo-vite]: https://github.com/Haixing-Hu/vue3-class-component-demo-vite
799+ [vue3-class-component-demo-vite]: https://github.com/Haixing-Hu/vue3-class-component-demo-vite
800+ [vite-plugin-vue]: https://www.npmjs.com/package/@vitejs/plugin-vue
801+ [vite-plugin-babel]: https://www.npmjs.com/package/vite-plugin-babel
0 commit comments