CompatRuntimeModule.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. */
  4. "use strict";
  5. const RuntimeGlobals = require("../RuntimeGlobals");
  6. const RuntimeModule = require("../RuntimeModule");
  7. /** @typedef {import("../MainTemplate")} MainTemplate */
  8. class CompatRuntimeModule extends RuntimeModule {
  9. constructor() {
  10. super("compat", RuntimeModule.STAGE_ATTACH);
  11. this.fullHash = true;
  12. }
  13. /**
  14. * @returns {string} runtime code
  15. */
  16. generate() {
  17. const { chunkGraph, chunk, compilation } = this;
  18. const {
  19. runtimeTemplate,
  20. mainTemplate,
  21. moduleTemplates,
  22. dependencyTemplates
  23. } = compilation;
  24. const bootstrap = mainTemplate.hooks.bootstrap.call(
  25. "",
  26. chunk,
  27. compilation.hash || "XXXX",
  28. moduleTemplates.javascript,
  29. dependencyTemplates
  30. );
  31. const localVars = mainTemplate.hooks.localVars.call(
  32. "",
  33. chunk,
  34. compilation.hash || "XXXX"
  35. );
  36. const requireExtensions = mainTemplate.hooks.requireExtensions.call(
  37. "",
  38. chunk,
  39. compilation.hash || "XXXX"
  40. );
  41. const runtimeRequirements = chunkGraph.getTreeRuntimeRequirements(chunk);
  42. let requireEnsure = "";
  43. if (runtimeRequirements.has(RuntimeGlobals.ensureChunk)) {
  44. const requireEnsureHandler = mainTemplate.hooks.requireEnsure.call(
  45. "",
  46. chunk,
  47. compilation.hash || "XXXX",
  48. "chunkId"
  49. );
  50. if (requireEnsureHandler) {
  51. requireEnsure = `${
  52. RuntimeGlobals.ensureChunkHandlers
  53. }.compat = ${runtimeTemplate.basicFunction(
  54. "chunkId, promises",
  55. requireEnsureHandler
  56. )};`;
  57. }
  58. }
  59. return [bootstrap, localVars, requireEnsure, requireExtensions]
  60. .filter(Boolean)
  61. .join("\n");
  62. }
  63. /**
  64. * @returns {boolean} true, if the runtime module should get it's own scope
  65. */
  66. shouldIsolate() {
  67. // We avoid isolating this to have better backward-compat
  68. return false;
  69. }
  70. }
  71. module.exports = CompatRuntimeModule;