From 6ccd5589c295a0cefacc7b3d2758344aae261199 Mon Sep 17 00:00:00 2001 From: Jack Works Date: Mon, 28 Feb 2022 20:08:48 +0800 Subject: [PATCH 1/7] chore: wip --- packages/mask/.webpack/config.ts | 13 +++++++++++-- packages/mask/.webpack/flags.ts | 4 +++- packages/mask/.webpack/wasm-to-asm.ts | 25 +++++++++++++++++++++++++ packages/mask/package.json | 1 + pnpm-lock.yaml | 7 +++++++ 5 files changed, 47 insertions(+), 3 deletions(-) create mode 100644 packages/mask/.webpack/wasm-to-asm.ts diff --git a/packages/mask/.webpack/config.ts b/packages/mask/.webpack/config.ts index 4dd1cf69efc4..e4e420389979 100644 --- a/packages/mask/.webpack/config.ts +++ b/packages/mask/.webpack/config.ts @@ -20,7 +20,7 @@ import './clean-hmr' export function createConfiguration(rawFlags: BuildFlags): Configuration { const normalizedFlags = normalizeBuildFlags(rawFlags) - const { sourceMapKind } = computedBuildFlags(normalizedFlags) + const { sourceMapKind, supportWebAssembly } = computedBuildFlags(normalizedFlags) const { hmr, mode, profiling, reactRefresh, readonlyCache, reproducibleBuild, runtime, outputPath } = normalizedFlags @@ -39,7 +39,7 @@ export function createConfiguration(rawFlags: BuildFlags): Configuration { devtool: sourceMapKind, target: ['web', 'es2019'], entry: {}, - experiments: { backCompat: false, asyncWebAssembly: true }, + experiments: { backCompat: false, asyncWebAssembly: supportWebAssembly }, cache: { type: 'filesystem', buildDependencies: { config: [__filename] }, @@ -124,6 +124,14 @@ export function createConfiguration(rawFlags: BuildFlags): Configuration { rules: [ // Opt in source map { test: /(async-call|webextension).+\.js$/, enforce: 'pre', use: ['source-map-loader'] }, + // Manifest v3 does not support + supportWebAssembly + ? { + test: /\.wasm?$/, + loader: require.resolve('./wasm-to-asm.ts'), + type: 'javascript/auto', + } + : undefined!, // TypeScript { test: /\.tsx?$/, @@ -265,6 +273,7 @@ export function createConfiguration(rawFlags: BuildFlags): Configuration { } as DevServerConfiguration, stats: process.env.CI ? 'errors-warnings' : undefined, } + baseConfig.module!.rules = baseConfig.module!.rules!.filter(Boolean) const plugins = baseConfig.plugins! const entries: Record = (baseConfig.entry = { diff --git a/packages/mask/.webpack/flags.ts b/packages/mask/.webpack/flags.ts index 5bf775dd60e0..fe708b3b4793 100644 --- a/packages/mask/.webpack/flags.ts +++ b/packages/mask/.webpack/flags.ts @@ -38,16 +38,18 @@ export function normalizeBuildFlags(flags: BuildFlags) { export function computedBuildFlags(flags: ReturnType) { const { runtime, mode } = flags let sourceMapKind: Configuration['devtool'] = false + let supportWebAssembly = true if (runtime.engine === 'safari' && runtime.architecture === 'app') { // Due to webextension-polyfill, eval on iOS is async. sourceMapKind = false } else if (runtime.manifest === 3 && mode === 'development') { // MV3 does not allow eval even in production sourceMapKind = 'inline-cheap-source-map' + supportWebAssembly = false } else if (mode === 'development') { sourceMapKind = 'eval-cheap-source-map' } else { sourceMapKind = false } - return { sourceMapKind, lockdown: mode === 'development' } as const + return { sourceMapKind, lockdown: mode === 'development', supportWebAssembly } as const } diff --git a/packages/mask/.webpack/wasm-to-asm.ts b/packages/mask/.webpack/wasm-to-asm.ts new file mode 100644 index 000000000000..632d1c980259 --- /dev/null +++ b/packages/mask/.webpack/wasm-to-asm.ts @@ -0,0 +1,25 @@ +import { spawn } from 'child_process' +import { writeFileSync } from 'fs' +import { join } from 'path' +import { LoaderContext } from 'webpack' + +export default function loader(this: LoaderContext, source: Buffer) { + return new Promise((resolve, reject) => { + const child = spawn(process.execPath, [ + join(__dirname, '../node_modules/binaryen/bin/wasm2js'), + this.resourcePath, + ]) + const out: string[] = [] + const err: string[] = [] + child.stdout.setEncoding('utf-8') + child.stderr.setEncoding('utf-8') + child.stdout.on('data', (data) => out.push(data.toString())) + child.stderr.on('data', (data) => err.push(data.toString())) + child.on('close', (code) => { + if (code !== 0) reject(new Error(err.join('\n'))) + else resolve(out.join('')) + writeFileSync('./out.js', out.join('')) + }) + }) +} +loader.raw = true diff --git a/packages/mask/package.json b/packages/mask/package.json index 31ea8ea9929e..95528595e955 100644 --- a/packages/mask/package.json +++ b/packages/mask/package.json @@ -133,6 +133,7 @@ "@pmmmwh/react-refresh-webpack-plugin": "^0.5.4", "@typescript-eslint/eslint-plugin": "^5.14.0", "@typescript-eslint/parser": "^5.14.0", + "binaryen": "^106.0.0", "copy-webpack-plugin": "^10.2.4", "html-webpack-plugin": "^5.5.0", "react-devtools-core": "^4.23.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index d5b408f9b28a..3a0f51eaae32 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -359,6 +359,7 @@ importers: assert: ^2.0.0 async-call-rpc: ^6.0.2 bignumber.js: ^9.0.2 + binaryen: ^106.0.0 bip39: ^3.0.2 buffer: ^6.0.3 classnames: ^2.3.1 @@ -557,6 +558,7 @@ importers: '@pmmmwh/react-refresh-webpack-plugin': 0.5.4_cb46ffc29cbac33b1cd7a2454d0f4425 '@typescript-eslint/eslint-plugin': 5.14.0_f4054b8c3cd621db16ae1b9d571bccc0 '@typescript-eslint/parser': 5.14.0_eslint@8.10.0+typescript@4.6.2 + binaryen: 106.0.0 copy-webpack-plugin: 10.2.4_webpack@5.70.0 html-webpack-plugin: 5.5.0_webpack@5.70.0 react-devtools-core: 4.23.0 @@ -11129,6 +11131,11 @@ packages: resolution: {integrity: sha512-jDctJ/IVQbZoJykoeHbhXpOlNBqGNcwXJKJog42E5HDPUwQTSdjCHdihjj0DlnheQ7blbT6dHOafNAiS8ooQKA==} engines: {node: '>=8'} + /binaryen/106.0.0: + resolution: {integrity: sha512-AuYxvPDPZKt9hYhyw2xFoNhrleI8dQDMMjSunETPJtViUsRSJmvA48GweKV6PZUU7F8E7ipsvmhZLSAsDIamRw==} + hasBin: true + dev: true + /bindings/1.5.0: resolution: {integrity: sha512-p2q/t/mhvuOj/UeLlV6566GD/guowlr0hHxClI0W9m7MWYkL1F0hLo+0Aexs9HSPCtR1SXQ0TD3MMKrXZajbiQ==} dependencies: From d29fbf989a787dfc492c6405167164de2e779b98 Mon Sep 17 00:00:00 2001 From: Jack Works Date: Mon, 28 Feb 2022 20:12:20 +0800 Subject: [PATCH 2/7] chore: wip --- packages/mask/.webpack/config.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/mask/.webpack/config.ts b/packages/mask/.webpack/config.ts index e4e420389979..37164a5b0fa0 100644 --- a/packages/mask/.webpack/config.ts +++ b/packages/mask/.webpack/config.ts @@ -125,7 +125,7 @@ export function createConfiguration(rawFlags: BuildFlags): Configuration { // Opt in source map { test: /(async-call|webextension).+\.js$/, enforce: 'pre', use: ['source-map-loader'] }, // Manifest v3 does not support - supportWebAssembly + !supportWebAssembly ? { test: /\.wasm?$/, loader: require.resolve('./wasm-to-asm.ts'), From 9811b5f8c199d2eb108ff7f9779df302de490d7e Mon Sep 17 00:00:00 2001 From: Jack Works Date: Mon, 28 Feb 2022 20:12:50 +0800 Subject: [PATCH 3/7] chore: wip --- packages/mask/.webpack/wasm-to-asm.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/mask/.webpack/wasm-to-asm.ts b/packages/mask/.webpack/wasm-to-asm.ts index 632d1c980259..fcf83c324e4d 100644 --- a/packages/mask/.webpack/wasm-to-asm.ts +++ b/packages/mask/.webpack/wasm-to-asm.ts @@ -18,7 +18,6 @@ export default function loader(this: LoaderContext, source: Buffer) { child.on('close', (code) => { if (code !== 0) reject(new Error(err.join('\n'))) else resolve(out.join('')) - writeFileSync('./out.js', out.join('')) }) }) } From b2b970aa1267540b0fa0d039f7449736fc1f4b64 Mon Sep 17 00:00:00 2001 From: Jack Works Date: Tue, 1 Mar 2022 15:40:10 +0800 Subject: [PATCH 4/7] chore: wip --- packages/mask/web-workers/env.d.ts | 2 ++ packages/mask/web-workers/wallet.ts | 13 +++++++++++-- 2 files changed, 13 insertions(+), 2 deletions(-) create mode 100644 packages/mask/web-workers/env.d.ts diff --git a/packages/mask/web-workers/env.d.ts b/packages/mask/web-workers/env.d.ts new file mode 100644 index 000000000000..64a8c3276ea7 --- /dev/null +++ b/packages/mask/web-workers/env.d.ts @@ -0,0 +1,2 @@ +/// +/// diff --git a/packages/mask/web-workers/wallet.ts b/packages/mask/web-workers/wallet.ts index f4a96ff4d0ea..b736c7772f4c 100644 --- a/packages/mask/web-workers/wallet.ts +++ b/packages/mask/web-workers/wallet.ts @@ -4,10 +4,19 @@ import type { api } from '@dimensiondev/mask-wallet-core/proto' export type Input = { id: number; data: api.IMWRequest } export type Output = { id: number; response: api.MWResponse } +async function load() { + if (process.env.manifest === '3') { + return import('@dimensiondev/mask-wallet-core/bundle') + } else { + const { default: init, ...rest } = await import('@dimensiondev/mask-wallet-core/web') + // @ts-expect-error + await init() + return rest + } +} const promise = (async () => { - const { request, default: init } = await import('@dimensiondev/mask-wallet-core/web') + const { request } = await load() const { api } = await import('@dimensiondev/mask-wallet-core/proto') - await init() return { request, api } })() From 89236a1c81b7eea435265160e29660b3f6b56b6b Mon Sep 17 00:00:00 2001 From: Jack Works Date: Mon, 21 Mar 2022 16:57:49 +0800 Subject: [PATCH 5/7] fix: cannot load wasm --- packages/mask/.webpack/wasm-to-asm.ts | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/packages/mask/.webpack/wasm-to-asm.ts b/packages/mask/.webpack/wasm-to-asm.ts index fcf83c324e4d..448eb73020e4 100644 --- a/packages/mask/.webpack/wasm-to-asm.ts +++ b/packages/mask/.webpack/wasm-to-asm.ts @@ -1,14 +1,11 @@ import { spawn } from 'child_process' -import { writeFileSync } from 'fs' -import { join } from 'path' +import { join, relative } from 'path' import { LoaderContext } from 'webpack' export default function loader(this: LoaderContext, source: Buffer) { return new Promise((resolve, reject) => { - const child = spawn(process.execPath, [ - join(__dirname, '../node_modules/binaryen/bin/wasm2js'), - this.resourcePath, - ]) + const cli = join(__dirname, '../node_modules/binaryen/bin/wasm2js') + const child = spawn(process.execPath, [cli, relative(process.cwd(), this.resourcePath)], { cwd: process.cwd() }) const out: string[] = [] const err: string[] = [] child.stdout.setEncoding('utf-8') From ed1e8758a8d8e93491c6f0c109e35833afdd68d4 Mon Sep 17 00:00:00 2001 From: Jack Works Date: Mon, 21 Mar 2022 17:06:44 +0800 Subject: [PATCH 6/7] fix: cspell --- cspell.json | 1 + 1 file changed, 1 insertion(+) diff --git a/cspell.json b/cspell.json index ab6b0b7bf5fa..d15958b641d4 100644 --- a/cspell.json +++ b/cspell.json @@ -129,6 +129,7 @@ "linkedin", "linkswap", "locationchange", + "lockdown", "maarten", "macbinary", "majeure", From 5e80542525f2ce54de4835bf151a8051997e7570 Mon Sep 17 00:00:00 2001 From: Jack Works Date: Mon, 21 Mar 2022 17:06:54 +0800 Subject: [PATCH 7/7] fix: cspell --- packages/mask/.webpack/wasm-to-asm.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/mask/.webpack/wasm-to-asm.ts b/packages/mask/.webpack/wasm-to-asm.ts index 448eb73020e4..9cdce58d814b 100644 --- a/packages/mask/.webpack/wasm-to-asm.ts +++ b/packages/mask/.webpack/wasm-to-asm.ts @@ -4,6 +4,7 @@ import { LoaderContext } from 'webpack' export default function loader(this: LoaderContext, source: Buffer) { return new Promise((resolve, reject) => { + // cspell:ignore binaryen const cli = join(__dirname, '../node_modules/binaryen/bin/wasm2js') const child = spawn(process.execPath, [cli, relative(process.cwd(), this.resourcePath)], { cwd: process.cwd() }) const out: string[] = []