RequireIncludePlugin.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const {
  7. JAVASCRIPT_MODULE_TYPE_AUTO,
  8. JAVASCRIPT_MODULE_TYPE_DYNAMIC
  9. } = require("../ModuleTypeConstants");
  10. const RequireIncludeDependency = require("./RequireIncludeDependency");
  11. const RequireIncludeDependencyParserPlugin = require("./RequireIncludeDependencyParserPlugin");
  12. /** @typedef {import("../../declarations/WebpackOptions").JavascriptParserOptions} JavascriptParserOptions */
  13. /** @typedef {import("../Compiler")} Compiler */
  14. /** @typedef {import("../javascript/JavascriptParser")} Parser */
  15. const PLUGIN_NAME = "RequireIncludePlugin";
  16. class RequireIncludePlugin {
  17. /**
  18. * Apply the plugin
  19. * @param {Compiler} compiler the compiler instance
  20. * @returns {void}
  21. */
  22. apply(compiler) {
  23. compiler.hooks.compilation.tap(
  24. PLUGIN_NAME,
  25. (compilation, { normalModuleFactory }) => {
  26. compilation.dependencyFactories.set(
  27. RequireIncludeDependency,
  28. normalModuleFactory
  29. );
  30. compilation.dependencyTemplates.set(
  31. RequireIncludeDependency,
  32. new RequireIncludeDependency.Template()
  33. );
  34. /**
  35. * @param {Parser} parser parser parser
  36. * @param {JavascriptParserOptions} parserOptions parserOptions
  37. * @returns {void}
  38. */
  39. const handler = (parser, parserOptions) => {
  40. if (parserOptions.requireInclude === false) return;
  41. const warn = parserOptions.requireInclude === undefined;
  42. new RequireIncludeDependencyParserPlugin(warn).apply(parser);
  43. };
  44. normalModuleFactory.hooks.parser
  45. .for(JAVASCRIPT_MODULE_TYPE_AUTO)
  46. .tap(PLUGIN_NAME, handler);
  47. normalModuleFactory.hooks.parser
  48. .for(JAVASCRIPT_MODULE_TYPE_DYNAMIC)
  49. .tap(PLUGIN_NAME, handler);
  50. }
  51. );
  52. }
  53. }
  54. module.exports = RequireIncludePlugin;