neactor-cli 1.0.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.
Files changed (5) hide show
  1. package/build.js +29 -0
  2. package/cli.js +21 -0
  3. package/dev.js +63 -0
  4. package/package.json +15 -0
  5. package/preview.js +15 -0
package/build.js ADDED
@@ -0,0 +1,29 @@
1
+ // build.mjs
2
+ import * as esbuild from "esbuild";
3
+ import fs from "fs";
4
+ import crypto from "crypto";
5
+
6
+ export const build = async () => {
7
+ // 1. Bundle JS
8
+ const result = await esbuild.build({
9
+ entryPoints: ["./src/main.jsx"],
10
+ bundle: true,
11
+ outdir: "dist",
12
+ jsxFactory: "myCreateElement",
13
+ entryNames: "bundle",
14
+ write: true,
15
+ metafile: true,
16
+ });
17
+
18
+ // 2. Get the output filename
19
+ const outputFile = Object.keys(result.metafile.outputs)[0];
20
+
21
+ // 3. Rewrite index.html
22
+ let html = fs.readFileSync("index.html", "utf8");
23
+ html = html.replace(
24
+ /<script type="module" src="[^"]+"><\/script>/,
25
+ `<script src="./bundle.js"></script>`,
26
+ );
27
+ fs.mkdirSync("dist", { recursive: true });
28
+ fs.writeFileSync("dist/index.html", html);
29
+ };
package/cli.js ADDED
@@ -0,0 +1,21 @@
1
+ #!/usr/bin/env node
2
+
3
+ const command = process.argv[2];
4
+
5
+ if (command === "dev") {
6
+ const { dev } = await import("./dev.js");
7
+ await dev();
8
+ } else if (command === "build") {
9
+ const { build } = await import("./build.js");
10
+ await build();
11
+ } else if (command === "preview") {
12
+ const { preview } = await import("./preview.js");
13
+ await preview();
14
+ } else {
15
+ console.log(`
16
+ Usage:
17
+ mybundle dev start dev server
18
+ mybundle build production build
19
+ mybundle preview preview production build
20
+ `);
21
+ }
package/dev.js ADDED
@@ -0,0 +1,63 @@
1
+ import * as esbuild from "esbuild";
2
+ import fs from "fs";
3
+ import http from "http";
4
+
5
+ export const dev = async () => {
6
+ const clients = new Set();
7
+
8
+ const ctx = await esbuild.context({
9
+ entryPoints: ["./src/main.jsx"],
10
+ bundle: true,
11
+ outdir: "dist",
12
+ sourcemap: true,
13
+ jsxFactory: "myCreateElement",
14
+ plugins: [
15
+ {
16
+ name: "live-reload",
17
+ setup(build) {
18
+ build.onEnd(() => {
19
+ clients.forEach((res) => res.write("data: reload\n\n"));
20
+ });
21
+ },
22
+ },
23
+ ],
24
+ });
25
+
26
+ await ctx.watch();
27
+
28
+ let html = fs.readFileSync("index.html", "utf8");
29
+ html = html.replace(
30
+ /<script src="[^"]+"><\/script>/,
31
+ `<script src="./main.js"></script>`,
32
+ );
33
+ fs.mkdirSync("dist", { recursive: true });
34
+ fs.writeFileSync("dist/index.html", html);
35
+
36
+ // simple server that serves dist/ + SSE endpoint
37
+ http
38
+ .createServer((req, res) => {
39
+ if (req.url === "/__reload") {
40
+ res.writeHead(200, {
41
+ "Content-Type": "text/event-stream",
42
+ "Cache-Control": "no-cache",
43
+ Connection: "keep-alive",
44
+ });
45
+ clients.add(res);
46
+ req.on("close", () => clients.delete(res));
47
+ return;
48
+ }
49
+
50
+ // serve files from dist/
51
+ const filePath = `dist${req.url === "/" ? "/index.html" : req.url}`;
52
+ fs.readFile(filePath, (err, data) => {
53
+ if (err) {
54
+ res.writeHead(404);
55
+ res.end();
56
+ return;
57
+ }
58
+ res.writeHead(200);
59
+ res.end(data);
60
+ });
61
+ })
62
+ .listen(3000, () => console.log("http://localhost:3000"));
63
+ };
package/package.json ADDED
@@ -0,0 +1,15 @@
1
+ {
2
+ "name": "neactor-cli",
3
+ "version": "1.0.0",
4
+ "description": "",
5
+ "license": "ISC",
6
+ "author": "",
7
+ "type": "module",
8
+ "main": "./cli.js",
9
+ "bin": {
10
+ "neactor": "./cli.js"
11
+ },
12
+ "dependencies": {
13
+ "esbuild": "^0.28.0"
14
+ }
15
+ }
package/preview.js ADDED
@@ -0,0 +1,15 @@
1
+ import { createServer } from "http";
2
+ import { readFileSync } from "fs";
3
+
4
+ export const preview = async () => {
5
+ createServer(async (req, res) => {
6
+ if (req.url === "/") {
7
+ res.writeHead(200, { "content-type": "text/html" });
8
+ res.end(readFileSync("./dist/index.html", "utf-8"));
9
+ }
10
+ if (req.url.endsWith(".js")) {
11
+ res.writeHead(200, { "content-type": "application/javascript" });
12
+ res.end(readFileSync("./dist/bundle.js", "utf-8"));
13
+ }
14
+ }).listen(4000, () => console.log("prod server is running on port: 4000"));
15
+ };