Skip to content

AI-native knowledge base & Evernote/Obsidian alternative with instant sync & native MCP. Free on Cloudflare or self-hosted via Docker.

fix(extension): bundle page capture as classic script

tianma-if312c4a027b37
diff --git a/apps/extension/capture-bundle.test.mjs b/apps/extension/capture-bundle.test.mjs
new file mode 100644
index 00000000..37f5d895
--- /dev/null
+++ b/apps/extension/capture-bundle.test.mjs
@@ -0,0 +1,24 @@
+import { expect, test } from "bun:test";
+import { mkdtemp, readFile, rm } from "node:fs/promises";
+import { tmpdir } from "node:os";
+import { join } from "node:path";
+import { Script } from "node:vm";
+import { fileURLToPath } from "node:url";
+import { build } from "vite";
+
+test("the injected page capture is a standalone classic script", async () => {
+ const outDir = await mkdtemp(join(tmpdir(), "edgeever-capture-"));
+ try {
+ await build({
+ configFile: fileURLToPath(new URL("./vite.config.ts", import.meta.url)),
+ logLevel: "silent",
+ build: { outDir },
+ });
+
+ const capture = await readFile(join(outDir, "assets/capture.js"), "utf8");
+ expect(() => new Script(capture)).not.toThrow();
+ expect(capture).toContain("capturedPage");
+ } finally {
+ await rm(outDir, { recursive: true, force: true });
+ }
+});
diff --git a/apps/extension/package.json b/apps/extension/package.json
index a42dd8e1..2f2703e3 100644
--- a/apps/extension/package.json
+++ b/apps/extension/package.json
@@ -9,7 +9,7 @@
"dev": "vite --config vite.config.ts",
"lint:firefox": "web-ext lint --source-dir dist-firefox --output text",
"package:firefox": "bun run build:firefox && bun run lint:firefox && web-ext build --source-dir dist-firefox --artifacts-dir web-ext-artifacts --overwrite-dest",
- "test": "bun test manifest.test.mjs context-menus.test.mjs popup.test.mjs image-clip.test.mjs tweet-clip.test.mjs selection-clip.test.mjs github-repo.test.mjs xhs-clip.test.mjs zhihu-clip.test.mjs reddit-clip.test.mjs"
+ "test": "bun test manifest.test.mjs context-menus.test.mjs popup.test.mjs capture-bundle.test.mjs image-clip.test.mjs tweet-clip.test.mjs selection-clip.test.mjs github-repo.test.mjs xhs-clip.test.mjs zhihu-clip.test.mjs reddit-clip.test.mjs"
},
"dependencies": {
"@mixmark-io/domino": "2.2.0",
diff --git a/apps/extension/vite.config.ts b/apps/extension/vite.config.ts
index 1d9479d4..be634a18 100644
--- a/apps/extension/vite.config.ts
+++ b/apps/extension/vite.config.ts
@@ -1,4 +1,5 @@
import { fileURLToPath, URL } from "node:url";
+import { build as buildStandaloneScript } from "esbuild";
import { defineConfig } from "vite";
import extensionPackage from "./package.json";
import { buildExtensionManifest, type ExtensionTarget } from "./manifest";
@@ -12,12 +13,30 @@ export default defineConfig({
plugins: [
{
name: "edgeever-extension-manifest",
- generateBundle() {
+ async generateBundle() {
this.emitFile({
type: "asset",
fileName: "manifest.json",
source: `${JSON.stringify(buildExtensionManifest(target, extensionPackage.version), null, 2)}\n`,
});
+
+ // chrome.scripting.executeScript({ files }) loads classic scripts, so
+ // capture must not inherit Vite's shared ESM chunks.
+ const capture = await buildStandaloneScript({
+ entryPoints: [fileURLToPath(new URL("./src/capture.ts", import.meta.url))],
+ outfile: "capture.js",
+ bundle: true,
+ format: "iife",
+ platform: "browser",
+ target: "es2022",
+ minify: true,
+ write: false,
+ });
+ this.emitFile({
+ type: "asset",
+ fileName: "assets/capture.js",
+ source: capture.outputFiles[0].contents,
+ });
},
},
],
@@ -30,7 +49,6 @@ export default defineConfig({
options: fileURLToPath(new URL("./options.html", import.meta.url)),
"image-save": fileURLToPath(new URL("./image-save.html", import.meta.url)),
background: fileURLToPath(new URL("./src/background.ts", import.meta.url)),
- capture: fileURLToPath(new URL("./src/capture.ts", import.meta.url)),
"capture-image": fileURLToPath(new URL("./src/capture-image.ts", import.meta.url)),
"capture-tweet": fileURLToPath(new URL("./src/capture-tweet.ts", import.meta.url)),
"capture-github": fileURLToPath(new URL("./src/capture-github.ts", import.meta.url)),