@t8/serve 0.1.19 → 0.1.21

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
@@ -1,6 +1,10 @@
1
1
  # @t8/serve
2
2
 
3
- Simple static file server + bundler, primarily for tests, manual or automated
3
+ Simple static file server + bundler, primarily for demo apps and tests, manual or automated
4
+
5
+ Use cases:
6
+ - Use the CLI-based server to launch a demo or test app with a single CLI command.
7
+ - Use the code-based setup to launch an own server from multiple tests, each with its own app.
4
8
 
5
9
  ## CLI
6
10
 
@@ -19,7 +23,7 @@ npx @t8/serve 3000 app public dist -b src/index.ts
19
23
  ```
20
24
 
21
25
  <details>
22
- <summary>Example</summary>
26
+ <summary>Example 1 (flat)</summary>
23
27
 
24
28
  ```
25
29
  // package.json
@@ -32,7 +36,8 @@ npx @t8/serve 3000 app public dist -b src/index.ts
32
36
  /playground
33
37
  - index.css
34
38
  - index.html
35
- + <script src="/dist/index.js"></script>
39
+ contains <script src="/dist/index.js"></script>
40
+ contains <link rel="stylesheet" href="/index.css"></script>
36
41
  - index.ts
37
42
  ```
38
43
 
@@ -54,6 +59,46 @@ webServer: {
54
59
 
55
60
  </details>
56
61
 
62
+ <details>
63
+ <summary>Example 2 (nested)</summary>
64
+
65
+ ```
66
+ // package.json
67
+ "scripts": {
68
+ "play": "npx @t8/serve 3000 * playground -b src/index.tsx"
69
+ }
70
+ ```
71
+
72
+ ```
73
+ /playground
74
+ src
75
+ - App.tsx
76
+ - index.css
77
+ - index.tsx // imports "./App.tsx", "./index.css"
78
+ - index.html
79
+ contains <script src="/dist/index.js"></script>
80
+ contains <link rel="stylesheet" href="/dist/index.css"></script>
81
+ ```
82
+
83
+ ```sh
84
+ npm run play
85
+ ```
86
+
87
+ ```
88
+ // With Playwright:
89
+ // playwright.config.ts
90
+ ...
91
+ use: {
92
+ baseURL: "http://localhost:3000",
93
+ },
94
+ webServer: {
95
+ command: "npm run play",
96
+ url: "http://localhost:3000",
97
+ },
98
+ ```
99
+
100
+ </details>
101
+
57
102
  ## Code
58
103
 
59
104
  ```ts
@@ -63,8 +108,8 @@ import { serve } from "@t8/serve";
63
108
  let server = await serve({
64
109
  port: 3000,
65
110
  path: "app",
111
+ bundle: true, // or input path, or `{ input, output, dir }`
66
112
  spa: true,
67
- bundle: true,
68
113
  });
69
114
 
70
115
  // Stop
@@ -72,28 +117,66 @@ server.close();
72
117
  ```
73
118
 
74
119
  <details>
75
- <summary>Example</summary>
120
+ <summary>Example 1 (flat)</summary>
76
121
 
77
122
  ```
78
123
  /playground
79
124
  - index.css
80
125
  - index.html
81
- + <script src="/dist/index.js"></script>
126
+ contains <script src="/dist/index.js"></script>
127
+ contains <link rel="stylesheet" href="/index.css"></script>
82
128
  - index.ts
83
129
  ```
84
130
 
85
131
  ```ts
86
132
  // x.test.ts
87
133
  import { test } from "@playwright/test";
88
- import { serve, type Server } from "@t8/serve";
134
+ import { type Server, serve } from "@t8/serve";
89
135
 
90
136
  let server: Server;
91
137
 
92
138
  test.beforeAll(async () => {
93
139
  server = await serve({
94
140
  path: "playground",
95
- spa: true,
96
141
  bundle: true,
142
+ spa: true,
143
+ });
144
+ });
145
+
146
+ test.afterAll(() => {
147
+ server.close();
148
+ });
149
+ ```
150
+
151
+ </details>
152
+
153
+ <details>
154
+ <summary>Example 2 (nested)</summary>
155
+
156
+ ```
157
+ /tests/x
158
+ src
159
+ - index.css
160
+ - index.tsx // imports "./App.tsx", "./index.css"
161
+ - index.html
162
+ contains <script src="/dist/index.js"></script>
163
+ contains <link rel="stylesheet" href="/dist/index.css"></script>
164
+ - index.ts
165
+ - index.test.ts
166
+ ```
167
+
168
+ ```ts
169
+ // tests/x/index.test.ts
170
+ import { test } from "@playwright/test";
171
+ import { serve, type Server } from "@t8/serve";
172
+
173
+ let server: Server;
174
+
175
+ test.beforeAll(async () => {
176
+ server = await serve({
177
+ path: "tests/x",
178
+ bundle: "src/index.tsx",
179
+ spa: true,
97
180
  });
98
181
  });
