您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

RequireIncludePlugin.js 1.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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;