From 88a10660819d0ec1399ad602008cfcd95d8dbf3b Mon Sep 17 00:00:00 2001 From: Bauke Date: Thu, 24 Mar 2022 14:56:42 +0100 Subject: [PATCH] Replace separated manifests with createManifest function. --- source/chromium-manifest.json | 26 ----------------- source/firefox-manifest.json | 36 ----------------------- source/manifest.ts | 50 +++++++++++++++++++++++++++++++ vite.config.ts | 55 ++++++++++++----------------------- 4 files changed, 68 insertions(+), 99 deletions(-) delete mode 100644 source/chromium-manifest.json delete mode 100644 source/firefox-manifest.json create mode 100644 source/manifest.ts diff --git a/source/chromium-manifest.json b/source/chromium-manifest.json deleted file mode 100644 index daa51f3..0000000 --- a/source/chromium-manifest.json +++ /dev/null @@ -1,26 +0,0 @@ -{ - "manifest_version": 3, - "name": "Queue", - "description": "A WebExtension for queueing links.", - "permissions": [ - "contextMenus", - "storage", - "tabs" - ], - "icons": { - "128": "assets/queue.png" - }, - "action": { - "default_icon": { - "128": "assets/queue.png" - } - }, - "options_ui": { - "page": "options/index.html", - "open_in_tab": true - }, - "background": { - "service_worker": "background-scripts/initialize.ts", - "type": "module" - } -} diff --git a/source/firefox-manifest.json b/source/firefox-manifest.json deleted file mode 100644 index 0b45ad0..0000000 --- a/source/firefox-manifest.json +++ /dev/null @@ -1,36 +0,0 @@ -{ - "manifest_version": 2, - "name": "Queue", - "description": "A WebExtension for queueing links.", - "permissions": [ - "contextMenus", - "storage", - "tabs" - ], - "content_security_policy": "script-src 'self'; object-src 'self'; style-src 'unsafe-inline'", - "web_accessible_resources": [ - "assets/**" - ], - "icons": { - "128": "assets/queue.png" - }, - "browser_action": { - "default_icon": { - "128": "assets/queue.png" - } - }, - "options_ui": { - "page": "options/index.html", - "open_in_tab": true - }, - "background": { - "scripts": [ - "background-scripts/initialize.ts" - ] - }, - "applications": { - "gecko": { - "id": "{c3560e6b-00e5-4ab3-b89e-8a54ee5b2c9f}" - } - } -} diff --git a/source/manifest.ts b/source/manifest.ts new file mode 100644 index 0000000..863edfb --- /dev/null +++ b/source/manifest.ts @@ -0,0 +1,50 @@ +/* eslint-disable @typescript-eslint/naming-convention */ + +export default function createManifest( + target: string, +): Record { + const manifest: Record = { + name: 'Queue', + description: 'A WebExtension for queueing links.', + version: '0.2.5', + permissions: ['contextMenus', 'storage'], + options_ui: { + page: 'options/index.html', + open_in_tab: true, + }, + }; + + const icons = { + 128: 'assets/queue.png', + }; + + manifest.icons = icons; + + const browserAction = { + default_icon: icons, + }; + + const backgroundScript = 'background-scripts/initialize.ts'; + + if (target === 'chromium') { + manifest.manifest_version = 3; + manifest.action = browserAction; + manifest.background = { + service_worker: backgroundScript, + type: 'module', + }; + } else { + manifest.manifest_version = 2; + manifest.browser_action = browserAction; + manifest.background = { + scripts: [backgroundScript], + }; + manifest.applications = { + gecko: { + id: '{c3560e6b-00e5-4ab3-b89e-8a54ee5b2c9f}', + }, + }; + } + + return manifest; +} diff --git a/vite.config.ts b/vite.config.ts index f1e0377..2a65d71 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -1,5 +1,4 @@ import fs from 'node:fs'; -import fsp from 'node:fs/promises'; import path from 'node:path'; import process from 'node:process'; import url from 'node:url'; @@ -10,38 +9,32 @@ import {defineConfig} from 'vite'; import preactPreset from '@preact/preset-vite'; import webExtension from 'vite-plugin-web-extension'; -const currentDir = path.dirname(url.fileURLToPath(import.meta.url)); -const sourceDir = path.join(currentDir, 'source'); - -const queueVersion = '0.2.5'; +import createManifest from './source/manifest.js'; const targetBrowser = process.env.VITE_BROWSER ?? 'firefox'; process.env.VITE_BROWSER = targetBrowser; -let webExtConfig; +const currentDir = path.dirname(url.fileURLToPath(import.meta.url)); +const buildDir = path.join(currentDir, 'build', targetBrowser); +const sourceDir = path.join(currentDir, 'source'); + +fs.mkdirSync(path.join(currentDir, targetBrowser), {recursive: true}); + +const webExtConfig: Record = { + browserConsole: true, + chromiumProfile: 'chromium/', + firefoxProfile: 'firefox/', + keepProfileChanges: true, +}; if (targetBrowser === 'chromium') { - fs.mkdirSync(path.join(currentDir, 'chromium'), {recursive: true}); - webExtConfig = { - browserConsole: true, - chromiumProfile: 'chromium/', - keepProfileChanges: true, - startUrl: 'chrome://extensions/', - target: 'chromium', - }; + webExtConfig.startUrl = 'chrome://extensions/'; + webExtConfig.target = 'chromium'; } else { - fs.mkdirSync(path.join(currentDir, 'firefox'), {recursive: true}); - webExtConfig = { - browserConsole: true, - firefoxProfile: 'firefox/', - keepProfileChanges: true, - startUrl: 'about:debugging#/runtime/this-firefox', - target: 'firefox-desktop', - }; + webExtConfig.startUrl = 'about:debugging#/runtime/this-firefox'; + webExtConfig.target = 'firefox-desktop'; } -const buildDir = path.join(currentDir, 'build', targetBrowser); - export default defineConfig({ build: { minify: false, @@ -55,19 +48,7 @@ export default defineConfig({ webExtension({ assets: 'assets', browser: targetBrowser, - async manifest() { - const manifest = JSON.parse( - await fsp.readFile( - path.join(sourceDir, `${targetBrowser}-manifest.json`), - // eslint-disable-next-line unicorn/prefer-json-parse-buffer - 'utf-8', - ), - ) as Record; - - manifest.version = queueVersion; - - return manifest; - }, + manifest: () => createManifest(targetBrowser), webExtConfig, }), ],