CompatRuntimeModule.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. /** @import Chunk from "../Chunk" */
  8. /** @import ChunkGraph from "../ChunkGraph" */
  9. /** @import Compilation from "../Compilation" */
  10. class CompatRuntimeModule extends RuntimeModule {
  11. constructor() {
  12. super("compat", RuntimeModule.STAGE_ATTACH);
  13. /** @type {boolean} */
  14. this.fullHash = true;
  15. }
  16. /**
  17. * Generates runtime code for this runtime module.
  18. * @returns {string | null} runtime code
  19. */
  20. generate() {
  21. const compilation = /** @type {Compilation} */ (this.compilation);
  22. const chunkGraph = /** @type {ChunkGraph} */ (this.chunkGraph);
  23. const chunk = /** @type {Chunk} */ (this.chunk);
  24. const {
  25. runtimeTemplate,
  26. mainTemplate,
  27. moduleTemplates,
  28. dependencyTemplates
  29. } = compilation;
  30. const bootstrap = mainTemplate.hooks.bootstrap.call(
  31. "",
  32. chunk,
  33. compilation.hash || "XXXX",
  34. moduleTemplates.javascript,
  35. dependencyTemplates
  36. );
  37. const localVars = mainTemplate.hooks.localVars.call(
  38. "",
  39. chunk,
  40. compilation.hash || "XXXX"
  41. );
  42. const requireExtensions = mainTemplate.hooks.requireExtensions.call(
  43. "",
  44. chunk,
  45. compilation.hash || "XXXX"
  46. );
  47. const runtimeRequirements = chunkGraph.getTreeRuntimeRequirements(chunk);
  48. let requireEnsure = "";
  49. if (runtimeRequirements.has(RuntimeGlobals.ensureChunk)) {
  50. const requireEnsureHandler = mainTemplate.hooks.requireEnsure.call(
  51. "",
  52. chunk,
  53. compilation.hash || "XXXX",
  54. "chunkId"
  55. );
  56. if (requireEnsureHandler) {
  57. requireEnsure = `${
  58. RuntimeGlobals.ensureChunkHandlers
  59. }.compat = ${runtimeTemplate.basicFunction(
  60. "chunkId, promises",
  61. requireEnsureHandler
  62. )};`;
  63. }
  64. }
  65. return [bootstrap, localVars, requireEnsure, requireExtensions]
  66. .filter(Boolean)
  67. .join("\n");
  68. }
  69. /**
  70. * Returns true, if the runtime module should get it's own scope.
  71. * When false, `generate()` must emit complete statements ending with `;`
  72. * so a following runtime IIFE is not parsed as a call (ASI).
  73. * @returns {boolean} true, if the runtime module should get it's own scope
  74. */
  75. shouldIsolate() {
  76. // We avoid isolating this to have better backward-compat
  77. return false;
  78. }
  79. }
  80. module.exports = CompatRuntimeModule;