Skip to content

Commit 9863445

Browse files
abenezerabebesendilkumarn
authored andcommittedMar 16, 2019
fix: reset files
Move files back to uglify
1 parent ef23fec commit 9863445

20 files changed

+398
-82
lines changed
 

‎MIGRATE.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ Given a basic configuration file like so:
3434
const ExtractTextPlugin = require('extract-text-webpack-plugin');
3535
const HtmlWebpackPlugin = require('html-webpack-plugin');
3636
const path = require('path');
37-
const TerserPlugin = require('terser-webpack-plugin');
37+
const UglifyJSPlugin = require('uglifyjs-webpack-plugin');
3838
const webpack = require('webpack');
3939

4040
module.exports = {
@@ -68,7 +68,7 @@ module.exports = {
6868
},
6969

7070
plugins: [
71-
new TerserPlugin(),
71+
new UglifyJSPlugin(),
7272

7373
new ExtractTextPlugin('styles-[contentHash].css'),
7474

@@ -115,7 +115,7 @@ After it has run, we have our new webpack config file!
115115
const ExtractTextPlugin = require('extract-text-webpack-plugin');
116116
const HtmlWebpackPlugin = require('html-webpack-plugin');
117117
const path = require('path');
118-
const TerserPlugin = require('terser-webpack-plugin');
118+
const UglifyJSPlugin = require('uglifyjs-webpack-plugin');
119119
const webpack = require('webpack');
120120

121121
module.exports = {
@@ -154,7 +154,7 @@ module.exports = {
154154
},
155155

156156
plugins: [
157-
new TerserPlugin(),
157+
new UglifyJSPlugin(),
158158

159159
new ExtractTextPlugin('styles-[contentHash].css'),
160160

‎package-lock.json

-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎packages/generators/init-generator.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export default class InitGenerator extends Generator {
4343
this.dependencies = [
4444
"webpack",
4545
"webpack-cli",
46-
"terser-webpack-plugin",
46+
"uglifyjs-webpack-plugin",
4747
"babel-plugin-syntax-dynamic-import",
4848
];
4949
this.configuration = {
@@ -430,12 +430,12 @@ export default class InitGenerator extends Generator {
430430
public installPlugins() {
431431
if (this.isProd) {
432432
this.dependencies = this.dependencies.filter(
433-
(p: string) => p !== "terser-webpack-plugin",
433+
(p: string) => p !== "uglifyjs-webpack-plugin",
434434
);
435435
} else {
436436
this.configuration.config.topScope.push(
437-
tooltip.terser(),
438-
"const TerserPlugin = require('terser-webpack-plugin');",
437+
tooltip.uglify(),
438+
"const UglifyJSPlugin = require('uglifyjs-webpack-plugin');",
439439
"\n",
440440
);
441441
}

‎packages/generators/utils/plugins.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
* Callable function with the initial plugins
44
*
55
* @returns {Function} An function that returns an array
6-
* that consists of the terser plugin
6+
* that consists of the uglify plugin
77
*/
88

99
export default function(_?: void): string[] {
10-
return ["new TerserPlugin()"];
10+
return ["new UglifyJSPlugin()"];
1111
}

‎packages/generators/utils/tooltip.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,12 @@ export default {
5050
*/`;
5151
},
5252

53-
terser: (_?: void): string => {
53+
uglify: (_?: void): string => {
5454
return `/*
55-
* We've enabled TerserJSPlugin for you! This minifies your app
55+
* We've enabled UglifyJSPlugin for you! This minifies your app
5656
* in order to load faster and run less javascript.
5757
*
58-
* https://github.com/webpack-contrib/terser-webpack-plugin
58+
* https://github.com/webpack-contrib/uglifyjs-webpack-plugin
5959
*
6060
*/`;
6161
},

‎packages/migrate/__testfixtures__/failing.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
const webpack = require('webpack');
22
const nodeEnvironment = process.env.NODE_ENV;
33
const _ = require("lodash");
4-
const TerserPlugin = require('terser-webpack-plugin');
54

65
const config = {
76
entry: {
@@ -44,7 +43,7 @@ const config = {
4443

4544
switch (nodeEnvironment) {
4645
case "production":
47-
config.plugins.push(new TerserPlugin());
46+
config.plugins.push(new webpack.optimize.UglifyJsPlugin());
4847
case "preproduction":
4948
config.output.path = __dirname + "/dist";
5049
config.plugins.push(new webpack.optimize.DedupePlugin());

‎packages/migrate/__tests__/__snapshots__/migrate.test.ts.snap

+25-37
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ module.exports = {
2222
modules: ['node_modules']
2323
},
2424
plugins: [
25-
new TerserPlugin(),
25+
new webpack.optimize.UglifyJsPlugin(),
2626
new webpack.optimize.OccurrenceOrderPlugin()
2727
],
2828
debug: true
@@ -50,7 +50,7 @@ module.exports = {
5050
modules: ['node_modules']
5151
},
5252
plugins: [
53-
new TerserPlugin(),
53+
new webpack.optimize.UglifyJsPlugin(),
5454
new webpack.optimize.OccurrenceOrderPlugin()
5555
],
5656
debug: true
@@ -61,18 +61,15 @@ module.exports = {
6161
exports[`transform should respect recast options 1`] = `
6262
"
6363
module.exports = {
64-
devtool: 'eval',
65-
66-
entry: [
64+
devtool: 'eval',
65+
entry: [
6766
'./src/index'
6867
],
69-
70-
output: {
68+
output: {
7169
path: path.join(__dirname, 'dist'),
7270
filename: 'index.js'
7371
},
74-
75-
module: {
72+
module: {
7673
rules: [{
7774
test: /.js$/,
7875
use: [{
@@ -81,18 +78,15 @@ module.exports = {
8178
include: path.join(__dirname, 'src')
8279
}]
8380
},
84-
85-
resolve: {
81+
resolve: {
8682
modules: ['node_modules', path.resolve('/src')],
8783
},
88-
89-
plugins: [
90-
new TerserPlugin(),
91-
new webpack.LoaderOptionsPlugin({
92-
debug: true,
93-
minimize: true,
94-
})
95-
],
84+
plugins: [new webpack.LoaderOptionsPlugin({
85+
debug: true,
86+
})],
87+
optimization: {
88+
minimize: true,
89+
}
9690
};
9791
"
9892
`;
@@ -122,7 +116,7 @@ module.exports = {
122116
modules: ['node_modules']
123117
},
124118
plugins: [
125-
new TerserPlugin(),
119+
new webpack.optimize.UglifyJsPlugin(),
126120
new webpack.optimize.OccurrenceOrderPlugin()
127121
],
128122
debug: true
@@ -133,18 +127,15 @@ module.exports = {
133127
exports[`transform should transform using all transformations 1`] = `
134128
"
135129
module.exports = {
136-
devtool: 'eval',
137-
138-
entry: [
130+
devtool: 'eval',
131+
entry: [
139132
'./src/index'
140133
],
141-
142-
output: {
134+
output: {
143135
path: path.join(__dirname, 'dist'),
144136
filename: 'index.js'
145137
},
146-
147-
module: {
138+
module: {
148139
rules: [{
149140
test: /.js$/,
150141
use: [{
@@ -153,18 +144,15 @@ module.exports = {
153144
include: path.join(__dirname, 'src')
154145
}]
155146
},
156-
157-
resolve: {
147+
resolve: {
158148
modules: ['node_modules', path.resolve('/src')]
159149
},
160-
161-
plugins: [
162-
new TerserPlugin(),
163-
new webpack.LoaderOptionsPlugin({
164-
debug: true,
165-
minimize: true
166-
})
167-
]
150+
plugins: [new webpack.LoaderOptionsPlugin({
151+
debug: true
152+
})],
153+
optimization: {
154+
minimize: true
155+
}
168156
};
169157
"
170158
`;

‎packages/migrate/loaderOptionsPlugin/__tests__/__snapshots__/loaderOptionsPlugin.test.ts.snap

+4-5
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,15 @@ module.exports = {
1111
`;
1212

1313
exports[`loaderOptionsPlugin transforms correctly using "loaderOptionsPlugin-1" data 1`] = `
14-
"const TerserPlugin = require(\\"terser-webpack-plugin\\");
15-
module.exports = {
14+
"module.exports = {
1615
plugins: [
17-
new TerserPlugin(),
18-
new webpack.LoaderOptionsPlugin({
16+
new webpack.optimize.UglifyJsPlugin(),
17+
new webpack.LoaderOptionsPlugin({
1918
foo: 'bar',
2019
debug: true,
2120
minimize: true
2221
})
23-
]
22+
]
2423
}
2524
"
2625
`;
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
const TerserPlugin = require("terser-webpack-plugin");
21
module.exports = {
3-
debug: true,
4-
plugins: [
5-
new TerserPlugin(),
6-
new webpack.LoaderOptionsPlugin({
7-
foo: 'bar'
8-
})
9-
]
2+
debug: true,
3+
plugins: [
4+
new webpack.optimize.UglifyJsPlugin(),
5+
new webpack.LoaderOptionsPlugin({
6+
foo: 'bar'
7+
})
8+
]
109
}

‎packages/migrate/loaderOptionsPlugin/loaderOptionsPlugin.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ export default function(j: IJSCodeshift, ast: INode): INode {
3737
});
3838
}
3939

40-
// If there is TerserPlugin, set minimize: true
41-
if (findPluginsByName(j, ast, ["TerserPlugin"]).size()) {
40+
// If there is UglifyJsPlugin, set minimize: true
41+
if (findPluginsByName(j, ast, ["webpack.optimize.UglifyJsPlugin"]).size()) {
4242
loaderOptions.minimize = true;
4343
}
4444

‎packages/migrate/migrate.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import noEmitOnErrorsPluginTransform from "./noEmitOnErrorsPlugin/noEmitOnErrors
1111
import removeDeprecatedPluginsTransform from "./removeDeprecatedPlugins/removeDeprecatedPlugins";
1212
import removeJsonLoaderTransform from "./removeJsonLoader/removeJsonLoader";
1313
import resolveTransform from "./resolve/resolve";
14-
import terserPluginTransform from "./terserPlugin/terserPlugin";
1514
import { INode } from "./types/NodePath";
15+
import uglifyJsPluginTransform from "./uglifyJsPlugin/uglifyJsPlugin";
1616

1717
interface ITransformsObject {
1818
bannerPluginTransform: object;
@@ -24,15 +24,15 @@ interface ITransformsObject {
2424
removeDeprecatedPluginsTransform: object;
2525
removeJsonLoaderTransform: object;
2626
resolveTransform: object;
27-
terserPluginTransform: object;
27+
uglifyJsPluginTransform: object;
2828
}
2929

3030
/* tslint:disable object-literal-sort-keys */
3131
const transformsObject: ITransformsObject = {
3232
loadersTransform,
3333
resolveTransform,
3434
removeJsonLoaderTransform,
35-
terserPluginTransform,
35+
uglifyJsPluginTransform,
3636
loaderOptionsPluginTransform,
3737
bannerPluginTransform,
3838
extractTextPluginTransform,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`uglifyJsPlugin transforms correctly using "uglifyJsPlugin-0" data 1`] = `
4+
"module.exports = {
5+
optimization: {
6+
minimize: true
7+
}
8+
}
9+
"
10+
`;
11+
12+
exports[`uglifyJsPlugin transforms correctly using "uglifyJsPlugin-1" data 1`] = `
13+
"const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
14+
module.exports = {
15+
devtool: \\"source-map\\",
16+
optimization: {
17+
minimize: true,
18+
19+
minimizer: [new UglifyJsPlugin({
20+
sourceMap: true,
21+
compress: {}
22+
})]
23+
}
24+
}
25+
"
26+
`;
27+
28+
exports[`uglifyJsPlugin transforms correctly using "uglifyJsPlugin-2" data 1`] = `
29+
"const Uglify = require('uglifyjs-webpack-plugin');
30+
module.exports = {
31+
devtool: \\"source-map\\",
32+
optimization: {
33+
minimize: true,
34+
35+
minimizer: [new Uglify({
36+
sourceMap: true,
37+
compress: {}
38+
})]
39+
}
40+
}
41+
"
42+
`;
43+
44+
exports[`uglifyJsPlugin transforms correctly using "uglifyJsPlugin-3" data 1`] = `
45+
"module.exports = {
46+
devtool: 'eval',
47+
48+
entry: [
49+
'./src/index'
50+
],
51+
52+
output: {
53+
path: path.join(__dirname, 'dist'),
54+
filename: 'index.js'
55+
},
56+
57+
module: {
58+
loaders: [{
59+
test: /.js$/,
60+
loaders: ['babel'],
61+
include: path.join(__dirname, 'src')
62+
}]
63+
},
64+
65+
resolve: {
66+
root: path.resolve('/src'),
67+
modules: ['node_modules']
68+
},
69+
70+
plugins: [new webpack.optimize.OccurrenceOrderPlugin()],
71+
debug: true,
72+
73+
optimization: {
74+
minimize: true
75+
}
76+
};
77+
"
78+
`;
79+
80+
exports[`uglifyJsPlugin transforms correctly using "uglifyJsPlugin-4" data 1`] = `
81+
"const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
82+
module.exports = {
83+
devtool: 'eval',
84+
85+
entry: [
86+
'./src/index'
87+
],
88+
89+
output: {
90+
path: path.join(__dirname, 'dist'),
91+
filename: 'index.js'
92+
},
93+
94+
module: {
95+
loaders: [{
96+
test: /.js$/,
97+
loaders: ['babel'],
98+
include: path.join(__dirname, 'src')
99+
}]
100+
},
101+
102+
resolve: {
103+
root: path.resolve('/src'),
104+
modules: ['node_modules']
105+
},
106+
107+
plugins: [new webpack.optimize.OccurrenceOrderPlugin()],
108+
debug: true,
109+
110+
optimization: {
111+
minimize: true,
112+
113+
minimizer: [new UglifyJsPlugin({
114+
sourceMap: true
115+
})]
116+
}
117+
};
118+
"
119+
`;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
2+
3+
module.exports = {
4+
plugins: [
5+
new UglifyJsPlugin()
6+
]
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
2+
module.exports = {
3+
devtool: "source-map",
4+
plugins: [
5+
new UglifyJsPlugin({
6+
sourceMap: true,
7+
compress: {}
8+
})
9+
]
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const Uglify = require('uglifyjs-webpack-plugin');
2+
module.exports = {
3+
devtool: "source-map",
4+
plugins: [
5+
new Uglify({
6+
sourceMap: true,
7+
compress: {}
8+
})
9+
]
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
module.exports = {
2+
devtool: 'eval',
3+
entry: [
4+
'./src/index'
5+
],
6+
output: {
7+
path: path.join(__dirname, 'dist'),
8+
filename: 'index.js'
9+
},
10+
module: {
11+
loaders: [{
12+
test: /.js$/,
13+
loaders: ['babel'],
14+
include: path.join(__dirname, 'src')
15+
}]
16+
},
17+
resolve: {
18+
root: path.resolve('/src'),
19+
modules: ['node_modules']
20+
},
21+
plugins: [
22+
new webpack.optimize.UglifyJsPlugin(),
23+
new webpack.optimize.OccurrenceOrderPlugin()
24+
],
25+
debug: true
26+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
module.exports = {
2+
devtool: 'eval',
3+
entry: [
4+
'./src/index'
5+
],
6+
output: {
7+
path: path.join(__dirname, 'dist'),
8+
filename: 'index.js'
9+
},
10+
module: {
11+
loaders: [{
12+
test: /.js$/,
13+
loaders: ['babel'],
14+
include: path.join(__dirname, 'src')
15+
}]
16+
},
17+
resolve: {
18+
root: path.resolve('/src'),
19+
modules: ['node_modules']
20+
},
21+
plugins: [
22+
new webpack.optimize.UglifyJsPlugin({
23+
sourceMap: true
24+
}),
25+
new webpack.optimize.OccurrenceOrderPlugin()
26+
],
27+
debug: true
28+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
"use strict";
2+
3+
const defineTest = require("@webpack-cli/utils/defineTest").default;
4+
5+
defineTest(__dirname, "uglifyJsPlugin", "uglifyJsPlugin-0");
6+
defineTest(__dirname, "uglifyJsPlugin", "uglifyJsPlugin-1");
7+
defineTest(__dirname, "uglifyJsPlugin", "uglifyJsPlugin-2");
8+
defineTest(__dirname, "uglifyJsPlugin", "uglifyJsPlugin-3");
9+
defineTest(__dirname, "uglifyJsPlugin", "uglifyJsPlugin-4");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
import {
2+
createProperty,
3+
findPluginsArrayAndRemoveIfEmpty,
4+
findPluginsByName,
5+
getRequire,
6+
safeTraverse,
7+
} from "@webpack-cli/utils/ast-utils";
8+
9+
import { IJSCodeshift, INode } from "../types/NodePath";
10+
11+
/**
12+
*
13+
* Transform which:
14+
* Removes UglifyWebpackPlugin from plugins array, if no options is passed to the plugin.
15+
* and adds `optimization.minimize: true` to config
16+
*
17+
* If any configuration is passed to UglifyWebpackPlugin
18+
* plugin instantiation is moved to `optimization.minimizer`.
19+
*
20+
* @param {Object} j - jscodeshift top-level import
21+
* @param {Node} ast - jscodeshift ast to transform
22+
* @returns {Node} ast - jscodeshift ast
23+
*/
24+
25+
export default function(j: IJSCodeshift, ast: INode): INode {
26+
27+
let pluginVariableAssignment: string = null;
28+
29+
const searchForRequirePlugin: INode = ast
30+
.find(j.VariableDeclarator)
31+
.filter(
32+
j.filters.VariableDeclarator.requiresModule("uglifyjs-webpack-plugin"),
33+
);
34+
35+
/**
36+
* Look for a variable declaration which requires uglifyjs-webpack-plugin
37+
* saves the name of this variable.
38+
*/
39+
searchForRequirePlugin.forEach((node: INode): void => {
40+
pluginVariableAssignment = node.value.id.name;
41+
});
42+
43+
pluginVariableAssignment = !pluginVariableAssignment
44+
? "webpack.optimize.UglifyJsPlugin"
45+
: pluginVariableAssignment;
46+
47+
findPluginsByName(j, ast, [pluginVariableAssignment])
48+
.forEach((node: INode): void => {
49+
let expressionContent: object = null;
50+
51+
const configBody: INode = safeTraverse(node, ["parent", "parent", "parent"]);
52+
53+
// options passed to plugin
54+
const pluginOptions: INode[] = node.value.arguments;
55+
56+
/**
57+
* check if there are any options passed to UglifyWebpackPlugin
58+
* If so, they are moved to optimization.minimizer.
59+
* Otherwise, rely on default options
60+
*/
61+
if (pluginOptions.length) {
62+
/*
63+
* If user is using UglifyJsPlugin directly from webpack
64+
* transformation must:
65+
* - remove it
66+
* - add require for uglify-webpack-plugin
67+
* - add to minimizer
68+
*/
69+
if (pluginVariableAssignment && pluginVariableAssignment.includes("webpack")) {
70+
// create require for uglify-webpack-plugin
71+
const pathRequire: INode = getRequire(
72+
j,
73+
"UglifyJsPlugin",
74+
"uglifyjs-webpack-plugin",
75+
);
76+
// append to source code.
77+
ast
78+
.find(j.Program)
79+
.replaceWith((p: INode): INode =>
80+
j.program([].concat(pathRequire).concat(p.value.body)),
81+
);
82+
83+
expressionContent = j.property(
84+
"init",
85+
j.identifier("minimizer"),
86+
j.arrayExpression([
87+
j.newExpression(j.identifier("UglifyJsPlugin"), [pluginOptions[0]]),
88+
]),
89+
);
90+
} else {
91+
expressionContent = j.property(
92+
"init",
93+
j.identifier("minimizer"),
94+
j.arrayExpression([node.value]),
95+
);
96+
}
97+
} else {
98+
searchForRequirePlugin.forEach((n: INode): void => j(n).remove());
99+
}
100+
101+
const minimizeProperty = createProperty(j, "minimize", "true");
102+
// creates optimization property at the body of the config.
103+
if (expressionContent) {
104+
configBody.value.properties.push(
105+
j.property(
106+
"init",
107+
j.identifier("optimization"),
108+
j.objectExpression([minimizeProperty, expressionContent]),
109+
),
110+
);
111+
} else {
112+
configBody.value.properties.push(
113+
j.property(
114+
"init",
115+
j.identifier("optimization"),
116+
j.objectExpression([minimizeProperty]),
117+
),
118+
);
119+
}
120+
121+
// remove the old Uglify plugin from Plugins array.
122+
j(node).remove();
123+
});
124+
125+
findPluginsArrayAndRemoveIfEmpty(j, ast);
126+
127+
return ast;
128+
}

‎packages/utils/__tests__/ast-utils.test.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -35,28 +35,28 @@ describe("utils", () => {
3535
describe("findPluginsByName", () => {
3636
it("should find plugins in AST", () => {
3737
const ast = j(`
38-
{ foo: new TerserPlugin() }
38+
{ foo: new webpack.optimize.UglifyJsPlugin() }
3939
`);
40-
const res = utils.findPluginsByName(j, ast, ["TerserPlugin"]);
40+
const res = utils.findPluginsByName(j, ast, ["webpack.optimize.UglifyJsPlugin"]);
4141
expect(res.size()).toEqual(1);
4242
});
4343

4444
it("should find all plugins in AST", () => {
4545
const ast = j(`
4646
[
47-
new TerserPlugin(),
47+
new UglifyJsPlugin(),
4848
new TestPlugin()
4949
]
5050
`);
51-
const res = utils.findPluginsByName(j, ast, ["TerserPlugin", "TestPlugin"]);
51+
const res = utils.findPluginsByName(j, ast, ["UglifyJsPlugin", "TestPlugin"]);
5252
expect(res.size()).toEqual(2);
5353
});
5454

5555
it("should not find false positives", () => {
5656
const ast = j(`
57-
{ foo: new webpack.optimize.TerserPlugin() }
57+
{ foo: new UglifyJsPlugin() }
5858
`);
59-
const res = utils.findPluginsByName(j, ast, ["TerserPlugin"]);
59+
const res = utils.findPluginsByName(j, ast, ["webpack.optimize.UglifyJsPlugin"]);
6060
expect(res.size()).toEqual(0);
6161
});
6262
});

0 commit comments

Comments
 (0)
Please sign in to comment.