CompatRuntimeModule.js 2.1 KB

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