bannerPlugin.js 987 B

123456789101112131415161718192021222324252627282930313233
  1. const utils = require("../../utils/ast-utils");
  2. /**
  3. *
  4. * Transform for BannerPlugin arguments. Consolidates first and second argument (if
  5. * both are present) into single options object.
  6. *
  7. * @param {Object} j - jscodeshift top-level import
  8. * @param {Node} ast - jscodeshift ast to transform
  9. * @returns {Node} ast - jscodeshift ast
  10. */
  11. module.exports = function(j, ast) {
  12. return utils
  13. .findPluginsByName(j, ast, ["webpack.BannerPlugin"])
  14. .forEach(path => {
  15. const args = path.value.arguments; // any node
  16. // If the first argument is a literal replace it with object notation
  17. // See https://webpack.js.org/guides/migrating/#bannerplugin-breaking-change
  18. if (args && args.length > 1 && args[0].type === j.Literal.name) {
  19. // and remove the first argument
  20. path.value.arguments = [path.value.arguments[1]];
  21. utils.createOrUpdatePluginByName(
  22. j,
  23. path.parent,
  24. "webpack.BannerPlugin",
  25. {
  26. banner: args[0].value
  27. }
  28. );
  29. }
  30. });
  31. };