Skip to content

Commit

Permalink
[Enhance] modify to detect and reload manifest changes (#266)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonghakseo authored Nov 11, 2023
1 parent dbe8b00 commit 0a2b007
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 28 deletions.
7 changes: 4 additions & 3 deletions manifest.ts → manifest.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import packageJson from './package.json';
import packageJson from './package.json' assert { type: 'json' };

/**
* After changing, please reload the extension at `chrome://extensions`
* @type {chrome.runtime.ManifestV3}
*/
const manifest: chrome.runtime.ManifestV3 = {
const manifest = {
manifest_version: 3,
name: packageJson.name,
version: packageJson.version,
Expand All @@ -22,7 +23,7 @@ const manifest: chrome.runtime.ManifestV3 = {
newtab: 'src/pages/newtab/index.html',
},
icons: {
'128': 'icon-128.png',
128: 'icon-128.png',
},
content_scripts: [
{
Expand Down
21 changes: 13 additions & 8 deletions utils/plugins/make-manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ import type { PluginOption } from 'vite';

const { resolve } = path;

const distDir = resolve(__dirname, '..', '..', 'dist');
const rootDir = resolve(__dirname, '..', '..');
const distDir = resolve(rootDir, 'dist');
const manifestFile = resolve(rootDir, 'manifest.js');

export default function makeManifest(
manifest: chrome.runtime.ManifestV3,
config: { contentScriptCssKey?: string },
): PluginOption {
function makeManifest(to: string) {
const getManifestWithCacheBurst = () => import(manifestFile + '?' + Date.now().toString());

export default function makeManifest(config: { contentScriptCssKey?: string }): PluginOption {
function makeManifest(manifest: chrome.runtime.ManifestV3, to: string) {
if (!fs.existsSync(to)) {
fs.mkdirSync(to);
}
Expand All @@ -32,8 +33,12 @@ export default function makeManifest(

return {
name: 'make-manifest',
writeBundle() {
makeManifest(distDir);
buildStart() {
this.addWatchFile(manifestFile);
},
async writeBundle() {
const manifest = await getManifestWithCacheBurst();
makeManifest(manifest.default, distDir);
},
};
}
9 changes: 0 additions & 9 deletions utils/plugins/watch-rebuild.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,12 @@
import type { PluginOption } from 'vite';
import { resolve } from 'path';
import { WebSocket } from 'ws';
import MessageInterpreter from '../reload/interpreter';
import { LOCAL_RELOAD_SOCKET_URL } from '../reload/constant';

const rootDir = resolve(__dirname, '..', '..');
const manifestFile = resolve(rootDir, 'manifest.ts');
const viteConfigFile = resolve(rootDir, 'vite.config.ts');

export default function watchRebuild(): PluginOption {
const ws = new WebSocket(LOCAL_RELOAD_SOCKET_URL);
return {
name: 'watch-rebuild',
buildStart() {
this.addWatchFile(manifestFile);
this.addWatchFile(viteConfigFile);
},
writeBundle() {
/**
* When the build is complete, send a message to the reload server.
Expand Down
6 changes: 6 additions & 0 deletions utils/reload/initReloadClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ let needToUpdate = false;
export default function initReloadClient({
watchPath,
onUpdate,
onForceReload,
}: {
watchPath: string;
onUpdate: () => void;
onForceReload?: () => void;
}): WebSocket {
const socket = new WebSocket(LOCAL_RELOAD_SOCKET_URL);

Expand All @@ -34,6 +36,10 @@ export default function initReloadClient({
}
return;
}
case 'force_reload': {
onForceReload?.();
return;
}
}
});

Expand Down
12 changes: 12 additions & 0 deletions utils/reload/initReloadServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import MessageInterpreter from './interpreter';
import { debounce } from './utils';

const clientsThatNeedToUpdate: Set<WebSocket> = new Set();
let needToForceReload = false;

function initReloadServer() {
const wss = new WebSocketServer({ port: LOCAL_RELOAD_SOCKET_PORT });
Expand All @@ -25,6 +26,12 @@ function initReloadServer() {
}
if (message.type === 'build_complete') {
clientsThatNeedToUpdate.forEach((ws: WebSocket) => ws.send(MessageInterpreter.send({ type: 'do_update' })));
if (needToForceReload) {
needToForceReload = false;
clientsThatNeedToUpdate.forEach((ws: WebSocket) =>
ws.send(MessageInterpreter.send({ type: 'force_reload' })),
);
}
}
});
});
Expand All @@ -40,4 +47,9 @@ const debounceSrc = debounce(function (path: string) {
}, 100);
chokidar.watch('src', { ignorePermissionErrors: true }).on('all', (_, path) => debounceSrc(path));

/** CHECK:: manifest.js was updated **/
chokidar.watch('manifest.js', { ignorePermissionErrors: true }).on('all', () => {
needToForceReload = true;
});

initReloadServer();
9 changes: 6 additions & 3 deletions utils/reload/injections/script.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import initReloadClient from '../initReloadClient';

export default function addHmrIntoScript(watchPath: string) {
const reload = () => {
chrome.runtime.reload();
};

initReloadClient({
watchPath,
onUpdate: () => {
chrome.runtime.reload();
},
onUpdate: reload,
onForceReload: reload,
});
}
4 changes: 3 additions & 1 deletion utils/reload/interpreter/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ type UpdateRequestMessage = {
};
type UpdateCompleteMessage = { type: 'done_update' };
type BuildCompletionMessage = { type: 'build_complete' };
type ForceReloadMessage = { type: 'force_reload' };

export type SerializedMessage = string;
export type WebSocketMessage =
| UpdateCompleteMessage
| UpdateRequestMessage
| UpdatePendingMessage
| BuildCompletionMessage;
| BuildCompletionMessage
| ForceReloadMessage;
7 changes: 3 additions & 4 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import makeManifest from './utils/plugins/make-manifest';
import customDynamicImport from './utils/plugins/custom-dynamic-import';
import addHmr from './utils/plugins/add-hmr';
import watchRebuild from './utils/plugins/watch-rebuild';
import manifest from './manifest';

const rootDir = resolve(__dirname);
const srcDir = resolve(rootDir, 'src');
Expand All @@ -30,10 +29,10 @@ export default defineConfig({
},
},
plugins: [
react(),
makeManifest(manifest, {
makeManifest({
contentScriptCssKey: regenerateCacheInvalidationKey(),
}),
react(),
customDynamicImport(),
addHmr({ background: enableHmrInBackgroundScript, view: true }),
isDev && watchRebuild(),
Expand All @@ -46,7 +45,7 @@ export default defineConfig({
minify: isProduction,
modulePreload: false,
reportCompressedSize: isProduction,
emptyOutDir: false,
emptyOutDir: !isDev,
rollupOptions: {
input: {
devtools: resolve(pagesDir, 'devtools', 'index.html'),
Expand Down

0 comments on commit 0a2b007

Please sign in to comment.