Skip to content

Commit

Permalink
esbuild
Browse files Browse the repository at this point in the history
  • Loading branch information
Debdut Karmakar authored and Debdut Karmakar committed Jan 27, 2023
1 parent a49bc54 commit a11949d
Show file tree
Hide file tree
Showing 23 changed files with 719 additions and 1,432 deletions.
8 changes: 0 additions & 8 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -386,12 +386,4 @@ $RECYCLE.BIN/
# etc
.idea

#generated manifest
public/v2/manifest.json
public/v3/manifest.json

#copied contentStyle
public/v2/contentStyle.css
public/v3/contentStyle.css

tmp
1,541 changes: 563 additions & 978 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,16 @@
"webextension-polyfill": "^0.10.0"
},
"devDependencies": {
"@esbuilder/html": "^0.0.6",
"@types/chrome": "^0.0.209",
"@types/node": "^18.11.18",
"@types/react": "^18.0.27",
"@types/react-dom": "^18.0.10",
"@typescript-eslint/eslint-plugin": "^5.48.2",
"@typescript-eslint/parser": "^5.48.2",
"@vitejs/plugin-react": "^3.0.1",
"autoprefixer": "^10.4.13",
"concurrently": "^7.6.0",
"esbuild": "^0.17.4",
"eslint": "^8.32.0",
"eslint-config-prettier": "^8.6.0",
"eslint-plugin-import": "^2.27.5",
Expand All @@ -47,7 +48,6 @@
"tailwindcss": "^3.2.4",
"ts-node": "^10.9.1",
"typescript": "^4.9.4",
"vite": "^4.0.4",
"web-ext": "^7.4.0"
}
}
Binary file removed public/v2/icon-128.png
Binary file not shown.
Binary file removed public/v2/icon-34.png
Binary file not shown.
Binary file removed public/v3/icon-128.png
Binary file not shown.
Binary file removed public/v3/icon-34.png
Binary file not shown.
180 changes: 111 additions & 69 deletions scripts/build.ts
Original file line number Diff line number Diff line change
@@ -1,92 +1,134 @@
import { GetInstalledBrowsers, BrowserPath } from "get-installed-browsers";
import { resolve, dirname } from "path";
import fs from "fs";
import { dirname, resolve } from "path";
import { fileURLToPath } from "url";
import { execSync } from "child_process";

import { build } from "esbuild";
import { html } from "@esbuilder/html";
import { root } from "postcss";

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

if (process.argv.length < 3) {
console.error("Usage: npm run build [<browser>...]");
process.exit(1);
}
const rootDir = resolve(__dirname, "..");
const srcDir = resolve(rootDir, "src");
const pagesDir = resolve(srcDir, "pages");
const assetsDir = resolve(srcDir, "assets");
const outDir = resolve(__dirname, "..", "dist");
const publicDir = resolve(__dirname, "..", "public");

const pageDirs = fs.readdirSync(pagesDir);

function getPageInputs() {
const input: { [x: string]: any } = {};
const entryPoints = [
"index.html",
"index.ts",
"index.tsx",
"index.js",
"index.jsx",
"main.html",
"main.ts",
"main.tsx",
"main.js",
"main.jsx",
];

const getFirstExistingFile = (folder: string): string | undefined => {
for (const entryPoint of entryPoints) {
const file = resolve(pagesDir, folder, entryPoint);
if (fs.existsSync(file)) {
return file;
}
}
};

const browsers = process.argv
.splice(2)
.reduce((acc, browser) => {
const browserName = browser.toLowerCase();
if (acc.indexOf(browserName) === -1) {
acc.push(browserName);
pageDirs.forEach((folder) => {
const pages = resolve(pagesDir, folder);
if (!fs.statSync(pages).isDirectory()) {
return;
}
const entry = getFirstExistingFile(folder)
?.replace(rootDir + "/", "");
if (entry) {
input[folder] = entry;
}
return acc;
}, [] as string[]);
});

function toKebabCase(str: string) {
return str.replace(/([a-z]) ([A-Z])/g, "$1-$2").toLowerCase();
return input;
}

const dist = resolve(__dirname, "..", "dist");
const v2 = resolve(dist, "v2");
const v3 = resolve(dist, "v3");

function manifestVersion(browser: BrowserPath) {
if (browser.type === "firefox") {
return 2;
} else if (browser.type === "chrome") {
return 3;
} else if (browser.type === "safari") {
return 2;
}

return -1;
// get extension from path
function getExtension(path: string) {
return path.split(".").pop();
}


function Init() {
const availableBrowsers = GetInstalledBrowsers();
const matchedBrowsers: BrowserPath[] = [];

for (const availableBrowser of availableBrowsers) {
const availableBrowserName = toKebabCase(availableBrowser.name);
for (const browser of browsers) {
if (availableBrowserName === browser) {
matchedBrowsers.push(availableBrowser);
}
function buildPage(name: string, entry: string) {
const ext = getExtension(entry);
if (ext === "html") {
if (name === "content") {
throw new Error(`Content page cannot have a HTML entry: ${entry}`);
}
if (name === "background") {
throw new Error(`Background page cannot have a HTML entry: ${entry}`);
}
}

if (matchedBrowsers.length === 0) {
console.error("No browser found");
process.exit(1);
}

const commands: string[] = [];
const versions: Set<number> = new Set();

for (const matchedBrowser of matchedBrowsers) {
versions.add(manifestVersion(matchedBrowser));
return buildHtmlPage(name, entry);
}

for (const version of versions) {
commands.unshift(`npm run build:v${version}`);
if (ext === "ts"
|| ext === "tsx"
|| ext === "js"
|| ext === "jsx") {
return buildJSPage(name, entry);
}

for (const matchedBrowser of matchedBrowsers) {
const version = manifestVersion(matchedBrowser);
const inputDir = version === 2 ? v2 : v3;
const outDir = resolve(dist, toKebabCase(matchedBrowser.name));
throw new Error(`Unknown entry point extension: ${entry} ${ext}`);
}

commands.push(`cp -r ${inputDir} ${outDir}` )
}
function buildHtmlPage(name: string, entry: string) {
return build({
entryPoints: [entry],
bundle: true,
outdir: `dist/${name}`,
sourcemap: true,
minify: true,
target: ["chrome58", "firefox57", "safari11", "edge16"],
loader: {
".png": "dataurl",
".webp": "dataurl",
".jpeg": "dataurl",
".svg": "dataurl",
".json": "json",
},
plugins: [
html({
entryNames: "[name]-[hash]",
}),
],
});
}

for (const command of commands) {
console.log(command + "\n\n");
function buildJSPage(name: string, entry: string) {
return build({
entryPoints: [entry],
bundle: true,
outdir: `dist/${name}`,
sourcemap: true,
minify: true,
target: ["chrome58", "firefox57", "safari11", "edge16"],
loader: {
".png": "dataurl",
".webp": "dataurl",
".jpeg": "dataurl",
".svg": "dataurl",
".json": "json",
},
});
}

try {
execSync(command, { stdio: "inherit" });
} catch (error) {
}
async function main() {
for (const [name, entry] of Object.entries(getPageInputs())) {
await buildPage(name, entry);
}
}

Init();
main();
Loading

0 comments on commit a11949d

Please sign in to comment.