` compiled with a helper functions (`_defineProperty()`) by `babel`
110 |
111 | And I checked the [Doc of babel-plugin-transform-computed-properties](https://babeljs.io/docs/en/babel-plugin-transform-computed-properties), found this is by designed.
112 |
113 | 
114 |
115 | But we don't need this helper function, with this function we will get `Error in render: "ReferenceError: _defineProperty is not defined"`.
116 |
117 | So we should remove `defineProperty` from compiled result.
118 |
119 | > Why we get this error:
120 | >
121 | > Because we only keep the code inner render function, and strip other part of babel compile result by [getFunctionBody()](https://github.com/JuniorTour/vue-template-babel-compiler/blob/c68a991be1fb91f26397ee5e61ef307a15f05d3c/src/templateCompiler.js#L18)
122 |
123 | After search through [Google](https://google.com/), [stackoverflow.com/](https://stackoverflow.com/)
124 |
125 | Then I found there is an option for babel to remove this helper function: [assumptions.setComputedProperties](https://babeljs.io/docs/en/babel-plugin-transform-computed-properties#loose)
126 |
127 | After add this option to `vue-template-babel-compiler-nuxt-project\node_modules\vue-template-babel-compiler\lib\index.js ` during debug.
128 |
129 | I found the error will NOT throw, and everything works fine.
130 |
131 | So I think this is the solution, After merge my changes to `main branch`, the issue#13 will be fixed.
132 |
133 | ## 5. Fork this Repo and setup
134 |
135 | ``` shell script
136 | git clone https://github.com/JuniorTour/vue-template-babel-compiler.git
137 |
138 | cd vue-template-babel-compiler
139 |
140 | yarn install
141 |
142 | yarn test // ensure your setup is right
143 | ```
144 |
145 |
146 | ## 6. Add test case and run `yarn test`
147 | Before we make a Pull Request, We need to ensure this change doesn't break other logic of this lib.
148 |
149 | That is the reason why we need `Unit Test` based on [jest](https://jestjs.io/) .
150 |
151 | When we add new code, it will be better to add some test case to ensure our new code never break by others later added.
152 |
153 | ### Write test case is Easy!
154 | #### A. Find a `*.spec.js` file to write
155 |
156 | #### B. Add new test case based on `jest API`
157 |
158 | > If you are not familiar with [jest](https://jestjs.io/), its Doc and [Google](https://google.com/) will help you.
159 |
160 | > You can also refer to the [existing cases of this project](https://github.com/JuniorTour/vue-template-babel-compiler/tree/main/test), or just copy the reproduce code snippet, and modify its content.
161 | ``` js
162 | test('should use simple assign for computed properties', () => {
163 | // https://github.com/JuniorTour/vue-template-babel-compiler/issues/13
164 | const {ast, render, staticRenderFns, tips, errors} = templateCompiler.compile(
165 | ''
166 | )
167 |
168 | expect(errors.length === 0).toBeTruthy()
169 | expect(render).toMatch('class: (_class = {}, _class[`${_vm.foo}_bar`] = true, _class)')
170 | })
171 | ```
172 |
173 | #### C. Check test case by run `yarn test`
174 | Make sure you see:
175 | ```shell script
176 | // ...
177 | Test Suites: M passed, M total
178 | Tests: N passed, N total
179 | Snapshots: 0 total
180 | Time: 5.054s
181 | Ran all test suites.
182 | Done in 11.64s.
183 | ```
184 |
185 | This means your code doesn't break anything :)
186 |
187 | #### Debug test case:
188 | - add `debugger` to your test case code.
189 | - run `yarn debugTest` will start Chome DevTool like [3-locate-error](https://github.com/JuniorTour/vue-template-babel-compiler/blob/main/CONTRIBUTING.md#3-locate-error)
190 |
191 | ## 7. Git commit and Push
192 |
193 | Finally, commit your changes like [fix: use simple assign for computed properties (fix #13)](https://github.com/JuniorTour/vue-template-babel-compiler/commit/b5e8bd13e603bfb1b9dd87f1222b831fd2a68c49#) and `git push`.
194 |
195 | > Don't forget Follow the [commit convention](https://www.conventionalcommits.org/en/v1.0.0/): `#{fix || chore || feat: xxx (fix #13)}`
196 |
197 | > yarn.lock, package.json changes should be kept.
198 |
199 | Create a new `Pull Request` fellow the tips from terminal and GitHub.
200 |
201 | Then wait a moment, the maintainer will reply ASAP!
202 |
203 |
204 |
205 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # vue-template-babel-compiler · [](https://github.com/JuniorTour/vue-template-babel-compiler) [](https://github.com/JuniorTour/vue-template-babel-compiler/blob/main/CONTRIBUTING.md)
2 |
3 | Enable `Optional Chaining(?.)`, `Nullish Coalescing(??)` and many new ES syntax for [Vue.js SFC](https://vuejs.org/v2/guide/single-file-components.html) based on [Babel](https://babeljs.io/).
4 |
5 |
6 |
7 |
10 |
11 |
12 |
15 |
16 |
17 |
20 |
21 |
22 |
25 |
26 |
27 |
30 |
31 |
32 |
33 | ## DEMO
34 |
35 | Visit Online Playground →
36 |
37 |
38 | 
39 |
40 | ## Features
41 | - All features of [vue-template-compiler](https://github.com/vuejs/vue/tree/dev/packages/vue-template-compiler#readme) && [vue-template-es2015-compiler](https://github.com/vuejs/vue-template-es2015-compiler)
42 | - New ES syntax: `Optional Chaining`, `Bigint`, `Nullish Coalescing` and more
43 | - Use babel to transpile vue render function, customization syntax, babel options customization
44 |
45 | ## Usage
46 | ### 1. Install
47 | ``` bash
48 | npm install vue-template-babel-compiler --save-dev
49 | ```
50 |
51 | ### 2. Config
52 | #### 1. [Vue-CLI](https://cli.vuejs.org/guide/webpack.html#modifying-options-of-a-loader)
53 |
54 |
55 | Vue-CLI Online Example Project
56 |
57 |
58 | ``` js
59 | // vue.config.js
60 | module.exports = {
61 | chainWebpack: config => {
62 | config.module
63 | .rule('vue')
64 | .use('vue-loader')
65 | .tap(options => {
66 | options.compiler = require('vue-template-babel-compiler')
67 | return options
68 | })
69 | }
70 | }
71 | ```
72 |
73 | #### 2. [Nuxt.js](https://nuxtjs.org/docs/2.x/features/configuration#extend-webpack-to-load-audio-files)
74 |
75 |
76 | Nuxt.js Online Example Project
77 |
78 |
79 | ``` js
80 | // nuxt.config.js
81 | export default {
82 | // Build Configuration: https://go.nuxtjs.dev/config-build
83 | build: {
84 | loaders: {
85 | vue: {
86 | compiler: require('vue-template-babel-compiler')
87 | }
88 | },
89 | },
90 | // ...
91 | }
92 | ```
93 |
94 | ## [Doc](https://github.com/JuniorTour/vue-template-babel-compiler/blob/main/doc/Usage.md)
95 | - [vue-jest Usage](https://github.com/JuniorTour/vue-template-babel-compiler/blob/main/doc/Usage.md#1-vue-jest)
96 | - [Webpack Usage](https://github.com/JuniorTour/vue-template-babel-compiler/blob/main/doc/Usage.md#2-webpack)
97 | - [Vite Usage](https://github.com/JuniorTour/vue-template-babel-compiler/blob/main/doc/Usage.md#4-vite-usage)
98 | - [VueUse && `