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.

74 lines
1.9 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 RequireEnsureItemDependency = require("./RequireEnsureItemDependency");
  7. const RequireEnsureDependency = require("./RequireEnsureDependency");
  8. const NullFactory = require("../NullFactory");
  9. const RequireEnsureDependenciesBlockParserPlugin = require("./RequireEnsureDependenciesBlockParserPlugin");
  10. const ParserHelpers = require("../ParserHelpers");
  11. class RequireEnsurePlugin {
  12. apply(compiler) {
  13. compiler.hooks.compilation.tap(
  14. "RequireEnsurePlugin",
  15. (compilation, { normalModuleFactory }) => {
  16. compilation.dependencyFactories.set(
  17. RequireEnsureItemDependency,
  18. normalModuleFactory
  19. );
  20. compilation.dependencyTemplates.set(
  21. RequireEnsureItemDependency,
  22. new RequireEnsureItemDependency.Template()
  23. );
  24. compilation.dependencyFactories.set(
  25. RequireEnsureDependency,
  26. new NullFactory()
  27. );
  28. compilation.dependencyTemplates.set(
  29. RequireEnsureDependency,
  30. new RequireEnsureDependency.Template()
  31. );
  32. const handler = (parser, parserOptions) => {
  33. if (
  34. parserOptions.requireEnsure !== undefined &&
  35. !parserOptions.requireEnsure
  36. )
  37. return;
  38. new RequireEnsureDependenciesBlockParserPlugin().apply(parser);
  39. parser.hooks.evaluateTypeof
  40. .for("require.ensure")
  41. .tap(
  42. "RequireEnsurePlugin",
  43. ParserHelpers.evaluateToString("function")
  44. );
  45. parser.hooks.typeof
  46. .for("require.ensure")
  47. .tap(
  48. "RequireEnsurePlugin",
  49. ParserHelpers.toConstantDependency(
  50. parser,
  51. JSON.stringify("function")
  52. )
  53. );
  54. };
  55. normalModuleFactory.hooks.parser
  56. .for("javascript/auto")
  57. .tap("RequireEnsurePlugin", handler);
  58. normalModuleFactory.hooks.parser
  59. .for("javascript/dynamic")
  60. .tap("RequireEnsurePlugin", handler);
  61. }
  62. );
  63. }
  64. }
  65. module.exports = RequireEnsurePlugin;