Skip to content

⚡️⚡️⚡️ Self-hosted collaborative bookmark manager to collect, read, annotate, and fully preserve what matters, all in one place.

refactor(extension): generate the manifest from a single config file

daniel31x1326013d59f32f
diff --git a/apps/extension/README.md b/apps/extension/README.md
index 35211c7e..8e62db0e 100644
--- a/apps/extension/README.md
+++ b/apps/extension/README.md
@@ -66,9 +66,9 @@ yarn dev:firefox
```
Either command builds the extension, launches the browser with it already installed,
-and reloads it whenever a file under `src/` changes. Both run off the same
-`manifest.json` and `dist/` folder, so there is nothing else to set up. Firefox
-reports that `background.service_worker` is ignored, which is expected: it uses
+and reloads it whenever a file under `src/` changes. Both run off the same manifest
+and `dist/` folder, so there is nothing else to set up. Firefox reports that
+`background.service_worker` is ignored, which is expected: it uses
`background.scripts` from that same manifest.
The browser starts from a temporary profile, so your Linkwarden server settings are
@@ -78,6 +78,18 @@ gone on the next start. Add `--keep-profile-changes --chromium-profile ./.profil
To load the extension into a browser yourself, `yarn dev` runs only the watching
build and keeps `dist/` up to date.
+### The manifest
+
+`manifest.config.ts` is the only manifest source. It describes one shared manifest
+plus the few keys that differ per target, and a vite plugin writes the result to
+`manifest.json` in the build output. There are two targets: `default` for the
+Chrome and Firefox build in `dist/`, and `safari` for `dist-safari/`, selected by
+the `EXT_TARGET` environment variable that `yarn build:safari` sets.
+
+Version bumps and permission changes go in that one file. Anything Safari cannot
+do (the bookmarks permission, the service worker, the omnibox keyword) is a flag
+on the target rather than a second copy of the manifest.
+
## Safari
Safari is built through the Xcode project in `safari/`, which is committed to this
diff --git a/apps/extension/manifest.config.ts b/apps/extension/manifest.config.ts
new file mode 100644
index 00000000..be8790a3
--- /dev/null
+++ b/apps/extension/manifest.config.ts
@@ -0,0 +1,118 @@
+/**
+ * Single source of truth for the extension manifest.
+ *
+ * Every browser gets the same manifest apart from the handful of keys collected
+ * in `targets` below. The vite plugin in vite.config.ts writes the result into
+ * the build output, so there is no manifest.json checked into the repo.
+ */
+
+/**
+ * `default` is the Chrome and Firefox build (`dist/`), which the two browsers
+ * share, so it carries the keys for both. `safari` is `dist-safari/`.
+ */
+export type ManifestTarget = "default" | "safari";
+
+type TargetOptions = {
+ /** `minimum_chrome_version`, left off where it means nothing. */
+ minimumChromeVersion?: string;
+ /** Safari web extensions have no bookmarks API. */
+ bookmarks: boolean;
+ /** Chrome runs the background as a service worker; the others use `scripts`. */
+ serviceWorker: boolean;
+ /** Address bar keyword, which Safari does not support. */
+ omnibox: boolean;
+ browserSpecificSettings: Record<string, unknown>;
+};
+
+const targets: Record<ManifestTarget, TargetOptions> = {
+ default: {
+ minimumChromeVersion: "121",
+ bookmarks: true,
+ serviceWorker: true,
+ omnibox: true,
+ browserSpecificSettings: {
+ gecko: {
+ strict_min_version: "121.0",
+ },
+ },
+ },
+ safari: {
+ bookmarks: false,
+ serviceWorker: false,
+ omnibox: false,
+ browserSpecificSettings: {
+ safari: {
+ strict_min_version: "15.4",
+ },
+ },
+ },
+};
+
+const version = "1.5.4";
+
+const icons = {
+ "16": "16.png",
+ "32": "32.png",
+ "48": "48.png",
+ "128": "128.png",
+};
+
+export function buildManifest(target: ManifestTarget) {
+ const options = targets[target];
+
+ return {
+ manifest_version: 3,
+ ...(options.minimumChromeVersion
+ ? { minimum_chrome_version: options.minimumChromeVersion }
+ : {}),
+ name: "Linkwarden",
+ description:
+ "Save webpages, capture screenshots, and organize links in your Linkwarden collections.",
+ homepage_url: "https://linkwarden.app/",
+ version,
+ action: {
+ default_popup: "index.html",
+ default_icon: icons,
+ default_title: "Linkwarden",
+ },
+ options_ui: {
+ page: "src/pages/Options/options.html",
+ browser_style: false,
+ },
+ icons,
+ permissions: [
+ "storage",
+ "scripting",
+ "activeTab",
+ "tabs",
+ ...(options.bookmarks ? ["bookmarks"] : []),
+ "contextMenus",
+ ],
+ host_permissions: ["<all_urls>"],
+ background: {
+ ...(options.serviceWorker ? { service_worker: "background.js" } : {}),
+ scripts: ["background.js"],
+ type: "module",
+ },
+ content_security_policy: {
+ extension_pages:
+ "script-src 'self'; object-src 'self'; connect-src 'self' http: https:;",
+ },
+ ...(options.omnibox ? { omnibox: { keyword: "lk" } } : {}),
+ commands: {
+ _execute_action: {
+ suggested_key: { default: "Ctrl+Shift+F", mac: "Command+Shift+Y" },
+ },
+ },
+ browser_specific_settings: options.browserSpecificSettings,
+ };
+}
+
+export function resolveTarget(value = process.env.EXT_TARGET): ManifestTarget {
+ if (value === undefined || value === "default") return "default";
+ if (value === "safari") return "safari";
+ throw new Error(
+ `Unknown EXT_TARGET "${value}". Expected "default" or "safari".`
+ );
+}
diff --git a/apps/extension/manifest.json b/apps/extension/manifest.json
deleted file mode 100644
index 83639840..00000000
--- a/apps/extension/manifest.json
+++ /dev/null
@@ -1,52 +0,0 @@
-{
- "manifest_version": 3,
- "minimum_chrome_version": "121",
- "name": "Linkwarden",
- "description": "Save webpages, capture screenshots, and organize links in your Linkwarden collections.",
- "homepage_url": "https://linkwarden.app/",
- "version": "1.5.4",
- "action": {
- "default_popup": "index.html",
- "default_icon": {
- "16": "16.png",
- "32": "32.png",
- "48": "48.png",
- "128": "128.png"
- },
- "default_title": "Linkwarden"
- },
- "options_ui": {
- "page": "src/pages/Options/options.html",
- "browser_style": false
- },
- "icons": { "16": "16.png", "32": "32.png", "48": "48.png", "128": "128.png" },
- "permissions": [
- "storage",
- "scripting",
- "activeTab",
- "tabs",
- "bookmarks",
- "contextMenus"
- ],
- "host_permissions": ["<all_urls>"],
- "background": {
- "service_worker": "background.js",
- "scripts": ["background.js"],
- "type": "module"
- },
- "content_security_policy": {
- "extension_pages": "script-src 'self'; object-src 'self'; connect-src 'self' http: https:;"
- },
- "omnibox": { "keyword": "lk" },
- "commands": {
- "_execute_action": {
- "suggested_key": { "default": "Ctrl+Shift+F", "mac": "Command+Shift+Y" }
- }
- },
- "browser_specific_settings": {
- "gecko": {
- "strict_min_version": "121.0"
- }
- }
-}
diff --git a/apps/extension/manifest.safari.json b/apps/extension/manifest.safari.json
deleted file mode 100644
index fb11fda7..00000000
--- a/apps/extension/manifest.safari.json
+++ /dev/null
@@ -1,47 +0,0 @@
-{
- "manifest_version": 3,
- "name": "Linkwarden",
- "description": "Save webpages, capture screenshots, and organize links in your Linkwarden collections.",
- "homepage_url": "https://linkwarden.app/",
- "version": "1.5.4",
- "action": {
- "default_popup": "index.html",
- "default_icon": {
- "16": "16.png",
- "32": "32.png",
- "48": "48.png",
- "128": "128.png"
- },
- "default_title": "Linkwarden"
- },
- "options_ui": {
- "page": "src/pages/Options/options.html",
- "browser_style": false
- },
- "icons": { "16": "16.png", "32": "32.png", "48": "48.png", "128": "128.png" },
- "permissions": [
- "storage",
- "scripting",
- "activeTab",
- "tabs",
- "contextMenus"
- ],
- "host_permissions": ["<all_urls>"],
- "background": {
- "scripts": ["background.js"],
- "type": "module"
- },
- "content_security_policy": {
- "extension_pages": "script-src 'self'; object-src 'self'; connect-src 'self' http: https:;"
- },
- "commands": {
- "_execute_action": {
- "suggested_key": { "default": "Ctrl+Shift+F", "mac": "Command+Shift+Y" }
- }
- },
- "browser_specific_settings": {
- "safari": {
- "strict_min_version": "15.4"
- }
- }
-}
diff --git a/apps/extension/package.json b/apps/extension/package.json
index bcf6b671..0a1917e2 100644
--- a/apps/extension/package.json
+++ b/apps/extension/package.json
@@ -7,8 +7,8 @@
"dev": "vite build --watch",
"dev:chrome": "yarn build && concurrently -k -n build,chrome -c blue,green \"vite build --watch\" \"web-ext run --target chromium --source-dir dist\"",
"dev:firefox": "yarn build && concurrently -k -n build,firefox -c blue,magenta \"vite build --watch\" \"web-ext run --target firefox-desktop --source-dir dist\"",
- "build": "tsc && vite build && cp ./manifest.json dist/manifest.json",
- "build:safari": "tsc && vite build --outDir dist-safari && cp ./manifest.safari.json dist-safari/manifest.json && cp public/*.png dist-safari/",
+ "build": "tsc && vite build",
+ "build:safari": "tsc && EXT_TARGET=safari vite build --outDir dist-safari && cp public/*.png dist-safari/",
"lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
"preview": "vite preview"
},
diff --git a/apps/extension/tsconfig.node.json b/apps/extension/tsconfig.node.json
index 6eeef8b2..89cac732 100644
--- a/apps/extension/tsconfig.node.json
+++ b/apps/extension/tsconfig.node.json
@@ -6,5 +6,5 @@
"moduleResolution": "bundler",
"allowSyntheticDefaultImports": true
},
- "include": ["vite.config.ts"],
+ "include": ["vite.config.ts", "manifest.config.ts"],
}
diff --git a/apps/extension/vite.config.ts b/apps/extension/vite.config.ts
index fa53c11a..2efcffb5 100644
--- a/apps/extension/vite.config.ts
+++ b/apps/extension/vite.config.ts
@@ -2,33 +2,36 @@ import fs from 'fs';
import path from 'path';
import react from '@vitejs/plugin-react';
import { defineConfig, type Plugin } from 'vite';
+import { buildManifest, resolveTarget } from './manifest.config';
+
+// `build:safari` sets EXT_TARGET; every other build, including the watching dev
+// builds, gets the Chrome and Firefox manifest.
+const target = resolveTarget();
-// `vite build --watch` never returns, so the `cp ./manifest.json` step in the
-// build scripts is unreachable while watching. The dev builds copy the manifest
-// themselves instead. `build` and `build:safari` are untouched and keep copying
-// their own manifest, which is what picks manifest.safari.json for Safari.
const isWatch = process.argv.includes('--watch') || process.argv.includes('-w');
-function devManifest(): Plugin {
+// Writing the manifest here rather than copying a file afterwards keeps it in
+// place for `vite build --watch`, which never returns to run a follow-up step.
+function manifest(): Plugin {
let outDir = path.resolve(__dirname, 'dist');
return {
- name: 'linkwarden:dev-manifest',
+ name: 'linkwarden:manifest',
apply: 'build',
configResolved(config) {
outDir = path.resolve(config.root, config.build.outDir);
},
writeBundle() {
- fs.copyFileSync(
- path.resolve(__dirname, 'manifest.json'),
- path.join(outDir, 'manifest.json')
+ fs.writeFileSync(
+ path.join(outDir, 'manifest.json'),
+ JSON.stringify(buildManifest(target), null, 2) + '\n'
);
},
};
}
export default defineConfig({
- plugins: [react(), ...(isWatch ? [devManifest()] : [])],
+ plugins: [react(), manifest()],
resolve: {
alias: {
'@': path.resolve(__dirname, './src'),