1 |
|
2 |
|
3 |
|
4 |
|
5 | "use strict";
|
6 |
|
7 | const RuntimeGlobals = require("../RuntimeGlobals");
|
8 | const Template = require("../Template");
|
9 | const HelperRuntimeModule = require("./HelperRuntimeModule");
|
10 |
|
11 | class CreateScriptUrlRuntimeModule extends HelperRuntimeModule {
|
12 | constructor() {
|
13 | super("trusted types");
|
14 | }
|
15 |
|
16 | |
17 |
|
18 |
|
19 | generate() {
|
20 | const { compilation } = this;
|
21 | const { runtimeTemplate, outputOptions } = compilation;
|
22 | const { trustedTypes } = outputOptions;
|
23 | const fn = RuntimeGlobals.createScriptUrl;
|
24 |
|
25 | if (!trustedTypes) {
|
26 |
|
27 | return Template.asString([
|
28 | `${fn} = ${runtimeTemplate.returningFunction("url", "url")};`
|
29 | ]);
|
30 | }
|
31 |
|
32 | return Template.asString([
|
33 | "var policy;",
|
34 | `${fn} = ${runtimeTemplate.basicFunction("url", [
|
35 | "// Create Trusted Type policy if Trusted Types are available and the policy doesn't exist yet.",
|
36 | "if (policy === undefined) {",
|
37 | Template.indent([
|
38 | "policy = {",
|
39 | Template.indent([
|
40 | `createScriptURL: ${runtimeTemplate.returningFunction(
|
41 | "url",
|
42 | "url"
|
43 | )}`
|
44 | ]),
|
45 | "};",
|
46 | 'if (typeof trustedTypes !== "undefined" && trustedTypes.createPolicy) {',
|
47 | Template.indent([
|
48 | `policy = trustedTypes.createPolicy(${JSON.stringify(
|
49 | trustedTypes.policyName
|
50 | )}, policy);`
|
51 | ]),
|
52 | "}"
|
53 | ]),
|
54 | "}",
|
55 | "return policy.createScriptURL(url);"
|
56 | ])};`
|
57 | ]);
|
58 | }
|
59 | }
|
60 |
|
61 | module.exports = CreateScriptUrlRuntimeModule;
|