forked from webpack/webpack-cli
/
removeDeprecatedPlugins.ts
46 lines (38 loc) · 1.74 KB
/
removeDeprecatedPlugins.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import { red, underline } from 'colorette';
import { findPluginsByName, isType, safeTraverse } from '@webpack-cli/utils';
import { JSCodeshift, Node } from '../types/NodePath';
import { utils } from 'webpack-cli';
const { logger } = utils;
/**
*
* Find deprecated plugins and remove them from the `plugins` array, if possible.
* Otherwise, warn the user about removing deprecated plugins manually.
*
* @param {Object} j - jscodeshift top-level import
* @param {Node} ast - jscodeshift ast to transform
* @returns {Node} ast - jscodeshift ast
*/
export default function (j: JSCodeshift, ast: Node): Node {
// List of deprecated plugins to remove
// each item refers to webpack.optimize.[NAME] construct
const deprecatedPlugingsList: string[] = ['webpack.optimize.OccurrenceOrderPlugin', 'webpack.optimize.DedupePlugin'];
return findPluginsByName(j, ast, deprecatedPlugingsList).forEach((path: Node): void => {
// For now we only support the case where plugins are defined in an Array
const arrayPath = safeTraverse(path, ['parent', 'value']) as Node;
if (arrayPath && isType(arrayPath, 'ArrayExpression')) {
// Check how many plugins are defined and
// if there is only last plugin left remove `plugins: []` node
//
const arrayElementsPath = safeTraverse(arrayPath, ['elements']) as Node[];
if (arrayElementsPath && arrayElementsPath.length === 1) {
j(path.parent.parent).remove();
} else {
j(path).remove();
}
} else {
logger.log(`
${red('Please remove deprecated plugins manually. ')}
See ${underline('https://webpack.js.org/guides/migrating/')} for more information.`);
}
});
}