Skip to content

Commit

Permalink
Merge pull request #375 from cocrafts/kayaba/chrome-web-store-fix-con…
Browse files Browse the repository at this point in the history
…tinued

build: chrome web store fix continued
  • Loading branch information
fluctlight-kayaba authored Dec 4, 2023
2 parents af6e0e7 + a99d117 commit 1894a75
Show file tree
Hide file tree
Showing 17 changed files with 412 additions and 316 deletions.
56 changes: 28 additions & 28 deletions apps/landing/package.json
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
{
"name": "@walless/landing",
"version": "0.0.1",
"private": true,
"scripts": {
"dev": "next dev -p 3005",
"build": "next build",
"start": "next start",
"lint": "next lint",
"lint:fix": "next lint --fix"
},
"dependencies": {
"@tamagui/animations-css": "1.15.16",
"@tamagui/next-theme": "1.15.16",
"@walless/gui": "*",
"@walless/markdown": "*",
"@walless/ui": "*",
"firebase": "10.5.0",
"html-to-image": "^1.11.11",
"next": "13.4.9",
"react-dom": "^18.2.0",
"valtio": "1.10.5"
},
"devDependencies": {
"@tamagui/next-plugin": "1.15.16",
"eslint": "8.38.0",
"next-compose-plugins": "^2.2.1",
"next-transpile-modules": "10.0.0"
}
"name": "@walless/landing",
"version": "0.0.1",
"private": true,
"scripts": {
"dev": "next dev -p 3005",
"build": "next build",
"start": "next start",
"lint": "next lint",
"lint:fix": "next lint --fix"
},
"dependencies": {
"@tamagui/animations-css": "1.15.16",
"@tamagui/next-theme": "1.15.16",
"@walless/gui": "*",
"@walless/markdown": "*",
"@walless/ui": "*",
"firebase": "10.7.0",
"html-to-image": "^1.11.11",
"next": "13.4.9",
"react-dom": "^18.2.0",
"valtio": "1.10.5"
},
"devDependencies": {
"@tamagui/next-plugin": "1.15.16",
"eslint": "8.38.0",
"next-compose-plugins": "^2.2.1",
"next-transpile-modules": "10.0.0"
}
}
6 changes: 3 additions & 3 deletions apps/mobile/src/utils/ioc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ export const injectModules = async () => {
modules.engine = await createEngine();
modules.engine.start();

const endTime = new Date();
const milliseconds = endTime.getMilliseconds() - startTime.getMilliseconds();
console.log(`Took ${milliseconds} milliseconds to configure IoC module.`);
const milliseconds =
new Date().getMilliseconds() - startTime.getMilliseconds();
console.log(`Configured IoC module in ${milliseconds}ms`);

return modules;
};
Expand Down
75 changes: 51 additions & 24 deletions apps/web/metacraft.config.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,64 @@
const { web3Polyfills } = require('@metacraft/cli-web3-polyfills');
const { copyAssets } = require('../../tool/webpack/asset');
const { useCache } = require('../../tool/webpack/optimization');
const { setEnvironments } = require('../../tool/webpack/env');

const isProd = process.env.ENV === 'production';
const isExtension = process.env.BUILD_TARGET === 'extension';

