Skip to content

Commit

Permalink
feat: replace uglifyJsPlugin with terserPlugin in migrate
Browse files Browse the repository at this point in the history
  • Loading branch information
abenezerabebe authored and sendilkumarn committed Mar 16, 2019
1 parent 2106e70 commit d467f3b
Show file tree
Hide file tree
Showing 8 changed files with 209 additions and 7 deletions.
117 changes: 117 additions & 0 deletions 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
})]
}
};
"
`;
@@ -0,0 +1,5 @@
const TerserPlugin = require("terser-webpack-plugin");

module.exports = {
plugins: [new TerserPlugin()]
};
@@ -0,0 +1,10 @@
const TerserPlugin = require("terser-webpack-plugin");
module.exports = {
devtool: "source-map",
plugins: [
new TerserPlugin({
sourceMap: true,
compress: {}
})
]
};
@@ -0,0 +1,10 @@
const TerserPlugin = require("terser-webpack-plugin");
module.exports = {
devtool: "source-map",
plugins: [
new TerserPlugin({
sourceMap: true,
compress: {}
})
]
};
@@ -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
};
@@ -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
};
9 changes: 9 additions & 0 deletions 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");
Expand Up @@ -29,19 +29,19 @@ 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 => {
pluginVariableAssignment = node.value.id.name;
});

pluginVariableAssignment = !pluginVariableAssignment
? "webpack.optimize.UglifyJsPlugin"
? "webpack.optimize.TerserPlugin"
: pluginVariableAssignment;

findPluginsByName(j, ast, [pluginVariableAssignment])
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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 {
Expand Down

0 comments on commit d467f3b

Please sign in to comment.