Skip to content

Commit

Permalink
extension reloading webpack plugin (#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
turbocrime committed Jul 10, 2024
1 parent 801ad3c commit e8f4891
Show file tree
Hide file tree
Showing 6 changed files with 1,699 additions and 21 deletions.
1 change: 1 addition & 0 deletions apps/extension/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
bin
auto-login.ts
!.env
chromium-profile
1 change: 1 addition & 0 deletions apps/extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
"tailwindcss": "^3.4.4",
"ts-loader": "^9.5.1",
"ts-node": "^10.9.2",
"web-ext": "^8.2.0",
"webpack": "^5.91.0",
"webpack-cli": "^5.1.4",
"webpack-merge": "^5.10.0",
Expand Down
19 changes: 19 additions & 0 deletions apps/extension/web-ext.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
declare module 'web-ext' {
export interface WebExtRunner {
run(): Promise<void>;
reloadAllExtensions(): Promise<unknown[]>;
registerCleanup(fn: () => void): void;
exit(): Promise<void>;
}

export const cmd: {
run: (opt: {
target: 'firefox-desktop' | 'firefox-android' | 'chromium';
sourceDir?: string | string[];
startUrl?: string | string[];
keepProfileChanges?: boolean;
profileCreateIfMissing?: boolean;
chromiumProfile?: string;
}) => Promise<WebExtRunner>;
};
}
28 changes: 28 additions & 0 deletions apps/extension/webpack.config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// eslint-disable-next-line @typescript-eslint/triple-slash-reference
/// <reference path="web-ext.d.ts" />

// eslint-disable-next-line import/no-relative-packages
import rootPackageJson from '../../package.json' with { type: 'json' };

Expand All @@ -7,6 +10,7 @@ import HtmlWebpackPlugin from 'html-webpack-plugin';
import { spawn } from 'node:child_process';
import path from 'node:path';
import url from 'node:url';
import { type WebExtRunner, cmd as WebExtCmd } from 'web-ext';
import webpack from 'webpack';
import WatchExternalFilesPlugin from 'webpack-watch-external-files-plugin';

Expand All @@ -26,6 +30,8 @@ const srcDir = path.join(__dirname, 'src');
const entryDir = path.join(srcDir, 'entry');
const injectDir = path.join(srcDir, 'content-scripts');

const CHROMIUM_PROFILE = process.env['CHROMIUM_PROFILE'];

/*
* The DefinePlugin replaces specified tokens with specified values.
* - These should be declared in `prax.d.ts` for TypeScript awareness.
Expand All @@ -39,6 +45,27 @@ const DefinePlugin = new webpack.DefinePlugin({
'globalThis.__ASSERT_ROOT__': JSON.stringify(false),
});

const WebExtReloadPlugin = {
webExtRun: undefined as WebExtRunner | undefined,
apply({ hooks }: webpack.Compiler) {
hooks.afterEmit.tapPromise(
{ name: 'WebExt Reloader' },
async ({ options }: webpack.Compilation) => {
await this.webExtRun?.reloadAllExtensions();
this.webExtRun ??= await WebExtCmd.run({
target: 'chromium',
chromiumProfile: CHROMIUM_PROFILE,
keepProfileChanges: Boolean(CHROMIUM_PROFILE),
profileCreateIfMissing: Boolean(CHROMIUM_PROFILE),
sourceDir: options.output.path,
startUrl: 'https://localhost:5173/',
});
this.webExtRun.registerCleanup(() => (this.webExtRun = undefined));
},
);
},
};

/**
* This custom plugin will run `pnpm install` before each watch-mode build. This
* combined with WatchExternalFilesPlugin will ensure that tarball dependencies
Expand Down Expand Up @@ -193,6 +220,7 @@ export default ({
// watch tarballs for changes
WEBPACK_WATCH && new WatchExternalFilesPlugin({ files: localPackages }),
WEBPACK_WATCH && PnpmInstallPlugin,
CHROMIUM_PROFILE && WebExtReloadPlugin,
],
experiments: {
asyncWebAssembly: true,
Expand Down
Loading

0 comments on commit e8f4891

Please sign in to comment.