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.
Vue Cli 3 + Vue 2 does not support optional chaining.
Vue CLI uses babel. config. js which is a new config format in Babel 7.
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.
@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.
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.
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.
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.
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']
}
}
}
]
}
},
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With