Skip to content

Commit

Permalink
ast: replace requires and inits for uglify with terser
Browse files Browse the repository at this point in the history
Given that uglify is initialised with options, then replace its require and usage with terser
  • Loading branch information
abenezerabebe authored and sendilkumarn committed Mar 16, 2019
1 parent e48507d commit 3011a6c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
Expand Up @@ -10,13 +10,13 @@ exports[`uglifyJsPlugin transforms correctly using "uglifyJsPlugin-0" data 1`] =
`;

exports[`uglifyJsPlugin transforms correctly using "uglifyJsPlugin-1" data 1`] = `
"const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
"const TerserPlugin = require('terser-webpack-plugin');
module.exports = {
devtool: \\"source-map\\",
optimization: {
minimize: true,
minimizer: [new UglifyJsPlugin({
minimizer: [new TerserPlugin({
sourceMap: true,
compress: {}
})]
Expand All @@ -26,13 +26,13 @@ module.exports = {
`;

exports[`uglifyJsPlugin transforms correctly using "uglifyJsPlugin-2" data 1`] = `
"const Uglify = require('uglifyjs-webpack-plugin');
"const TerserPlugin = require('terser-webpack-plugin');
module.exports = {
devtool: \\"source-map\\",
optimization: {
minimize: true,
minimizer: [new Uglify({
minimizer: [new TerserPlugin({
sourceMap: true,
compress: {}
})]
Expand Down
7 changes: 5 additions & 2 deletions packages/migrate/uglifyJsPlugin/uglifyJsPlugin.ts
Expand Up @@ -67,14 +67,17 @@ export default function(j: IJSCodeshift, ast: INode): INode {
* - add require for terser-webpack-plugin
* - add to minimizer
*/
if (pluginVariableAssignment && pluginVariableAssignment.includes("webpack")) {
if (pluginVariableAssignment) {
// remove require for uglify-js-plugin
searchForRequirePlugin.remove();

// create require for terser-webpack-plugin
const pathRequire: INode = getRequire(
j,
"TerserPlugin",
"terser-webpack-plugin",
);
// append to source code.
// prepend to source code.
ast
.find(j.Program)
.replaceWith((p: INode): INode =>
Expand Down

0 comments on commit 3011a6c

Please sign in to comment.