bun-dev-server 0.0.5 → 0.0.7

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 CHANGED
@@ -3,6 +3,7 @@
3
3
  ```
4
4
  //devserver.ts
5
5
  import {startBunDevServer} from "bun-dev-server"
6
+ import { file } from "bun"
6
7
 
7
8
  startBunDevServer({
8
9
  buildConfig: {
@@ -14,6 +15,11 @@ startBunDevServer({
14
15
  chunk: "chunks/[name]-[hash].[ext]",
15
16
  }
16
17
  },
18
+ tls: {
19
+ cert: file("./serve_cert.pem"),
20
+ key: file("./serve_key.pem"),
21
+ },
22
+ writeManifest: true,
17
23
  cleanServePath: true,
18
24
  port: 4567,
19
25
  enableTypeScriptWatch: true,
package/bunManifest.ts ADDED
@@ -0,0 +1,18 @@
1
+ import { type BuildOutput, write, pathToFileURL } from "bun";
2
+ export function writeManifest(output: BuildOutput, outdir: string, manifestName = "manifest.txt") {
3
+ const entryPoints = output.outputs.filter(o => o.kind === "entry-point");
4
+ const epTable = [];
5
+ for (const ep of entryPoints) {
6
+ const basePathUrl = pathToFileURL(outdir);
7
+ const epUrl = pathToFileURL(ep.path);
8
+ const relativePath = epUrl.href.replace(`${basePathUrl.href}/`, "");
9
+ const nameNoJs = relativePath.replace(".js", "");
10
+ const hashedImport = `${relativePath}?${ep.hash}`;
11
+ epTable.push({ name: nameNoJs, path: hashedImport });
12
+ }
13
+ const outObj = {};
14
+ for (const element of epTable) {
15
+ Object.assign(outObj, { [element.name]: { js: [`${element.path}`] } });
16
+ }
17
+ write(`${outdir}/${manifestName}`, JSON.stringify(outObj));
18
+ }
package/bunServeConfig.ts CHANGED
@@ -5,6 +5,9 @@ export interface BunDevServerConfig extends Partial<BunDevServerSocketConfig> {
5
5
  buildConfig: BuildConfig;
6
6
  watchDir?: string;
7
7
  enableTypeScriptWatch?: boolean;
8
+ writeManifest?: boolean;
9
+ manifestName?: string;
10
+ reloadOnChange?: boolean;
8
11
  /**
9
12
  * The path to the directory to serve files from.
10
13
  * Takes precedence over `buildConfig.outdir`.
package/index.ts CHANGED
@@ -8,6 +8,7 @@ import { type FileChangeInfo } from "fs/promises";
8
8
  import { startTSWatcher } from "./bunTSWatcher";
9
9
  import { getBunHMRFooter } from "./bunHmrPlugin";
10
10
  import { type BunDevServerConfig } from "./bunServeConfig";
11
+ import { writeManifest } from "./bunManifest";
11
12
 
12
13
 
13
14
 
@@ -49,7 +50,7 @@ export async function startBunDevServer(serverConfig: BunDevServerConfig) {
49
50
  development: true,
50
51
  tls: finalConfig.tls,
51
52
  async fetch(req, server) {
52
- if(req.method === "OPTIONS") {
53
+ if (req.method === "OPTIONS") {
53
54
  const response = new Response("", { status: 200 });
54
55
  augumentHeaders(response);
55
56
  return response;
@@ -132,6 +133,9 @@ export async function startBunDevServer(serverConfig: BunDevServerConfig) {
132
133
  const output = await Bun.build(buildCfg);
133
134
  publishOutputLogs(output, { filename: "Initial", eventType: "change" });
134
135
  publishIndexHTML(output, { filename: "Initial", eventType: "change" });
136
+ if (finalConfig.writeManifest) {
137
+ writeManifest(output, dst, finalConfig.manifestName);
138
+ }
135
139
  // $`tsc --watch`.then((tsc) => {
136
140
  // console.log("ASDASD");
137
141
  // });
@@ -151,6 +155,13 @@ export async function startBunDevServer(serverConfig: BunDevServerConfig) {
151
155
  const output = await Bun.build(buildCfg);
152
156
  publishOutputLogs(output, event);
153
157
  publishIndexHTML(output, event);
158
+ if (finalConfig.writeManifest) {
159
+ writeManifest(output, dst, finalConfig.manifestName);
160
+ }
161
+ if(finalConfig.reloadOnChange) {
162
+ bunServer.publish("message", JSON.stringify({ type: "reload" }));
163
+ }
164
+
154
165
  }
155
166
 
156
167
  function publishOutputLogs(output: Bun.BuildOutput, event: FileChangeInfo<string>) {
@@ -170,13 +181,15 @@ export async function startBunDevServer(serverConfig: BunDevServerConfig) {
170
181
  }
171
182
 
172
183
  function publishIndexHTML(output: Bun.BuildOutput, event: FileChangeInfo<string>) {
173
- const ep = output.outputs.find(o => o.kind === "entry-point");
174
- if (ep) {
184
+ const eps = output.outputs.filter(o => o.kind === "entry-point");
185
+ const hashedImports: string[] = [];
186
+ for (const ep of eps) {
175
187
  const basePathUrl = Bun.pathToFileURL(dst);
176
188
  const epUrl = Bun.pathToFileURL(ep.path);
177
189
  const hashedImport = `${epUrl.href.replace(basePathUrl.href, "")}?${ep.hash}`;
178
- Bun.write(dst + "/index.html", render(finalConfig.serveOutputHtml, { hashedImport }));
190
+ hashedImports.push(hashedImport);
179
191
  }
192
+ Bun.write(dst + "/index.html", render(finalConfig.serveOutputHtml, { hashedImports }));
180
193
  }
181
194
 
182
195
  }
@@ -4,7 +4,9 @@
4
4
  <meta charset="UTF-8" />
5
5
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
6
6
  <title>Bun HTML File</title>
7
- <script type="module" src="<%= hashedImport %>"></script>
7
+ <% for (const hashedJs of hashedImports) { %>
8
+ <script type="module" src="<%= hashedJs %>"></script>
9
+ <% } %>
8
10
  </head>
9
11
 
10
12
  <body>
package/package.json CHANGED
@@ -6,7 +6,7 @@
6
6
  "type": "git",
7
7
  "url": "https://github.com/SPWizard01/bun-dev-server"
8
8
  },
9
- "version": "0.0.5",
9
+ "version": "0.0.7",
10
10
  "module": "index.ts",
11
11
  "type": "module",
12
12
  "license": "MIT",
@@ -14,7 +14,7 @@
14
14
  "serve": "bun --hot ./serve.ts"
15
15
  },
16
16
  "devDependencies": {
17
- "@types/bun": "^1.1.11",
17
+ "@types/bun": "latest",
18
18
  "@types/ejs": "^3.1.5"
19
19
  },
20
20
  "peerDependencies": {