99
182
 
package/dist/index.js CHANGED
@@ -16,9 +16,10 @@ async function bundle({ path = "", bundle: options } = {}) {
16
16
  input: options
17
17
  };
18
18
  else normalizedOptions = options;
19
+ let dir = normalizedOptions.dir ?? "dist";
19
20
  let inputFile = join(path, normalizedOptions.input ?? "index.ts");
20
- let outputFile = join(path, "dist", normalizedOptions.output ?? "index.js");
21
- await rm(join(path, "dist"), { recursive: true, force: true });
21
+ let outputFile = join(path, dir, normalizedOptions.output ?? "index.js");
22
+ await rm(join(path, dir), { recursive: true, force: true });
22
23
  await build({
23
24
  entryPoints: [inputFile],
24
25
  outfile: outputFile,
package/dist/run.cjs CHANGED
@@ -19,9 +19,10 @@ async function bundle({ path = "", bundle: options } = {}) {
19
19
  input: options
20
20
  };
21
21
  else normalizedOptions = options;
22
+ let dir = normalizedOptions.dir ?? "dist";
22
23
  let inputFile = (0, import_node_path.join)(path, normalizedOptions.input ?? "index.ts");
23
- let outputFile = (0, import_node_path.join)(path, "dist", normalizedOptions.output ?? "index.js");
24
- await (0, import_promises.rm)((0, import_node_path.join)(path, "dist"), { recursive: true, force: true });
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 });
25
26
  await (0, import_esbuild.build)({
26
27
  entryPoints: [inputFile],
27
28
  outfile: outputFile,
package/dist/run.mjs CHANGED
@@ -18,9 +18,10 @@ async function bundle({ path = "", bundle: options } = {}) {
18
18
  input: options
19
19
  };
20
20
  else normalizedOptions = options;
21
+ let dir = normalizedOptions.dir ?? "dist";
21
22
  let inputFile = join(path, normalizedOptions.input ?? "index.ts");
22
- let outputFile = join(path, "dist", normalizedOptions.output ?? "index.js");
23
- await rm(join(path, "dist"), { recursive: true, force: true });
23
+ let outputFile = join(path, dir, normalizedOptions.output ?? "index.js");
24
+ await rm(join(path, dir), { recursive: true, force: true });
24
25
  await build({
25
26
  entryPoints: [inputFile],
26
27
  outfile: outputFile,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@t8/serve",
3
- "version": "0.1.19",
4
- "description": "",
3
+ "version": "0.1.21",
4
+ "description": "Simple static file server + bundler, primarily for demo apps and tests, manual or automated",
5
5
  "keywords": [
6
6
  "node",
7
7
  "server",
@@ -20,7 +20,7 @@
20
20
  "main": "dist/index.js",
21
21
  "scripts": {
22
22
  "build": "npx npm-run-all clean -p compile compile-mjs-bin compile-cjs-bin",
23
- "clean": "node -e \"require('node:fs').rmSync('dist', {force: true, recursive: true});\"",
23
+ "clean": "node -e \"require('node:fs').rmSync('dist', { force: true, recursive: true });\"",
24
24
  "compile": "npx esbuild index.ts --bundle --outdir=dist --platform=node --format=esm --external:esbuild",
25
25
  "compile-mjs-bin": "npx esbuild src/run.ts --bundle --outfile=dist/run.mjs --platform=node --format=esm --external:esbuild",
26
26
  "compile-cjs-bin": "npx esbuild src/run.ts --bundle --outfile=dist/run.cjs --platform=node --format=cjs --external:esbuild",
@@ -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
- bundle?: boolean | string | BundleConfig;
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
@@ -16,10 +16,11 @@ export async function bundle({ path = "", bundle: options }: Config = {}) {
16
16
  };
17
17
  else normalizedOptions = options;
18
18
 
19
+ let dir = normalizedOptions.dir ?? "dist";
19
20
  let inputFile = join(path, normalizedOptions.input ?? "index.ts");
20
- let outputFile = join(path, "dist", normalizedOptions.output ?? "index.js");
21
+ let outputFile = join(path, dir, normalizedOptions.output ?? "index.js");
21
22
 
22
- await rm(join(path, "dist"), { recursive: true, force: true });
23
+ await rm(join(path, dir), { recursive: true, force: true });
23
24
 
24
25
  await build({
25
26
  entryPoints: [inputFile],
@@ -1,6 +1,7 @@
1
1
  import { access, lstat } from "node:fs/promises";
2
2
 
3
3
  export async function isValidFilePath(filePath: string, dirPath: string) {
4
+ // Disallow path traversal outside `dirPath`
4
5
  if (!filePath.startsWith(dirPath)) return false;
5
6
 
6
7
  try {