removeDeprecatedPlugins.js 1.5 KB

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