Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f8f0163
commit f2524b4
Showing
14 changed files
with
72 additions
and
45 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import webpack, { type Compiler } from 'webpack' | ||
|
||
const { RuntimeModule, Template } = webpack | ||
class TrustedTypesRuntimeModule extends RuntimeModule { | ||
constructor() { | ||
super('trustedTypes', RuntimeModule.STAGE_TRIGGER) | ||
} | ||
override generate(): string { | ||
const { compilation } = this | ||
if (!compilation) | ||
return Template.asString('/* TrustedTypesRuntimeModule skipped because compilation is undefined. */') | ||
return Template.asString([ | ||
'if (typeof trustedTypes !== "undefined" && location.protocol.includes("extension") && !trustedTypes.defaultPolicy) {', | ||
Template.indent([`trustedTypes.createPolicy('default', { createScriptURL: (string) => string });`]), | ||
'}', | ||
]) | ||
} | ||
} | ||
export class TrustedTypesPlugin { | ||
apply(compiler: Compiler) { | ||
compiler.hooks.compilation.tap('TrustedTypes', (compilation) => { | ||
compilation.hooks.afterChunks.tap('TrustedTypes', (chunks) => { | ||
for (const c of chunks) { | ||
if (!c.hasEntryModule()) continue | ||
compilation.addRuntimeModule(c, new TrustedTypesRuntimeModule(), compilation.chunkGraph) | ||
} | ||
}) | ||
}) | ||
} | ||
} |
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
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,10 @@ | ||
/// <reference path="../../../polyfills/types/dom.d.ts" /> | ||
|
||
// This file must not be loaded via importScripts in a Worker, doing so the importScripts will fail due to trustedTypes restriction. | ||
// You can import this file (not importScripts) so webpack will bundle this into the initial chunk. This requires the worker have no multiple initial chunks. | ||
if (typeof trustedTypes === 'object' && location.protocol.includes('extension')) { | ||
// DO NOT add createHTML or createScript. | ||
trustedTypes.createPolicy('default', { createScriptURL: (string) => string }) | ||
} | ||
|
||
undefined |
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 |
---|---|---|
@@ -1,25 +1,2 @@ | ||
/// <reference path="../../polyfills/types/dom.d.ts" /> | ||
if (typeof trustedTypes === 'object' && location.protocol.includes('extension')) { | ||
trustedTypes.createPolicy('default', { | ||
// do not add createHTML or createScript. | ||
// createScriptURL is safe because according to the CSP we have, it is impossible to | ||
// include/create a script from cross-origin. | ||
createScriptURL: (string) => string, | ||
}) | ||
} | ||
|
||
importScripts('/worker.js') | ||
if (typeof self !== 'undefined') { | ||
/** | ||
* Workaround: Webpack child compiler doesn't inherit plugins but inherit loaders. | ||
* That make loaders/plugins settings mismatch and cause runtime errors. | ||
*/ | ||
Reflect.set(self, '$RefreshReg$', function () {}) | ||
Reflect.set(self, '$RefreshSig$', function () { | ||
return function (type: any) { | ||
return type | ||
} | ||
}) | ||
} | ||
export {} | ||
setTimeout(() => self.postMessage('Alive'), 0) |
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