From b1e4a883a3ee4473360da91a2f9e140db47853ee Mon Sep 17 00:00:00 2001 From: Jack Works Date: Sun, 23 Jan 2022 03:17:30 +0800 Subject: [PATCH] feat: enables trusted types in develop mode --- package.json | 2 +- packages/mask/.webpack/config.ts | 32 +- packages/mask/.webpack/template.html | 1 + .../Jobs/InjectContentScripts.ts | 24 +- packages/theme/src/constants.ts | 2 +- ...pack+5.65.0.patch => webpack+5.67.0.patch} | 4 +- pnpm-lock.yaml | 370 +++++++++++++----- 7 files changed, 310 insertions(+), 125 deletions(-) rename patches/{webpack+5.65.0.patch => webpack+5.67.0.patch} (97%) diff --git a/package.json b/package.json index 7e7635cfd17a..f88a8d813bfd 100644 --- a/package.json +++ b/package.json @@ -73,7 +73,7 @@ "ts-jest": "^27.1.2", "ts-node": "^10.4.0", "typescript": "4.6.0-dev.20211202", - "webpack": "5.65.0" + "webpack": "5.67.0" }, "engines": { "node": ">=16.0.0", diff --git a/packages/mask/.webpack/config.ts b/packages/mask/.webpack/config.ts index 83a6c573aeaa..99a1175661da 100644 --- a/packages/mask/.webpack/config.ts +++ b/packages/mask/.webpack/config.ts @@ -247,6 +247,9 @@ export function createConfiguration(rawFlags: BuildFlags): Configuration { globalObject: 'globalThis', publicPath: '/', clean: mode === 'production', + trustedTypes: { + policyName: 'webpack', + }, }, ignoreWarnings: [/Failed to parse source map/], // @ts-ignore @@ -266,10 +269,14 @@ export function createConfiguration(rawFlags: BuildFlags): Configuration { debug: normalizeEntryDescription(join(__dirname, '../src/extension/debug-page/index.tsx')), }) baseConfig.plugins!.push( - addHTMLEntry({ chunks: ['dashboard'], filename: 'dashboard.html' }), - addHTMLEntry({ chunks: ['popups'], filename: 'popups.html' }), - addHTMLEntry({ chunks: ['contentScript'], filename: 'generated__content__script.html' }), - addHTMLEntry({ chunks: ['debug'], filename: 'debug.html' }), + addHTMLEntry({ chunks: ['dashboard'], filename: 'dashboard.html', sourceMap: !!sourceMapKind }), + addHTMLEntry({ chunks: ['popups'], filename: 'popups.html', sourceMap: !!sourceMapKind }), + addHTMLEntry({ + chunks: ['contentScript'], + filename: 'generated__content__script.html', + sourceMap: !!sourceMapKind, + }), + addHTMLEntry({ chunks: ['debug'], filename: 'debug.html', sourceMap: !!sourceMapKind }), ) // background if (runtime.manifest === 3) { @@ -281,7 +288,14 @@ export function createConfiguration(rawFlags: BuildFlags): Configuration { } else { entries.background = normalizeEntryDescription(join(__dirname, '../src/background-service.ts')) plugins.push(new WebExtensionPlugin({ background: { entry: 'background', manifest: 2 } })) - plugins.push(addHTMLEntry({ chunks: ['background'], filename: 'background.html', secp256k1: true })) + plugins.push( + addHTMLEntry({ + chunks: ['background'], + filename: 'background.html', + secp256k1: true, + sourceMap: !!sourceMapKind, + }), + ) } for (const entry in entries) { withReactDevTools(entries[entry]) @@ -303,7 +317,7 @@ export function createConfiguration(rawFlags: BuildFlags): Configuration { } } } -function addHTMLEntry(options: HTMLPlugin.Options & { secp256k1?: boolean } = {}) { +function addHTMLEntry(options: HTMLPlugin.Options & { secp256k1?: boolean; sourceMap: boolean }) { let templateContent = readFileSync(join(__dirname, './template.html'), 'utf8') if (options.secp256k1) { templateContent = templateContent.replace( @@ -311,6 +325,12 @@ function addHTMLEntry(options: HTMLPlugin.Options & { secp256k1?: boolean } = {} '', ) } + if (options.sourceMap) { + templateContent = templateContent.replace( + ``, + ``, + ) + } return new HTMLPlugin({ templateContent, inject: 'body', diff --git a/packages/mask/.webpack/template.html b/packages/mask/.webpack/template.html index ecfb1fdfdbb7..40eb2887090b 100644 --- a/packages/mask/.webpack/template.html +++ b/packages/mask/.webpack/template.html @@ -4,6 +4,7 @@ Mask Network + diff --git a/packages/mask/src/extension/background-script/Jobs/InjectContentScripts.ts b/packages/mask/src/extension/background-script/Jobs/InjectContentScripts.ts index 8263eeb2819b..542ab8555b7d 100644 --- a/packages/mask/src/extension/background-script/Jobs/InjectContentScripts.ts +++ b/packages/mask/src/extension/background-script/Jobs/InjectContentScripts.ts @@ -63,17 +63,23 @@ export default function (signal: AbortSignal) { } function fetchInjectContentScript(entryHTML: string) { - const contentScripts: Array<{ code: string } | { file: string }> = [] + const contentScripts: string[] = [] const task = fetch(entryHTML) .then((x) => x.text()) .then((html) => { - const parser = new DOMParser() - const root = parser.parseFromString(html, 'text/html') - for (const script of root.querySelectorAll('script')) { - if (script.innerText) contentScripts.push({ code: script.innerText }) - else if (script.src) - contentScripts.push({ file: new URL(script.src, browser.runtime.getURL('')).pathname }) - } + // We're not going to use DOMParser because it is not available in MV3. + Array.from(html.matchAll(/