plgg-bundle 0.0.1

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.
Files changed (235) hide show
  1. package/README.md +68 -0
  2. package/bin/appAliasHook.mjs +89 -0
  3. package/bin/hook.mjs +94 -0
  4. package/bin/plgg-bundle.mjs +16 -0
  5. package/dist/Dev/fixtures/devPoc.spec.d.ts +2 -0
  6. package/dist/Dev/fixtures/devPoc.spec.d.ts.map +1 -0
  7. package/dist/Dev/fixtures/devPoc.spec.js +145 -0
  8. package/dist/Dev/fixtures/devPoc.spec.js.map +1 -0
  9. package/dist/Dev/model/Fetch.d.ts +20 -0
  10. package/dist/Dev/model/Fetch.d.ts.map +1 -0
  11. package/dist/Dev/model/Fetch.js +8 -0
  12. package/dist/Dev/model/Fetch.js.map +1 -0
  13. package/dist/Dev/model/ModuleGraph.d.ts +27 -0
  14. package/dist/Dev/model/ModuleGraph.d.ts.map +1 -0
  15. package/dist/Dev/model/ModuleGraph.js +2 -0
  16. package/dist/Dev/model/ModuleGraph.js.map +1 -0
  17. package/dist/Dev/model/Protocol.d.ts +22 -0
  18. package/dist/Dev/model/Protocol.d.ts.map +1 -0
  19. package/dist/Dev/model/Protocol.js +31 -0
  20. package/dist/Dev/model/Protocol.js.map +1 -0
  21. package/dist/Dev/node/devServer.d.ts +21 -0
  22. package/dist/Dev/node/devServer.d.ts.map +1 -0
  23. package/dist/Dev/node/devServer.js +298 -0
  24. package/dist/Dev/node/devServer.js.map +1 -0
  25. package/dist/Dev/node/httpAdapter.d.ts +19 -0
  26. package/dist/Dev/node/httpAdapter.d.ts.map +1 -0
  27. package/dist/Dev/node/httpAdapter.js +76 -0
  28. package/dist/Dev/node/httpAdapter.js.map +1 -0
  29. package/dist/Dev/node/scanGraph.d.ts +21 -0
  30. package/dist/Dev/node/scanGraph.d.ts.map +1 -0
  31. package/dist/Dev/node/scanGraph.js +145 -0
  32. package/dist/Dev/node/scanGraph.js.map +1 -0
  33. package/dist/Dev/node/watch.d.ts +10 -0
  34. package/dist/Dev/node/watch.d.ts.map +1 -0
  35. package/dist/Dev/node/watch.js +44 -0
  36. package/dist/Dev/node/watch.js.map +1 -0
  37. package/dist/Dev/usecase/allowedHost.d.ts +16 -0
  38. package/dist/Dev/usecase/allowedHost.d.ts.map +1 -0
  39. package/dist/Dev/usecase/allowedHost.js +33 -0
  40. package/dist/Dev/usecase/allowedHost.js.map +1 -0
  41. package/dist/Dev/usecase/allowedHost.spec.d.ts +2 -0
  42. package/dist/Dev/usecase/allowedHost.spec.d.ts.map +1 -0
  43. package/dist/Dev/usecase/allowedHost.spec.js +13 -0
  44. package/dist/Dev/usecase/allowedHost.spec.js.map +1 -0
  45. package/dist/Dev/usecase/buildGraph.d.ts +12 -0
  46. package/dist/Dev/usecase/buildGraph.d.ts.map +1 -0
  47. package/dist/Dev/usecase/buildGraph.js +24 -0
  48. package/dist/Dev/usecase/buildGraph.js.map +1 -0
  49. package/dist/Dev/usecase/buildGraph.spec.d.ts +2 -0
  50. package/dist/Dev/usecase/buildGraph.spec.d.ts.map +1 -0
  51. package/dist/Dev/usecase/buildGraph.spec.js +16 -0
  52. package/dist/Dev/usecase/buildGraph.spec.js.map +1 -0
  53. package/dist/Dev/usecase/decorateDevHtml.d.ts +12 -0
  54. package/dist/Dev/usecase/decorateDevHtml.d.ts.map +1 -0
  55. package/dist/Dev/usecase/decorateDevHtml.js +15 -0
  56. package/dist/Dev/usecase/decorateDevHtml.js.map +1 -0
  57. package/dist/Dev/usecase/decorateDevHtml.spec.d.ts +2 -0
  58. package/dist/Dev/usecase/decorateDevHtml.spec.d.ts.map +1 -0
  59. package/dist/Dev/usecase/decorateDevHtml.spec.js +19 -0
  60. package/dist/Dev/usecase/decorateDevHtml.spec.js.map +1 -0
  61. package/dist/Dev/usecase/invalidate.d.ts +17 -0
  62. package/dist/Dev/usecase/invalidate.d.ts.map +1 -0
  63. package/dist/Dev/usecase/invalidate.js +57 -0
  64. package/dist/Dev/usecase/invalidate.js.map +1 -0
  65. package/dist/Dev/usecase/invalidate.spec.d.ts +2 -0
  66. package/dist/Dev/usecase/invalidate.spec.d.ts.map +1 -0
  67. package/dist/Dev/usecase/invalidate.spec.js +64 -0
  68. package/dist/Dev/usecase/invalidate.spec.js.map +1 -0
  69. package/dist/Dev/usecase/parseImports.d.ts +24 -0
  70. package/dist/Dev/usecase/parseImports.d.ts.map +1 -0
  71. package/dist/Dev/usecase/parseImports.js +40 -0
  72. package/dist/Dev/usecase/parseImports.js.map +1 -0
  73. package/dist/Dev/usecase/parseImports.spec.d.ts +2 -0
  74. package/dist/Dev/usecase/parseImports.spec.d.ts.map +1 -0
  75. package/dist/Dev/usecase/parseImports.spec.js +24 -0
  76. package/dist/Dev/usecase/parseImports.spec.js.map +1 -0
  77. package/dist/Dev/usecase/reloadDecision.d.ts +20 -0
  78. package/dist/Dev/usecase/reloadDecision.d.ts.map +1 -0
  79. package/dist/Dev/usecase/reloadDecision.js +52 -0
  80. package/dist/Dev/usecase/reloadDecision.js.map +1 -0
  81. package/dist/Dev/usecase/reloadDecision.spec.d.ts +2 -0
  82. package/dist/Dev/usecase/reloadDecision.spec.d.ts.map +1 -0
  83. package/dist/Dev/usecase/reloadDecision.spec.js +17 -0
  84. package/dist/Dev/usecase/reloadDecision.spec.js.map +1 -0
  85. package/dist/domain/model/BundleConfig.d.ts +136 -0
  86. package/dist/domain/model/BundleConfig.d.ts.map +1 -0
  87. package/dist/domain/model/BundleConfig.js +18 -0
  88. package/dist/domain/model/BundleConfig.js.map +1 -0
  89. package/dist/domain/model/BundleConfig.spec.d.ts +2 -0
  90. package/dist/domain/model/BundleConfig.spec.d.ts.map +1 -0
  91. package/dist/domain/model/BundleConfig.spec.js +8 -0
  92. package/dist/domain/model/BundleConfig.spec.js.map +1 -0
  93. package/dist/domain/usecase/asBundleConfig.d.ts +17 -0
  94. package/dist/domain/usecase/asBundleConfig.d.ts.map +1 -0
  95. package/dist/domain/usecase/asBundleConfig.js +179 -0
  96. package/dist/domain/usecase/asBundleConfig.js.map +1 -0
  97. package/dist/domain/usecase/asBundleConfig.spec.d.ts +2 -0
  98. package/dist/domain/usecase/asBundleConfig.spec.d.ts.map +1 -0
  99. package/dist/domain/usecase/asBundleConfig.spec.js +119 -0
  100. package/dist/domain/usecase/asBundleConfig.spec.js.map +1 -0
  101. package/dist/domain/usecase/build.d.ts +14 -0
  102. package/dist/domain/usecase/build.d.ts.map +1 -0
  103. package/dist/domain/usecase/build.js +177 -0
  104. package/dist/domain/usecase/build.js.map +1 -0
  105. package/dist/domain/usecase/collectModules.d.ts +53 -0
  106. package/dist/domain/usecase/collectModules.d.ts.map +1 -0
  107. package/dist/domain/usecase/collectModules.js +118 -0
  108. package/dist/domain/usecase/collectModules.js.map +1 -0
  109. package/dist/domain/usecase/deriveExternal.d.ts +25 -0
  110. package/dist/domain/usecase/deriveExternal.d.ts.map +1 -0
  111. package/dist/domain/usecase/deriveExternal.js +79 -0
  112. package/dist/domain/usecase/deriveExternal.js.map +1 -0
  113. package/dist/domain/usecase/deriveExternal.spec.d.ts +2 -0
  114. package/dist/domain/usecase/deriveExternal.spec.d.ts.map +1 -0
  115. package/dist/domain/usecase/deriveExternal.spec.js +56 -0
  116. package/dist/domain/usecase/deriveExternal.spec.js.map +1 -0
  117. package/dist/domain/usecase/discoverWorkspace.d.ts +27 -0
  118. package/dist/domain/usecase/discoverWorkspace.d.ts.map +1 -0
  119. package/dist/domain/usecase/discoverWorkspace.js +116 -0
  120. package/dist/domain/usecase/discoverWorkspace.js.map +1 -0
  121. package/dist/domain/usecase/discoverWorkspace.spec.d.ts +2 -0
  122. package/dist/domain/usecase/discoverWorkspace.spec.d.ts.map +1 -0
  123. package/dist/domain/usecase/discoverWorkspace.spec.js +23 -0
  124. package/dist/domain/usecase/discoverWorkspace.spec.js.map +1 -0
  125. package/dist/domain/usecase/emitBundle.d.ts +43 -0
  126. package/dist/domain/usecase/emitBundle.d.ts.map +1 -0
  127. package/dist/domain/usecase/emitBundle.js +157 -0
  128. package/dist/domain/usecase/emitBundle.js.map +1 -0
  129. package/dist/domain/usecase/emitBundle.spec.d.ts +2 -0
  130. package/dist/domain/usecase/emitBundle.spec.d.ts.map +1 -0
  131. package/dist/domain/usecase/emitBundle.spec.js +80 -0
  132. package/dist/domain/usecase/emitBundle.spec.js.map +1 -0
  133. package/dist/domain/usecase/emitDts.d.ts +32 -0
  134. package/dist/domain/usecase/emitDts.d.ts.map +1 -0
  135. package/dist/domain/usecase/emitDts.js +99 -0
  136. package/dist/domain/usecase/emitDts.js.map +1 -0
  137. package/dist/domain/usecase/isExternal.d.ts +12 -0
  138. package/dist/domain/usecase/isExternal.d.ts.map +1 -0
  139. package/dist/domain/usecase/isExternal.js +16 -0
  140. package/dist/domain/usecase/isExternal.js.map +1 -0
  141. package/dist/domain/usecase/isExternal.spec.d.ts +2 -0
  142. package/dist/domain/usecase/isExternal.spec.d.ts.map +1 -0
  143. package/dist/domain/usecase/isExternal.spec.js +16 -0
  144. package/dist/domain/usecase/isExternal.spec.js.map +1 -0
  145. package/dist/domain/usecase/resolveSpecifier.d.ts +35 -0
  146. package/dist/domain/usecase/resolveSpecifier.d.ts.map +1 -0
  147. package/dist/domain/usecase/resolveSpecifier.js +83 -0
  148. package/dist/domain/usecase/resolveSpecifier.js.map +1 -0
  149. package/dist/domain/usecase/resolveSpecifier.spec.d.ts +2 -0
  150. package/dist/domain/usecase/resolveSpecifier.spec.d.ts.map +1 -0
  151. package/dist/domain/usecase/resolveSpecifier.spec.js +44 -0
  152. package/dist/domain/usecase/resolveSpecifier.spec.js.map +1 -0
  153. package/dist/domain/usecase/resolveWorkspaceSpecifier.d.ts +29 -0
  154. package/dist/domain/usecase/resolveWorkspaceSpecifier.d.ts.map +1 -0
  155. package/dist/domain/usecase/resolveWorkspaceSpecifier.js +89 -0
  156. package/dist/domain/usecase/resolveWorkspaceSpecifier.js.map +1 -0
  157. package/dist/domain/usecase/resolveWorkspaceSpecifier.spec.d.ts +2 -0
  158. package/dist/domain/usecase/resolveWorkspaceSpecifier.spec.d.ts.map +1 -0
  159. package/dist/domain/usecase/resolveWorkspaceSpecifier.spec.js +27 -0
  160. package/dist/domain/usecase/resolveWorkspaceSpecifier.spec.js.map +1 -0
  161. package/dist/domain/usecase/rewriteDtsAliases.d.ts +25 -0
  162. package/dist/domain/usecase/rewriteDtsAliases.d.ts.map +1 -0
  163. package/dist/domain/usecase/rewriteDtsAliases.js +71 -0
  164. package/dist/domain/usecase/rewriteDtsAliases.js.map +1 -0
  165. package/dist/domain/usecase/rewriteDtsAliases.spec.d.ts +2 -0
  166. package/dist/domain/usecase/rewriteDtsAliases.spec.d.ts.map +1 -0
  167. package/dist/domain/usecase/rewriteDtsAliases.spec.js +35 -0
  168. package/dist/domain/usecase/rewriteDtsAliases.spec.js.map +1 -0
  169. package/dist/entrypoints/cli.d.ts +2 -0
  170. package/dist/entrypoints/cli.d.ts.map +1 -0
  171. package/dist/entrypoints/cli.js +79 -0
  172. package/dist/entrypoints/cli.js.map +1 -0
  173. package/dist/index.d.ts +21 -0
  174. package/dist/index.d.ts.map +1 -0
  175. package/dist/index.js +28 -0
  176. package/dist/index.js.map +1 -0
  177. package/dist/vendors/runner.d.ts +20 -0
  178. package/dist/vendors/runner.d.ts.map +1 -0
  179. package/dist/vendors/runner.js +60 -0
  180. package/dist/vendors/runner.js.map +1 -0
  181. package/dist/vendors/runner.spec.d.ts +2 -0
  182. package/dist/vendors/runner.spec.d.ts.map +1 -0
  183. package/dist/vendors/runner.spec.js +20 -0
  184. package/dist/vendors/runner.spec.js.map +1 -0
  185. package/dist/vendors/transpiler.d.ts +19 -0
  186. package/dist/vendors/transpiler.d.ts.map +1 -0
  187. package/dist/vendors/transpiler.js +42 -0
  188. package/dist/vendors/transpiler.js.map +1 -0
  189. package/package.json +31 -0
  190. package/src/Dev/fixtures/devPoc.spec.ts +235 -0
  191. package/src/Dev/model/Fetch.ts +31 -0
  192. package/src/Dev/model/ModuleGraph.ts +30 -0
  193. package/src/Dev/model/Protocol.ts +35 -0
  194. package/src/Dev/node/devServer.ts +503 -0
  195. package/src/Dev/node/httpAdapter.ts +162 -0
  196. package/src/Dev/node/scanGraph.ts +203 -0
  197. package/src/Dev/node/watch.ts +54 -0
  198. package/src/Dev/usecase/allowedHost.spec.ts +50 -0
  199. package/src/Dev/usecase/allowedHost.ts +39 -0
  200. package/src/Dev/usecase/buildGraph.spec.ts +30 -0
  201. package/src/Dev/usecase/buildGraph.ts +31 -0
  202. package/src/Dev/usecase/decorateDevHtml.spec.ts +39 -0
  203. package/src/Dev/usecase/decorateDevHtml.ts +21 -0
  204. package/src/Dev/usecase/invalidate.spec.ts +88 -0
  205. package/src/Dev/usecase/invalidate.ts +67 -0
  206. package/src/Dev/usecase/parseImports.spec.ts +58 -0
  207. package/src/Dev/usecase/parseImports.ts +50 -0
  208. package/src/Dev/usecase/reloadDecision.spec.ts +50 -0
  209. package/src/Dev/usecase/reloadDecision.ts +65 -0
  210. package/src/domain/model/BundleConfig.spec.ts +35 -0
  211. package/src/domain/model/BundleConfig.ts +162 -0
  212. package/src/domain/usecase/asBundleConfig.spec.ts +230 -0
  213. package/src/domain/usecase/asBundleConfig.ts +257 -0
  214. package/src/domain/usecase/build.ts +259 -0
  215. package/src/domain/usecase/collectModules.ts +226 -0
  216. package/src/domain/usecase/deriveExternal.spec.ts +125 -0
  217. package/src/domain/usecase/deriveExternal.ts +108 -0
  218. package/src/domain/usecase/discoverWorkspace.spec.ts +63 -0
  219. package/src/domain/usecase/discoverWorkspace.ts +183 -0
  220. package/src/domain/usecase/emitBundle.spec.ts +162 -0
  221. package/src/domain/usecase/emitBundle.ts +215 -0
  222. package/src/domain/usecase/emitDts.ts +156 -0
  223. package/src/domain/usecase/isExternal.spec.ts +53 -0
  224. package/src/domain/usecase/isExternal.ts +20 -0
  225. package/src/domain/usecase/resolveSpecifier.spec.ts +112 -0
  226. package/src/domain/usecase/resolveSpecifier.ts +127 -0
  227. package/src/domain/usecase/resolveWorkspaceSpecifier.spec.ts +79 -0
  228. package/src/domain/usecase/resolveWorkspaceSpecifier.ts +120 -0
  229. package/src/domain/usecase/rewriteDtsAliases.spec.ts +123 -0
  230. package/src/domain/usecase/rewriteDtsAliases.ts +133 -0
  231. package/src/entrypoints/cli.ts +105 -0
  232. package/src/index.ts +28 -0
  233. package/src/vendors/runner.spec.ts +50 -0
  234. package/src/vendors/runner.ts +78 -0
  235. package/src/vendors/transpiler.ts +48 -0
