You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
1.6 KiB

4 years ago
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const RequireIncludeDependency = require("./RequireIncludeDependency");
  7. const RequireIncludeDependencyParserPlugin = require("./RequireIncludeDependencyParserPlugin");
  8. const ParserHelpers = require("../ParserHelpers");
  9. class RequireIncludePlugin {
  10. apply(compiler) {
  11. compiler.hooks.compilation.tap(
  12. "RequireIncludePlugin",
  13. (compilation, { normalModuleFactory }) => {
  14. compilation.dependencyFactories.set(
  15. RequireIncludeDependency,
  16. normalModuleFactory
  17. );
  18. compilation.dependencyTemplates.set(
  19. RequireIncludeDependency,
  20. new RequireIncludeDependency.Template()
  21. );
  22. const handler = (parser, parserOptions) => {
  23. if (
  24. parserOptions.requireInclude !== undefined &&
  25. !parserOptions.requireInclude
  26. )
  27. return;
  28. new RequireIncludeDependencyParserPlugin().apply(parser);
  29. parser.hooks.evaluateTypeof
  30. .for("require.include")
  31. .tap(
  32. "RequireIncludePlugin",
  33. ParserHelpers.evaluateToString("function")
  34. );
  35. parser.hooks.typeof
  36. .for("require.include")
  37. .tap(
  38. "RequireIncludePlugin",
  39. ParserHelpers.toConstantDependency(
  40. parser,
  41. JSON.stringify("function")
  42. )
  43. );
  44. };
  45. normalModuleFactory.hooks.parser
  46. .for("javascript/auto")
  47. .tap("RequireIncludePlugin", handler);
  48. normalModuleFactory.hooks.parser
  49. .for("javascript/dynamic")
  50. .tap("RequireIncludePlugin", handler);
  51. }
  52. );
  53. }
  54. }
  55. module.exports = RequireIncludePlugin;