bun-dev-server 0.0.5 → 0.0.6
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 +6 -0
- package/bunManifest.ts +18 -0
- package/bunServeConfig.ts +2 -0
- package/index.ts +14 -4
- package/indexHTMLTemplate.ejs +3 -1
- package/package.json +1 -1
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,8 @@ export interface BunDevServerConfig extends Partial<BunDevServerSocketConfig> {
|
|
|
5
5
|
buildConfig: BuildConfig;
|
|
6
6
|
watchDir?: string;
|
|
7
7
|
enableTypeScriptWatch?: boolean;
|
|
8
|
+
writeManifest?: boolean;
|
|
9
|
+
manifestName?: string;
|
|
8
10
|
/**
|
|
9
11
|
* The path to the directory to serve files from.
|
|
10
12
|
* 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,10 @@ 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
|
+
|
|
154
162
|
}
|
|
155
163
|
|
|
156
164
|
function publishOutputLogs(output: Bun.BuildOutput, event: FileChangeInfo<string>) {
|
|
@@ -170,13 +178,15 @@ export async function startBunDevServer(serverConfig: BunDevServerConfig) {
|
|
|
170
178
|
}
|
|
171
179
|
|
|
172
180
|
function publishIndexHTML(output: Bun.BuildOutput, event: FileChangeInfo<string>) {
|
|
173
|
-
const
|
|
174
|
-
|
|
181
|
+
const eps = output.outputs.filter(o => o.kind === "entry-point");
|
|
182
|
+
const hashedImports: string[] = [];
|
|
183
|
+
for (const ep of eps) {
|
|
175
184
|
const basePathUrl = Bun.pathToFileURL(dst);
|
|
176
185
|
const epUrl = Bun.pathToFileURL(ep.path);
|
|
177
186
|
const hashedImport = `${epUrl.href.replace(basePathUrl.href, "")}?${ep.hash}`;
|
|
178
|
-
|
|
187
|
+
hashedImports.push(hashedImport);
|
|
179
188
|
}
|
|
189
|
+
Bun.write(dst + "/index.html", render(finalConfig.serveOutputHtml, { hashedImports }));
|
|
180
190
|
}
|
|
181
191
|
|
|
182
192
|
}
|
package/indexHTMLTemplate.ejs
CHANGED
|
@@ -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
|
-
|
|
7
|
+
<% for (const hashedJs of hashedImports) { %>
|
|
8
|
+
<script type="module" src="<%= hashedJs %>"></script>
|
|
9
|
+
<% } %>
|
|
8
10
|
</head>
|
|
9
11
|
|
|
10
12
|
<body>
|