Skip to content

Commit

Permalink
fix: replace lookups for TerserPlugin in webpack.optimise
Browse files Browse the repository at this point in the history
replace lookups for TerserPlugin in webpack.optimise with requiring terser-webpack-plugin
  • Loading branch information
abenezerabebe authored and sendilkumarn committed Mar 16, 2019
1 parent ce9fbc8 commit ef23fec
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 35 deletions.
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion packages/migrate/__testfixtures__/failing.js
@@ -1,6 +1,7 @@
const webpack = require('webpack');
const nodeEnvironment = process.env.NODE_ENV;
const _ = require("lodash");
const TerserPlugin = require('terser-webpack-plugin');

const config = {
entry: {
Expand Down Expand Up @@ -43,7 +44,7 @@ const config = {

switch (nodeEnvironment) {
case "production":
config.plugins.push(new webpack.optimize.TerserPlugin());
config.plugins.push(new TerserPlugin());
case "preproduction":
config.output.path = __dirname + "/dist";
config.plugins.push(new webpack.optimize.DedupePlugin());
Expand Down
62 changes: 37 additions & 25 deletions packages/migrate/__tests__/__snapshots__/migrate.test.ts.snap
Expand Up @@ -22,7 +22,7 @@ module.exports = {
modules: ['node_modules']
},
plugins: [
new webpack.optimize.TerserPlugin(),
new TerserPlugin(),
new webpack.optimize.OccurrenceOrderPlugin()
],
debug: true
Expand Down Expand Up @@ -50,7 +50,7 @@ module.exports = {
modules: ['node_modules']
},
plugins: [
new webpack.optimize.TerserPlugin(),
new TerserPlugin(),
new webpack.optimize.OccurrenceOrderPlugin()
],
debug: true
Expand All @@ -61,15 +61,18 @@ module.exports = {
exports[`transform should respect recast options 1`] = `
"
module.exports = {
devtool: 'eval',
entry: [
devtool: 'eval',
entry: [
'./src/index'
],
output: {
output: {
path: path.join(__dirname, 'dist'),
filename: 'index.js'
},
module: {
module: {
rules: [{
test: /.js$/,
use: [{
Expand All @@ -78,15 +81,18 @@ module.exports = {
include: path.join(__dirname, 'src')
}]
},
resolve: {
resolve: {
modules: ['node_modules', path.resolve('/src')],
},
plugins: [new webpack.LoaderOptionsPlugin({
debug: true,
})],
optimization: {
minimize: true,
}
plugins: [
new TerserPlugin(),
new webpack.LoaderOptionsPlugin({
debug: true,
minimize: true,
})
],
};
"
`;
Expand Down Expand Up @@ -116,7 +122,7 @@ module.exports = {
modules: ['node_modules']
},
plugins: [
new webpack.optimize.TerserPlugin(),
new TerserPlugin(),
new webpack.optimize.OccurrenceOrderPlugin()
],
debug: true
Expand All @@ -127,15 +133,18 @@ module.exports = {
exports[`transform should transform using all transformations 1`] = `
"
module.exports = {
devtool: 'eval',
entry: [
devtool: 'eval',
entry: [
'./src/index'
],
output: {
output: {
path: path.join(__dirname, 'dist'),
filename: 'index.js'
},
module: {
module: {
rules: [{
test: /.js$/,
use: [{
Expand All @@ -144,15 +153,18 @@ module.exports = {
include: path.join(__dirname, 'src')
}]
},
resolve: {
resolve: {
modules: ['node_modules', path.resolve('/src')]
},
plugins: [new webpack.LoaderOptionsPlugin({
debug: true
})],
optimization: {
minimize: true
}
plugins: [
new TerserPlugin(),
new webpack.LoaderOptionsPlugin({
debug: true,
minimize: true
})
]
};
"
`;
Expand Up @@ -11,9 +11,10 @@ module.exports = {
`;

exports[`loaderOptionsPlugin transforms correctly using "loaderOptionsPlugin-1" data 1`] = `
"module.exports = {
"const TerserPlugin = require(\\"terser-webpack-plugin\\");
module.exports = {
plugins: [
new webpack.optimize.TerserPlugin(),
new TerserPlugin(),
new webpack.LoaderOptionsPlugin({
foo: 'bar',
debug: true,
Expand Down
@@ -1,7 +1,8 @@
const TerserPlugin = require("terser-webpack-plugin");
module.exports = {
debug: true,
plugins: [
new webpack.optimize.TerserPlugin(),
new TerserPlugin(),
new webpack.LoaderOptionsPlugin({
foo: 'bar'
})
Expand Down
Expand Up @@ -38,7 +38,7 @@ export default function(j: IJSCodeshift, ast: INode): INode {
}

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

Expand Down
@@ -1,3 +1,4 @@
const TerserPlugin = require("terser-webpack-plugin");
module.exports = {
devtool: "eval",
entry: ["./src/index"],
Expand All @@ -18,6 +19,6 @@ module.exports = {
root: path.resolve("/src"),
modules: ["node_modules"]
},
plugins: [new webpack.optimize.TerserPlugin(), new webpack.optimize.OccurrenceOrderPlugin()],
plugins: [new TerserPlugin(), new webpack.optimize.OccurrenceOrderPlugin()],
debug: true
};
8 changes: 4 additions & 4 deletions packages/utils/__tests__/ast-utils.test.ts
Expand Up @@ -35,9 +35,9 @@ describe("utils", () => {
describe("findPluginsByName", () => {
it("should find plugins in AST", () => {
const ast = j(`
{ foo: new webpack.optimize.TerserPlugin() }
{ foo: new TerserPlugin() }
`);
const res = utils.findPluginsByName(j, ast, ["webpack.optimize.TerserPlugin"]);
const res = utils.findPluginsByName(j, ast, ["TerserPlugin"]);
expect(res.size()).toEqual(1);
});

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

it("should not find false positives", () => {
const ast = j(`
{ foo: new TerserPlugin() }
{ foo: new webpack.optimize.TerserPlugin() }
`);
const res = utils.findPluginsByName(j, ast, ["webpack.optimize.TerserPlugin"]);
const res = utils.findPluginsByName(j, ast, ["TerserPlugin"]);
expect(res.size()).toEqual(0);
});
});
Expand Down

0 comments on commit ef23fec

Please sign in to comment.