Skip to content

Commit

Permalink
Support Firefox webextension
Browse files Browse the repository at this point in the history
  • Loading branch information
govza committed Aug 20, 2023
1 parent 2a4824f commit b998641
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@
},
"scripts": {
"build": "tsc --noEmit && vite build",
"build:firefox": "tsc --noEmit && cross-env __FIREFOX__=true vite build",
"build:watch": "cross-env __DEV__=true vite build -w",
"build:firefox:watch": "cross-env __DEV__=true __FIREFOX__=true vite build -w",
"build:hmr": "rollup --config utils/reload/rollup.config.ts",
"wss": "node utils/reload/initReloadServer.js",
"dev": "npm run build:hmr && (run-p wss build:watch)",
"dev:firefox": "npm run build:hmr && (run-p wss build:firefox:watch)",
"test": "jest"
},
"type": "module",
Expand Down
23 changes: 23 additions & 0 deletions utils/manifest-parser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,31 @@ class ManifestParser {
private constructor() {}

static convertManifestToString(manifest: Manifest): string {
if (process.env.__FIREFOX__) {
manifest = this.convertToFirefoxCompatibleManifest(manifest);
}
return JSON.stringify(manifest, null, 2);
}

static convertToFirefoxCompatibleManifest(manifest: Manifest) {
const manifestCopy = {
...manifest,
} as { [key: string]: unknown };

manifestCopy.background = {
scripts: [manifest.background?.service_worker],
type: "module",
};
manifestCopy.options_ui = {
page: manifest.options_page,
browser_style: false,
};
manifestCopy.content_security_policy = {
extension_pages: "script-src 'self'; object-src 'self'",
};
delete manifestCopy.options_page;
return manifestCopy as Manifest;
}
}

export default ManifestParser;
10 changes: 10 additions & 0 deletions utils/plugins/custom-dynamic-import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ export default function customDynamicImport(): PluginOption {
return {
name: "custom-dynamic-import",
renderDynamicImport() {
if (process.env.__FIREFOX__) {
return {
left: `
{
const dynamicImport = (path) => import(path);
dynamicImport(browser.runtime.getURL('./') +
`,
right: ".split('../').join(''))}",
};
}
return {
left: `
{
Expand Down

0 comments on commit b998641

Please sign in to comment.