CreateScriptUrlRuntimeModule.js 959 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 CreateScriptUrlRuntimeModule extends HelperRuntimeModule {
  10. constructor() {
  11. super("trusted types script url");
  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.createScriptUrl;
  21. return Template.asString(
  22. `${fn} = ${runtimeTemplate.returningFunction(
  23. trustedTypes
  24. ? `${RuntimeGlobals.getTrustedTypesPolicy}().createScriptURL(url)`
  25. : "url",
  26. "url"
  27. )};`
  28. );
  29. }
  30. }
  31. module.exports = CreateScriptUrlRuntimeModule;