ExportsInfoApiPlugin.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const ConstDependency = require("./dependencies/ConstDependency");
  7. const ExportsInfoDependency = require("./dependencies/ExportsInfoDependency");
  8. /** @typedef {import("./Compiler")} Compiler */
  9. /** @typedef {import("./javascript/JavascriptParser")} JavascriptParser */
  10. class ExportsInfoApiPlugin {
  11. /**
  12. * Apply the plugin
  13. * @param {Compiler} compiler the compiler instance
  14. * @returns {void}
  15. */
  16. apply(compiler) {
  17. compiler.hooks.compilation.tap(
  18. "ExportsInfoApiPlugin",
  19. (compilation, { normalModuleFactory }) => {
  20. compilation.dependencyTemplates.set(
  21. ExportsInfoDependency,
  22. new ExportsInfoDependency.Template()
  23. );
  24. /**
  25. * @param {JavascriptParser} parser the parser
  26. * @returns {void}
  27. */
  28. const handler = parser => {
  29. parser.hooks.expressionMemberChain
  30. .for("__webpack_exports_info__")
  31. .tap("ExportsInfoApiPlugin", (expr, members) => {
  32. const dep =
  33. members.length >= 2
  34. ? new ExportsInfoDependency(
  35. expr.range,
  36. members.slice(0, -1),
  37. members[members.length - 1]
  38. )
  39. : new ExportsInfoDependency(expr.range, null, members[0]);
  40. dep.loc = expr.loc;
  41. parser.state.module.addDependency(dep);
  42. return true;
  43. });
  44. parser.hooks.expression
  45. .for("__webpack_exports_info__")
  46. .tap("ExportsInfoApiPlugin", expr => {
  47. const dep = new ConstDependency("true", expr.range);
  48. dep.loc = expr.loc;
  49. parser.state.module.addPresentationalDependency(dep);
  50. return true;
  51. });
  52. };
  53. normalModuleFactory.hooks.parser
  54. .for("javascript/auto")
  55. .tap("ExportsInfoApiPlugin", handler);
  56. normalModuleFactory.hooks.parser
  57. .for("javascript/dynamic")
  58. .tap("ExportsInfoApiPlugin", handler);
  59. normalModuleFactory.hooks.parser
  60. .for("javascript/esm")
  61. .tap("ExportsInfoApiPlugin", handler);
  62. }
  63. );
  64. }
  65. }
  66. module.exports = ExportsInfoApiPlugin;