Konrad Viltersten
Konrad Viltersten

Reputation: 39190

Spread operator not recognized by WebPack under Vue

In this answer the following syntax's been suggested.

import { mapActions } from 'vuex'
export default {
  vuex: {
    getters: { activeDataRow: state => state.activeDataRow },
    actions: { updateData, resetData }
  },
  methods: { ...mapActions(['updateData', 'resetData']) }
}

I can't make it work and I'm getting the error:

Module build failed: SyntaxError: C:/.../navigation.vue: Unexpected token (30:8)

29 | methods: {
30 | ...mapActions(['updateData', 'resetData'])
| ^
31 | }

I've tried configuring Babel to stage-2 and adding plugins but it made no change. What can be done about it? How do I troubleshoot it?

babel: {
  presets: ["es2015", "stage-2"],
  plugins: ["transform-object-rest-spread"]
}

webpack.config.js

module.exports = {
  entry: './index.js',
  output: { path: __dirname, filename: 'bundle.js' },
  module: {
    loaders: [
      { test: /\.js$/, loader: 'babel', exclude: /node_modules/ },
      { test: /\.vue$/, loader: 'vue' }]
    },
    babel: {
      presets: ["es2015", "stage-2"],
      plugins: ['transform-runtime']
    },
    resolve: { alias: { 'vue$': 'vue/dist/vue.common.js' } }
}

Upvotes: 1

Views: 5214

Answers (1)

Saurabh
Saurabh

Reputation: 73639

This can be one of the solutions. You need to have a babel loader in your config file for js code, like following:

module: {
  loaders: [
    {
      test: /\.vue$/,
      loader: 'vue'
    },
    {
      test: /\.js$/,
      loader: 'babel',
      include: projectRoot,
      exclude: /node_modules/
    },
    ...
    ...

Following are my babel related dependencies:

"babel-core": "^6.0.0",
"babel-eslint": "^7.0.0",
"babel-loader": "^6.0.0",
"babel-plugin-transform-runtime": "^6.0.0",
"babel-polyfill": "^6.16.0",
"babel-preset-es2015": "^6.0.0",
"babel-preset-stage-2": "^6.0.0",
"babel-register": "^6.0.0",
"babel-core": "^6.0.0",
"babel-eslint": "^7.0.0",
"babel-loader": "^6.0.0",
"babel-plugin-transform-runtime": "^6.0.0",
"babel-polyfill": "^6.16.0",
"babel-preset-es2015": "^6.0.0",
"babel-preset-stage-2": "^6.0.0",
"babel-register": "^6.0.0",

You can see this config and other relevant code in this repo.

Upvotes: 3

Related Questions