From d467f3b6e6f1edff99021f746b15e0fb1d5b44b0 Mon Sep 17 00:00:00 2001 From: Abenezer Abebe Date: Tue, 4 Dec 2018 21:28:06 +0100 Subject: [PATCH] feat: replace uglifyJsPlugin with terserPlugin in migrate --- .../__snapshots__/terserPlugin.test.js.snap | 117 ++++++++++++++++++ .../__testfixtures__/terserPlugin-0.input.js | 5 + .../__testfixtures__/terserPlugin-1.input.js | 10 ++ .../__testfixtures__/terserPlugin-2.input.js | 10 ++ .../__testfixtures__/terserPlugin-3.input.js | 23 ++++ .../__testfixtures__/terserPlugin-4.input.js | 28 +++++ .../migrate/terserPlugin/terserPlugin.test.js | 9 ++ .../terserPlugin.ts} | 14 +-- 8 files changed, 209 insertions(+), 7 deletions(-) create mode 100644 packages/migrate/terserPlugin/__snapshots__/terserPlugin.test.js.snap create mode 100644 packages/migrate/terserPlugin/__testfixtures__/terserPlugin-0.input.js create mode 100644 packages/migrate/terserPlugin/__testfixtures__/terserPlugin-1.input.js create mode 100644 packages/migrate/terserPlugin/__testfixtures__/terserPlugin-2.input.js create mode 100644 packages/migrate/terserPlugin/__testfixtures__/terserPlugin-3.input.js create mode 100644 packages/migrate/terserPlugin/__testfixtures__/terserPlugin-4.input.js create mode 100644 packages/migrate/terserPlugin/terserPlugin.test.js rename packages/migrate/{uglifyJsPlugin/uglifyJsPlugin.ts => terserPlugin/terserPlugin.ts} (89%) diff --git a/packages/migrate/terserPlugin/__snapshots__/terserPlugin.test.js.snap b/packages/migrate/terserPlugin/__snapshots__/terserPlugin.test.js.snap new file mode 100644 index 00000000000..943fb9af169 --- /dev/null +++ b/packages/migrate/terserPlugin/__snapshots__/terserPlugin.test.js.snap @@ -0,0 +1,117 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`terserPlugin transforms correctly using "terserPlugin-0" data 1`] = ` +"module.exports = { + optimization: { + minimize: true + } +}; +" +`; + +exports[`terserPlugin transforms correctly using "terserPlugin-1" data 1`] = ` +"const TerserPlugin = require(\\"terser-webpack-plugin\\"); +module.exports = { + devtool: \\"source-map\\", + optimization: { + minimize: true, + + minimizer: [new TerserPlugin({ + sourceMap: true, + compress: {} + })] + } +}; +" +`; + +exports[`terserPlugin transforms correctly using "terserPlugin-2" data 1`] = ` +"const TerserPlugin = require(\\"terser-webpack-plugin\\"); +module.exports = { + devtool: \\"source-map\\", + optimization: { + minimize: true, + + minimizer: [new TerserPlugin({ + sourceMap: true, + compress: {} + })] + } +}; +" +`; + +exports[`terserPlugin transforms correctly using "terserPlugin-3" data 1`] = ` +"module.exports = { + devtool: \\"eval\\", + entry: [\\"./src/index\\"], + + output: { + path: path.join(__dirname, \\"dist\\"), + filename: \\"index.js\\" + }, + + module: { + loaders: [ + { + test: /.js$/, + loaders: [\\"babel\\"], + include: path.join(__dirname, \\"src\\") + } + ] + }, + + resolve: { + root: path.resolve(\\"/src\\"), + modules: [\\"node_modules\\"] + }, + + plugins: [new webpack.optimize.OccurrenceOrderPlugin()], + debug: true, + + optimization: { + minimize: true + } +}; +" +`; + +exports[`terserPlugin transforms correctly using "terserPlugin-4" data 1`] = ` +"const TerserPlugin = require('terser-webpack-plugin'); +module.exports = { + devtool: \\"eval\\", + entry: [\\"./src/index\\"], + + output: { + path: path.join(__dirname, \\"dist\\"), + filename: \\"index.js\\" + }, + + module: { + loaders: [ + { + test: /.js$/, + loaders: [\\"babel\\"], + include: path.join(__dirname, \\"src\\") + } + ] + }, + + resolve: { + root: path.resolve(\\"/src\\"), + modules: [\\"node_modules\\"] + }, + + plugins: [new webpack.optimize.OccurrenceOrderPlugin()], + debug: true, + + optimization: { + minimize: true, + + minimizer: [new TerserPlugin({ + sourceMap: true + })] + } +}; +" +`; diff --git a/packages/migrate/terserPlugin/__testfixtures__/terserPlugin-0.input.js b/packages/migrate/terserPlugin/__testfixtures__/terserPlugin-0.input.js new file mode 100644 index 00000000000..34096b69329 --- /dev/null +++ b/packages/migrate/terserPlugin/__testfixtures__/terserPlugin-0.input.js @@ -0,0 +1,5 @@ +const TerserPlugin = require("terser-webpack-plugin"); + +module.exports = { + plugins: [new TerserPlugin()] +}; diff --git a/packages/migrate/terserPlugin/__testfixtures__/terserPlugin-1.input.js b/packages/migrate/terserPlugin/__testfixtures__/terserPlugin-1.input.js new file mode 100644 index 00000000000..867830e92aa --- /dev/null +++ b/packages/migrate/terserPlugin/__testfixtures__/terserPlugin-1.input.js @@ -0,0 +1,10 @@ +const TerserPlugin = require("terser-webpack-plugin"); +module.exports = { + devtool: "source-map", + plugins: [ + new TerserPlugin({ + sourceMap: true, + compress: {} + }) + ] +}; diff --git a/packages/migrate/terserPlugin/__testfixtures__/terserPlugin-2.input.js b/packages/migrate/terserPlugin/__testfixtures__/terserPlugin-2.input.js new file mode 100644 index 00000000000..867830e92aa --- /dev/null +++ b/packages/migrate/terserPlugin/__testfixtures__/terserPlugin-2.input.js @@ -0,0 +1,10 @@ +const TerserPlugin = require("terser-webpack-plugin"); +module.exports = { + devtool: "source-map", + plugins: [ + new TerserPlugin({ + sourceMap: true, + compress: {} + }) + ] +}; diff --git a/packages/migrate/terserPlugin/__testfixtures__/terserPlugin-3.input.js b/packages/migrate/terserPlugin/__testfixtures__/terserPlugin-3.input.js new file mode 100644 index 00000000000..c9d4d9be1a4 --- /dev/null +++ b/packages/migrate/terserPlugin/__testfixtures__/terserPlugin-3.input.js @@ -0,0 +1,23 @@ +module.exports = { + devtool: "eval", + entry: ["./src/index"], + output: { + path: path.join(__dirname, "dist"), + filename: "index.js" + }, + module: { + loaders: [ + { + test: /.js$/, + loaders: ["babel"], + include: path.join(__dirname, "src") + } + ] + }, + resolve: { + root: path.resolve("/src"), + modules: ["node_modules"] + }, + plugins: [new webpack.optimize.TerserPlugin(), new webpack.optimize.OccurrenceOrderPlugin()], + debug: true +}; diff --git a/packages/migrate/terserPlugin/__testfixtures__/terserPlugin-4.input.js b/packages/migrate/terserPlugin/__testfixtures__/terserPlugin-4.input.js new file mode 100644 index 00000000000..078a76fa0ec --- /dev/null +++ b/packages/migrate/terserPlugin/__testfixtures__/terserPlugin-4.input.js @@ -0,0 +1,28 @@ +module.exports = { + devtool: "eval", + entry: ["./src/index"], + output: { + path: path.join(__dirname, "dist"), + filename: "index.js" + }, + module: { + loaders: [ + { + test: /.js$/, + loaders: ["babel"], + include: path.join(__dirname, "src") + } + ] + }, + resolve: { + root: path.resolve("/src"), + modules: ["node_modules"] + }, + plugins: [ + new webpack.optimize.TerserPlugin({ + sourceMap: true + }), + new webpack.optimize.OccurrenceOrderPlugin() + ], + debug: true +}; diff --git a/packages/migrate/terserPlugin/terserPlugin.test.js b/packages/migrate/terserPlugin/terserPlugin.test.js new file mode 100644 index 00000000000..b1b3ce7d647 --- /dev/null +++ b/packages/migrate/terserPlugin/terserPlugin.test.js @@ -0,0 +1,9 @@ +"use strict"; + +const defineTest = require("@webpack-cli/utils/defineTest").default; + +defineTest(__dirname, "terserPlugin", "terserPlugin-0"); +defineTest(__dirname, "terserPlugin", "terserPlugin-1"); +defineTest(__dirname, "terserPlugin", "terserPlugin-2"); +defineTest(__dirname, "terserPlugin", "terserPlugin-3"); +defineTest(__dirname, "terserPlugin", "terserPlugin-4"); diff --git a/packages/migrate/uglifyJsPlugin/uglifyJsPlugin.ts b/packages/migrate/terserPlugin/terserPlugin.ts similarity index 89% rename from packages/migrate/uglifyJsPlugin/uglifyJsPlugin.ts rename to packages/migrate/terserPlugin/terserPlugin.ts index dbf5f8152fa..863d0abf3eb 100644 --- a/packages/migrate/uglifyJsPlugin/uglifyJsPlugin.ts +++ b/packages/migrate/terserPlugin/terserPlugin.ts @@ -29,11 +29,11 @@ export default function(j: IJSCodeshift, ast: INode): INode { const searchForRequirePlugin: INode = ast .find(j.VariableDeclarator) .filter( - j.filters.VariableDeclarator.requiresModule("uglifyjs-webpack-plugin"), + j.filters.VariableDeclarator.requiresModule("terser-webpack-plugin"), ); /** - * Look for a variable declaration which requires uglifyjs-webpack-plugin + * Look for a variable declaration which requires terser-webpack-plugin * saves the name of this variable. */ searchForRequirePlugin.forEach((node: INode): void => { @@ -41,7 +41,7 @@ export default function(j: IJSCodeshift, ast: INode): INode { }); pluginVariableAssignment = !pluginVariableAssignment - ? "webpack.optimize.UglifyJsPlugin" + ? "webpack.optimize.TerserPlugin" : pluginVariableAssignment; findPluginsByName(j, ast, [pluginVariableAssignment]) @@ -60,7 +60,7 @@ export default function(j: IJSCodeshift, ast: INode): INode { */ if (pluginOptions.length) { /* - * If user is using UglifyJsPlugin directly from webpack + * If user is using TerserPlugin directly from webpack * transformation must: * - remove it * - add require for uglify-webpack-plugin @@ -70,8 +70,8 @@ export default function(j: IJSCodeshift, ast: INode): INode { // create require for uglify-webpack-plugin const pathRequire: INode = getRequire( j, - "UglifyJsPlugin", - "uglifyjs-webpack-plugin", + "TerserPlugin", + "terser-webpack-plugin", ); // append to source code. ast @@ -84,7 +84,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 {