From d1b5e4ec76e3e8d21a62e0a74b2a7f2e2c736c26 Mon Sep 17 00:00:00 2001 From: Jack Works Date: Mon, 21 Mar 2022 19:26:29 +0800 Subject: [PATCH] feat: move gun out of webpack compile --- packages/gun-utils/.gitignore | 1 + packages/gun-utils/builder.mjs | 71 +++++++++++++++++++++++ packages/gun-utils/package.json | 3 + packages/gun-utils/src/instance.ts | 7 +-- packages/mask/.webpack/config.ts | 15 ++++- packages/mask/.webpack/template.html | 1 + packages/mask/public/manifest-v3.entry.js | 2 +- packages/scripts/src/extension/normal.ts | 6 +- packages/scripts/src/index.ts | 1 + packages/scripts/src/projects/gun.ts | 7 +++ 10 files changed, 101 insertions(+), 13 deletions(-) create mode 100644 packages/gun-utils/.gitignore create mode 100644 packages/gun-utils/builder.mjs create mode 100644 packages/scripts/src/projects/gun.ts diff --git a/packages/gun-utils/.gitignore b/packages/gun-utils/.gitignore new file mode 100644 index 000000000000..54bb81a2508f --- /dev/null +++ b/packages/gun-utils/.gitignore @@ -0,0 +1 @@ +gun.js diff --git a/packages/gun-utils/builder.mjs b/packages/gun-utils/builder.mjs new file mode 100644 index 000000000000..dba90702b314 --- /dev/null +++ b/packages/gun-utils/builder.mjs @@ -0,0 +1,71 @@ +import { createRequire } from 'module' +import { readFile, writeFile } from 'fs/promises' +const require = createRequire(import.meta.url) +const files = await Promise.all( + [ + require.resolve('gun/gun.js'), + require.resolve('gun/sea'), + require.resolve('gun/lib/radix'), + require.resolve('gun/lib/radisk'), + require.resolve('gun/lib/store'), + require.resolve('gun/lib/rindexed'), + ].map((path) => readFile(path, 'utf8')), +) +function init() { + 'use strict' + // This log is required by Gun's license. + globalThis.console.log( + 'Hello wonderful person! :) Thanks for using GUN, please ask for help on http://chat.gun.eco if anything takes you longer than 5min to figure out!', + ) + function setTimeout() { + return globalThis.setTimeout.apply(globalThis, arguments) + } + const location = new URL('https://example.com') + const Object = { + prototype: globalThis.Object.prototype, + keys: globalThis.Object.keys, + create: globalThis.Object.create, + assign: globalThis.Object.assign, + } + const console = { log() {} } + function String() { + return globalThis.String.apply(this, arguments) + } + const window = { + setTimeout, + location, + Object, + console, + String, + TextEncoder: globalThis.TextEncoder, + TextDecoder: globalThis.TextDecoder, + crypto: globalThis.crypto, + get localStorage() { + return globalThis.localStorage + }, + get sessionStorage() { + return globalThis.sessionStorage + }, + indexedDB: globalThis.indexedDB, + } + try { + // Source Code Here + } finally { + return window + } +} + +const patchedSource = files + .join('\n;') + // patch .constructor != Object to .constructor == globalThis.Object + .replace(/!==?\s+Object/g, '!== globalThis.Object') + .replace(/===?\s+Object/g, '=== globalThis.Object') + // patch instanceof Object to instanceof globalThis.Object + .replace(/instanceof\s+Object/g, 'instanceof globalThis.Object') + +const result = ` +${init.toString().replace('// Source Code Here', patchedSource)}; +globalThis.Gun = ${init.name}().Gun; +undefined; +` +writeFile(new URL('./gun.js', import.meta.url), result) diff --git a/packages/gun-utils/package.json b/packages/gun-utils/package.json index acfcfb609881..8616b28293ef 100644 --- a/packages/gun-utils/package.json +++ b/packages/gun-utils/package.json @@ -7,5 +7,8 @@ "dependencies": { "event-iterator": "^2.0.0", "gun": "^0.2020.1234" + }, + "scripts": { + "build": "node ./builder.mjs" } } diff --git a/packages/gun-utils/src/instance.ts b/packages/gun-utils/src/instance.ts index 71e3f1904ad4..f9f45022f965 100644 --- a/packages/gun-utils/src/instance.ts +++ b/packages/gun-utils/src/instance.ts @@ -1,9 +1,4 @@ -import Gun from 'gun' -import 'gun/sea' -import 'gun/lib/radix' -import 'gun/lib/radisk' -import 'gun/lib/store' -import 'gun/lib/rindexed' +declare const Gun: typeof import('gun') import { gunServers } from './server' export type GunRoot = ReturnType diff --git a/packages/mask/.webpack/config.ts b/packages/mask/.webpack/config.ts index 37164a5b0fa0..fdbace324f36 100644 --- a/packages/mask/.webpack/config.ts +++ b/packages/mask/.webpack/config.ts @@ -117,8 +117,6 @@ export function createConfiguration(rawFlags: BuildFlags): Configuration { javascript: { // Treat as missing export as error strictExportPresence: true, - // gun and @unstoppabledomains/resolution - exprContextCritical: false, }, }, rules: [ @@ -203,6 +201,7 @@ export function createConfiguration(rawFlags: BuildFlags): Configuration { patterns: [ { from: join(__dirname, '../public/'), to: distFolder }, { from: join(__dirname, '../../injected-script/dist/injected-script.js'), to: distFolder }, + { from: join(__dirname, '../../gun-utils/gun.js'), to: distFolder }, { from: join(__dirname, '../../mask-sdk/dist/mask-sdk.js'), to: distFolder }, { context: join(__dirname, '../../polyfills/dist/'), @@ -307,6 +306,7 @@ export function createConfiguration(rawFlags: BuildFlags): Configuration { chunks: ['background'], filename: 'background.html', secp256k1: true, + gun: true, sourceMap: !!sourceMapKind, }), ) @@ -331,7 +331,13 @@ export function createConfiguration(rawFlags: BuildFlags): Configuration { } } } -function addHTMLEntry(options: HTMLPlugin.Options & { secp256k1?: boolean; sourceMap: boolean }) { +function addHTMLEntry( + options: HTMLPlugin.Options & { + secp256k1?: boolean + sourceMap: boolean + gun?: boolean + }, +) { let templateContent = readFileSync(join(__dirname, './template.html'), 'utf8') if (options.secp256k1) { templateContent = templateContent.replace( @@ -339,6 +345,9 @@ function addHTMLEntry(options: HTMLPlugin.Options & { secp256k1?: boolean; sourc '', ) } + if (options.gun) { + templateContent = templateContent.replace(``, '') + } if (options.sourceMap) { templateContent = templateContent.replace( ``, diff --git a/packages/mask/.webpack/template.html b/packages/mask/.webpack/template.html index 5a41e91c2b56..11a6e47c32df 100644 --- a/packages/mask/.webpack/template.html +++ b/packages/mask/.webpack/template.html @@ -7,6 +7,7 @@ + diff --git a/packages/mask/public/manifest-v3.entry.js b/packages/mask/public/manifest-v3.entry.js index be77dc5a247e..bf7ff5339f2a 100644 --- a/packages/mask/public/manifest-v3.entry.js +++ b/packages/mask/public/manifest-v3.entry.js @@ -26,4 +26,4 @@ }, }) } -importScripts('./worker_env.js', './polyfill/browser-polyfill.js', './js/background.js') +importScripts('./worker_env.js', './gun.js', './polyfill/browser-polyfill.js', './js/background.js') diff --git a/packages/scripts/src/extension/normal.ts b/packages/scripts/src/extension/normal.ts index 906f8975ba8d..22c2ccdb8e22 100644 --- a/packages/scripts/src/extension/normal.ts +++ b/packages/scripts/src/extension/normal.ts @@ -7,19 +7,19 @@ import { awaitChildProcess, PKG_PATH, watchTask } from '../utils' import { buildInjectedScript, watchInjectedScript } from '../projects/injected-scripts' import { buildMaskSDK, watchMaskSDK } from '../projects/mask-sdk' import { buildPolyfill } from '../projects/polyfill' +import { buildGun } from '../projects/gun' const presets = ['chromium', 'firefox', 'android', 'iOS', 'base'] as const const otherFlags = ['beta', 'insider', 'reproducible', 'profile', 'mv3', 'readonlyCache', 'progress'] as const export async function extension(f?: Function | ExtensionBuildArgs) { - await buildPolyfill() - await buildInjectedScript() - await buildMaskSDK() + await Promise.all([buildPolyfill(), buildInjectedScript(), buildMaskSDK(), buildGun()]) if (typeof f === 'function') return awaitChildProcess(webpack('build')) return awaitChildProcess(webpack('build', f)) } export async function extensionWatch(f?: Function | ExtensionBuildArgs) { buildPolyfill() + buildGun() watchInjectedScript() watchMaskSDK() if (typeof f === 'function') return awaitChildProcess(webpack('dev')) diff --git a/packages/scripts/src/index.ts b/packages/scripts/src/index.ts index 46fb098cb97b..bb86f5bd7d03 100644 --- a/packages/scripts/src/index.ts +++ b/packages/scripts/src/index.ts @@ -21,3 +21,4 @@ export { reorderSpellcheck } from './spellcheck' export { buildInjectedScript, watchInjectedScript } from './projects/injected-scripts' export { buildMaskSDK, watchMaskSDK } from './projects/mask-sdk' export { buildPolyfill } from './projects/polyfill' +export { buildGun } from './projects/gun' diff --git a/packages/scripts/src/projects/gun.ts b/packages/scripts/src/projects/gun.ts new file mode 100644 index 000000000000..d7c2d6fbf6c5 --- /dev/null +++ b/packages/scripts/src/projects/gun.ts @@ -0,0 +1,7 @@ +import { fromNPMTask } from '../utils' +import { resolve } from 'path' +export const [buildGun] = fromNPMTask( + resolve(__dirname, '../../../gun-utils'), + 'gun', + 'Build gun required for Mask Network.', +)