const injectEntries = (config) => {
config.entry.content = {
import: 'scripts/content/index.ts',
filename: 'content.js',
};
if (isExtension) {
config.entry.content = {
import: 'scripts/content/index.ts',
filename: 'content.js',
};

config.entry.injection = {
import: 'scripts/content/injection.ts',
filename: 'injection.js',
};
config.entry.injection = {
import: 'scripts/content/injection.ts',
filename: 'injection.js',
};

config.entry.background = {
import: 'scripts/background/index.ts',
filename: 'background.js',
};
config.entry.kernel = {
import: 'scripts/kernel/index.ts',
filename: 'kernel.js',
};
} else {
config.entry.w3ar = {
import: 'scripts/worker/w3a-response.ts',
filename: 'w3a-response.js',
};

config.entry.kernel = {
import: 'scripts/kernel/index.ts',
filename: 'kernel.js',
};
config.entry.fcm = {
import: 'scripts/worker/fcm.ts',
filename: 'firebase-messaging-sw.js',
};
}

config.entry.w3ar = {
import: 'scripts/worker/w3a-response.ts',
filename: 'w3a-response.js',
return config;
};

const registerExtFile = (config) => {
if (isExtension) {
config.resolve.extensions = [
'.ext.ts',
'.ext.tsx',
...config.resolve.extensions,
];
}

return config;
};

const buildOptimization = (config) => {
config.cache = {
type: 'filesystem',
};

config.entry.fcm = {
import: 'scripts/worker/fcm.ts',
filename: 'firebase-messaging-sw.js',
config.optimization = {
splitChunks: {
chunks: (chunk) => {
return ['content', 'injection'].indexOf(chunk.name) < 0;
},
},
};

return config;
Expand Down Expand Up @@ -82,8 +108,9 @@ module.exports = {
buildId: () => 'app',
swcOptions,
webpackMiddlewares: [
useCache,
buildOptimization,
copyAssets,
registerExtFile,
injectEntries,
web3Polyfills,
setEnvironments(),
Expand Down
8 changes: 5 additions & 3 deletions apps/web/package.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
{
"name": "@walless/web",
"version": "1.0.3",
"version": "1.0.2",
"private": true,
"main": "index.web.js",
"scripts": {
"dev": "metacraft",
"dev:ext": "BUILD_TARGET=extension && metacraft",
"batch": "node ../../tool/batch",
"build": "yarn build:ext && yarn build:web",
"build:web": "rimraf metacraft build && yarn batch && metacraft bundle",
"build:web": "rimraf metacraft builds && yarn batch && metacraft bundle",
"build:ext": "BUILD_TARGET=extension && yarn build:web && node scripts/bundler",
"build:store": "BUILD_TARGET=extension && yarn build:web && yarn badlinks && node scripts/bundler",
"lint": "eslint . --max-warnings=0 --ext .ts,.tsx",
"lint:fix": "eslint . --fix --max-warnings=0 --ext .ts,.tsx",
"badlinks": "npx badlinks metacraft -r",
"postinstall": "yarn batch"
},
"dependencies": {
Expand All @@ -38,7 +40,7 @@
"bip39": "^3.1.0",
"bn.js": "^5.2.1",
"ed25519-hd-key": "^1.3.0",
"firebase": "10.5.0",
"firebase": "10.7.0",
"graphql-request": "^6.1.0",
"numeral": "^2.0.6",
"pouchdb-adapter-http": "^8.0.1",
Expand Down
23 changes: 0 additions & 23 deletions apps/web/scripts/background/index.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,3 @@
const keepBackgroundAlive = () => {
const runtime = global.chrome?.runtime;
if (runtime) {
if (runtime?.id) {
// Keep Manifest V3 service worker alive
chrome.runtime.onInstalled.addListener(() => {
chrome.alarms.get('keep-alive', (a) => {
if (!a) {
chrome.alarms.create('keep-alive', { periodInMinutes: 0.1 });
}
});
});
}

// Add a noop listener to the alarm. Without this, the service worker seems
// to be deemed as idle by Chrome and will be killed after 30s.
chrome.alarms.onAlarm.addListener(() => {
Function.prototype();
});
}
};

keepBackgroundAlive();
importScripts('kernel.js');

export default {};
31 changes: 30 additions & 1 deletion apps/web/scripts/bundler/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require('dotenv').config({ path: '.env.production' });

const fs = require('fs-extra');
const { readFileSync } = require('fs');
const { readFileSync, readdirSync, writeFileSync } = require('fs');
const archiver = require('archiver');
const manifest = require('./manifest.json');
const project = require('../../package.json');
Expand Down Expand Up @@ -30,6 +30,8 @@ const cloneExtensionBuild = async (platform, override = {}) => {
const indexTemplate = readFileSync('./metacraft/index.html', 'utf8');
const popupTemplate = indexTemplate.replace('<body>', '<body class="popup">');

makeBackgroundJs(['kernel.js']);

if (platform === 'chrome') {
mergedManifest.permissions = [...mergedManifest.permissions, 'gcm'];
}
Expand All @@ -52,6 +54,33 @@ const cloneExtensionBuild = async (platform, override = {}) => {
await zipDir(platform);
};

const isChunkFile = (name) => {
const [id, extension] = name.split('.');
return !isNaN(id) && extension === 'js';
};

const makeBackgroundJs = (entries) => {
const buildFiles = readdirSync('./metacraft');
const existed = buildFiles.find((i) => i === 'background.js');

if (!existed) {
let fileContent = '';
const chunkFiles = buildFiles.filter(isChunkFile);

for (const chunk of chunkFiles) {
fileContent += `importScripts('${chunk}');\n`;
}

for (const entry of entries) {
fileContent += `importScripts('${entry}');\n`;
}

writeFileSync('./metacraft/background.js', fileContent, {
encoding: 'utf-8',
});
}
};

cloneExtensionBuild('chrome');
cloneExtensionBuild('edge');
cloneExtensionBuild('brave');
Expand Down
6 changes: 4 additions & 2 deletions apps/web/scripts/content/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import { injectScript } from './utils';

(async () => {
await initializeMessaging();
injectScript('injection.js');
console.log('[walless/prod] messaging module intialzied..');

console.log('content script loaded');
setTimeout(() => {
injectScript('injection.js');
}, 1000); /* delay to make sure messaging ready to serve! */
})();
1 change: 1 addition & 0 deletions apps/web/scripts/content/injection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const configureWalletStandard = async (): Promise<void> => {
const walless = new Walless();

try {
console.log('[walless/prod] registering wallet-standard');
Object.defineProperty(window, 'walless', { value: walless });
} catch {
console.warn('Could not define Walless, namespace already taken.');
Expand Down
7 changes: 5 additions & 2 deletions apps/web/scripts/kernel/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import { runtime } from '@walless/core';

import { keepBackgroundAlive } from './utils/extension';
import { injectModules } from './utils/ioc';
import { initializeMessaging } from './messaging';
import { configurePWA } from './pwa';

console.log('Init kernel', new Date().toISOString());
console.log(new Date().toISOString(), 'Initializing kernel..');

injectModules().then(async () => {
await Promise.all([initializeMessaging()]);
});

if (!runtime.isExtension) {
if (runtime.isExtension) {
keepBackgroundAlive();
} else {
configurePWA();
}
22 changes: 22 additions & 0 deletions apps/web/scripts/kernel/utils/extension.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const runtime = global.chrome?.runtime;

export const keepBackgroundAlive = () => {
if (runtime) {
if (runtime?.id) {
// Keep Manifest V3 service worker alive
chrome.runtime.onInstalled.addListener(() => {
chrome.alarms.get('keep-alive', (a) => {
if (!a) {
chrome.alarms.create('keep-alive', { periodInMinutes: 0.1 });
}
});
});
}

// Add a noop listener to the alarm. Without this, the service worker seems
// to be deemed as idle by Chrome and will be killed after 30s.
chrome.alarms.onAlarm.addListener(() => {
Function.prototype();
});
}
};
Loading

0 comments on commit 1894a75

Please sign in to comment.