@@ -0,0 +1,203 @@
1
+ import {
2
+ readdirSync,
3
+ readFileSync,
4
+ statSync,
5
+ } from "node:fs";
6
+ import {
7
+ resolve,
8
+ dirname,
9
+ join,
10
+ } from "node:path";
11
+ import { parseImports } from "plgg-bundle/Dev/usecase/parseImports";
12
+ import { buildGraph } from "plgg-bundle/Dev/usecase/buildGraph";
13
+ import { type ModuleGraph } from "plgg-bundle/Dev/model/ModuleGraph";
14
+
15
+ // The effectful graph scan: walk the watch roots, read
16
+ // each source file, and resolve its LOCAL import edges
17
+ // (relative specifiers + the app's own self-alias) to
18
+ // absolute paths. Feeds the pure `buildGraph` /
19
+ // reload-decision. In `Dev/node/` (fs seam), excluded from
20
+ // the coverage threshold and exercised by the fixture PoC.
21
+
22
+ /** Source extensions whose imports are scanned. */
23
+ const SOURCE_EXT: ReadonlyArray<string> = [
24
+ ".ts",
25
+ ".tsx",
26
+ ".mts",
27
+ ".js",
28
+ ".mjs",
29
+ ];
30
+
31
+ /**
32
+ * A self-alias resolution rule: `<prefix>`/`<prefix>/<sub>`
33
+ * → `<srcDir>`/`<srcDir>/<sub>`. One per package whose
34
+ * source participates in dev (the app's own, plus any
35
+ * cross-package `sourceAliases`).
36
+ */
37
+ export type Alias = Readonly<{
38
+ prefix: string;
39
+ srcDir: string;
40
+ }>;
41
+
42
+ /**
43
+ * Scan the watch roots and build the local
44
+ * {@link ModuleGraph}, resolving relative AND aliased
45
+ * imports (every rule in `aliases`) so cross-package source
46
+ * edges are captured. Unreadable roots/files are skipped (a
47
+ * scan is best-effort — the reload decision falls back to
48
+ * reloading when the graph is empty).
49
+ */
50
+ export const scanGraph = (
51
+ roots: ReadonlyArray<string>,
52
+ aliases: ReadonlyArray<Alias>,
53
+ ): ModuleGraph =>
54
+ buildGraph(
55
+ sourceFiles(roots).flatMap((file) =>
56
+ edgesOf(file, aliases),
57
+ ),
58
+ );
59
+
60
+ /** Every source file under the roots (recursive, de-duped). */
61
+ const sourceFiles = (
62
+ roots: ReadonlyArray<string>,
63
+ ): ReadonlyArray<string> => {
64
+ const seen = new Set<string>();
65
+ for (const root of roots) {
66
+ for (const abs of walk(root)) {
67
+ seen.add(abs);
68
+ }
69
+ }
70
+ return [...seen];
71
+ };
72
+
73
+ /**
74
+ * Directories never descended into during the graph scan —
75
+ * dependency trees, build output, VCS. A watch root of `.`
76
+ * (a package dir) would otherwise pull thousands of
77
+ * `node_modules` files into the graph.
78
+ */
79
+ const SKIP_DIRS: ReadonlyArray<string> = [
80
+ "node_modules",
81
+ "dist",
82
+ ".git",
83
+ ];
84
+
85
+ /**
86
+ * Recursively list source files under a directory, pruning
87
+ * {@link SKIP_DIRS}. A manual walk (not `readdirSync`'s
88
+ * `recursive`) so a huge `node_modules` is never read.
89
+ */
90
+ const walk = (
91
+ root: string,
92
+ ): ReadonlyArray<string> => {
93
+ let entries;
94
+ try {
95
+ entries = readdirSync(root, {
96
+ withFileTypes: true,
97
+ });
98
+ } catch {
99
+ return [];
100
+ }
101
+ return entries.flatMap((entry) => {
102
+ const abs = join(root, entry.name);
103
+ if (entry.isDirectory()) {
104
+ return SKIP_DIRS.includes(entry.name)
105
+ ? []
106
+ : walk(abs);
107
+ }
108
+ return entry.isFile() &&
109
+ SOURCE_EXT.some((e) =>
110
+ entry.name.endsWith(e),
111
+ )
112
+ ? [abs]
113
+ : [];
114
+ });
115
+ };
116
+
117
+ /** The resolved local edges a single file imports. */
118
+ const edgesOf = (
119
+ file: string,
120
+ aliases: ReadonlyArray<Alias>,
121
+ ): ReadonlyArray<{ from: string; to: string }> => {
122
+ const source = read(file);
123
+ if (source === null) {
124
+ return [];
125
+ }
126
+ return parseImports(source).flatMap((spec) => {
127
+ const to = resolveLocal(file, spec, aliases);
128
+ return to === null
129
+ ? []
130
+ : [{ from: file, to }];
131
+ });
132
+ };
133
+
134
+ /** Read a file, or null when it cannot be read. */
135
+ const read = (file: string): string | null => {
136
+ try {
137
+ return readFileSync(file, "utf8");
138
+ } catch {
139
+ return null;
140
+ }
141
+ };
142
+
143
+ /**
144
+ * Resolve a specifier to an absolute local file, or null
145
+ * for a package / `node:` specifier. Handles relative
146
+ * paths and every alias rule (`prefix` / `prefix/…`); tries
147
+ * the path itself, `.ts`, and `/index.ts`.
148
+ */
149
+ const resolveLocal = (
150
+ from: string,
151
+ spec: string,
152
+ aliases: ReadonlyArray<Alias>,
153
+ ): string | null => {
154
+ const base = baseOf(from, spec, aliases);
155
+ if (base === null) {
156
+ return null;
157
+ }
158
+ const candidates = [
159
+ base,
160
+ `${base}.ts`,
161
+ join(base, "index.ts"),
162
+ ];
163
+ // A FILE, never a directory: a bare `<prefix>` base is
164
+ // the src dir (which exists) — prefer its `index.ts`.
165
+ return (
166
+ candidates.find(isFile) ?? null
167
+ );
168
+ };
169
+
170
+ /** Whether a path exists and is a regular file. */
171
+ const isFile = (path: string): boolean => {
172
+ try {
173
+ return statSync(path).isFile();
174
+ } catch {
175
+ return false;
176
+ }
177
+ };
178
+
179
+ /** The pre-extension base path a specifier points at. */
180
+ const baseOf = (
181
+ from: string,
182
+ spec: string,
183
+ aliases: ReadonlyArray<Alias>,
184
+ ): string | null => {
185
+ if (
186
+ spec.startsWith("./") ||
187
+ spec.startsWith("../")
188
+ ) {
189
+ return resolve(dirname(from), spec);
190
+ }
191
+ for (const alias of aliases) {
192
+ if (spec === alias.prefix) {
193
+ return alias.srcDir;
194
+ }
195
+ if (spec.startsWith(`${alias.prefix}/`)) {
196
+ return join(
197
+ alias.srcDir,
198
+ spec.slice(alias.prefix.length + 1),
199
+ );
200
+ }
201
+ }
202
+ return null;
203
+ };
@@ -0,0 +1,54 @@
1
+ import { watch, type FSWatcher } from "node:fs";
2
+
3
+ // The fs.watch seam: a debounced, recursive watcher over
4
+ // several source roots. One save can emit multiple events,
5
+ // so an imperative timer coalesces them onto a single
6
+ // `onChange(path)`. Kept in `Dev/node/` (effectful,
7
+ // excluded from the coverage threshold) — the pure reload
8
+ // decision it feeds is unit-tested separately.
9
+
10
+ /** fs.watch debounce window in ms. */
11
+ const DEBOUNCE_MS = 80;
12
+
13
+ /**
14
+ * Watch each root recursively; coalesce bursts onto a
15
+ * single `onChange` carrying the last changed filename.
16
+ * Returns the watchers so the caller owns their lifecycle.
17
+ * A root that cannot be watched is skipped rather than
18
+ * crashing the dev loop.
19
+ */
20
+ export const watchRoots = (
21
+ roots: ReadonlyArray<string>,
22
+ onChange: (filename: string) => void,
23
+ ): ReadonlyArray<FSWatcher> => {
24
+ // Debounce seam: one shared timer + the latest filename
25
+ // across all roots.
26
+ let pending:
27
+ | ReturnType<typeof setTimeout>
28
+ | undefined;
29
+ let latest = "";
30
+ const fire = (filename: string): void => {
31
+ latest = filename;
32
+ clearTimeout(pending);
33
+ pending = setTimeout((): void => {
34
+ onChange(latest);
35
+ }, DEBOUNCE_MS);
36
+ };
37
+ return roots.flatMap((root) => {
38
+ try {
39
+ return [
40
+ watch(
41
+ root,
42
+ { recursive: true },
43
+ (_event, filename): void => {
44
+ if (filename !== null) {
45
+ fire(filename);
46
+ }
47
+ },
48
+ ),
49
+ ];
50
+ } catch {
51
+ return [];
52
+ }
53
+ });
54
+ };
@@ -0,0 +1,50 @@
1
+ import {
2
+ test,
3
+ check,
4
+ all,
5
+ toBe,
6
+ } from "plgg-test";
7
+ import {
8
+ hostName,
9
+ isAllowedHost,
10
+ } from "plgg-bundle/Dev/usecase/allowedHost";
11
+
12
+ test("hostName strips a :port suffix", () =>
13
+ all([
14
+ check(
15
+ hostName("localhost:5181"),
16
+ toBe("localhost"),
17
+ ),
18
+ check(
19
+ hostName("plgg-guide.qmu.dev"),
20
+ toBe("plgg-guide.qmu.dev"),
21
+ ),
22
+ ]));
23
+
24
+ test("isAllowedHost accepts loopback by default", () =>
25
+ all([
26
+ check(
27
+ isAllowedHost([])("localhost:5181"),
28
+ toBe(true),
29
+ ),
30
+ check(
31
+ isAllowedHost([])("127.0.0.1:5181"),
32
+ toBe(true),
33
+ ),
34
+ ]));
35
+
36
+ test("isAllowedHost accepts a config-supplied tunnel host", () =>
37
+ check(
38
+ isAllowedHost(["plgg-guide.qmu.dev"])(
39
+ "plgg-guide.qmu.dev",
40
+ ),
41
+ toBe(true),
42
+ ));
43
+
44
+ test("isAllowedHost refuses an unlisted host", () =>
45
+ check(
46
+ isAllowedHost(["plgg-guide.qmu.dev"])(
47
+ "evil.example.com",
48
+ ),
49
+ toBe(false),
50
+ ));
@@ -0,0 +1,39 @@
1
+ /**
2
+ * Hosts the dev server always accepts, on top of the
3
+ * config-supplied allowlist. Loopback is implicit so local
4
+ * development needs no configuration.
5
+ */
6
+ const DEFAULT_HOSTS: ReadonlyArray<string> = [
7
+ "localhost",
8
+ "127.0.0.1",
9
+ ];
10
+
11
+ /**
12
+ * The bare host name (any `:port` suffix stripped) of a
13
+ * `Host`-header value. A value with no colon is returned
14
+ * unchanged. `indexOf`/`slice` (not `split(":")[0]`) so
15
+ * there is no unreachable `undefined` case to branch on.
16
+ */
17
+ export const hostName = (
18
+ host: string,
19
+ ): string => {
20
+ const colon = host.indexOf(":");
21
+ return colon === -1
22
+ ? host
23
+ : host.slice(0, colon);
24
+ };
25
+
26
+ /**
27
+ * The Host allowlist check (data-last): loopback plus the
28
+ * config-supplied `allowedHosts`. Reproduces the tunnel
29
+ * safety plggpress's dev server gave — the guide adds its
30
+ * `*.qmu.dev` tunnel host so the Cloudflare tunnel is
31
+ * accepted while arbitrary Host headers are refused.
32
+ */
33
+ export const isAllowedHost =
34
+ (allowedHosts: ReadonlyArray<string>) =>
35
+ (host: string): boolean =>
36
+ [
37
+ ...DEFAULT_HOSTS,
38
+ ...allowedHosts,
39
+ ].includes(hostName(host));
@@ -0,0 +1,30 @@
1
+ import {
2
+ test,
3
+ check,
4
+ all,
5
+ toBe,
6
+ toEqual,
7
+ } from "plgg-test";
8
+ import { buildGraph } from "plgg-bundle/Dev/usecase/buildGraph";
9
+
10
+ test("buildGraph folds edges into a file → imports map", () => {
11
+ const graph = buildGraph([
12
+ { from: "/a.ts", to: "/b.ts" },
13
+ { from: "/a.ts", to: "/c.ts" },
14
+ { from: "/b.ts", to: "/c.ts" },
15
+ ]);
16
+ return all([
17
+ check(graph.size, toBe(2)),
18
+ check(
19
+ graph.get("/a.ts"),
20
+ toEqual(["/b.ts", "/c.ts"]),
21
+ ),
22
+ check(
23
+ graph.get("/b.ts"),
24
+ toEqual(["/c.ts"]),
25
+ ),
26
+ ]);
27
+ });
28
+
29
+ test("buildGraph yields an empty graph for no edges", () =>
30
+ check(buildGraph([]).size, toBe(0)));
@@ -0,0 +1,31 @@
1
+ import {
2
+ type Edge,
3
+ type ModuleGraph,
4
+ } from "plgg-bundle/Dev/model/ModuleGraph";
5
+
6
+ /**
7
+ * Fold a flat list of resolved import {@link Edge}s into a
8
+ * {@link ModuleGraph} (file → the local files it imports).
9
+ * Pure: the adapter does the effectful scan+resolve and
10
+ * hands the edges here. A file that imports nothing local
11
+ * still appears as a node (empty array) as soon as it is
12
+ * an edge's `from`, so the reload decision can tell "known
13
+ * module" from "unrelated file".
14
+ */
15
+ export const buildGraph = (
16
+ edges: ReadonlyArray<Edge>,
17
+ ): ModuleGraph => {
18
+ const graph = new Map<
19
+ string,
20
+ Array<string>
21
+ >();
22
+ for (const { from, to } of edges) {
23
+ const existing = graph.get(from);
24
+ if (existing === undefined) {
25
+ graph.set(from, [to]);
26
+ } else {
27
+ existing.push(to);
28
+ }
29
+ }
30
+ return graph;
31
+ };
@@ -0,0 +1,39 @@
1
+ import {
2
+ test,
3
+ check,
4
+ all,
5
+ toBe,
6
+ } from "plgg-test";
7
+ import { decorateDevHtml } from "plgg-bundle/Dev/usecase/decorateDevHtml";
8
+ import { LIVE_RELOAD_SCRIPT } from "plgg-bundle/Dev/model/Protocol";
9
+
10
+ test("decorateDevHtml injects the reload script before </body>", () => {
11
+ const out = decorateDevHtml(
12
+ "<html><body><h1>hi</h1></body></html>",
13
+ );
14
+ return all([
15
+ check(
16
+ out.includes(LIVE_RELOAD_SCRIPT),
17
+ toBe(true),
18
+ ),
19
+ check(
20
+ out.indexOf(LIVE_RELOAD_SCRIPT) <
21
+ out.indexOf("</body>"),
22
+ toBe(true),
23
+ ),
24
+ ]);
25
+ });
26
+
27
+ test("decorateDevHtml appends to a document with no </body>", () => {
28
+ const out = decorateDevHtml("<h1>bare</h1>");
29
+ return all([
30
+ check(
31
+ out.startsWith("<h1>bare</h1>"),
32
+ toBe(true),
33
+ ),
34
+ check(
35
+ out.endsWith(LIVE_RELOAD_SCRIPT),
36
+ toBe(true),
37
+ ),
38
+ ]);
39
+ });
@@ -0,0 +1,21 @@
1
+ import { LIVE_RELOAD_SCRIPT } from "plgg-bundle/Dev/model/Protocol";
2
+
3
+ /**
4
+ * The DEV-ONLY HTML decoration: string-append the
5
+ * {@link LIVE_RELOAD_SCRIPT} just before `</body>` (or at
6
+ * the very end when the document has none). Pure string
7
+ * surgery on rendered HTML OUTPUT — never inside a typed
8
+ * render tree (which would escape the `<script>`), and
9
+ * never in a production build path. This is the plgg-free
10
+ * toolchain counterpart to plggpress's old
11
+ * `decorateDevHtml`.
12
+ */
13
+ export const decorateDevHtml = (
14
+ html: string,
15
+ ): string =>
16
+ html.includes("</body>")
17
+ ? html.replace(
18
+ "</body>",
19
+ `${LIVE_RELOAD_SCRIPT}</body>`,
20
+ )
21
+ : html + LIVE_RELOAD_SCRIPT;
@@ -0,0 +1,88 @@
1
+ import {
2
+ test,
3
+ check,
4
+ all,
5
+ toBe,
6
+ } from "plgg-test";
7
+ import { buildGraph } from "plgg-bundle/Dev/usecase/buildGraph";
8
+ import { transitiveImporters } from "plgg-bundle/Dev/usecase/invalidate";
9
+
10
+ /** A imports B; editing B must re-evaluate A (and B). */
11
+ test("transitiveImporters: A imports B ⇒ invalidate(B) = {A, B}", () => {
12
+ const graph = buildGraph([
13
+ { from: "/a.ts", to: "/b.ts" },
14
+ ]);
15
+ const set = transitiveImporters(
16
+ graph,
17
+ "/b.ts",
18
+ );
19
+ return all([
20
+ check(set.has("/a.ts"), toBe(true)),
21
+ check(set.has("/b.ts"), toBe(true)),
22
+ check(set.size, toBe(2)),
23
+ ]);
24
+ });
25
+
26
+ /** Deep chain A→B→C: editing C invalidates the whole chain. */
27
+ test("transitiveImporters walks a transitive chain A→B→C", () => {
28
+ const graph = buildGraph([
29
+ { from: "/a.ts", to: "/b.ts" },
30
+ { from: "/b.ts", to: "/c.ts" },
31
+ ]);
32
+ const set = transitiveImporters(
33
+ graph,
34
+ "/c.ts",
35
+ );
36
+ return all([
37
+ check(set.has("/a.ts"), toBe(true)),
38
+ check(set.has("/b.ts"), toBe(true)),
39
+ check(set.has("/c.ts"), toBe(true)),
40
+ check(set.size, toBe(3)),
41
+ ]);
42
+ });
43
+
44
+ /** A leaf's editor invalidates only itself. */
45
+ test("transitiveImporters of a root importer is just itself", () => {
46
+ const graph = buildGraph([
47
+ { from: "/a.ts", to: "/b.ts" },
48
+ ]);
49
+ const set = transitiveImporters(
50
+ graph,
51
+ "/a.ts",
52
+ );
53
+ return all([
54
+ check(set.has("/a.ts"), toBe(true)),
55
+ check(set.size, toBe(1)),
56
+ ]);
57
+ });
58
+
59
+ /** Two importers of one leaf: editing C invalidates both A and B. */
60
+ test("transitiveImporters collects multiple importers of one file", () => {
61
+ const graph = buildGraph([
62
+ { from: "/a.ts", to: "/c.ts" },
63
+ { from: "/b.ts", to: "/c.ts" },
64
+ ]);
65
+ const set = transitiveImporters(
66
+ graph,
67
+ "/c.ts",
68
+ );
69
+ return all([
70
+ check(set.has("/a.ts"), toBe(true)),
71
+ check(set.has("/b.ts"), toBe(true)),
72
+ check(set.has("/c.ts"), toBe(true)),
73
+ check(set.size, toBe(3)),
74
+ ]);
75
+ });
76
+
77
+ /** A cycle (A↔B) terminates and includes both. */
78
+ test("transitiveImporters is cycle-safe", () => {
79
+ const graph = buildGraph([
80
+ { from: "/a.ts", to: "/b.ts" },
81
+ { from: "/b.ts", to: "/a.ts" },
82
+ ]);
83
+ const set = transitiveImporters(
84
+ graph,
85
+ "/a.ts",
86
+ );
87
+ return check(set.size, toBe(2));
88
+ });
@@ -0,0 +1,67 @@
1
+ import { type ModuleGraph } from "plgg-bundle/Dev/model/ModuleGraph";
2
+
3
+ /**
4
+ * The invalidation set for a changed file: the file itself
5
+ * plus every module that transitively imports it (its
6
+ * reverse-reachable closure in the {@link ModuleGraph}).
7
+ * These are exactly the modules a cache-busted re-import
8
+ * must re-evaluate for the edit to take effect — a change
9
+ * to a deep leaf `B` invalidates every `A` that imports it
10
+ * (directly or through a chain).
11
+ *
12
+ * Pure graph walk over reversed edges; cycle-safe via the
13
+ * visited set. This is the "transitive invalidation" the
14
+ * ticket flags as the hard part, isolated here as testable
15
+ * logic (A imports B; invalidate(B) ⊇ {A, B}).
16
+ */
17
+ export const transitiveImporters = (
18
+ graph: ModuleGraph,
19
+ changed: string,
20
+ ): ReadonlySet<string> => {
21
+ const importers = reverseEdges(graph);
22
+ const seen = new Set<string>([changed]);
23
+ // Breadth-first by layers: `for...of` over each frontier
24
+ // yields a `string` directly (no indexed access, no
25
+ // `.shift()` undefined case), keeping the walk `as`-free.
26
+ let frontier: ReadonlyArray<string> = [changed];
27
+ while (frontier.length > 0) {
28
+ const next: Array<string> = [];
29
+ for (const node of frontier) {
30
+ for (const importer of importers.get(
31
+ node,
32
+ ) ?? []) {
33
+ if (!seen.has(importer)) {
34
+ seen.add(importer);
35
+ next.push(importer);
36
+ }
37
+ }
38
+ }
39
+ frontier = next;
40
+ }
41
+ return seen;
42
+ };
43
+
44
+ /**
45
+ * Invert the graph: for each `from → to` forward edge,
46
+ * record `to → from` so a lookup answers "who imports
47
+ * this file". Local helper for {@link transitiveImporters}.
48
+ */
49
+ const reverseEdges = (
50
+ graph: ModuleGraph,
51
+ ): ReadonlyMap<string, ReadonlyArray<string>> => {
52
+ const reversed = new Map<
53
+ string,
54
+ Array<string>
55
+ >();
56
+ for (const [from, tos] of graph) {
57
+ for (const to of tos) {
58
+ const existing = reversed.get(to);
59
+ if (existing === undefined) {
60
+ reversed.set(to, [from]);
61
+ } else {
62
+ existing.push(from);
63
+ }
64
+ }
65
+ }
66
+ return reversed;
67
+ };
@@ -0,0 +1,58 @@
1
+ import {
2
+ test,
3
+ check,
4
+ all,
5
+ toBe,
6
+ toEqual,
7
+ } from "plgg-test";
8
+ import { parseImports } from "plgg-bundle/Dev/usecase/parseImports";
9
+
10
+ test("parseImports reads static import + re-export + dynamic forms", () =>
11
+ check(
12
+ parseImports(
13
+ [
14
+ `import { a } from "./a.ts";`,
15
+ `import b from './b';`,
16
+ `export { c } from "./c.ts";`,
17
+ `const d = await import("./d.ts");`,
18
+ `export * from "./e";`,
19
+ ].join("\n"),
20
+ ),
21
+ toEqual([
22
+ // `from "…"` matches (import/export/re-export) come
23
+ // first, then the dynamic `import("…")` pass.
24
+ "./a.ts",
25
+ "./b",
26
+ "./c.ts",
27
+ "./e",
28
+ "./d.ts",
29
+ ]),
30
+ ));
31
+
32
+ test("parseImports returns empty for a source with no imports", () =>
33
+ check(
34
+ parseImports(
35
+ "export const x = 1;\n",
36
+ ).length,
37
+ toBe(0),
38
+ ));
39
+
40
+ test("parseImports captures bare package + node specifiers too (adapter filters locality)", () =>
41
+ check(
42
+ parseImports(
43
+ `import { x } from "node:fs";\nimport y from "some-pkg";`,
44
+ ),
45
+ toEqual(["node:fs", "some-pkg"]),
46
+ ));
47
+
48
+ test("parseImports handles single and double quotes", () =>
49
+ all([
50
+ check(
51
+ parseImports(`import("./x.ts")`),
52
+ toEqual(["./x.ts"]),
53
+ ),
54
+ check(
55
+ parseImports(`import('./y.ts')`),
56
+ toEqual(["./y.ts"]),
57
+ ),
58
+ ]));