Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update build script #92

Merged
merged 1 commit into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"include": [
"src/**/*.ts",
"src/**/*.tsx",
"scripts/**/*.ts",
"vite.config.ts",
"tailwind.config.ts"
],
Expand Down
Binary file modified bun.lockb
Binary file not shown.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@
"webextension-polyfill": "^0.12.0"
},
"devDependencies": {
"@biomejs/biome": "latest",
"@types/bun": "latest",
"@biomejs/biome": "latest",
"@types/node": "^22.7.3",
"@types/react": "^18.3.9",
"@types/react-dom": "^18.3.0",
Expand Down
83 changes: 31 additions & 52 deletions scripts/build.ts
Original file line number Diff line number Diff line change
@@ -1,60 +1,39 @@
import fs from "fs";
import Bun from "bun";
import { cp, rmdir, writeFile } from "node:fs/promises";
import { build } from "vite";
// @ts-expect-error
import webExt from "web-ext";
import { manifest } from "./manifest";
import { minifyJs } from "./minify";
import { webExtBuild } from "./web-ext";

const browsers: browsers = ["chrome", "firefox"];

const browsers = ["chrome", "firefox"] as const;
process.env.NODE_ENV = "production";

function make_proc(cmd: string[]) {
return new Promise<void>((resolve) => {
const proc = Bun.spawn(cmd, {
stdout: "inherit",
onExit: async () => {
console.log(await new Response(proc.stdout).text());
resolve();
},
});
try {
await rmdir("dist", { recursive: true });

await build({
build: {
outDir: `../dist/${"chrome"}`,
},
});
}

const tsc = make_proc(["bunx", "--bun", "tsc"]);
const fmt = make_proc(["bun", "run", "format"]);
const lint = make_proc(["bun", "run", "lint"]);
await cp("dist/chrome", "dist/firefox", { recursive: true });

await Promise.all([fs.rmdirSync("dist", { recursive: true }), tsc, fmt, lint]).then(
() =>
build({
mode: "chrome",
build: {
outDir: `../dist/${"chrome"}`,
},
})
.then(async () => {
await minifyJs("chrome").then(async () => {
await make_proc(["cp", "-r", "dist/chrome", "dist/firefox"]);
});
browsers.map((browser) => {
Promise.all([
manifest(browser).then((manifest) => {
Bun.write(
Bun.file(`dist/${browser}/manifest.json`),
JSON.stringify(manifest)
);
}),
Bun.write(
Bun.file(`dist/${browser}/meta.json`),
JSON.stringify({
updated: new Date().toISOString(),
})
),
]);
});
})
.then(() => {
browsers.map((browser) => webExtBuild(browser));
})
);
for (const browser of browsers) {
const m = await manifest(browser);
const dist = `dist/${browser}`;

await writeFile(`${dist}/manifest.json`, JSON.stringify(m));
await cp("LICENSE.txt", `${dist}/LICENSE.txt`, { force: true });
webExt.cmd.build({
sourceDir: dist,
artifactsDir: "dist",
asNeeded: false,
overwriteDest: true,
ignoreFiles: [],
filename: `${browser}.zip`,
});
}
} catch (error) {
console.error(error);
process.exit(1);
}
35 changes: 7 additions & 28 deletions scripts/manifest.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import Bun from "bun";
import fs from "node:fs";

const pkg = await Bun.file("package.json")
.text()
.then((text) => JSON.parse(text));
const pkg = JSON.parse(fs.readFileSync("package.json", "utf-8"));

const base = {
name: pkg.displayName,
Expand All @@ -21,27 +19,20 @@ const base = {
options_ui: {
page: "options/options.html",
},
background: {},
content_scripts: [
{
js: ["d_comments.js"],
matches: ["*://animestore.docomo.ne.jp/*"],
run_at: "document_start",
},
],
web_accessible_resources: [],
permissions: ["cookies", "storage", "tabs"],
permissions: ["storage", "tabs"],
};

/**
* manifest.json を chrome, firefox 用にそれぞれ生成する
*/
export async function manifest(browser: browsers[number]) {
let manifest = {};

export async function manifest(browser: "chrome" | "firefox") {
switch (browser) {
case "chrome": {
manifest = {
return {
...base,
manifest_version: 3,
background: {
Expand All @@ -56,22 +47,17 @@ export async function manifest(browser: browsers[number]) {
web_accessible_resources: [
{
matches: ["*://animestore.docomo.ne.jp/*"],
resources: ["js/*.js", "assets/css/*.css", "assets/*.js"],
resources: ["*.js", "*.css"],
},
],
host_permissions: [
"*://*.nicovideo.jp/*",
"*://animestore.docomo.ne.jp/*",
"*://nvcomment.nicovideo.jp/*",
"*://nvapi.nicovideo.jp/v1/users/*",
"*://public.api.nicovideo.jp/v1/channel/channelapp/channels/*",
"*://api.search.nicovideo.jp/*",
],
};
break;
}
case "firefox": {
manifest = {
return {
...base,
manifest_version: 2,
background: {
Expand All @@ -92,19 +78,12 @@ export async function manifest(browser: browsers[number]) {
...base.permissions,
"https://*.nicovideo.jp/*",
"https://animestore.docomo.ne.jp/*",
"https://nvcomment.nicovideo.jp/*",
"https://nvapi.nicovideo.jp/v1/users/*",
"https://public.api.nicovideo.jp/v1/channel/channelapp/channels/*",
"https://api.search.nicovideo.jp/*",
],
developer: {
name: pkg.author,
url: pkg.repository.url,
},
};
break;
}
}

return manifest;
}
33 changes: 0 additions & 33 deletions scripts/minify.ts

This file was deleted.

1 change: 0 additions & 1 deletion scripts/types.d.ts

This file was deleted.

14 changes: 0 additions & 14 deletions scripts/web-ext.ts

This file was deleted.

15 changes: 3 additions & 12 deletions src/options/components/footer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,11 @@
along with d-comments. If not, see <https://www.gnu.org/licenses/>.
*/

import { useEffect, useRef } from "react";
import browser from "webextension-polyfill";

function Footer() {
const manifest = browser.runtime.getManifest();

const year = useRef(2022);

useEffect(() => {
fetch(browser.runtime.getURL("meta.json"))
.then((response) => response.json())
.then((data) => {
year.current = data.year;
});
}, []);

return (
<footer className="flex flex-row w-full h-16 p-2 text-sm text-center border-spacing-1 border-t border-gray-200">
<a
Expand All @@ -40,7 +29,9 @@ function Footer() {
className="flex flex-row items-center justify-center w-full h-full"
>
<span className="mx-3">{manifest.name}</span>
<span className="mx-1">2022 - {year.current}</span>
<span className="mx-1">
2022 - {new Date(manifest.version).getFullYear()}
</span>
<span className="mx-1">{browser.runtime.getManifest().author}</span>
<span className="mx-3">Version {manifest.version}</span>
</a>
Expand Down
Loading