/
uglifyJsPlugin.ts
132 lines (115 loc) · 3.52 KB
/
uglifyJsPlugin.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import {
createProperty,
findPluginsArrayAndRemoveIfEmpty,
findPluginsByName,
getRequire,
safeTraverse,
} from "@webpack-cli/utils/ast-utils";
import { IJSCodeshift, INode } from "../types/NodePath";
/**
*
* Transform which:
* Removes UglifyWebpackPlugin from plugins array, if no options is passed to the plugin.
* and adds `optimization.minimize: true` to config
*
* If any configuration is passed to UglifyWebpackPlugin
* UglifyWebpackPlugin is replaced with TerserPlugin
* and plugin instantiation is moved to `optimization.minimizer`.
*
* @param {Object} j - jscodeshift top-level import
* @param {Node} ast - jscodeshift ast to transform
* @returns {Node} ast - jscodeshift ast
*/
export default function(j: IJSCodeshift, ast: INode): INode {
let pluginVariableAssignment: string = null;
const searchForRequirePlugin: INode = ast
.find(j.VariableDeclarator)
.filter(
j.filters.VariableDeclarator.requiresModule("uglifyjs-webpack-plugin"),
);
/**
* Look for a variable declaration which requires uglifyjs-webpack-plugin
* saves the name of this variable.
*/
searchForRequirePlugin.forEach((node: INode): void => {
pluginVariableAssignment = node.value.id.name;
});
pluginVariableAssignment = !pluginVariableAssignment
? "webpack.optimize.UglifyJsPlugin"
: pluginVariableAssignment;
findPluginsByName(j, ast, [pluginVariableAssignment])
.forEach((node: INode): void => {
let expressionContent: object = null;
const configBody: INode = safeTraverse(node, ["parent", "parent", "parent"]);
// options passed to plugin
const pluginOptions: INode[] = node.value.arguments;
/**
* check if there are any options passed to UglifyWebpackPlugin
* If so, they are moved to optimization.minimizer.
* Otherwise, rely on default options
*/
if (pluginOptions.length) {
/*
* If user is using UglifyJsPlugin directly from webpack
* transformation must:
* - remove it
* - add require for terser-webpack-plugin
* - add to minimizer
*/
if (pluginVariableAssignment) {
// remove require for uglify-webpack-plugin
searchForRequirePlugin.remove();
// create require for terser-webpack-plugin
const pathRequire: INode = getRequire(
j,
"TerserPlugin",
"terser-webpack-plugin",
);
// prepend to source code.
ast
.find(j.Program)
.replaceWith((p: INode): INode =>
j.program([].concat(pathRequire).concat(p.value.body)),
);
expressionContent = j.property(
"init",
j.identifier("minimizer"),
j.arrayExpression([
j.newExpression(j.identifier("TerserPlugin"), [pluginOptions[0]]),
]),
);
} else {
expressionContent = j.property(
"init",
j.identifier("minimizer"),
j.arrayExpression([node.value]),
);
}
} else {
searchForRequirePlugin.forEach((n: INode): void => j(n).remove());
}
const minimizeProperty = createProperty(j, "minimize", "true");
// creates optimization property at the body of the config.
if (expressionContent) {
configBody.value.properties.push(
j.property(
"init",
j.identifier("optimization"),
j.objectExpression([minimizeProperty, expressionContent]),
),
);
} else {
configBody.value.properties.push(
j.property(
"init",
j.identifier("optimization"),
j.objectExpression([minimizeProperty]),
),
);
}
// remove the old Uglify plugin from Plugins array.
j(node).remove();
});
findPluginsArrayAndRemoveIfEmpty(j, ast);
return ast;
}