CompatRuntimeModule.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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("../Chunk")} Chunk */
  8. /** @typedef {import("../ChunkGraph")} ChunkGraph */
  9. /** @typedef {import("../Compilation")} 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. * @returns {boolean} true, if the runtime module should get it's own scope
  72. */
  73. shouldIsolate() {
  74. // We avoid isolating this to have better backward-compat
  75. return false;
  76. }
  77. }
  78. module.exports = CompatRuntimeModule;