It's not possible to modify the default configuration.
You can use the optimization.minimizer setting to instantiate your own UglifyJsPlugin, however. Using 4.0 we used this example to get source maps even when mode is set to 'production' for example (no longer necessary as of 4.1.1):
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
module.exports = {
optimization: {
minimizer: [
// we specify a custom UglifyJsPlugin here to get source maps in production
new UglifyJsPlugin({
cache: true,
parallel: true,
uglifyOptions: {
compress: false,
ecma: 6,
mangle: true
},
sourceMap: true
})
]
}
};
Answer from Beau on Stack OverflowIt's not possible to modify the default configuration.
You can use the optimization.minimizer setting to instantiate your own UglifyJsPlugin, however. Using 4.0 we used this example to get source maps even when mode is set to 'production' for example (no longer necessary as of 4.1.1):
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
module.exports = {
optimization: {
minimizer: [
// we specify a custom UglifyJsPlugin here to get source maps in production
new UglifyJsPlugin({
cache: true,
parallel: true,
uglifyOptions: {
compress: false,
ecma: 6,
mangle: true
},
sourceMap: true
})
]
}
};
Without adding uglifyjs-webpack-plugin, you can just add this to the end of your webpack.prod.config.js file:
optimization: {
minimize: false
}
You can use this:
const UglifyJsPlugin = require('uglifyjs-webpack-plugin')
...
optimization: {
minimizer: [
new UglifyJsPlugin({
uglifyOptions: { ... }
})
]
}
And some more options:
module.exports = {
// ...
plugins: [
// ... other plugins
optimization: {
minimizer: [
//https://github.com/mishoo/UglifyJS2/tree/harmony
new UglifyJsPlugin({
uglifyOptions: {
output: {
comments: false
},
minify: {},
compress: {
booleans: true,
//...
}
}
}),
]
},
]
// ...
}
more discussion: https://github.com/webpack-contrib/uglifyjs-webpack-plugin/issues/234#issuecomment-369134047
similar question in stackoverflow: https://stackoverflow.com/a/49059746/6200607
In Webpack docs: https://webpack.js.org/plugins/uglifyjs-webpack-plugin/
** Edit: replaced UglifyJSPlugin with UglifyJsPlugin **
Add this to the OfflinePlugin configuration:
ServiceWorker: {
minify: false,
},
in the webpack.prod.config.js:
new OfflinePlugin({
relativePaths: false,
publicPath: '/',
excludes: ['.htaccess'],
caches: {
main: [':rest:'],
},
ServiceWorker: {
minify: false,
},
safeToUseOptionalCaches: true,
AppCache: false,
}),
webpack.config.js:
const webpack = require("webpack");
module.exports = {
entry: {
"bundle": "./entry.js",
"bundle.min": "./entry.js",
},
devtool: "source-map",
output: {
path: "./dist",
filename: "[name].js"
},
plugins: [
new webpack.optimize.UglifyJsPlugin({
include: /\.min\.js$/,
minimize: true
})
]
};
Since Webpack 4, webpack.optimize.UglifyJsPlugin has been deprecated and its use results in error:
webpack.optimize.UglifyJsPlugin has been removed, please use config.optimization.minimize instead
As the manual explains, the plugin can be replaced with minimize option. Custom configuration can be provided to the plugin by specifying UglifyJsPlugin instance:
const webpack = require("webpack");
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
module.exports = {
// ...
optimization: {
minimize: true,
minimizer: [new UglifyJsPlugin({
include: /\.min\.js$/
})]
}
};
This does the job for a simple setup. A more effective solution is to use Gulp together with Webpack and do the same thing in one pass.
You can use a single config file, and include the UglifyJS plugin conditionally using an environment variable:
const webpack = require('webpack');
const TerserPlugin = require('terser-webpack-plugin');
const PROD = JSON.parse(process.env.PROD_ENV || '0');
module.exports = {
entry: './entry.js',
devtool: 'source-map',
output: {
path: './dist',
filename: PROD ? 'bundle.min.js' : 'bundle.js'
},
optimization: {
minimize: PROD,
minimizer: [
new TerserPlugin({ parallel: true })
]
};
and then just set this variable when you want to minify it:
$ PROD_ENV=1 webpack
Edit:
As mentioned in the comments, NODE_ENV is generally used (by convention) to state whether a particular environment is a production or a development environment. To check it, you can also set const PROD = (process.env.NODE_ENV === 'production'), and continue normally.
https://medium.com/webpack/webpack-4-mode-and-optimization-5423a6bc597a
If you look at the url it will explain all optimization options.
By default in dev mode webpack 4 won't minimize js, this is to speed up development. As soon as you switch mode to production or use the -p while running webpack it will automatically minimize your JS there is no need for uglifyjs setup anymore in webpack 4.
Webpack 4.X and minifying JS
UglifyJSPlugin:
const UglifyJSPlugin = require('uglifyjs-webpack-plugin')
...
optimization: {
minimizer: [
new UglifyJSPlugin({
uglifyOptions: { ... }
})
]
}
Other options:
Note that while the UglifyJSPlugin is a great place to start for minification, there are other options out there. Here are a few more popular ones:
[BabelMinifyWebpackPlugin][1]
[ClosureCompilerPlugin][1]
If you decide to try another, just make sure your new choice also drops dead code as described in the tree shaking guide.
source: https://webpack.js.org/guides/production/
Look at: https://stackoverflow.com/a/49767690/6200607
You're still putting it in config.plugins, have you tried putting it in config.optimization.minimizer?
const UglifyJSPlugin = require('uglifyjs-webpack-plugin')
...
optimization: {
minimizer: [
new UglifyJSPlugin({
uglifyOptions: {
compress: {
drop_console: true,
}
}
})
]
}
This no longer works with Webpack 4, need to use Terser plugin, see here https://stackoverflow.com/a/41041580/378506
As you may notice that the plugin uglifyjs-webpack-plugin is being deprecated and in the terser-webpack-plugin comes in as replacement. So UglifyJsPlugin plugin is likely unavailable in webpack.optimize. So here is a possible way to fix your issue:
- Just remove the following line in your config file:
new webpack.optimize.UglifyJsPlugin({ sourceMap: true })
// Remove this ^
- and add the plugin to
optimizer:
const TerserPlugin = require("terser-webpack-plugin");
module.exports = {
// ...
optimization: {
minimize: true,
minimizer: [new TerserPlugin()],
},
// ...
};
Regarding NamedModulesPlugin is not a constructor, it's also be deprecated you can find out here if you're using webpack 5. Basically you can remove that plugin and replace it with optimization option:
module.exports = {
//...
// NamedModulesPlugin → optimization.moduleIds: 'named'
optimization: {
moduleIds: 'named',
},
};
I searched the internet and came across issues like the following but could not solve my project problem
[https://stackoverflow.com/questions/46389267/using-async-await-with-webpack-simple-configuration-throwing-error-regeneratorr][1]