Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optional chaining not working (no, not in the template) #1697

Closed
jfbrennan opened this issue Jul 17, 2020 · 19 comments
Closed

Optional chaining not working (no, not in the template) #1697

jfbrennan opened this issue Jul 17, 2020 · 19 comments

Comments

@jfbrennan
Copy link

jfbrennan commented Jul 17, 2020

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!

@jfbrennan jfbrennan changed the title Optional chaining not working (no, not in template) Optional chaining not working (no, not in the template) Jul 17, 2020
@haoqunjiang
Copy link
Member

vuejs/vue#11088

@jfbrennan
Copy link
Author

jfbrennan commented Jul 20, 2020

@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.

@haoqunjiang
Copy link
Member

Then you need to provide a reproduction repo. It works for most people.

@haoqunjiang
Copy link
Member

A misconfigured tsconfig.json can also lead to this issue. See vuejs/vue-cli#4738 (comment)

@jfbrennan
Copy link
Author

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?

@jfbrennan
Copy link
Author

Here's my package.json if that helps:

{
  "name": "my-project",
  "version": "1.0.0",
  "private": true,
  "scripts": {
    "build": "vue-cli-service build --dest ../server/public --no-clean",
    "build:dev": "vue-cli-service build --mode development --dest ../server/public --no-clean --watch",
    "test:unit": "vue-cli-service test:unit"
  },
  "dependencies": {
    "flat": "^5.0.0",
    "vega-lite": "^4.11.0",
    "vue": "^2.6.11",
    "vue-router": "^3.1.6"
  },
  "devDependencies": {
    "@vue/cli-plugin-router": "^4.4.6",
    "@vue/cli-plugin-unit-jest": "^4.4.6",
    "@vue/cli-service": "^4.4.6",
    "@vue/test-utils": "1.0.0-beta.33",
    "canvas": "^2.6.1",
    "vue-template-compiler": "^2.6.11"
  },
  "browserslist": [
    "> 10%",
    "last 2 versions",
    "not dead"
  ],
  "jest": {
    "preset": "@vue/cli-plugin-unit-jest/presets/no-babel",
    "collectCoverage": true,
    "collectCoverageFrom": [
      "src/**/*.{js,vue}"
    ],
    "coverageReporters": [
      "html",
      "text-summary"
    ],
    "globals": {
      "vue-jest": {
        "babelConfig": {
          "plugins": [
            "babel-plugin-transform-es2015-modules-commonjs"
          ]
        }
      }
    }
  }
}

@jfbrennan
Copy link
Author

Vue info as well:

Environment Info:

  System:
    OS: macOS 10.15.4
    CPU: (16) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
  Binaries:
    Node: 13.12.0 - /usr/local/bin/node
    Yarn: Not Found
    npm: 6.14.4 - /usr/local/bin/npm
  Browsers:
    Chrome: 84.0.4147.89
    Edge: 84.0.522.40
    Firefox: 74.0.1
    Safari: 13.1
  npmPackages:
    @vue/cli-overlay:  4.4.6 
    @vue/cli-plugin-router: ^4.4.6 => 4.4.6 
    @vue/cli-plugin-unit-jest: ^4.4.6 => 4.4.6 
    @vue/cli-plugin-vuex:  4.4.6 
    @vue/cli-service: ^4.4.6 => 4.4.6 
    @vue/cli-shared-utils:  4.4.6 
    @vue/component-compiler-utils:  3.1.2 
    @vue/preload-webpack-plugin:  1.1.1 
    @vue/test-utils: 1.0.0-beta.33 => 1.0.0-beta.33 
    @vue/web-component-wrapper:  1.2.0 
    jest-serializer-vue:  2.0.2 
    vue: ^2.6.11 => 2.6.11 
    vue-hot-reload-api:  2.3.4 
    vue-jest:  3.0.6 
    vue-loader:  15.9.3 
    vue-router: ^3.1.6 => 3.1.6 
    vue-style-loader:  4.1.2 
    vue-template-compiler: ^2.6.11 => 2.6.11 
    vue-template-es2015-compiler:  1.9.1 
  npmGlobalPackages:
    @vue/cli: 4.4.6

@haoqunjiang
Copy link
Member

It's webpack that can't yet parse such syntax webpack/webpack#11198

@jfbrennan
Copy link
Author

@sodatea ok, thanks!

@wpitallo
Copy link

wpitallo commented Jan 7, 2021

This should be re-opened its not working without typescript

