-
-
Notifications
You must be signed in to change notification settings - Fork 602
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix: Only try to remove deprecated plugins from array expressions (#91)
* fix: Guard new exepressions outside of arrays For now only plugins instantiated in an array can be removed since this is safe. All other usages are considered not safe and thus won't be modified. - Display a message about the current support Closes #83 * style: Better error messages - Pass the source to the transform function - This allows displaying code snippets in error messages * fix: Use console.log to fix colors and use better error copy. - Don't use `console.error` to fix colors. - Use chalk instead to make output more prettier. - Better error message with an example of the expected usage.
- Loading branch information
Showing
7 changed files
with
57 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
...ansformations/removeDeprecatedPlugins/__testfixtures__/removeDeprecatedPlugins-3.input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// This should throw | ||
export default (config) => { | ||
config.plugins.push(new webpack.optimize.UglifyJsPlugin()); | ||
config.plugins.push(new webpack.optimize.DedupePlugin()); | ||
config.plugins.push(new webpack.optimize.OccurrenceOrderPlugin()); | ||
return config | ||
} |
43 changes: 35 additions & 8 deletions
43
lib/transformations/removeDeprecatedPlugins/removeDeprecatedPlugins.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,48 @@ | ||
const findPluginsByName = require('../utils').findPluginsByName; | ||
const codeFrame = require('babel-code-frame'); | ||
const chalk = require('chalk'); | ||
const utils = require('../utils'); | ||
|
||
module.exports = function(j, ast) { | ||
const example = `plugins: [ | ||
new webpack.optimize.OccurrenceOrderPlugin(), | ||
new webpack.optimize.UglifyJsPlugin(), | ||
new webpack.optimize.DedupePlugin() | ||
]`; | ||
|
||
module.exports = function(j, ast, source) { | ||
// List of deprecated plugins to remove | ||
// each item refers to webpack.optimize.[NAME] construct | ||
const deprecatedPlugingsList = [ | ||
'webpack.optimize.OccurrenceOrderPlugin', | ||
'webpack.optimize.DedupePlugin' | ||
]; | ||
|
||
return findPluginsByName(j, ast, deprecatedPlugingsList) | ||
return utils.findPluginsByName(j, ast, deprecatedPlugingsList) | ||
.forEach(path => { | ||
// Check how many plugins are defined and | ||
// if there is only last plugin left remove `plugins: []` completely | ||
if (path.parent.value.elements.length === 1) { | ||
j(path.parent.parent).remove(); | ||
// For now we only support the case there plugins are defined in an Array | ||
if (path.parent && | ||
path.parent.value && | ||
utils.isType(path.parent.value, 'ArrayExpression') | ||
) { | ||
// Check how many plugins are defined and | ||
// if there is only last plugin left remove `plugins: []` node | ||
if (path.parent.value.elements.length === 1) { | ||
j(path.parent.parent).remove(); | ||
} else { | ||
j(path).remove(); | ||
} | ||
} else { | ||
j(path).remove(); | ||
const startLoc = path.value.loc.start; | ||
console.log(` | ||
${ chalk.red('Only plugins instantiated in the array can be automatically removed i.e.:') } | ||
${ codeFrame(example, null, null, { highlightCode: true }) } | ||
${ chalk.red('but you use it like this:') } | ||
${ codeFrame(source, startLoc.line, startLoc.column, { highlightCode: true }) } | ||
${ chalk.red('Please remove deprecated plugins manually. ') } | ||
See ${ chalk.underline('https://webpack.js.org/guides/migrating/')} for more information.`); | ||
} | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters