Skip to content

Commit ef23fec

Browse files
abenezerabebesendilkumarn
authored andcommitted
fix: replace lookups for TerserPlugin in webpack.optimise
replace lookups for TerserPlugin in webpack.optimise with requiring terser-webpack-plugin
1 parent ce9fbc8 commit ef23fec

8 files changed

Lines changed: 57 additions & 35 deletions

File tree

package-lock.json

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/migrate/__testfixtures__/failing.js

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

56
const config = {
67
entry: {
@@ -43,7 +44,7 @@ const config = {
4344

4445
switch (nodeEnvironment) {
4546
case "production":
46-
config.plugins.push(new webpack.optimize.TerserPlugin());
47+
config.plugins.push(new TerserPlugin());
4748
case "preproduction":
4849
config.output.path = __dirname + "/dist";
4950
config.plugins.push(new webpack.optimize.DedupePlugin());

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

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ module.exports = {
2222
modules: ['node_modules']
2323
},
2424
plugins: [
25-
new webpack.optimize.TerserPlugin(),
25+
new TerserPlugin(),
2626
new webpack.optimize.OccurrenceOrderPlugin()
2727
],
2828
debug: true
@@ -50,7 +50,7 @@ module.exports = {
5050
modules: ['node_modules']
5151
},
5252
plugins: [
53-
new webpack.optimize.TerserPlugin(),
53+
new TerserPlugin(),
5454
new webpack.optimize.OccurrenceOrderPlugin()
5555
],
5656
debug: true
@@ -61,15 +61,18 @@ module.exports = {
6161
exports[`transform should respect recast options 1`] = `
6262
"
6363
module.exports = {
64-
devtool: 'eval',
65-
entry: [
64+
devtool: 'eval',
65+
66+
entry: [
6667
'./src/index'
6768
],
68-
output: {
69+
70+
output: {
6971
path: path.join(__dirname, 'dist'),
7072
filename: 'index.js'
7173
},
72-
module: {
74+
75+
module: {
7376
rules: [{
7477
test: /.js$/,
7578
use: [{
@@ -78,15 +81,18 @@ module.exports = {
7881
include: path.join(__dirname, 'src')
7982
}]
8083
},
81-
resolve: {
84+
85+
resolve: {
8286
modules: ['node_modules', path.resolve('/src')],
8387
},
84-
plugins: [new webpack.LoaderOptionsPlugin({
85-
debug: true,
86-
})],
87-
optimization: {
88-
minimize: true,
89-
}
88+
89+
plugins: [
90+
new TerserPlugin(),
91+
new webpack.LoaderOptionsPlugin({
92+
debug: true,
93+
minimize: true,
94+
})
95+
],
9096
};
9197
"
9298
`;
@@ -116,7 +122,7 @@ module.exports = {
116122
modules: ['node_modules']
117123
},
118124
plugins: [
119-
new webpack.optimize.TerserPlugin(),
125+
new TerserPlugin(),
120126
new webpack.optimize.OccurrenceOrderPlugin()
121127
],
122128
debug: true
@@ -127,15 +133,18 @@ module.exports = {
127133
exports[`transform should transform using all transformations 1`] = `
128134
"
129135
module.exports = {
130-
devtool: 'eval',
131-
entry: [
136+
devtool: 'eval',
137+
138+
entry: [
132139
'./src/index'
133140
],
134-
output: {
141+
142+
output: {
135143
path: path.join(__dirname, 'dist'),
136144
filename: 'index.js'
137145
},
138-
module: {
146+
147+
module: {
139148
rules: [{
140149
test: /.js$/,
141150
use: [{
@@ -144,15 +153,18 @@ module.exports = {
144153
include: path.join(__dirname, 'src')
145154
}]
146155
},
147-
resolve: {
156+
157+
resolve: {
148158
modules: ['node_modules', path.resolve('/src')]
149159
},
150-
plugins: [new webpack.LoaderOptionsPlugin({
151-
debug: true
152-
})],
153-
optimization: {
154-
minimize: true
155-
}
160+
161+
plugins: [
162+
new TerserPlugin(),
163+
new webpack.LoaderOptionsPlugin({
164+
debug: true,
165+
minimize: true
166+
})
167+
]
156168
};
157169
"
158170
`;

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ module.exports = {
1111
`;
1212

1313
exports[`loaderOptionsPlugin transforms correctly using "loaderOptionsPlugin-1" data 1`] = `
14-
"module.exports = {
14+
"const TerserPlugin = require(\\"terser-webpack-plugin\\");
15+
module.exports = {
1516
plugins: [
16-
new webpack.optimize.TerserPlugin(),
17+
new TerserPlugin(),
1718
new webpack.LoaderOptionsPlugin({
1819
foo: 'bar',
1920
debug: true,

packages/migrate/loaderOptionsPlugin/__tests__/__testfixtures__/loaderOptionsPlugin-1.input.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
const TerserPlugin = require("terser-webpack-plugin");
12
module.exports = {
23
debug: true,
34
plugins: [
4-
new webpack.optimize.TerserPlugin(),
5+
new TerserPlugin(),
56
new webpack.LoaderOptionsPlugin({
67
foo: 'bar'
78
})

packages/migrate/loaderOptionsPlugin/loaderOptionsPlugin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export default function(j: IJSCodeshift, ast: INode): INode {
3838
}
3939

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

packages/migrate/terserPlugin/__testfixtures__/terserPlugin-3.input.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const TerserPlugin = require("terser-webpack-plugin");
12
module.exports = {
23
devtool: "eval",
34
entry: ["./src/index"],
@@ -18,6 +19,6 @@ module.exports = {
1819
root: path.resolve("/src"),
1920
modules: ["node_modules"]
2021
},
21-
plugins: [new webpack.optimize.TerserPlugin(), new webpack.optimize.OccurrenceOrderPlugin()],
22+
plugins: [new TerserPlugin(), new webpack.optimize.OccurrenceOrderPlugin()],
2223
debug: true
2324
};

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

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

@@ -54,9 +54,9 @@ describe("utils", () => {
5454

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

0 commit comments

Comments
 (0)