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,127 @@
1
+ import { existsSync, statSync } from "node:fs";
2
+ import {
3
+ dirname,
4
+ resolve,
5
+ join,
6
+ } from "node:path";
7
+
8
+ /**
9
+ * Resolve a bare import specifier to an absolute source
10
+ * file path, given the importing file and the
11
+ * package-self-alias mapping. Pure resolution logic —
12
+ * it only consults the filesystem to pick between
13
+ * `X.ts` and `X/index.ts`, mirroring how the alias +
14
+ * relative imports across the plgg packages resolve
15
+ * under `tsc`.
16
+ *
17
+ * Returns `undefined` when the specifier is external
18
+ * (not the self-alias and not relative) or cannot be
19
+ * found on disk — the caller decides whether that means
20
+ * "external, leave it" or "broken import, fail".
21
+ */
22
+ export const resolveSpecifier = (args: {
23
+ specifier: string;
24
+ fromFile: string;
25
+ aliasPrefix: string;
26
+ aliasSrcRoot: string;
27
+ }): string | undefined => {
28
+ if (isRelative(args.specifier)) {
29
+ return pickExisting(
30
+ resolve(
31
+ dirname(args.fromFile),
32
+ args.specifier,
33
+ ),
34
+ );
35
+ }
36
+ const base = aliasTarget(args);
37
+ return base === undefined
38
+ ? undefined
39
+ : pickExisting(base);
40
+ };
41
+
42
+ /**
43
+ * Whether the specifier is a relative path (`./` or
44
+ * `../`). Exported for the app-mode workspace resolver,
45
+ * which shares this relative-import handling.
46
+ */
47
+ export const isRelative = (
48
+ specifier: string,
49
+ ): boolean =>
50
+ specifier.startsWith("./") ||
51
+ specifier.startsWith("../");
52
+
53
+ /**
54
+ * Map a self-alias specifier (`<prefix>` or
55
+ * `<prefix>/sub/path`) to its base path under the
56
+ * source root. A non-alias specifier yields `undefined`
57
+ * (it is external).
58
+ */
59
+ const aliasTarget = (args: {
60
+ specifier: string;
61
+ aliasPrefix: string;
62
+ aliasSrcRoot: string;
63
+ }): string | undefined =>
64
+ args.specifier === args.aliasPrefix
65
+ ? args.aliasSrcRoot
66
+ : args.specifier.startsWith(
67
+ `${args.aliasPrefix}/`,
68
+ )
69
+ ? join(
70
+ args.aliasSrcRoot,
71
+ args.specifier.slice(
72
+ args.aliasPrefix.length + 1,
73
+ ),
74
+ )
75
+ : undefined;
76
+
77
+ /**
78
+ * Given a base path with no extension, pick the
79
+ * existing source file: the path as-is, then `base.ts`,
80
+ * then `base/index.ts`. Exported so the app-mode
81
+ * workspace resolver picks sibling source files the same
82
+ * way.
83
+ */
84
+ export const pickExisting = (
85
+ base: string,
86
+ ): string | undefined =>
87
+ candidates(base).find(isFile);
88
+
89
+ /**
90
+ * Whether a path exists and is a regular file (not a
91
+ * directory). A bare `plgg/Abstracts` base resolves to
92
+ * the `Abstracts/` directory, which must be skipped in
93
+ * favor of `Abstracts/index.ts`.
94
+ */
95
+ const isFile = (p: string): boolean =>
96
+ existsSync(p) && statSync(p).isFile();
97
+
98
+ /**
99
+ * The ordered candidate file paths for a base path. A
100
+ * `.js`/`.mjs`/`.jsx` specifier is the NodeNext/ESM way
101
+ * to reference a TypeScript sibling (`./X.js` → `X.ts`),
102
+ * so its `.ts`/`.mts`/`.tsx` form is tried first.
103
+ */
104
+ const candidates = (
105
+ base: string,
106
+ ): ReadonlyArray<string> => [
107
+ ...tsForJs(base),
108
+ base,
109
+ `${base}.ts`,
110
+ join(base, "index.ts"),
111
+ ];
112
+
113
+ /**
114
+ * The TypeScript-source path implied by a JS-extension
115
+ * specifier, or nothing if `base` carries no such
116
+ * extension.
117
+ */
118
+ const tsForJs = (
119
+ base: string,
120
+ ): ReadonlyArray<string> =>
121
+ base.endsWith(".js")
122
+ ? [`${base.slice(0, -3)}.ts`]
123
+ : base.endsWith(".mjs")
124
+ ? [`${base.slice(0, -4)}.mts`]
125
+ : base.endsWith(".jsx")
126
+ ? [`${base.slice(0, -4)}.tsx`]
127
+ : [];
@@ -0,0 +1,79 @@
1
+ import {
2
+ test,
3
+ check,
4
+ all,
5
+ toBe,
6
+ } from "plgg-test";
7
+ import { join } from "node:path";
8
+ import { discoverWorkspace } from "plgg-bundle/domain/usecase/discoverWorkspace";
9
+ import { resolveWorkspaceSpecifier } from "plgg-bundle/domain/usecase/resolveWorkspaceSpecifier";
10
+
11
+ const bundleRoot = join(
12
+ import.meta.dirname,
13
+ "..",
14
+ "..",
15
+ "..",
16
+ );
17
+ const packagesDir = join(bundleRoot, "..");
18
+ const packages = discoverWorkspace(bundleRoot);
19
+
20
+ // Any file standing in for the importer.
21
+ const fromFile = join(
22
+ packagesDir,
23
+ "example",
24
+ "src",
25
+ "app.ts",
26
+ );
27
+ const resolve = (specifier: string) =>
28
+ resolveWorkspaceSpecifier({
29
+ specifier,
30
+ fromFile,
31
+ packages,
32
+ });
33
+ const pkgSrc = (
34
+ name: string,
35
+ ...rest: ReadonlyArray<string>
36
+ ) => join(packagesDir, name, "src", ...rest);
37
+
38
+ test("resolves a bare workspace package to its src index", () =>
39
+ check(
40
+ resolve("plgg"),
41
+ toBe(pkgSrc("plgg", "index.ts")),
42
+ ));
43
+
44
+ test("resolves a public export subpath, honouring the styleEntry rename", () =>
45
+ all([
46
+ check(
47
+ resolve("plgg-view/client"),
48
+ toBe(pkgSrc("plgg-view", "client.ts")),
49
+ ),
50
+ check(
51
+ resolve("plgg-view/style"),
52
+ toBe(pkgSrc("plgg-view", "styleEntry.ts")),
53
+ ),
54
+ ]));
55
+
56
+ test("resolves an internal self-alias path (not a declared export)", () =>
57
+ check(
58
+ resolve("plgg/Atomics/Num"),
59
+ toBe(pkgSrc("plgg", "Atomics", "Num.ts")),
60
+ ));
61
+
62
+ test("resolves a relative import against the importer", () =>
63
+ check(
64
+ resolve("./Todo.ts"),
65
+ toBe(
66
+ join(
67
+ packagesDir,
68
+ "example",
69
+ "src",
70
+ "Todo.ts",
71
+ ),
72
+ ),
73
+ ));
74
+
75
+ test("returns undefined for a non-workspace specifier", () =>
76
+ all([
77
+ check(resolve("react"), toBe(undefined)),
78
+ check(resolve("node:fs"), toBe(undefined)),
79
+ ]));
@@ -0,0 +1,120 @@
1
+ import { basename, join } from "node:path";
2
+ import {
3
+ resolveSpecifier,
4
+ isRelative,
5
+ pickExisting,
6
+ } from "plgg-bundle/domain/usecase/resolveSpecifier";
7
+ import { type WorkspacePackage } from "plgg-bundle/domain/usecase/discoverWorkspace";
8
+
9
+ /**
10
+ * App-mode resolver: resolve an import specifier to an
11
+ * absolute SOURCE file, inlining workspace siblings
12
+ * (`plgg`, `plgg-view/client`, …) by walking their `src`
13
+ * instead of leaving them external. This is the mirror of
14
+ * the library externalization — the leaf app is where
15
+ * bundling deps is correct.
16
+ *
17
+ * Order:
18
+ * 1. relative (`./x`) → resolve against the importer,
19
+ * 2. a workspace package's PUBLIC export subpath
20
+ * (`plgg-view/style`) → reverse its declared
21
+ * `exports` dist path to the entry source (honours the
22
+ * `styleEntry` rename),
23
+ * 3. a workspace package's INTERNAL self-alias path
24
+ * (`plgg-view/Html/x`, and `plgg` → its `src/index`)
25
+ * → `src/<path>`.
26
+ *
27
+ * Returns `undefined` when the specifier is neither
28
+ * relative nor a known workspace package (the caller
29
+ * treats that as external or a hard error).
30
+ */
31
+ export const resolveWorkspaceSpecifier = (args: {
32
+ specifier: string;
33
+ fromFile: string;
34
+ packages: ReadonlyArray<WorkspacePackage>;
35
+ }): string | undefined => {
36
+ if (isRelative(args.specifier)) {
37
+ return resolveSpecifier({
38
+ specifier: args.specifier,
39
+ fromFile: args.fromFile,
40
+ aliasPrefix: NO_ALIAS,
41
+ aliasSrcRoot: "",
42
+ });
43
+ }
44
+ const pkg = matchPackage(
45
+ args.packages,
46
+ args.specifier,
47
+ );
48
+ return pkg === undefined
49
+ ? undefined
50
+ : resolveInPackage(pkg, args.specifier);
51
+ };
52
+
53
+ /**
54
+ * A sentinel alias prefix no specifier equals, so the
55
+ * shared `resolveSpecifier` handles only the relative
56
+ * branch when reused for relative imports.
57
+ */
58
+ const NO_ALIAS = "\0";
59
+
60
+ /**
61
+ * The workspace package whose name is the specifier or a
62
+ * leading `name/` segment. The package list is sorted
63
+ * longest-name-first, so the first match is the most
64
+ * specific (`plgg-view` before `plgg`).
65
+ */
66
+ const matchPackage = (
67
+ packages: ReadonlyArray<WorkspacePackage>,
68
+ specifier: string,
69
+ ): WorkspacePackage | undefined =>
70
+ packages.find(
71
+ (p) =>
72
+ specifier === p.name ||
73
+ specifier.startsWith(`${p.name}/`),
74
+ );
75
+
76
+ /**
77
+ * Resolve a specifier already known to belong to `pkg`:
78
+ * a declared export subpath via the exports reversal,
79
+ * else the internal self-alias path.
80
+ */
81
+ const resolveInPackage = (
82
+ pkg: WorkspacePackage,
83
+ specifier: string,
84
+ ): string | undefined => {
85
+ const subpath =
86
+ specifier === pkg.name
87
+ ? "."
88
+ : `./${specifier.slice(
89
+ pkg.name.length + 1,
90
+ )}`;
91
+ const dist = pkg.exports.get(subpath);
92
+ return dist === undefined
93
+ ? resolveSpecifier({
94
+ specifier,
95
+ fromFile: pkg.dir,
96
+ aliasPrefix: pkg.name,
97
+ aliasSrcRoot: join(pkg.dir, "src"),
98
+ })
99
+ : pickExisting(
100
+ join(pkg.dir, "src", srcStem(dist)),
101
+ );
102
+ };
103
+
104
+ /**
105
+ * The source stem implied by an export's dist default —
106
+ * `"./dist/styleEntry.es.js"` → `"styleEntry"`. The entry
107
+ * output basename equals the entry name, which equals the
108
+ * source file stem under `src/`, so stripping the dual
109
+ * `.es.js`/`.cjs.js` (or bare `.js`) suffix recovers it.
110
+ *
111
+ * ASSUMES top-level entry stems: `basename` drops any
112
+ * directory, so an export pointing at a NESTED dist path
113
+ * (`./dist/sub/foo.es.js`) would reverse to `src/foo` and
114
+ * lose `sub/`. No package nests an export entry today, so
115
+ * this is correct now; revisit if one ever does.
116
+ */
117
+ const srcStem = (distDefault: string): string =>
118
+ basename(distDefault)
119
+ .replace(/\.(es|cjs)\.js$/, "")
120
+ .replace(/\.js$/, "");
@@ -0,0 +1,123 @@
1
+ import {
2
+ test,
3
+ check,
4
+ all,
5
+ toBe,
6
+ } from "plgg-test";
7
+ import { join } from "node:path";
8
+ import {
9
+ mkdtempSync,
10
+ mkdirSync,
11
+ writeFileSync,
12
+ readFileSync,
13
+ } from "node:fs";
14
+ import { tmpdir } from "node:os";
15
+ import {
16
+ rewriteDtsAliases,
17
+ rewriteDtsContent,
18
+ } from "plgg-bundle/domain/usecase/rewriteDtsAliases";
19
+
20
+ const distDir = "/pkg/dist";
21
+ // A declaration two levels deep, so a rewrite to a
22
+ // top-level sibling is a `../../` path.
23
+ const file = join(
24
+ distDir,
25
+ "Disjunctives",
26
+ "Result.d.ts",
27
+ );
28
+
29
+ test("rewriteDtsContent relativizes an alias in `from` position", () =>
30
+ check(
31
+ rewriteDtsContent(
32
+ file,
33
+ `import { Ok } from "plgg/index";`,
34
+ distDir,
35
+ "plgg",
36
+ ),
37
+ toBe(`import { Ok } from "../index";`),
38
+ ));
39
+
40
+ test("rewriteDtsContent relativizes a re-export and an inline import()", () =>
41
+ all([
42
+ check(
43
+ rewriteDtsContent(
44
+ join(distDir, "index.d.ts"),
45
+ `export * from "plgg/Atomics";`,
46
+ distDir,
47
+ "plgg",
48
+ ),
49
+ toBe(`export * from "./Atomics";`),
50
+ ),
51
+ check(
52
+ rewriteDtsContent(
53
+ join(distDir, "index.d.ts"),
54
+ `export type T = import("plgg/Atomics/Num").Num;`,
55
+ distDir,
56
+ "plgg",
57
+ ),
58
+ toBe(
59
+ `export type T = import("./Atomics/Num").Num;`,
60
+ ),
61
+ ),
62
+ ]));
63
+
64
+ test("rewriteDtsContent leaves a literal-type string untouched (gap #7)", () =>
65
+ // A string-literal type that merely looks like an
66
+ // alias must NOT be rewritten — it is not an
67
+ // import/export specifier position.
68
+ check(
69
+ rewriteDtsContent(
70
+ join(distDir, "index.d.ts"),
71
+ `export type Tag = "plgg/not-a-module";`,
72
+ distDir,
73
+ "plgg",
74
+ ),
75
+ toBe(
76
+ `export type Tag = "plgg/not-a-module";`,
77
+ ),
78
+ ));
79
+
80
+ test("rewriteDtsAliases rewrites aliases across a nested dist tree in place", () => {
81
+ const dist = mkdtempSync(
82
+ join(tmpdir(), "plgg-dts-"),
83
+ );
84
+ mkdirSync(join(dist, "Sub"));
85
+ writeFileSync(
86
+ join(dist, "index.d.ts"),
87
+ `export * from "plgg/Sub/x";`,
88
+ );
89
+ writeFileSync(
90
+ join(dist, "Sub", "x.d.ts"),
91
+ `import { Y } from "plgg/index";`,
92
+ );
93
+ // A non-.d.ts sibling must be skipped by the walk.
94
+ writeFileSync(
95
+ join(dist, "notes.txt"),
96
+ `plgg/index (not a declaration)`,
97
+ );
98
+ rewriteDtsAliases(dist, "plgg");
99
+ return all([
100
+ check(
101
+ readFileSync(
102
+ join(dist, "index.d.ts"),
103
+ "utf8",
104
+ ),
105
+ toBe(`export * from "./Sub/x";`),
106
+ ),
107
+ check(
108
+ readFileSync(
109
+ join(dist, "Sub", "x.d.ts"),
110
+ "utf8",
111
+ ),
112
+ toBe(`import { Y } from "../index";`),
113
+ ),
114
+ // Untouched: not a declaration file.
115
+ check(
116
+ readFileSync(
117
+ join(dist, "notes.txt"),
118
+ "utf8",
119
+ ),
120
+ toBe(`plgg/index (not a declaration)`),
121
+ ),
122
+ ]);
123
+ });
@@ -0,0 +1,133 @@
1
+ import {
2
+ readdirSync,
3
+ statSync,
4
+ readFileSync,
5
+ writeFileSync,
6
+ } from "node:fs";
7
+ import {
8
+ join,
9
+ dirname,
10
+ relative,
11
+ } from "node:path";
12
+
13
+ /**
14
+ * Rewrite the package self-alias (`<prefix>/<sub>`) in
15
+ * every emitted `.d.ts` under `distDir` to a path
16
+ * relative to the importing declaration file — the same
17
+ * transform vite-plugin-dts applied. `tsc` leaves
18
+ * `tsconfig` `paths` aliases verbatim in declaration
19
+ * output, so without this pass a consumer (who has no
20
+ * `plgg/*` alias) cannot resolve `from "plgg/Atomics"`.
21
+ *
22
+ * Because `dist` mirrors `src` and the alias maps
23
+ * `<prefix>/*` → `src/*`, `<prefix>/<sub>` from
24
+ * `dist/A/B.d.ts` resolves to `dist/<sub>`, i.e. the
25
+ * relative path from `dist/A` to `dist/<sub>`.
26
+ */
27
+ export const rewriteDtsAliases = (
28
+ distDir: string,
29
+ aliasPrefix: string,
30
+ ): void => {
31
+ // fs walk seam: visit every emitted declaration and
32
+ // relativize its alias imports in place.
33
+ for (const file of dtsFiles(distDir)) {
34
+ const rewritten = rewriteDtsContent(
35
+ file,
36
+ readFileSync(file, "utf8"),
37
+ distDir,
38
+ aliasPrefix,
39
+ );
40
+ writeFileSync(file, rewritten, "utf8");
41
+ }
42
+ };
43
+
44
+ /**
45
+ * Rewrite a single declaration file's alias specifiers
46
+ * (pure string transform, exposed for unit testing). The
47
+ * match is ANCHORED to import/export specifier positions
48
+ * — preceded by `from ` or `import(` — so a literal
49
+ * string-type value like `type T = "plgg/x"` in a
50
+ * `.d.ts` is left untouched (gap #7).
51
+ */
52
+ export const rewriteDtsContent = (
53
+ file: string,
54
+ content: string,
55
+ distDir: string,
56
+ aliasPrefix: string,
57
+ ): string =>
58
+ content.replace(
59
+ aliasPattern(aliasPrefix),
60
+ (
61
+ _match,
62
+ lead: string,
63
+ quote: string,
64
+ sub: string,
65
+ ) =>
66
+ `${lead}${quote}${toRelative(
67
+ file,
68
+ distDir,
69
+ sub,
70
+ )}${quote}`,
71
+ );
72
+
73
+ /**
74
+ * A specifier-matching regex for `<prefix>/<sub>` only
75
+ * in an import/export specifier position: immediately
76
+ * after `from ` or `import(`. Captures the lead, the
77
+ * quote, and the sub-path. The prefix is escaped so a
78
+ * regex-special character in a package name is literal.
79
+ */
80
+ const aliasPattern = (
81
+ aliasPrefix: string,
82
+ ): RegExp =>
83
+ new RegExp(
84
+ `(from\\s+|import\\s*\\()(["'])${escapeRegExp(
85
+ aliasPrefix,
86
+ )}\\/([^"']+)\\2`,
87
+ "g",
88
+ );
89
+
90
+ /**
91
+ * The relative specifier from a declaration file to the
92
+ * aliased sub-module's location in `dist`, POSIX-style
93
+ * and `./`-prefixed when in the same/!-parent dir.
94
+ */
95
+ const toRelative = (
96
+ file: string,
97
+ distDir: string,
98
+ sub: string,
99
+ ): string =>
100
+ dotted(
101
+ relative(dirname(file), join(distDir, sub))
102
+ .split("\\")
103
+ .join("/"),
104
+ );
105
+
106
+ /**
107
+ * Ensure a relative path is import-specifier shaped:
108
+ * a same-or-child path gets a leading `./`; a `../`
109
+ * path already is.
110
+ */
111
+ const dotted = (rel: string): string =>
112
+ rel.startsWith(".") ? rel : `./${rel}`;
113
+
114
+ /**
115
+ * Escape regex metacharacters in a literal string.
116
+ */
117
+ const escapeRegExp = (s: string): string =>
118
+ s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
119
+
120
+ /**
121
+ * All `.d.ts` files under a directory, recursively.
122
+ */
123
+ const dtsFiles = (
124
+ dir: string,
125
+ ): ReadonlyArray<string> =>
126
+ readdirSync(dir).flatMap((name) => {
127
+ const p = join(dir, name);
128
+ return statSync(p).isDirectory()
129
+ ? dtsFiles(p)
130
+ : p.endsWith(".d.ts")
131
+ ? [p]
132
+ : [];
133
+ });
@@ -0,0 +1,105 @@
1
+ import { resolve } from "node:path";
2
+ import { pathToFileURL } from "node:url";
3
+ import { existsSync } from "node:fs";
4
+ import { type BundleConfig } from "plgg-bundle/domain/model/BundleConfig";
5
+ import { asBundleConfig } from "plgg-bundle/domain/usecase/asBundleConfig";
6
+ import { build } from "plgg-bundle/domain/usecase/build";
7
+ import { runDevServer } from "plgg-bundle/Dev/node/devServer";
8
+
9
+ /**
10
+ * CLI entry with two modes:
11
+ *
12
+ * - `plgg-bundle [config]` — the one-shot build (default):
13
+ * validate the config and emit the bundle.
14
+ * - `plgg-bundle dev [config]` — the dev server: serve the
15
+ * config's dev entry over node:http, watch its source,
16
+ * and hot-reload the browser on a code edit.
17
+ *
18
+ * The shell stays thin and is the single place a thrown
19
+ * error becomes a non-zero exit — the plain-TS counterpart
20
+ * to a `Result` edge. The `dev` server itself runs for the
21
+ * process lifetime (it never resolves).
22
+ */
23
+ const main = async (): Promise<void> =>
24
+ process.argv[2] === "dev"
25
+ ? runDev(process.argv[3])
26
+ : runBuild(process.argv[2]);
27
+
28
+ /** The one-shot build mode. */
29
+ const runBuild = async (
30
+ arg: string | undefined,
31
+ ): Promise<void> => {
32
+ const config = await loadConfig(arg);
33
+ if (config === null) {
34
+ return;
35
+ }
36
+ const files = build(config);
37
+ process.stdout.write(
38
+ `plgg-bundle: wrote ${files.length} file(s) to ${config.outDir}\n`,
39
+ );
40
+ };
41
+
42
+ /** The dev-server mode. */
43
+ const runDev = async (
44
+ arg: string | undefined,
45
+ ): Promise<void> => {
46
+ const config = await loadConfig(arg);
47
+ if (config === null) {
48
+ return;
49
+ }
50
+ const server = await runDevServer(config);
51
+ process.stdout.write(
52
+ `plgg-bundle: dev server at ${server.url}\n`,
53
+ );
54
+ };
55
+
56
+ /**
57
+ * Resolve, import (Node strips its types), and validate
58
+ * the bundle config, or set a non-zero exit and return
59
+ * null when it is missing. Shared by both modes.
60
+ */
61
+ const loadConfig = async (
62
+ arg: string | undefined,
63
+ ): Promise<BundleConfig | null> => {
64
+ const configPath = resolve(
65
+ process.cwd(),
66
+ arg ?? "bundle.config.ts",
67
+ );
68
+ if (!existsSync(configPath)) {
69
+ fail(`config not found: ${configPath}`);
70
+ return null;
71
+ }
72
+ const mod: unknown = await import(
73
+ pathToFileURL(configPath).href
74
+ );
75
+ return asBundleConfig(pickDefault(mod));
76
+ };
77
+
78
+ /**
79
+ * Extract a module's `default` export as `unknown`,
80
+ * tolerating a module that is itself the config.
81
+ */
82
+ const pickDefault = (mod: unknown): unknown =>
83
+ typeof mod === "object" &&
84
+ mod !== null &&
85
+ "default" in mod
86
+ ? mod.default
87
+ : mod;
88
+
89
+ /**
90
+ * Print an error and set a non-zero exit code.
91
+ */
92
+ const fail = (message: string): void => {
93
+ process.stderr.write(
94
+ `plgg-bundle: ${message}\n`,
95
+ );
96
+ process.exitCode = 1;
97
+ };
98
+
99
+ try {
100
+ await main();
101
+ } catch (e) {
102
+ fail(
103
+ e instanceof Error ? e.message : String(e),
104
+ );
105
+ }