@peachy/plugin-runner 0.0.1

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/README.md ADDED
@@ -0,0 +1,5 @@
1
+ # @peachy/plugin-runner
2
+
3
+ A Rolldown plugin that runs your application as it's being edited.
4
+
5
+ Useful for development and debugging.
@@ -0,0 +1,33 @@
1
+ import { Plugin } from "rolldown";
2
+
3
+ //#region src/index.d.ts
4
+ declare function gjsRunner({
5
+ sendMessage,
6
+ debug,
7
+ setReloadHandler
8
+ }: ReactGJSPluginOptions): Plugin;
9
+ interface ReactGJSPluginOptions {
10
+ /**
11
+ * Whether to print debug messages
12
+ */
13
+ debug?: boolean;
14
+ /**
15
+ * Broadcast message to all connected clients
16
+ */
17
+ sendMessage: (message: HMRMessage) => void;
18
+ /**
19
+ * Called when a reload is needed
20
+ */
21
+ setReloadHandler: (handler: () => void) => void;
22
+ }
23
+ interface HMRUpdate {
24
+ type: "update";
25
+ id: string;
26
+ url: string;
27
+ }
28
+ interface HMRMessage {
29
+ type: "hmr";
30
+ updates: HMRUpdate[];
31
+ }
32
+ //#endregion
33
+ export { ReactGJSPluginOptions, gjsRunner };
package/dist/index.mjs ADDED
@@ -0,0 +1,70 @@
1
+ import { spawn } from "node:child_process";
2
+ import { join, resolve } from "node:path";
3
+
4
+ //#region src/index.ts
5
+ let gjs = null;
6
+ async function spawnConsole(entry, debug = false) {
7
+ gjs = spawn("gjs", ["-m", entry], {
8
+ stdio: "inherit",
9
+ env: {
10
+ ...process.env,
11
+ G_MESSAGES_DEBUG: debug ? "all" : void 0
12
+ }
13
+ });
14
+ gjs.on("close", (code) => {
15
+ if (code !== null && code !== 0) console.error(`\n[HMR] GJS exited with error code ${code}`);
16
+ gjs = null;
17
+ });
18
+ }
19
+ function killConsole() {
20
+ if (!gjs || gjs.killed) return;
21
+ console.log("[HMR] Restarting the application");
22
+ if (!gjs.kill()) gjs.kill("SIGKILL");
23
+ gjs = null;
24
+ }
25
+ function reloadConsole(...args) {
26
+ killConsole();
27
+ spawnConsole(...args);
28
+ }
29
+ function gjsRunner({ sendMessage, debug, setReloadHandler }) {
30
+ const changedFiles = /* @__PURE__ */ new Set();
31
+ return {
32
+ name: "@peachy/plugin-runner",
33
+ watchChange(id) {
34
+ changedFiles.add(id);
35
+ },
36
+ writeBundle(options, bundle) {
37
+ const entryName = Object.keys(bundle)[0];
38
+ const entry = options.file || resolve(options.dir || "dist", entryName);
39
+ setReloadHandler(() => {
40
+ reloadConsole(entry, debug);
41
+ });
42
+ if (!this.meta.watchMode || changedFiles.size === 0) {
43
+ reloadConsole(entry, debug);
44
+ return;
45
+ }
46
+ const inputToOutput = getInputToOutputMap(bundle);
47
+ sendMessage({
48
+ type: "hmr",
49
+ updates: Array.from(changedFiles).filter((id) => inputToOutput.has(id)).map((id) => ({
50
+ type: "update",
51
+ id,
52
+ url: resolve(join(process.cwd(), options.dir || "."), inputToOutput.get(id))
53
+ }))
54
+ });
55
+ changedFiles.clear();
56
+ }
57
+ };
58
+ }
59
+ function getInputToOutputMap(bundle) {
60
+ const map = /* @__PURE__ */ new Map();
61
+ Object.entries(bundle).forEach(([outputPath, chunk]) => {
62
+ if (chunk.type === "chunk") Object.keys(chunk.modules).forEach((inputPath) => {
63
+ map.set(inputPath, outputPath);
64
+ });
65
+ });
66
+ return map;
67
+ }
68
+
69
+ //#endregion
70
+ export { gjsRunner };
package/package.json ADDED
@@ -0,0 +1,24 @@
1
+ {
2
+ "name": "@peachy/plugin-runner",
3
+ "version": "0.0.1",
4
+ "type": "module",
5
+ "description": "Run your GJS applications",
6
+ "main": "./dist/index.mjs",
7
+ "author": "",
8
+ "license": "MIT",
9
+ "devDependencies": {
10
+ "rolldown": "1.0.0-beta.58",
11
+ "tsdown": "0.20.0-beta.3",
12
+ "typescript": "^5.9.3"
13
+ },
14
+ "peerDependencies": {
15
+ "rolldown": "1.0.0-beta.58"
16
+ },
17
+ "files": [
18
+ "dist"
19
+ ],
20
+ "scripts": {
21
+ "build": "tsdown src/index.ts --dts"
22
+ },
23
+ "types": "./dist/index.d.mts"
24
+ }