I use webpack build vue and use UglifyJs,but its error,for this:
ERROR in index.js from UglifyJs
SyntaxError: Unexpected token punc «(», expected punc «:» [index.js:386,6]
hello.vue code like this:
<script>
export default {
name: 'app',
data () {
return {
msg: 'Welcome to Your Vue.js App'
}
}
}
</script>
webpack.config.js code like this:
{
test: /\.vue$/,
loader: 'vue-loader',
options: {
babel: {
babelrc: false,
presets: ['es2015', "stage-2"],
plugins: ['babel-plugin-transform-runtime']
}
}
...
new webpack.optimize.UglifyJsPlugin({
sourceMap: false,
mangle: false,
compress: {
warnings: false
drop_console: true
}
})
package.json code for this:
"dependencies": {
"autoprefixer": "^6.6.1",
"babel": "^6.5.2",
"babel-core": "^6.21.0",
"babel-loader": "^6.2.10",
"babel-plugin-transform-runtime": "^6.15.0",
"babel-preset-es2015": "^6.18.0",
"babel-preset-stage-2": "^6.18.0",
"cross-env": "^3.1.4",
"css-loader": "^0.26.1",
"html-loader": "^0.4.4",
"sass-loader": "^4.1.1",
"url-loader": "^0.5.7",
"vue-html-loader": "^1.2.3",
"vue-loader": "^10.0.2",
"vue-style-loader": "^1.0.0",
"vue-template-compiler": "^2.1.8",
"webpack": "^2.1.0-beta.25",
"webpack-dev-server": "^2.1.0-beta.9",
"vue": "^2.1.8"
}
After build,the code like this:
/* harmony default export */ exports["default"] = {
name: 'app',
data() {
return {
msg: 'Welcome to Your Vue.js App'
};
}
};
I found after build, hellow.vue file to index.js,its code ES6 doesnt to ES5,so UglifyJs is error.
I tried all the methods, but it failed.
Please help me,thanks.
I just solved this issue myself. To apply babel to
.vuefiles as well you must not specify babel options inside yourwebpack.config.jsbut inside a.babelrcfile instead.1) Remove the
optionsproperty from thevue-loaderentirely so it looks like this:2) Create a
.babelrcfile in your project root (next topackage.jsonandwebpack.config.js) with the following content:But beware: this is a JSON5 file, not an ordinary JSON file or JavaScript file!
I'm not sure if your
options.babel: {}approach is supposed to work but the.babelrcapproach is used by the official vue-cli templates and it also works for my custom setup.