SyntaxError: Unexpected token 'const' (with Vue, Karma, Webpack, PhantomJS)

前端 未结 3 975
天命终不由人
天命终不由人 2020-12-18 01:28

I have a more-or-less vanilla Laravel + Vue.js app and I am trying to do some JS testing with Karma and Jasmine. If I try to use () => {} style functions or

3条回答
  •  心在旅途
    2020-12-18 02:03

    The comments by @craig_h and @PanJunjie set me on the track of looking at the configuration for karma-babel-preprocessor, which led me to the config for karma-webpack. I'm still not sure what was causing the original problem, but it appears that my webpack config for Karma was incorrect or incomplete, and was failing silently. I added babel-loader and babel-preset-es2015 packages via

    yarn add babel-loader babel-preset-es2015
    

    And then I redid and cleaned up my karma.conf.js as such:

    module.exports = function(config) {
    
        config.set({
    
            singleRun: false, // false => watch for changes and rerun tests
            autoWatch: true, // enable / disable watching files & then run tests
    
            frameworks: ['jasmine'],
            browsers: ['PhantomJS'],
    
            // Options: LOG_DISABLE, LOG_ERROR, LOG_WARN, LOG_INFO, LOG_DEBUG
            logLevel: config.LOG_INFO,
    
            basePath: './resources/assets/js/',
    
            files: [
                { pattern: 'tests/**/*.spec.js', watched: false },
            ],
    
            // how to process files before serving them to the browser for testing
            preprocessors: {
                'app.js': ['webpack'],
                'tests/**/*.spec.js': ['webpack'],
            },
    
            webpack: {
                module: {
                    loaders: [
                        { test: /\.vue$/, loader: 'vue' },
                        { test: /\.js$/,
                          loader: 'babel-loader',
                          exclude: /node_modules/,
                          query: { presets: ['es2015'] }
                        }
                    ]
                },
                // make sure to use the stand-alone version of Vue
                resolve: {
                    alias: {vue: 'vue/dist/vue.js'}
                }
            },
    
            webpackMiddleware: {
              noInfo: true,
              stats: 'errors-only'
            }
        });
    };
    

    My package.json now looks like this:

    {
      "private": true,
      "scripts": {
        "prod": "gulp --production",
        "dev": "gulp watch"
      },
      "devDependencies": {
        "bootstrap-sass": "^3.3.7",
        "gulp": "^3.9.1",
        "jquery": "^3.1.0",
        "laravel-elixir": "^6.0.0-11",
        "laravel-elixir-vue-2": "^0.2.0",
        "laravel-elixir-webpack-official": "^1.0.2",
        "lodash": "^4.16.2",
        "vue": "^2.0.1",
        "vue-resource": "^1.0.3"
      },
      "dependencies": {
        "babel-loader": "^6.2.8",
        "babel-preset-es2015": "^6.18.0",
        "jasmine-core": "^2.5.2",
        "karma": "^1.3.0",
        "karma-babel-preprocessor": "^6.0.1",
        "karma-chrome-launcher": "^2.0.0",
        "karma-firefox-launcher": "^1.0.0",
        "karma-jasmine": "^1.0.2",
        "karma-phantomjs-launcher": "^1.0.2",
        "karma-webpack": "^1.8.0"
      }
    }
    

    With all of that, I can now use all of the ES2015 goodness like const and () => {}. Sorry to answer my own question, but I hope this helps someone else that encounters a similar issue.

提交回复
热议问题