CssServerStylesRuntimeModule.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Alexander Akait @alexander-akait
  4. */
  5. "use strict";
  6. const RuntimeGlobals = require("../RuntimeGlobals");
  7. const RuntimeModule = require("../RuntimeModule");
  8. const Template = require("../Template");
  9. /** @import Compilation from "../Compilation" */
  10. class CssServerStylesRuntimeModule extends RuntimeModule {
  11. constructor() {
  12. super("css server styles");
  13. }
  14. /**
  15. * Returns true, if the runtime module should get it's own scope.
  16. * When false, `generate()` must emit complete statements ending with `;`
  17. * so a following runtime IIFE is not parsed as a call (ASI).
  18. * @returns {boolean} true, if the runtime module should get it's own scope
  19. */
  20. shouldIsolate() {
  21. return false;
  22. }
  23. /**
  24. * Generates runtime code for this runtime module.
  25. * @returns {string | null} runtime code
  26. */
  27. generate() {
  28. const compilation = /** @type {Compilation} */ (this.compilation);
  29. const { runtimeTemplate } = compilation;
  30. const cst = runtimeTemplate.renderConst();
  31. return Template.asString([
  32. `${RuntimeGlobals.getCssServerStyles} = ${runtimeTemplate.basicFunction(
  33. "",
  34. [
  35. `${cst} registry = ${runtimeTemplate.cssServerStyleRegistry()};`,
  36. 'var css = "";',
  37. // keys are string-prefixed at the write site, so this yields
  38. // insertion order rather than ascending numeric module id order
  39. "for (var key in registry) css += registry[key];",
  40. "return css;"
  41. ]
  42. )};`
  43. ]);
  44. }
  45. }
  46. module.exports = CssServerStylesRuntimeModule;