-->

Webpack: Why does vue-loader not transpile?

2019-08-25 06:45发布

问题:

I am using vue-loader V14 and Webpack 3.12. In the vue-loader documentation is says that javascript will automatically be transpiled using babel-loader, but when I look at the output I still see ES6, such as:

data() {
    return {
        current: ''
    };
},

Here's the (relevant parts of) the webpack config:

resolve: {
    alias: {
      'vue$': 'vue/dist/vue.esm.js'
    }
},
module: {
    rules : [
        {
            test: /\.js$/,
            use: {
                loader: 'babel-loader',
                options: {
                    presets: ['env']
                }
            }
        },
        {
            test: /\.vue$/,
            use: 'vue-loader'
        }
    ]
}

I have tried setting the vue$ alias to point to the vue.common.js version.

What am I doing wrong?

UPDATE: Here's another config I tried for the vue-loader rule:

{
    test: /\.vue$/, 
    loader: "vue-loader",
    options: {
        loaders: { js: 'babel-loader' }
    }
}

回答1:

I had to set this preset in .babelrc

"presets": [
    ["env", { "modules": "commonjs" }]
],