Skip to content

Commit 4020043

Browse files
author
Pavithra Kodmad
authored
fix: add safe traverse to loaderoptionsplugin (#77)
* fix: add safe traverse to loaderoptionsplugin * test: write a failing test case
1 parent 68a2dfd commit 4020043

File tree

4 files changed

+41
-1
lines changed

4 files changed

+41
-1
lines changed

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,24 @@ module.exports = {
3737
}
3838
"
3939
`;
40+
41+
exports[`loaderOptionsPlugin transforms correctly using "loaderOptionsPlugin-3" data 1`] = `
42+
"// Don't modify LoaderOptionsPlugin
43+
44+
const ExtractTextPlugin = require('extract-text-webpack-plugin');
45+
module.exports = {
46+
entry: ['./index.js'],
47+
output: {
48+
filename: 'bundle.js'
49+
},
50+
module: {
51+
rules: [{
52+
test: /\\\\.css$/,
53+
use: ExtractTextPlugin.extract([
54+
'css-loader'
55+
])
56+
}]
57+
},
58+
}
59+
"
60+
`;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Don't modify LoaderOptionsPlugin
2+
3+
const ExtractTextPlugin = require('extract-text-webpack-plugin');
4+
module.exports = {
5+
entry: ['./index.js'],
6+
output: {
7+
filename: 'bundle.js'
8+
},
9+
module: {
10+
rules: [{
11+
test: /\.css$/,
12+
use: ExtractTextPlugin.extract([
13+
'css-loader'
14+
])
15+
}]
16+
},
17+
}

lib/transformations/loaderOptionsPlugin/loaderOptionsPlugin.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const isEmpty = require('lodash/isEmpty');
22
const findPluginsByName = require('../utils').findPluginsByName;
33
const createOrUpdatePluginByName = require('../utils').createOrUpdatePluginByName;
4+
const safeTraverse = require('../utils').safeTraverse;
45

56
module.exports = function(j, ast) {
67
const loaderOptions = {};
@@ -19,7 +20,7 @@ module.exports = function(j, ast) {
1920

2021
return ast
2122
.find(j.ArrayExpression)
22-
.filter(path => path.parent.value.key.name === 'plugins')
23+
.filter(path => safeTraverse(path, ['parent', 'value', 'key', 'name']) === 'plugins')
2324
.forEach(path => {
2425
!isEmpty(loaderOptions) &&
2526
createOrUpdatePluginByName(j, path, 'webpack.optimize.LoaderOptionsPlugin', loaderOptions);

lib/transformations/loaderOptionsPlugin/loaderOptionsPlugin.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ const defineTest = require('../defineTest');
33
defineTest(__dirname, 'loaderOptionsPlugin', 'loaderOptionsPlugin-0');
44
defineTest(__dirname, 'loaderOptionsPlugin', 'loaderOptionsPlugin-1');
55
defineTest(__dirname, 'loaderOptionsPlugin', 'loaderOptionsPlugin-2');
6+
defineTest(__dirname, 'loaderOptionsPlugin', 'loaderOptionsPlugin-3');

0 commit comments

Comments
 (0)