I am implementing a project with vue js.
When I perform build production with the npm run build command, the UglifyJs error appears as follows:
ERROR in app/0.4743e576731afa06aec6.chunk.js from UglifyJs
Unexpected token: keyword «const» [./node_modules/vue2-editor/dist/vue2-editor.esm.js:876,0][app/0.4743e576731afa06aec6.chunk.js:141928,0]
As I found out on google, because UglifyJs cannot build with ES6. However, I still do not know how to fix it.
I get the answer is to use TerserWebpackPlugin. However, when I tried to run with TerserWebpackPlugin, the program did not seem to respond. I felt like it was running without a stop.
This is config webpack.prod
webpack.prod.js
'use strict';
const utils = require('./vue.utils');
const webpack = require('webpack');
const config = require('../config');
const merge = require('webpack-merge');
const baseWebpackConfig = require('./webpack.common');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const OptimizeCSSPlugin = require('optimize-css-assets-webpack-plugin');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const jhiUtils = require('./utils.js');
const env = require('../config/prod.env');
const webpackConfig = merge(baseWebpackConfig, {
mode: 'production',
module: {
rules: utils.styleLoaders({
sourceMap: config.build.productionSourceMap,
extract: true,
usePostCSS: true
})
},
devtool: config.build.productionSourceMap ? config.build.devtool : false,
entry: {
global: './src/main/webapp/content/scss/global.scss',
main: './src/main/webapp/app/main'
},
output: {
path: jhiUtils.root('target/classes/static/'),
filename: 'app/[name].[hash].bundle.js',
chunkFilename: 'app/[id].[hash].chunk.js'
},
optimization: {
splitChunks: {
cacheGroups: {
commons: {
test: /[\\/]node_modules[\\/]/,
name: 'vendors',
chunks: 'all'
}
}
}
},
plugins: [
// http://vuejs.github.io/vue-loader/en/workflow/production.html
new webpack.DefinePlugin({
'process.env': env
}),
new UglifyJsPlugin({
uglifyOptions: {
compress: {
warnings: false
}
},
sourceMap: config.build.productionSourceMap,
parallel: true
}),
// extract css into its own file
new MiniCssExtractPlugin({
filename: '[name].[contenthash].css',
chunkFilename: '[id].css'
}),
// Compress extracted CSS. We are using this plugin so that possible
// duplicated CSS from different components can be deduped.
new OptimizeCSSPlugin({}),
// generate dist index.html with correct asset hash for caching.
// you can customize output by editing /index.html
// see https://github.com/ampedandwired/html-webpack-plugin
new HtmlWebpackPlugin({
template: './src/main/webapp/index.html',
chunks: ['vendors', 'main', 'global'],
chunksSortMode: 'manual',
inject: true,
minify: {
removeComments: true,
collapseWhitespace: true,
removeAttributeQuotes: true
// more options:
// https://github.com/kangax/html-minifier#options-quick-reference
},
// necessary to consistently work with multiple chunks via CommonsChunkPlugin
chunksSortMode: 'dependency'
}),
// keep module.id stable when vendor modules does not change
new webpack.HashedModuleIdsPlugin()
]
});
if (config.build.productionGzip) {
const CompressionWebpackPlugin = require('compression-webpack-plugin');
webpackConfig.plugins.push(
new CompressionWebpackPlugin({
asset: '[path].gz[query]',
algorithm: 'gzip',
test: new RegExp('\\.(' + config.build.productionGzipExtensions.join('|') + ')$'),
threshold: 10240,
minRatio: 0.8
})
);
}
if (config.build.bundleAnalyzerReport) {
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
webpackConfig.plugins.push(new BundleAnalyzerPlugin());
}
module.exports = webpackConfig;
JHipster Vue.js blueprint has been updated 16 days ago to replace uglifyjs by terser so I suppose you generated your project with an older version, try upgrading blueprint to 1.8.1 or copy what they did in https://github.com/jhipster/jhipster-vuejs/commit/ef369bb66643cc193aeb2868fc60e557252f6669