neactor-cli 1.0.2 → 1.0.4

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/build.js CHANGED
@@ -2,6 +2,9 @@
2
2
  import * as esbuild from "esbuild";
3
3
  import fs from "fs";
4
4
  import crypto from "crypto";
5
+ import { fileURLToPath } from "url";
6
+
7
+ const jsxShimPath = fileURLToPath(new URL("./jsx-shim.js", import.meta.url));
5
8
 
6
9
  export const build = async () => {
7
10
  // 1. Bundle JS
@@ -9,13 +12,12 @@ export const build = async () => {
9
12
  entryPoints: ["./src/main.jsx"],
10
13
  bundle: true,
11
14
  outdir: "dist",
15
+ format: "esm",
12
16
  jsxFactory: "neactorCreateElement",
13
17
  jsx: "transform",
14
18
  jsxFactory: "neactorCreateElement",
15
19
  jsxImportSource: "neactor-dom",
16
- banner: {
17
- js: `import { createElement } from 'neactor-dom';`,
18
- },
20
+ inject: [jsxShimPath],
19
21
  entryNames: "bundle",
20
22
  write: true,
21
23
  metafile: true,
@@ -28,8 +30,9 @@ export const build = async () => {
28
30
  let html = fs.readFileSync("index.html", "utf8");
29
31
  html = html.replace(
30
32
  /<script type="module" src="[^"]+"><\/script>/,
31
- `<script src="./bundle.js"></script>`,
33
+ `<script type="module" src="./bundle.js"></script>`,
32
34
  );
35
+
33
36
  fs.mkdirSync("dist", { recursive: true });
34
37
  fs.writeFileSync("dist/index.html", html);
35
38
  };
package/cli.js CHANGED
@@ -14,8 +14,8 @@ if (command === "dev") {
14
14
  } else {
15
15
  console.log(`
16
16
  Usage:
17
- mybundle dev start dev server
18
- mybundle build production build
19
- mybundle preview preview production build
17
+ neactor dev start dev server
18
+ neactor build production build
19
+ neactor preview preview production build
20
20
  `);
21
21
  }
package/dev.js CHANGED
@@ -1,6 +1,10 @@
1
1
  import * as esbuild from "esbuild";
2
2
  import fs from "fs";
3
3
  import http from "http";
4
+ import path from "path";
5
+ import { fileURLToPath } from "url";
6
+
7
+ const jsxShimPath = fileURLToPath(new URL("./jsx-shim.js", import.meta.url));
4
8
 
5
9
  export const dev = async () => {
6
10
  const clients = new Set();
@@ -10,18 +14,17 @@ export const dev = async () => {
10
14
  bundle: true,
11
15
  outdir: "dist",
12
16
  sourcemap: true,
17
+ format: "esm",
13
18
  jsx: "transform",
14
19
  jsxFactory: "neactorCreateElement",
15
20
  jsxImportSource: "neactor-dom",
16
- banner: {
17
- js: `import { createElement } from 'neactor-dom';`,
18
- },
21
+ inject: [jsxShimPath],
19
22
  plugins: [
20
23
  {
21
24
  name: "live-reload",
22
25
  setup(build) {
23
26
  build.onEnd(() => {
24
- clients.forEach((res) => res.write("data: reload\n\n"));
27
+ clients.forEach((res) => res.write("data: reload\\n\\n"));
25
28
  });
26
29
  },
27
30
  },
@@ -32,13 +35,20 @@ export const dev = async () => {
32
35
 
33
36
  let html = fs.readFileSync("index.html", "utf8");
34
37
  html = html.replace(
35
- /<script src="[^"]+"><\/script>/,
36
- `<script src="./main.js"></script>`,
38
+ /<script type="module" src="[^"]+"><\/script>/,
39
+ `<script type="module" src="./main.js"></script>`,
37
40
  );
38
41
  fs.mkdirSync("dist", { recursive: true });
39
42
  fs.writeFileSync("dist/index.html", html);
40
43
 
41
- // simple server that serves dist/ + SSE endpoint
44
+ const mimeTypes = {
45
+ ".html": "text/html; charset=utf-8",
46
+ ".js": "application/javascript; charset=utf-8",
47
+ ".map": "application/json; charset=utf-8",
48
+ ".css": "text/css; charset=utf-8",
49
+ ".json": "application/json; charset=utf-8",
50
+ };
51
+
42
52
  http
43
53
  .createServer((req, res) => {
44
54
  if (req.url === "/__reload") {
@@ -52,15 +62,20 @@ export const dev = async () => {
52
62
  return;
53
63
  }
54
64
 
55
- // serve files from dist/
56
- const filePath = `dist${req.url === "/" ? "/index.html" : req.url}`;
65
+ const requestPath = req.url === "/" ? "/index.html" : req.url;
66
+ const filePath = path.join("dist", requestPath);
67
+
57
68
  fs.readFile(filePath, (err, data) => {
58
69
  if (err) {
59
- res.writeHead(404);
60
- res.end();
70
+ res.writeHead(404, { "Content-Type": "text/plain; charset=utf-8" });
71
+ res.end("Not Found");
61
72
  return;
62
73
  }
63
- res.writeHead(200);
74
+
75
+ const ext = path.extname(filePath);
76
+ res.writeHead(200, {
77
+ "Content-Type": mimeTypes[ext] || "application/octet-stream",
78
+ });
64
79
  res.end(data);
65
80
  });
66
81
  })
package/jsx-shim.js ADDED
@@ -0,0 +1,3 @@
1
+ export function neactorCreateElement(type, props, ...children) {
2
+ return { type, props, children };
3
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "neactor-cli",
3
- "version": "1.0.2",
3
+ "version": "1.0.4",
4
4
  "description": "",
5
5
  "license": "ISC",
6
6
  "author": "",
package/preview.js CHANGED
@@ -1,15 +1,32 @@
1
1
  import { createServer } from "http";
2
- import { readFileSync } from "fs";
2
+ import fs from "fs";
3
+ import path from "path";
4
+
5
+ const mimeTypes = {
6
+ ".html": "text/html; charset=utf-8",
7
+ ".js": "application/javascript; charset=utf-8",
8
+ ".css": "text/css; charset=utf-8",
9
+ ".json": "application/json; charset=utf-8",
10
+ ".map": "application/json; charset=utf-8",
11
+ };
3
12
 
4
13
  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
+ createServer((req, res) => {
15
+ const requestPath = req.url === "/" ? "/index.html" : req.url;
16
+ const filePath = path.join("dist", requestPath);
17
+
18
+ fs.readFile(filePath, (err, data) => {
19
+ if (err) {
20
+ res.writeHead(404, { "Content-Type": "text/plain; charset=utf-8" });
21
+ res.end("Not Found");
22
+ return;
23
+ }
24
+
25
+ const ext = path.extname(filePath);
26
+ res.writeHead(200, {
27
+ "Content-Type": mimeTypes[ext] || "application/octet-stream",
28
+ });
29
+ res.end(data);
30
+ });
14
31
  }).listen(4000, () => console.log("prod server is running on port: 4000"));
15
32
  };