@jfbrennan
Copy link
Author

@sodatea

It's webpack that can't yet parse such syntax webpack/webpack#11198

Optional chaining is only in Webpack v5. Looks like Vue Loader is on v4. When is will Vue Loader move to v5?

@Liwoj
Copy link

Liwoj commented Feb 17, 2021

Encountered same error after I changed my .browserlistrc to support only modern browsers (Vue CLI 4)

Changing babel.config.js fixed the problem:

module.exports = {
  presets: [
    '@vue/cli-plugin-babel/preset',
  ],
  plugins: ['@babel/plugin-proposal-nullish-coalescing-operator', '@babel/plugin-proposal-optional-chaining'],
}

@atflick
Copy link

atflick commented Feb 26, 2021

I spent several hours on this today and what ultimately worked for me was setting the presets and plugins directly in the webpack rule:

{ test: /\.js$/,
  use: {
    loader: 'babel-loader',
    options: {
      presets: ['@vue/cli-plugin-babel/preset', '@babel/preset-env'],
      plugins: ['@babel/plugin-proposal-optional-chaining']
    }
  },
}

@jfbrennan
Copy link
Author

jfbrennan commented Mar 1, 2021

Have tried some of the workarounds and none seem to work for my minimal setup. This needs to be reopened and resolved inside Vue Loader itself by updating to Webpack 5, which is required for optional chaining support.

@sodatea is there something preventing the upgrade to WP5?

@haoqunjiang
Copy link
Member

@jfbrennan Vue Loader has supported webpack 5 for a while.
If you are using Vue CLI, v5 alpha is based on webpack 5.

@JuniorTour
Copy link
Contributor

Try vue-template-babel-compiler

It will enable Optional Chaining(?.), Nullish Coalescing(??) and many new ES syntax for Vue.js SFC based onBabel.

Github Repo: vue-template-babel-compiler

DEMO

DEMO

Usage

Please refer to REAMDE for detail usage

Support for Vue-CLI, Nuxt.js, Webpack , any environment use vue-loader.

@anvaka
Copy link

anvaka commented Dec 18, 2021

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:

const path = require('path');

module.exports = {
  chainWebpack: config => {
    config.module
      .rule('monaco')
      .test({
        test: /\.js$/,
        // See: I'm pointing to a single folder that had the problem, so that I'm not killing
        // babel's caches for other modules
        include: path.join(__dirname, 'node_modules/monaco-editor/esm/vs'),
      })
      .use('babel-loader')
      .loader('babel-loader')
      .options({
        presets: ['@babel/preset-env'],
        plugins: ['@babel/plugin-proposal-optional-chaining']
      }).end();
  }
}

@Rainst9
Copy link

Rainst9 commented Mar 24, 2023

Encountered same error after I changed my .browserlistrc to support only modern browsers (Vue CLI 4)

Changing babel.config.js fixed the problem:

module.exports = {
  presets: [
    '@vue/cli-plugin-babel/preset',
  ],
  plugins: ['@babel/plugin-proposal-nullish-coalescing-operator', '@babel/plugin-proposal-optional-chaining'],
}

Great, this has solved my problem, but in my project, after this configuration, an error is reported as follows. I also need to install core js.

`These dependencies were not found:

  • core-js/modules/es.function.name.js in ./node_modules/cache-loader/dist/cjs.js?{"cacheDirectory":"node_modules/.cache/vue-loader","cacheIdentifier":"10cc503a-vue-loader-template"}!./node_modules/babel-loader/lib??ref--13-0!./node_modules/vue-loader/lib/loaders/templateLoader.js??ref--6!./node_modules/cache-loader/dist/cjs.js??ref--1-0!./node_modules/vue-loader/lib??vue-loader-options!.
  • core-js/modules/es.string.iterator.js in =...........&type=template&id=553c6d43&scoped=true&
  • core-js/modules/web.dom-collections.iterator.js in ./src/router/index.js

To install them, you can run: npm install --save core-js/modules/es.function.name.js core-js/modules/es.object.to-string.js core-js/modules/es.regexp.exec.js core-js/modules/es.string.iterator.js core-js/modules/es.string.search.js core-js/modules/web.dom-collections.iterator.js`

@brhaka
Copy link

brhaka commented May 18, 2023

I had the same issue and the problem was that babel was not set as the parser.
Adding this to package.json under eslintConfig solved it for me:

"parserOptions": {
  "parser": "babel-eslint"
},

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants