CreateScriptRuntimeModule.js 952 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. */
  4. "use strict";
  5. const RuntimeGlobals = require("../RuntimeGlobals");
  6. const Template = require("../Template");
  7. const HelperRuntimeModule = require("./HelperRuntimeModule");
  8. /** @typedef {import("../Compilation")} Compilation */
  9. class CreateScriptRuntimeModule extends HelperRuntimeModule {
  10. constructor() {
  11. super("trusted types script");
  12. }
  13. /**
  14. * @returns {string | null} runtime code
  15. */
  16. generate() {
  17. const compilation = /** @type {Compilation} */ (this.compilation);
  18. const { runtimeTemplate, outputOptions } = compilation;
  19. const { trustedTypes } = outputOptions;
  20. const fn = RuntimeGlobals.createScript;
  21. return Template.asString(
  22. `${fn} = ${runtimeTemplate.returningFunction(
  23. trustedTypes
  24. ? `${RuntimeGlobals.getTrustedTypesPolicy}().createScript(script)`
  25. : "script",
  26. "script"
  27. )};`
  28. );
  29. }
  30. }
  31. module.exports = CreateScriptRuntimeModule;