RuntimeIdRuntimeModule.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. class RuntimeIdRuntimeModule extends RuntimeModule {
  10. constructor() {
  11. super("runtimeId");
  12. }
  13. /**
  14. * Returns true, if the runtime module should get it's own scope.
  15. * When false, `generate()` must emit complete statements ending with `;`
  16. * so a following runtime IIFE is not parsed as a call (ASI).
  17. * @returns {boolean} true, if the runtime module should get it's own scope
  18. */
  19. shouldIsolate() {
  20. return false;
  21. }
  22. /**
  23. * Generates runtime code for this runtime module.
  24. * @returns {string | null} runtime code
  25. */
  26. generate() {
  27. const chunkGraph = /** @type {ChunkGraph} */ (this.chunkGraph);
  28. const chunk = /** @type {Chunk} */ (this.chunk);
  29. const runtime = chunk.runtime;
  30. if (typeof runtime !== "string") {
  31. throw new Error("RuntimeIdRuntimeModule must be in a single runtime");
  32. }
  33. const id = chunkGraph.getRuntimeId(runtime);
  34. return `${RuntimeGlobals.runtimeId} = ${JSON.stringify(id)};`;
  35. }
  36. }
  37. module.exports = RuntimeIdRuntimeModule;