vue-loader: Optional chaining not working (no, not in the template)
Version
15.9.2
Reproduction link
[Bogus link removed] Seriously, why do you make it so hard to submit a simple issue???
Steps to reproduce
I am trying to use the optional chaining operator inside one of my methods:
methods: {
fetchJobs() {
...
if (res.jobs?.length) { ... }
...
}
}
I have the latest versions of Vue (2.6.11) and Vue CLI (4.4.6). My package-lock tells me Vue CLI pulled in vue-loader version 15.9.2. I also use SFC. I do not use TypeScript. I do not have any special Babel config or extra plugins. It makes no sense to me that I can use other modern parts of js, but something is barfing on the optional chaining operator.
What is expected?
My code would compile.
What is actually happening?
My code fails to compile and Vue CLI outputs this error (doesn’t like the optional chaining operator):
Building for development...
ERROR Failed to compile with 1 errors 3:41:18 PM
error in ./src/views/benchmarks.vue?vue&type=script&lang=js&
Module parse failed: Unexpected token (258:23)
File was processed with these loaders:
* ./node_modules/cache-loader/dist/cjs.js
* ./node_modules/vue-loader/lib/index.js
You may need an additional loader to handle the result of these loaders.
| api.getjobs()
| .then(res => {
> if (res.jobs?.length) {
| this.jobs = res.jobs
| // Filter out completed jobs
@ ./src/views/benchmarks.vue?vue&type=script&lang=js& 1:0-179 1:195-198 1:200-376 1:200-376
@ ./src/views/benchmarks.vue
@ ./src/router.js
@ ./src/main.js
@ multi ./src/main.js
ERROR Build failed with errors.
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [redacted]@0.1.0 build:dev: `vue-cli-service build --mode development --dest ../server/public --no-clean --watch`
I am under the impression this should work because 1) this is just vanilla js!!!, 2) Babel, which I think vue-loader is using, supports the optional chaining operator, and 3) it’s not in the template which won’t be supported until Vue 3.
I tried explicitly adding @vue/cli-plugin-babel": "^4.4.6" to my project and that didn’t change anything.
Please help!
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 5
- Comments: 19 (6 by maintainers)
Encountered same error after I changed my
.browserlistrcto support only modern browsers (Vue CLI 4)Changing
babel.config.jsfixed the problem:@sodatea 11088 issue is concerned with in the template, my issue is not in the template (see title and description). The problem looks like it’s in vue-loader. Please read and reopen.
https://github.com/vuejs/vue/issues/11088
I spent several hours on this today and what ultimately worked for me was setting the presets and plugins directly in the webpack rule:
Try vue-template-babel-compiler
It will enable
Optional Chaining(?.),Nullish Coalescing(??)and many new ES syntax forVue.js SFCbased onBabel.Github Repo: vue-template-babel-compiler
DEMO
Usage
Please refer to REAMDE for detail usage
Support for
Vue-CLI, Nuxt.js, Webpack, any environment usevue-loader.I suppose most people use Babel and have the Babel plugin and that’s why it works for them. I don’t want to have to do all that just to get totally valid JavaScript to work. And I also “do not use TypeScript,” so it’s not a ts config issue. One of the reasons I avoid Babel and TS is to minimize these kinds of time-wasting issues.
The error I shared leads me to believe that out-of-the-box whatever Vue CLI (or vue-loader) comes with and its default config does not support optional chaining. Perhaps it’s as simple as an internal version bump?
This should be re-opened its not working without typescript
Then you need to provide a reproduction repo. It works for most people.
@sodatea
Optional chaining is only in Webpack v5. Looks like Vue Loader is on v4. When is will Vue Loader move to v5?
Idea from @atflick worked for me, I wanted to be very explicit though about module that I couldn’t compile and set this in
vue.config.js:@jfbrennan Vue Loader has supported webpack 5 for a while. If you are using Vue CLI, v5 alpha is based on webpack 5.