extrojs 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Sahil Mulani
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,31 @@
1
+ # extrojs
2
+
3
+ > Next.js for Chrome extensions.
4
+
5
+ File-based entrypoints, automatic Manifest V3 generation, and type-safe routing for popup / options / sidepanel surfaces, driven by a single Vite plugin. ESM-only, React, MV3.
6
+
7
+ > **Status:** pre-stable. APIs may change between releases.
8
+
9
+ ## Install
10
+
11
+ ```bash
12
+ pnpm add -D extrojs
13
+ pnpm add react react-dom
14
+ ```
15
+
16
+ ## Quick start
17
+
18
+ ```bash
19
+ extro dev # dev server with HMR, writes .output/chrome-mv3-dev/
20
+ extro build # production build to .output/chrome-mv3-prod/
21
+ ```
22
+
23
+ Load `.output/chrome-mv3-dev/` (or `.output/chrome-mv3-prod/`) in Chrome via **Load Unpacked**.
24
+
25
+ ## Docs and source
26
+
27
+ Full documentation, architecture notes, and the source tree live at [github.com/Sahilm416/extro](https://github.com/Sahilm416/extro).
28
+
29
+ ## License
30
+
31
+ MIT
@@ -0,0 +1,3 @@
1
+ import type { ExtroConfig } from "@extrojs/types";
2
+ export type { ExtroConfig } from "@extrojs/types";
3
+ export declare const defineConfig: (config: ExtroConfig) => ExtroConfig;
package/dist/config.js ADDED
@@ -0,0 +1 @@
1
+ export const defineConfig = (config) => config;
@@ -0,0 +1,18 @@
1
+ import type { ExtroConfig } from "@extrojs/types";
2
+ import { type AppTree } from "@extrojs/vite-plugin/internal";
3
+ interface WriteDevAssetsOptions {
4
+ tree: AppTree;
5
+ root: string;
6
+ outDir: string;
7
+ port: number;
8
+ signalPort: number;
9
+ config: ExtroConfig;
10
+ }
11
+ /**
12
+ * @describe Writes the dev manifest + HTML shells + icons to disk so Chrome
13
+ * can load the unpacked extension while the Vite dev server serves modules
14
+ * over HTTP. Background/content scripts are written by the build-watch
15
+ * sidecar (a Vite watch-mode build) — not here.
16
+ */
17
+ export declare const writeDevAssets: ({ tree, root, outDir, port, signalPort, config, }: WriteDevAssetsOptions) => Promise<void>;
18
+ export {};
@@ -0,0 +1,26 @@
1
+ import fs from "node:fs/promises";
2
+ import path from "node:path";
3
+ import { emitAssets, } from "@extrojs/vite-plugin/internal";
4
+ /**
5
+ * @describe Writes the dev manifest + HTML shells + icons to disk so Chrome
6
+ * can load the unpacked extension while the Vite dev server serves modules
7
+ * over HTTP. Background/content scripts are written by the build-watch
8
+ * sidecar (a Vite watch-mode build) — not here.
9
+ */
10
+ export const writeDevAssets = async ({ tree, root, outDir, port, signalPort, config, }) => {
11
+ await fs.mkdir(outDir, { recursive: true });
12
+ const pkgRaw = await fs.readFile(path.join(root, "package.json"), "utf8").catch(() => "{}");
13
+ const pkg = JSON.parse(pkgRaw);
14
+ await emitAssets({ tree, root, pkg, config, dev: { port, signalPort } }, (fileName, source) => fs.writeFile(path.join(outDir, fileName), source));
15
+ await copyIcons(root, outDir);
16
+ };
17
+ const copyIcons = async (root, outDir) => {
18
+ const srcDir = path.join(root, "icons");
19
+ const exists = await fs.stat(srcDir).then((s) => s.isDirectory()).catch(() => false);
20
+ if (!exists)
21
+ return;
22
+ const dstDir = path.join(outDir, "icons");
23
+ await fs.mkdir(dstDir, { recursive: true });
24
+ const files = await fs.readdir(srcDir);
25
+ await Promise.all(files.map((f) => fs.copyFile(path.join(srcDir, f), path.join(dstDir, f))));
26
+ };
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env node
2
+ export {};
package/dist/index.js ADDED
@@ -0,0 +1,137 @@
1
+ #!/usr/bin/env node
2
+ import path from "node:path";
3
+ import { createServer, build as viteBuild } from "vite";
4
+ import { WebSocketServer } from "ws";
5
+ import { extro } from "@extrojs/vite-plugin";
6
+ import react from "@vitejs/plugin-react";
7
+ import { scanAppTree } from "@extrojs/vite-plugin/internal";
8
+ import { loadConfig } from "./load-config.js";
9
+ import { writeDevAssets } from "./dev-assets.js";
10
+ const command = process.argv[2];
11
+ const root = process.cwd();
12
+ // Separate output dirs let dev artifacts (with the bridge installed) persist
13
+ // across `extro dev` sessions without needing a prod-restore on shutdown.
14
+ const devOutDir = path.join(root, ".output", "chrome-mv3-dev");
15
+ const prodOutDir = path.join(root, ".output", "chrome-mv3-prod");
16
+ const dev = async () => {
17
+ const config = await loadConfig(root);
18
+ // 1. Scan once up front so we can decide what to start.
19
+ const tree = await scanAppTree(root);
20
+ validateTree(tree);
21
+ // 2. Signal WS — the dev bridge in the extension's BG SW connects here.
22
+ // Fixed port (not :0) so the port baked into a previously-loaded BG SW
23
+ // keeps working across `extro dev` restarts — otherwise users have to
24
+ // refresh the extension every time to pick up a new random port.
25
+ const signalPort = 9012;
26
+ const wss = new WebSocketServer({ port: signalPort });
27
+ wss.on("error", (err) => {
28
+ if (err.code === "EADDRINUSE") {
29
+ console.error(`\n[extro] Signal port ${signalPort} is in use — is another \`extro dev\` already running?\n`);
30
+ process.exit(1);
31
+ }
32
+ throw err;
33
+ });
34
+ await once(wss, "listening");
35
+ const broadcast = (msg) => {
36
+ const payload = JSON.stringify(msg);
37
+ for (const client of wss.clients) {
38
+ if (client.readyState === 1)
39
+ client.send(payload);
40
+ }
41
+ };
42
+ // 3. Vite dev server for routable surfaces.
43
+ // `broadcastHmr` lets the plugin push HMR updates over our signal WS —
44
+ // we can't piggy-back on Vite's own HMR WS because its origin check
45
+ // rejects chrome-extension:// service workers.
46
+ const server = await createServer({
47
+ root,
48
+ plugins: [
49
+ react(),
50
+ extro({
51
+ root,
52
+ config,
53
+ broadcastHmr: (payload) => broadcast({ kind: "vite-hmr", payload }),
54
+ }),
55
+ ],
56
+ server: { cors: true },
57
+ });
58
+ await server.listen();
59
+ const addr = server.httpServer?.address();
60
+ const port = addr && typeof addr === "object" ? addr.port : server.config.server.port ?? 5173;
61
+ // 4. Dev manifest + HTML + icons.
62
+ await writeDevAssets({ tree, root, outDir: devOutDir, port, signalPort, config });
63
+ // 5. Build-watch sidecar for background + content. Always runs in dev so
64
+ // the dev bridge gets bundled into background.js (even if the user has
65
+ // no BG of their own).
66
+ const watcher = await viteBuild({
67
+ root,
68
+ plugins: [extro({ root, config, scriptsOnly: true, devBridge: { signalPort } })],
69
+ // emptyOutDir: false so the watcher doesn't wipe the manifest / HTML /
70
+ // icons that writeDevAssets just put down.
71
+ build: { watch: {}, emptyOutDir: false, outDir: devOutDir },
72
+ logLevel: "error",
73
+ });
74
+ // The watcher is a RollupWatcher; bundle events fire on every rebuild
75
+ // (including the first one). Broadcast on each so the bridge reloads.
76
+ if (watcher && typeof watcher.on === "function") {
77
+ ;
78
+ watcher.on("event", (event) => {
79
+ if (event.code === "BUNDLE_END") {
80
+ broadcast({ kind: "scripts-rebuilt" });
81
+ }
82
+ });
83
+ }
84
+ console.log(`\nExtro dev server: http://localhost:${port}`);
85
+ console.log(`Load unpacked extension from: ${devOutDir}\n`);
86
+ let shuttingDown = false;
87
+ const shutdown = async () => {
88
+ if (shuttingDown) {
89
+ console.log("\nAlready shutting down, please wait...");
90
+ return;
91
+ }
92
+ shuttingDown = true;
93
+ console.log("\nShutting down dev server...");
94
+ if (watcher && typeof watcher.close === "function") {
95
+ await watcher.close();
96
+ }
97
+ wss.close();
98
+ await server.close();
99
+ // No prod-restore: dev artifacts live in their own .output/chrome-mv3-dev
100
+ // dir so the loaded extension stays untouched. Run `extro build` for a
101
+ // standalone prod bundle in .output/chrome-mv3-prod.
102
+ process.exit(0);
103
+ };
104
+ process.on("SIGINT", shutdown);
105
+ process.on("SIGTERM", shutdown);
106
+ };
107
+ const validateTree = (tree) => {
108
+ const empty = Object.keys(tree.scripts).length === 0 &&
109
+ Object.keys(tree.surfaces).length === 0;
110
+ if (!empty)
111
+ return;
112
+ throw new Error("Extro: No extension entrypoints found.\n\nExpected files like:\n src/app/popup/page.tsx\n src/app/options/page.tsx\n src/app/sidepanel/page.tsx\n src/app/background/index.ts\n src/app/content/index.ts");
113
+ };
114
+ const once = (emitter, event) => new Promise((resolve) => emitter.once(event, () => resolve()));
115
+ const build = async () => {
116
+ console.log("Building extension...");
117
+ const config = await loadConfig(root);
118
+ await viteBuild({
119
+ root,
120
+ plugins: [react(), extro({ root, config })],
121
+ build: { outDir: prodOutDir },
122
+ });
123
+ console.log(`Build complete. Load unpacked extension from: ${prodOutDir}`);
124
+ };
125
+ switch (command) {
126
+ case "dev":
127
+ await dev();
128
+ break;
129
+ case "build":
130
+ await build();
131
+ break;
132
+ case "init":
133
+ console.log("Initializing Extro project...");
134
+ break;
135
+ default:
136
+ console.log("Extro CLI");
137
+ }
@@ -0,0 +1,2 @@
1
+ import type { ExtroConfig } from "@extrojs/types";
2
+ export declare const loadConfig: (root: string) => Promise<ExtroConfig>;
@@ -0,0 +1,14 @@
1
+ import { createJiti } from "jiti";
2
+ import fs from "node:fs";
3
+ import path from "node:path";
4
+ const CONFIG_FILENAMES = ["extro.config.ts", "extro.config.js"];
5
+ export const loadConfig = async (root) => {
6
+ const configPath = CONFIG_FILENAMES
7
+ .map((name) => path.join(root, name))
8
+ .find((p) => fs.existsSync(p));
9
+ if (!configPath)
10
+ return {};
11
+ const jiti = createJiti(root, { interopDefault: true });
12
+ const mod = await jiti.import(configPath);
13
+ return "default" in mod ? mod.default : mod;
14
+ };
package/package.json ADDED
@@ -0,0 +1,63 @@
1
+ {
2
+ "name": "extrojs",
3
+ "version": "0.1.0",
4
+ "description": "Next.js for Chrome extensions. File-based entrypoints, automatic Manifest V3 generation, and React routing in a single Vite-powered CLI.",
5
+ "keywords": [
6
+ "extro",
7
+ "chrome-extension",
8
+ "browser-extension",
9
+ "manifest-v3",
10
+ "vite",
11
+ "react",
12
+ "framework"
13
+ ],
14
+ "author": "Sahil Mulani <sahilmulani501@gmail.com>",
15
+ "license": "MIT",
16
+ "homepage": "https://github.com/Sahilm416/extro#readme",
17
+ "bugs": {
18
+ "url": "https://github.com/Sahilm416/extro/issues"
19
+ },
20
+ "repository": {
21
+ "type": "git",
22
+ "url": "git+https://github.com/Sahilm416/extro.git",
23
+ "directory": "packages/cli"
24
+ },
25
+ "type": "module",
26
+ "main": "./dist/config.js",
27
+ "types": "./dist/config.d.ts",
28
+ "exports": {
29
+ ".": {
30
+ "types": "./dist/config.d.ts",
31
+ "import": "./dist/config.js"
32
+ }
33
+ },
34
+ "bin": {
35
+ "extro": "./dist/index.js"
36
+ },
37
+ "files": [
38
+ "dist"
39
+ ],
40
+ "publishConfig": {
41
+ "access": "public"
42
+ },
43
+ "engines": {
44
+ "node": ">=20"
45
+ },
46
+ "dependencies": {
47
+ "@vitejs/plugin-react": "^6.0.1",
48
+ "jiti": "^2.4.2",
49
+ "vite": "^8.0.0",
50
+ "ws": "^8.20.0",
51
+ "@extrojs/react": "0.1.0",
52
+ "@extrojs/vite-plugin": "0.1.0",
53
+ "@extrojs/types": "0.1.0"
54
+ },
55
+ "devDependencies": {
56
+ "@types/ws": "^8.18.1"
57
+ },
58
+ "scripts": {
59
+ "build": "tsc",
60
+ "dev": "tsc -w",
61
+ "typecheck": "tsc --noEmit"
62
+ }
63
+ }