From b9d22c9fe80a4382f2fd1bd98aa17cc0ddac7778 Mon Sep 17 00:00:00 2001 From: Andrey Okonetchnikov Date: Fri, 24 Feb 2017 09:39:52 +0100 Subject: [PATCH] fix: Do not create LoaderOptionsPlugin if loaderOptions is empty (#72) --- .../loaderOptionsPlugin.test.js.snap | 25 ++++++++++++++++++- .../loaderOptionsPlugin-0.input.js | 6 +++++ ...nput.js => loaderOptionsPlugin-1.input.js} | 0 .../loaderOptionsPlugin-2.input.js | 9 +++++++ .../loaderOptionsPlugin.js | 2 ++ .../loaderOptionsPlugin.test.js | 4 ++- package.json | 1 + 7 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 lib/transformations/loaderOptionsPlugin/__testfixtures__/loaderOptionsPlugin-0.input.js rename lib/transformations/loaderOptionsPlugin/__testfixtures__/{loaderOptionsPlugin.input.js => loaderOptionsPlugin-1.input.js} (100%) create mode 100644 lib/transformations/loaderOptionsPlugin/__testfixtures__/loaderOptionsPlugin-2.input.js diff --git a/lib/transformations/loaderOptionsPlugin/__snapshots__/loaderOptionsPlugin.test.js.snap b/lib/transformations/loaderOptionsPlugin/__snapshots__/loaderOptionsPlugin.test.js.snap index db38d48df07..5b48ac421b8 100644 --- a/lib/transformations/loaderOptionsPlugin/__snapshots__/loaderOptionsPlugin.test.js.snap +++ b/lib/transformations/loaderOptionsPlugin/__snapshots__/loaderOptionsPlugin.test.js.snap @@ -1,4 +1,14 @@ -exports[`loaderOptionsPlugin transforms correctly 1`] = ` +exports[`loaderOptionsPlugin transforms correctly using "loaderOptionsPlugin-0" data 1`] = ` +"// Do not create LoaderOptionsPlugin is not necessary +module.exports = { + plugins: [ + new SomePlugin() + ] +} +" +`; + +exports[`loaderOptionsPlugin transforms correctly using "loaderOptionsPlugin-1" data 1`] = ` "module.exports = { debug: true, plugins: [ @@ -12,3 +22,16 @@ exports[`loaderOptionsPlugin transforms correctly 1`] = ` } " `; + +exports[`loaderOptionsPlugin transforms correctly using "loaderOptionsPlugin-2" data 1`] = ` +"// Don\'t modify LoaderOptionsPlugin +module.exports = { + plugins: [ + new SomePlugin(), + new webpack.optimize.LoaderOptionsPlugin({ + foo: \'bar\' + }) + ] +} +" +`; diff --git a/lib/transformations/loaderOptionsPlugin/__testfixtures__/loaderOptionsPlugin-0.input.js b/lib/transformations/loaderOptionsPlugin/__testfixtures__/loaderOptionsPlugin-0.input.js new file mode 100644 index 00000000000..e809d6a36a9 --- /dev/null +++ b/lib/transformations/loaderOptionsPlugin/__testfixtures__/loaderOptionsPlugin-0.input.js @@ -0,0 +1,6 @@ +// Do not create LoaderOptionsPlugin is not necessary +module.exports = { + plugins: [ + new SomePlugin() + ] +} diff --git a/lib/transformations/loaderOptionsPlugin/__testfixtures__/loaderOptionsPlugin.input.js b/lib/transformations/loaderOptionsPlugin/__testfixtures__/loaderOptionsPlugin-1.input.js similarity index 100% rename from lib/transformations/loaderOptionsPlugin/__testfixtures__/loaderOptionsPlugin.input.js rename to lib/transformations/loaderOptionsPlugin/__testfixtures__/loaderOptionsPlugin-1.input.js diff --git a/lib/transformations/loaderOptionsPlugin/__testfixtures__/loaderOptionsPlugin-2.input.js b/lib/transformations/loaderOptionsPlugin/__testfixtures__/loaderOptionsPlugin-2.input.js new file mode 100644 index 00000000000..94fad11f784 --- /dev/null +++ b/lib/transformations/loaderOptionsPlugin/__testfixtures__/loaderOptionsPlugin-2.input.js @@ -0,0 +1,9 @@ +// Don't modify LoaderOptionsPlugin +module.exports = { + plugins: [ + new SomePlugin(), + new webpack.optimize.LoaderOptionsPlugin({ + foo: 'bar' + }) + ] +} diff --git a/lib/transformations/loaderOptionsPlugin/loaderOptionsPlugin.js b/lib/transformations/loaderOptionsPlugin/loaderOptionsPlugin.js index 910f19f3bb4..8e9835fe451 100644 --- a/lib/transformations/loaderOptionsPlugin/loaderOptionsPlugin.js +++ b/lib/transformations/loaderOptionsPlugin/loaderOptionsPlugin.js @@ -1,3 +1,4 @@ +const isEmpty = require('lodash/isEmpty'); const findPluginsByName = require('../utils').findPluginsByName; const createOrUpdatePluginByName = require('../utils').createOrUpdatePluginByName; @@ -20,6 +21,7 @@ module.exports = function(j, ast) { .find(j.ArrayExpression) .filter(path => path.parent.value.key.name === 'plugins') .forEach(path => { + !isEmpty(loaderOptions) && createOrUpdatePluginByName(j, path, 'webpack.optimize.LoaderOptionsPlugin', loaderOptions); }); }; diff --git a/lib/transformations/loaderOptionsPlugin/loaderOptionsPlugin.test.js b/lib/transformations/loaderOptionsPlugin/loaderOptionsPlugin.test.js index d0ef4fbd923..a69f6871fd5 100644 --- a/lib/transformations/loaderOptionsPlugin/loaderOptionsPlugin.test.js +++ b/lib/transformations/loaderOptionsPlugin/loaderOptionsPlugin.test.js @@ -1,3 +1,5 @@ const defineTest = require('../defineTest'); -defineTest(__dirname, 'loaderOptionsPlugin'); +defineTest(__dirname, 'loaderOptionsPlugin', 'loaderOptionsPlugin-0'); +defineTest(__dirname, 'loaderOptionsPlugin', 'loaderOptionsPlugin-1'); +defineTest(__dirname, 'loaderOptionsPlugin', 'loaderOptionsPlugin-2'); diff --git a/package.json b/package.json index eb79c907ccd..a71443e27be 100644 --- a/package.json +++ b/package.json @@ -38,6 +38,7 @@ "interpret": "^1.0.1", "jscodeshift": "^0.3.30", "loader-utils": "^0.2.16", + "lodash": "^4.17.4", "recast": "git://github.com/kalcifer/recast.git#bug/allowbreak", "rx": "^4.1.0", "supports-color": "^3.1.2",