Skip to content

Commit

Permalink
fix: add safe traverse to loaderoptionsplugin (#77)
Browse files Browse the repository at this point in the history
* fix: add safe traverse to loaderoptionsplugin

* test: write a failing test case
  • Loading branch information
Pavithra Kodmad authored Mar 1, 2017
1 parent 68a2dfd commit 4020043
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,24 @@ module.exports = {
}
"
`;

exports[`loaderOptionsPlugin transforms correctly using "loaderOptionsPlugin-3" data 1`] = `
"// Don't modify LoaderOptionsPlugin
const ExtractTextPlugin = require('extract-text-webpack-plugin');
module.exports = {
entry: ['./index.js'],
output: {
filename: 'bundle.js'
},
module: {
rules: [{
test: /\\\\.css$/,
use: ExtractTextPlugin.extract([
'css-loader'
])
}]
},
}
"
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Don't modify LoaderOptionsPlugin

const ExtractTextPlugin = require('extract-text-webpack-plugin');
module.exports = {
entry: ['./index.js'],
output: {
filename: 'bundle.js'
},
module: {
rules: [{
test: /\.css$/,
use: ExtractTextPlugin.extract([
'css-loader'
])
}]
},
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const isEmpty = require('lodash/isEmpty');
const findPluginsByName = require('../utils').findPluginsByName;
const createOrUpdatePluginByName = require('../utils').createOrUpdatePluginByName;
const safeTraverse = require('../utils').safeTraverse;

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

return ast
.find(j.ArrayExpression)
.filter(path => path.parent.value.key.name === 'plugins')
.filter(path => safeTraverse(path, ['parent', 'value', 'key', 'name']) === 'plugins')
.forEach(path => {
!isEmpty(loaderOptions) &&
createOrUpdatePluginByName(j, path, 'webpack.optimize.LoaderOptionsPlugin', loaderOptions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ const defineTest = require('../defineTest');
defineTest(__dirname, 'loaderOptionsPlugin', 'loaderOptionsPlugin-0');
defineTest(__dirname, 'loaderOptionsPlugin', 'loaderOptionsPlugin-1');
defineTest(__dirname, 'loaderOptionsPlugin', 'loaderOptionsPlugin-2');
defineTest(__dirname, 'loaderOptionsPlugin', 'loaderOptionsPlugin-3');

0 comments on commit 4020043

Please sign in to comment.