Navigation Menu

Skip to content

Commit

Permalink
ast: replace UglifyJsPlugin with TerserPlugin
Browse files Browse the repository at this point in the history
if a module requires uglify-js-plugin, replace it with a require for terser-webpack-plugin
  • Loading branch information
abenezerabebe authored and sendilkumarn committed Mar 16, 2019
1 parent 9863445 commit 21da35f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
Expand Up @@ -78,7 +78,7 @@ exports[`uglifyJsPlugin transforms correctly using "uglifyJsPlugin-3" data 1`] =
`;

exports[`uglifyJsPlugin transforms correctly using "uglifyJsPlugin-4" data 1`] = `
"const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
"const TerserPlugin = require('terser-webpack-plugin');
module.exports = {
devtool: 'eval',
Expand Down Expand Up @@ -110,7 +110,7 @@ module.exports = {
optimization: {
minimize: true,
minimizer: [new UglifyJsPlugin({
minimizer: [new TerserPlugin({
sourceMap: true
})]
}
Expand Down
13 changes: 7 additions & 6 deletions packages/migrate/uglifyJsPlugin/uglifyJsPlugin.ts
Expand Up @@ -15,7 +15,8 @@ import { IJSCodeshift, INode } from "../types/NodePath";
* and adds `optimization.minimize: true` to config
*
* If any configuration is passed to UglifyWebpackPlugin
* plugin instantiation is moved to `optimization.minimizer`.
* Replaces UglifyWebpackPlugin with TerserPlugin and
* and moves plugin instantiation to `optimization.minimizer`.
*
* @param {Object} j - jscodeshift top-level import
* @param {Node} ast - jscodeshift ast to transform
Expand Down Expand Up @@ -63,15 +64,15 @@ export default function(j: IJSCodeshift, ast: INode): INode {
* If user is using UglifyJsPlugin directly from webpack
* transformation must:
* - remove it
* - add require for uglify-webpack-plugin
* - add require for terser-webpack-plugin
* - add to minimizer
*/
if (pluginVariableAssignment && pluginVariableAssignment.includes("webpack")) {
// create require for uglify-webpack-plugin
// create require for terser-webpack-plugin
const pathRequire: INode = getRequire(
j,
"UglifyJsPlugin",
"uglifyjs-webpack-plugin",
"TerserPlugin",
"terser-webpack-plugin",
);
// append to source code.
ast
Expand All @@ -84,7 +85,7 @@ export default function(j: IJSCodeshift, ast: INode): INode {
"init",
j.identifier("minimizer"),
j.arrayExpression([
j.newExpression(j.identifier("UglifyJsPlugin"), [pluginOptions[0]]),
j.newExpression(j.identifier("TerserPlugin"), [pluginOptions[0]]),
]),
);
} else {
Expand Down

0 comments on commit 21da35f

Please sign in to comment.