Skip to content

Commit b9d22c9

Browse files
authored
fix: Do not create LoaderOptionsPlugin if loaderOptions is empty (#72)
1 parent a46edbb commit b9d22c9

7 files changed

Lines changed: 45 additions & 2 deletions

File tree

lib/transformations/loaderOptionsPlugin/__snapshots__/loaderOptionsPlugin.test.js.snap

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
1-
exports[`loaderOptionsPlugin transforms correctly 1`] = `
1+
exports[`loaderOptionsPlugin transforms correctly using "loaderOptionsPlugin-0" data 1`] = `
2+
"// Do not create LoaderOptionsPlugin is not necessary
3+
module.exports = {
4+
plugins: [
5+
new SomePlugin()
6+
]
7+
}
8+
"
9+
`;
10+
11+
exports[`loaderOptionsPlugin transforms correctly using "loaderOptionsPlugin-1" data 1`] = `
212
"module.exports = {
313
debug: true,
414
plugins: [
@@ -12,3 +22,16 @@ exports[`loaderOptionsPlugin transforms correctly 1`] = `
1222
}
1323
"
1424
`;
25+
26+
exports[`loaderOptionsPlugin transforms correctly using "loaderOptionsPlugin-2" data 1`] = `
27+
"// Don\'t modify LoaderOptionsPlugin
28+
module.exports = {
29+
plugins: [
30+
new SomePlugin(),
31+
new webpack.optimize.LoaderOptionsPlugin({
32+
foo: \'bar\'
33+
})
34+
]
35+
}
36+
"
37+
`;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Do not create LoaderOptionsPlugin is not necessary
2+
module.exports = {
3+
plugins: [
4+
new SomePlugin()
5+
]
6+
}

lib/transformations/loaderOptionsPlugin/__testfixtures__/loaderOptionsPlugin.input.js renamed to lib/transformations/loaderOptionsPlugin/__testfixtures__/loaderOptionsPlugin-1.input.js

File renamed without changes.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Don't modify LoaderOptionsPlugin
2+
module.exports = {
3+
plugins: [
4+
new SomePlugin(),
5+
new webpack.optimize.LoaderOptionsPlugin({
6+
foo: 'bar'
7+
})
8+
]
9+
}

lib/transformations/loaderOptionsPlugin/loaderOptionsPlugin.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const isEmpty = require('lodash/isEmpty');
12
const findPluginsByName = require('../utils').findPluginsByName;
23
const createOrUpdatePluginByName = require('../utils').createOrUpdatePluginByName;
34

@@ -20,6 +21,7 @@ module.exports = function(j, ast) {
2021
.find(j.ArrayExpression)
2122
.filter(path => path.parent.value.key.name === 'plugins')
2223
.forEach(path => {
24+
!isEmpty(loaderOptions) &&
2325
createOrUpdatePluginByName(j, path, 'webpack.optimize.LoaderOptionsPlugin', loaderOptions);
2426
});
2527
};
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
const defineTest = require('../defineTest');
22

3-
defineTest(__dirname, 'loaderOptionsPlugin');
3+
defineTest(__dirname, 'loaderOptionsPlugin', 'loaderOptionsPlugin-0');
4+
defineTest(__dirname, 'loaderOptionsPlugin', 'loaderOptionsPlugin-1');
5+
defineTest(__dirname, 'loaderOptionsPlugin', 'loaderOptionsPlugin-2');

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"interpret": "^1.0.1",
3939
"jscodeshift": "^0.3.30",
4040
"loader-utils": "^0.2.16",
41+
"lodash": "^4.17.4",
4142
"recast": "git://github.com/kalcifer/recast.git#bug/allowbreak",
4243
"rx": "^4.1.0",
4344
"supports-color": "^3.1.2",

0 commit comments

Comments
 (0)