Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14173 from tosmolka/tosmolka/14075
Support Trusted Types in EvalSourceMapDevToolPlugin
- Loading branch information
Showing
12 changed files
with
267 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
MIT License http://www.opensource.org/licenses/mit-license.php | ||
*/ | ||
|
||
"use strict"; | ||
|
||
const RuntimeGlobals = require("../RuntimeGlobals"); | ||
const Template = require("../Template"); | ||
const HelperRuntimeModule = require("./HelperRuntimeModule"); | ||
|
||
class CreateScriptRuntimeModule extends HelperRuntimeModule { | ||
constructor() { | ||
super("trusted types script"); | ||
} | ||
|
||
/** | ||
* @returns {string} runtime code | ||
*/ | ||
generate() { | ||
const { compilation } = this; | ||
const { runtimeTemplate, outputOptions } = compilation; | ||
const { trustedTypes } = outputOptions; | ||
const fn = RuntimeGlobals.createScript; | ||
|
||
return Template.asString( | ||
`${fn} = ${runtimeTemplate.returningFunction( | ||
trustedTypes | ||
? `${RuntimeGlobals.getTrustedTypesPolicy}().createScript(script)` | ||
: "script", | ||
"script" | ||
)};` | ||
); | ||
} | ||
} | ||
|
||
module.exports = CreateScriptRuntimeModule; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
MIT License http://www.opensource.org/licenses/mit-license.php | ||
*/ | ||
|
||
"use strict"; | ||
|
||
const RuntimeGlobals = require("../RuntimeGlobals"); | ||
const Template = require("../Template"); | ||
const HelperRuntimeModule = require("./HelperRuntimeModule"); | ||
|
||
class GetTrustedTypesPolicyRuntimeModule extends HelperRuntimeModule { | ||
/** | ||
* @param {Set<string>} runtimeRequirements runtime requirements | ||
*/ | ||
constructor(runtimeRequirements) { | ||
super("trusted types policy"); | ||
this.runtimeRequirements = runtimeRequirements; | ||
} | ||
|
||
/** | ||
* @returns {string} runtime code | ||
*/ | ||
generate() { | ||
const { compilation } = this; | ||
const { runtimeTemplate, outputOptions } = compilation; | ||
const { trustedTypes } = outputOptions; | ||
const fn = RuntimeGlobals.getTrustedTypesPolicy; | ||
|
||
return Template.asString([ | ||
"var policy;", | ||
`${fn} = ${runtimeTemplate.basicFunction("", [ | ||
"// Create Trusted Type policy if Trusted Types are available and the policy doesn't exist yet.", | ||
"if (policy === undefined) {", | ||
Template.indent([ | ||
"policy = {", | ||
Template.indent( | ||
[ | ||
...(this.runtimeRequirements.has(RuntimeGlobals.createScript) | ||
? [ | ||
`createScript: ${runtimeTemplate.returningFunction( | ||
"script", | ||
"script" | ||
)}` | ||
] | ||
: []), | ||
...(this.runtimeRequirements.has(RuntimeGlobals.createScriptUrl) | ||
? [ | ||
`createScriptURL: ${runtimeTemplate.returningFunction( | ||
"url", | ||
"url" | ||
)}` | ||
] | ||
: []) | ||
].join(",\n") | ||
), | ||
"};", | ||
...(trustedTypes | ||
? [ | ||
'if (typeof trustedTypes !== "undefined" && trustedTypes.createPolicy) {', | ||
Template.indent([ | ||
`policy = trustedTypes.createPolicy(${JSON.stringify( | ||
trustedTypes.policyName | ||
)}, policy);` | ||
]), | ||
"}" | ||
] | ||
: []) | ||
]), | ||
"}", | ||
"return policy;" | ||
])};` | ||
]); | ||
} | ||
} | ||
|
||
module.exports = GetTrustedTypesPolicyRuntimeModule; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.