Skip to content

Commit

Permalink
Improved manifest compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianRappl committed Feb 4, 2024
1 parent 9982ac3 commit 6234c2f
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 26 deletions.
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@
"start": "npm run watch:firefox",
"build:all": "npm run build:firefox && npm run build:opera && npm run build:chrome && npm run build:edge",
"watch:firefox": "concurrently --kill-others \"npm run watch:parcel firefox\" \"npm run watch:web-ext -- -s dist/firefox -t firefox-desktop\"",
"build:firefox": "npm run build:parcel firefox && npm run build:web-ext -- -s dist/firefox -a web-ext-artifacts/firefox",
"build:firefox": "npm run build:parcel firefox && npm run build:web-ext -- -s dist/firefox -a web-ext-artifacts/firefox --overwrite-dest",
"watch:chrome": "concurrently --kill-others \"npm run watch:parcel chrome\" \"npm run watch:web-ext -- -s dist/chrome -t chromium\"",
"build:chrome": "npm run build:parcel chrome && npm run build:web-ext -- -s dist/chrome -a web-ext-artifacts/chrome",
"build:chrome": "npm run build:parcel chrome && npm run build:web-ext -- -s dist/chrome -a web-ext-artifacts/chrome --overwrite-dest",
"watch:opera": "concurrently --kill-others \"npm run watch:parcel opera\" \"npm run watch:web-ext -- -s dist/opera -t chromium\"",
"build:opera": "npm run build:parcel opera && npm run build:web-ext -- -s dist/opera -a web-ext-artifacts/opera",
"build:opera": "npm run build:parcel opera && npm run build:web-ext -- -s dist/opera -a web-ext-artifacts/opera --overwrite-dest",
"watch:edge": "concurrently --kill-others \"npm run watch:parcel edge\" \"npm run watch:web-ext -- -s dist/edge -t chromium\"",
"build:edge": "npm run build:parcel edge && npm run build:web-ext -- -s dist/edge -a web-ext-artifacts/edge",
"build:edge": "npm run build:parcel edge && npm run build:web-ext -- -s dist/edge -a web-ext-artifacts/edge --overwrite-dest",
"watch:parcel": "node tools/serve.mjs",
"build:parcel": "node tools/build.mjs",
"watch:web-ext": "wait-on -d 6000 package.json && web-ext run",
Expand Down
22 changes: 1 addition & 21 deletions tools/build.mjs
Original file line number Diff line number Diff line change
@@ -1,27 +1,7 @@
import esbuild from 'esbuild';
import { readFile, writeFile } from 'fs/promises';
import { resolve } from 'path';
import { config, target } from './config.mjs';

async function normalizeManifest() {
const cwd = process.cwd();
const packageJsonPath = resolve(cwd, 'package.json');
const packageJsonContent = await readFile(packageJsonPath, 'utf8');
const { version } = JSON.parse(packageJsonContent);
const manifestPath = `${target}/manifest.json`;
const manifestContent = await readFile(manifestPath, 'utf8');
const manifest = JSON.parse(manifestContent);

if (manifest.version !== version) {
manifest.version = version;
const newContent = JSON.stringify(manifest, undefined, 2);
await writeFile(manifestPath, newContent, 'utf8');
}
}
import { config } from './config.mjs';

await esbuild.build({
...config,
sourcemap: false,
});

await normalizeManifest();
38 changes: 37 additions & 1 deletion tools/config.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import copyStaticFiles from 'esbuild-copy-static-files';
import { resolve } from 'path';
import { readFile, writeFile } from 'fs/promises';

const browser = process.argv.pop();

Expand All @@ -18,7 +19,41 @@ const cwd = process.cwd();
const src = resolve(cwd, 'src');
const dst = resolve(cwd, `dist/${browser}`);

export const target = dst;
async function normalizeManifest(target) {
const cwd = process.cwd();
const packageJsonPath = resolve(cwd, 'package.json');
const packageJsonContent = await readFile(packageJsonPath, 'utf8');
const { version } = JSON.parse(packageJsonContent);
const manifestPath = `${target}/manifest.json`;
const manifestContent = await readFile(manifestPath, 'utf8');
const manifest = JSON.parse(manifestContent);

if (manifest.version !== version) {
manifest.version = version;
}

if (browser === 'firefox') {
manifest.background = {
scripts: [manifest.background.service_worker],
persistent: false,
type: 'module',
};
}

const newContent = JSON.stringify(manifest, undefined, 2);
await writeFile(manifestPath, newContent, 'utf8');
}

function modifyManifest(dir) {
return {
name: 'modify-manifest',
setup(build) {
build.onEnd(result => {
return normalizeManifest(dir);
});
},
};
}

export const config = {
entryPoints: [
Expand All @@ -37,5 +72,6 @@ export const config = {
src: resolve(src, 'public'),
dest: dst,
}),
modifyManifest(dst),
],
};

0 comments on commit 6234c2f

Please sign in to comment.