Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@babel/plugin-proposal-optional-chaining not working inside Vue.js <script> tag

In the vue/vuetify project, while trying to get optional chaining (https://babeljs.io/docs/en/babel-plugin-proposal-optional-chaining) to work, I always encounter an error:


./src/App.vue?vue&type=script&lang=ts& (./node_modules/cache-loader/dist/cjs.js??ref--0-0!./node_modules/vue-loader/lib??vue-loader-options!./src/App.vue?vue&type=script&lang=ts&) 155:24
Module parse failed: Unexpected token (155:24)
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.
|         }
| 
>         const baz = obj?.foo?.bar?.baz // 42
| 
|         const safe = obj?.qux?.baz // undefined

I have added @babel/plugin-proposal-optional-chaining using the yarn add @babel/plugin-proposal-optional-chaining --dev command

In my App.vue I have this code, at created():

const obj = {
  foo: {
    bar: {
      baz: 42,
    },
  },
}

const baz = obj?.foo?.bar?.baz // 42
const safe = obj?.qux?.baz // undefined

console.log({ baz, safe })

My babel.config.js looks like this:

module.exports = {
    presets: [
        [
            "@babel/preset-env",
            {
                targets: {
                    node: "current",
                },
            },
        ],
    ],
    plugins: ["@babel/plugin-proposal-optional-chaining"],
}

I'm using vue v.2.6.9.

Seems like after that setup everything should work just fine, the only similar issue I have found is this one: https://github.com/vuejs/vue-loader/issues/1697, but here I am, actually using the Babel and not using TypeScript.

like image 376
Marek Hückmann Avatar asked Dec 23 '20 16:12

Marek Hückmann


People also ask

Does Vue support optional chaining?

Vue Cli 3 + Vue 2 does not support optional chaining.

Does Vue CLI use Babel?

Vue CLI uses babel. config. js which is a new config format in Babel 7.

What is Babel config js in Vue?

vue. config. js configures Vue. These are two different things. Babel transforms newer Javascript into old Javascript so that older browsers (notably IE11) can understand it.

Does vuejs support optional chaining?

@Flimm: Not for browser. iI am taking about the optional chaining in vuejs only.In react and angular we have optional chaining support but in vue js there has no suport. One quick update is that Vue 3 comes bundled with support for optional chaining. To test you can try compiling the below Vue component code.

Why @Babel/plugin-proposal-optional-chaining Vue-jest#200 cannot understand token “?

Jest cannot understand token "?" from @babel/plugin-proposal-optional-chaining vuejs/vue-jest#200 because you can test it in the test unit. because according to the Clean Code Book , your application logic is inside a wrapper ( function ) , This helps to read the code and the others will understand the reason for this.

Is it possible to use this plugin directly with Babel?

It's unlikely you want to use this plugin directly as it only enables Babel to parse this syntax. Instead, use plugin-proposal-optional-chaining to both parse and transform this syntax.

Why can’t I Use Stage 3 proposals with Vue typescript?

It seems the vue typescript plugin and the way its setup (even when using babel) is that it first transpiles the typescript then passes it to babel (if I understood correctly). That’s why < stage 3 proposals won’t work. To use them you have to use babel to transpile the typescript as well.


1 Answers

You may have fixed this already, but I had the same issue and adding the babel configuration directly into webpack solved the issue for me. It's included in @babel/preset-env now so no need to install the plugin directly it seems.

vue.config.js

module.exports = {
  configureWebpack: {
    module: {
      rules: [
        {
          test: /\.m?js$/,
          exclude: /(node_modules|bower_components)/,
          use: {
            loader: 'babel-loader',
            options: {
              presets: ["@vue/app", '@babel/preset-env']
            }
          }
        }
      ]
    }
  },
like image 124
tom_h Avatar answered Oct 16 '22 19:10

tom_h