@t8/serve 0.1.18 → 0.1.20
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 +4 -4
- package/dist/index.js +11 -8
- package/dist/run.cjs +11 -8
- package/dist/run.mjs +11 -8
- package/package.json +7 -4
- package/src/BundleConfig.ts +16 -0
- package/src/Config.ts +30 -1
- package/src/bundle.ts +12 -9
package/README.md
CHANGED
|
@@ -32,7 +32,7 @@ npx @t8/serve 3000 app public dist -b src/index.ts
|
|
|
32
32
|
/playground
|
|
33
33
|
- index.css
|
|
34
34
|
- index.html
|
|
35
|
-
|
|
35
|
+
+ <script src="/dist/index.js"></script>
|
|
36
36
|
- index.ts
|
|
37
37
|
```
|
|
38
38
|
|
|
@@ -63,8 +63,8 @@ import { serve } from "@t8/serve";
|
|
|
63
63
|
let server = await serve({
|
|
64
64
|
port: 3000,
|
|
65
65
|
path: "app",
|
|
66
|
+
bundle: true, // or `{ input, output }`, or `input` path
|
|
66
67
|
spa: true,
|
|
67
|
-
bundle: true,
|
|
68
68
|
});
|
|
69
69
|
|
|
70
70
|
// Stop
|
|
@@ -78,7 +78,7 @@ server.close();
|
|
|
78
78
|
/playground
|
|
79
79
|
- index.css
|
|
80
80
|
- index.html
|
|
81
|
-
|
|
81
|
+
+ <script src="/dist/index.js"></script>
|
|
82
82
|
- index.ts
|
|
83
83
|
```
|
|
84
84
|
|
|
@@ -92,8 +92,8 @@ let server: Server;
|
|
|
92
92
|
test.beforeAll(async () => {
|
|
93
93
|
server = await serve({
|
|
94
94
|
path: "playground",
|
|
95
|
+
bundle: "src/index.tsx",
|
|
95
96
|
spa: true,
|
|
96
|
-
bundle: true,
|
|
97
97
|
});
|
|
98
98
|
});
|
|
99
99
|
|
package/dist/index.js
CHANGED
|
@@ -4,11 +4,9 @@ import { createServer } from "node:http";
|
|
|
4
4
|
import { extname } from "node:path";
|
|
5
5
|
|
|
6
6
|
// src/bundle.ts
|
|
7
|
-
import { exec as originalExec } from "node:child_process";
|
|
8
7
|
import { rm } from "node:fs/promises";
|
|
9
8
|
import { join } from "node:path";
|
|
10
|
-
import {
|
|
11
|
-
var exec = promisify(originalExec);
|
|
9
|
+
import { build } from "esbuild";
|
|
12
10
|
async function bundle({ path = "", bundle: options } = {}) {
|
|
13
11
|
if (!options) return;
|
|
14
12
|
let normalizedOptions;
|
|
@@ -18,12 +16,17 @@ async function bundle({ path = "", bundle: options } = {}) {
|
|
|
18
16
|
input: options
|
|
19
17
|
};
|
|
20
18
|
else normalizedOptions = options;
|
|
19
|
+
let dir = normalizedOptions.dir ?? "dist";
|
|
21
20
|
let inputFile = join(path, normalizedOptions.input ?? "index.ts");
|
|
22
|
-
let outputFile = join(path,
|
|
23
|
-
await rm(join(path,
|
|
24
|
-
await
|
|
25
|
-
|
|
26
|
-
|
|
21
|
+
let outputFile = join(path, dir, normalizedOptions.output ?? "index.js");
|
|
22
|
+
await rm(join(path, dir), { recursive: true, force: true });
|
|
23
|
+
await build({
|
|
24
|
+
entryPoints: [inputFile],
|
|
25
|
+
outfile: outputFile,
|
|
26
|
+
bundle: true,
|
|
27
|
+
platform: "browser",
|
|
28
|
+
logLevel: "warning"
|
|
29
|
+
});
|
|
27
30
|
}
|
|
28
31
|
|
|
29
32
|
// src/getFilePath.ts
|
package/dist/run.cjs
CHANGED
|
@@ -7,11 +7,9 @@ var import_node_http = require("node:http");
|
|
|
7
7
|
var import_node_path3 = require("node:path");
|
|
8
8
|
|
|
9
9
|
// src/bundle.ts
|
|
10
|
-
var import_node_child_process = require("node:child_process");
|
|
11
10
|
var import_promises = require("node:fs/promises");
|
|
12
11
|
var import_node_path = require("node:path");
|
|
13
|
-
var
|
|
14
|
-
var exec = (0, import_node_util.promisify)(import_node_child_process.exec);
|
|
12
|
+
var import_esbuild = require("esbuild");
|
|
15
13
|
async function bundle({ path = "", bundle: options } = {}) {
|
|
16
14
|
if (!options) return;
|
|
17
15
|
let normalizedOptions;
|
|
@@ -21,12 +19,17 @@ async function bundle({ path = "", bundle: options } = {}) {
|
|
|
21
19
|
input: options
|
|
22
20
|
};
|
|
23
21
|
else normalizedOptions = options;
|
|
22
|
+
let dir = normalizedOptions.dir ?? "dist";
|
|
24
23
|
let inputFile = (0, import_node_path.join)(path, normalizedOptions.input ?? "index.ts");
|
|
25
|
-
let outputFile = (0, import_node_path.join)(path,
|
|
26
|
-
await (0, import_promises.rm)((0, import_node_path.join)(path,
|
|
27
|
-
await
|
|
28
|
-
|
|
29
|
-
|
|
24
|
+
let outputFile = (0, import_node_path.join)(path, dir, normalizedOptions.output ?? "index.js");
|
|
25
|
+
await (0, import_promises.rm)((0, import_node_path.join)(path, dir), { recursive: true, force: true });
|
|
26
|
+
await (0, import_esbuild.build)({
|
|
27
|
+
entryPoints: [inputFile],
|
|
28
|
+
outfile: outputFile,
|
|
29
|
+
bundle: true,
|
|
30
|
+
platform: "browser",
|
|
31
|
+
logLevel: "warning"
|
|
32
|
+
});
|
|
30
33
|
}
|
|
31
34
|
|
|
32
35
|
// src/getFilePath.ts
|
package/dist/run.mjs
CHANGED
|
@@ -6,11 +6,9 @@ import { createServer } from "node:http";
|
|
|
6
6
|
import { extname } from "node:path";
|
|
7
7
|
|
|
8
8
|
// src/bundle.ts
|
|
9
|
-
import { exec as originalExec } from "node:child_process";
|
|
10
9
|
import { rm } from "node:fs/promises";
|
|
11
10
|
import { join } from "node:path";
|
|
12
|
-
import {
|
|
13
|
-
var exec = promisify(originalExec);
|
|
11
|
+
import { build } from "esbuild";
|
|
14
12
|
async function bundle({ path = "", bundle: options } = {}) {
|
|
15
13
|
if (!options) return;
|
|
16
14
|
let normalizedOptions;
|
|
@@ -20,12 +18,17 @@ async function bundle({ path = "", bundle: options } = {}) {
|
|
|
20
18
|
input: options
|
|
21
19
|
};
|
|
22
20
|
else normalizedOptions = options;
|
|
21
|
+
let dir = normalizedOptions.dir ?? "dist";
|
|
23
22
|
let inputFile = join(path, normalizedOptions.input ?? "index.ts");
|
|
24
|
-
let outputFile = join(path,
|
|
25
|
-
await rm(join(path,
|
|
26
|
-
await
|
|
27
|
-
|
|
28
|
-
|
|
23
|
+
let outputFile = join(path, dir, normalizedOptions.output ?? "index.js");
|
|
24
|
+
await rm(join(path, dir), { recursive: true, force: true });
|
|
25
|
+
await build({
|
|
26
|
+
entryPoints: [inputFile],
|
|
27
|
+
outfile: outputFile,
|
|
28
|
+
bundle: true,
|
|
29
|
+
platform: "browser",
|
|
30
|
+
logLevel: "warning"
|
|
31
|
+
});
|
|
29
32
|
}
|
|
30
33
|
|
|
31
34
|
// src/getFilePath.ts
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@t8/serve",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.20",
|
|
4
4
|
"description": "",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"node",
|
|
@@ -21,9 +21,9 @@
|
|
|
21
21
|
"scripts": {
|
|
22
22
|
"build": "npx npm-run-all clean -p compile compile-mjs-bin compile-cjs-bin",
|
|
23
23
|
"clean": "node -e \"require('node:fs').rmSync('dist', {force: true, recursive: true});\"",
|
|
24
|
-
"compile": "npx esbuild index.ts --bundle --outdir=dist --platform=node --format=esm",
|
|
25
|
-
"compile-mjs-bin": "npx esbuild src/run.ts --bundle --outfile=dist/run.mjs --platform=node --format=esm",
|
|
26
|
-
"compile-cjs-bin": "npx esbuild src/run.ts --bundle --outfile=dist/run.cjs --platform=node --format=cjs",
|
|
24
|
+
"compile": "npx esbuild index.ts --bundle --outdir=dist --platform=node --format=esm --external:esbuild",
|
|
25
|
+
"compile-mjs-bin": "npx esbuild src/run.ts --bundle --outfile=dist/run.mjs --platform=node --format=esm --external:esbuild",
|
|
26
|
+
"compile-cjs-bin": "npx esbuild src/run.ts --bundle --outfile=dist/run.cjs --platform=node --format=cjs --external:esbuild",
|
|
27
27
|
"prepublishOnly": "npm run build",
|
|
28
28
|
"preversion": "npx npm-run-all typecheck shape build",
|
|
29
29
|
"shape": "npx codeshape",
|
|
@@ -32,5 +32,8 @@
|
|
|
32
32
|
"devDependencies": {
|
|
33
33
|
"@types/node": "^24.5.2",
|
|
34
34
|
"typescript": "^5.9.2"
|
|
35
|
+
},
|
|
36
|
+
"dependencies": {
|
|
37
|
+
"esbuild": "^0.25.10"
|
|
35
38
|
}
|
|
36
39
|
}
|
package/src/BundleConfig.ts
CHANGED
|
@@ -1,4 +1,20 @@
|
|
|
1
1
|
export type BundleConfig = {
|
|
2
|
+
/**
|
|
3
|
+
* Output directory path relative to the server config `path`.
|
|
4
|
+
*
|
|
5
|
+
* @defaultValue "dist"
|
|
6
|
+
*/
|
|
7
|
+
dir?: string;
|
|
8
|
+
/**
|
|
9
|
+
* Input path relative to the server config `path`.
|
|
10
|
+
*
|
|
11
|
+
* @defaultValue "index.ts"
|
|
12
|
+
*/
|
|
2
13
|
input?: string;
|
|
14
|
+
/**
|
|
15
|
+
* Output path relative to the bundle config `dir`.
|
|
16
|
+
*
|
|
17
|
+
* @defaultValue "index.js"
|
|
18
|
+
*/
|
|
3
19
|
output?: string;
|
|
4
20
|
};
|
package/src/Config.ts
CHANGED
|
@@ -1,12 +1,41 @@
|
|
|
1
1
|
import type { BundleConfig } from "./BundleConfig";
|
|
2
2
|
|
|
3
3
|
export type Config = {
|
|
4
|
+
/** Server URL. */
|
|
4
5
|
url?: string;
|
|
6
|
+
/**
|
|
7
|
+
* Server host.
|
|
8
|
+
*
|
|
9
|
+
* @defaultValue "localhost"
|
|
10
|
+
*/
|
|
5
11
|
host?: string;
|
|
12
|
+
/**
|
|
13
|
+
* Server port.
|
|
14
|
+
*
|
|
15
|
+
* @defaultValue 3000
|
|
16
|
+
*/
|
|
6
17
|
port?: number;
|
|
18
|
+
/** Application path. */
|
|
7
19
|
path?: string;
|
|
20
|
+
/**
|
|
21
|
+
* Public assets directories. If not provided, the application
|
|
22
|
+
* `path` is served as a public assets directory.
|
|
23
|
+
*/
|
|
8
24
|
dirs?: string[];
|
|
25
|
+
/**
|
|
26
|
+
* Enables single-page application (SPA) mode. If `true`, all
|
|
27
|
+
* unmatched URLs are served as "/".
|
|
28
|
+
*/
|
|
9
29
|
spa?: boolean;
|
|
30
|
+
/** Whether to log to the console. */
|
|
10
31
|
log?: boolean;
|
|
11
|
-
|
|
32
|
+
/**
|
|
33
|
+
* Bundle config.
|
|
34
|
+
*
|
|
35
|
+
* If `undefined`, bundling is skipped.
|
|
36
|
+
* Otherwise, its type is `BundleConfig`, with the following shorthand options:
|
|
37
|
+
* If `true`, it's equivalent to `{ input: "index.ts", output: "index.js" }`.
|
|
38
|
+
* If `string`, it's equivalent to `input` in `{ input, ouput: "index.js" }`.
|
|
39
|
+
*/
|
|
40
|
+
bundle?: boolean | string | BundleConfig | undefined;
|
|
12
41
|
};
|
package/src/bundle.ts
CHANGED
|
@@ -1,12 +1,9 @@
|
|
|
1
|
-
import { exec as originalExec } from "node:child_process";
|
|
2
1
|
import { rm } from "node:fs/promises";
|
|
3
2
|
import { join } from "node:path";
|
|
4
|
-
import {
|
|
3
|
+
import { build } from "esbuild";
|
|
5
4
|
import type { BundleConfig } from "./BundleConfig";
|
|
6
5
|
import type { Config } from "./Config";
|
|
7
6
|
|
|
8
|
-
const exec = promisify(originalExec);
|
|
9
|
-
|
|
10
7
|
export async function bundle({ path = "", bundle: options }: Config = {}) {
|
|
11
8
|
if (!options) return;
|
|
12
9
|
|
|
@@ -19,11 +16,17 @@ export async function bundle({ path = "", bundle: options }: Config = {}) {
|
|
|
19
16
|
};
|
|
20
17
|
else normalizedOptions = options;
|
|
21
18
|
|
|
19
|
+
let dir = normalizedOptions.dir ?? "dist";
|
|
22
20
|
let inputFile = join(path, normalizedOptions.input ?? "index.ts");
|
|
23
|
-
let outputFile = join(path,
|
|
21
|
+
let outputFile = join(path, dir, normalizedOptions.output ?? "index.js");
|
|
22
|
+
|
|
23
|
+
await rm(join(path, dir), { recursive: true, force: true });
|
|
24
24
|
|
|
25
|
-
await
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
25
|
+
await build({
|
|
26
|
+
entryPoints: [inputFile],
|
|
27
|
+
outfile: outputFile,
|
|
28
|
+
bundle: true,
|
|
29
|
+
platform: "browser",
|
|
30
|
+
logLevel: "warning",
|
|
31
|
+
});
|
|
29
32
|
}
|