@celljs/vite 0.0.0-stage → 4.0.0

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 (166) hide show
  1. package/README.md +29 -2
  2. package/assets/favicon.ico +0 -0
  3. package/lib/builder/cell-builder.d.ts +19 -0
  4. package/lib/builder/cell-builder.d.ts.map +1 -0
  5. package/lib/builder/cell-builder.js +59 -0
  6. package/lib/builder/cell-builder.js.map +1 -0
  7. package/lib/builder/cell-builder.spec.d.ts +2 -0
  8. package/lib/builder/cell-builder.spec.d.ts.map +1 -0
  9. package/lib/builder/cell-builder.spec.js +280 -0
  10. package/lib/builder/cell-builder.spec.js.map +1 -0
  11. package/lib/constants.d.ts +24 -0
  12. package/lib/constants.d.ts.map +1 -0
  13. package/lib/constants.js +23 -0
  14. package/lib/constants.js.map +1 -0
  15. package/lib/dev/cell-dev-server.d.ts +113 -0
  16. package/lib/dev/cell-dev-server.d.ts.map +1 -0
  17. package/lib/dev/cell-dev-server.js +307 -0
  18. package/lib/dev/cell-dev-server.js.map +1 -0
  19. package/lib/dev/cell-dev-server.spec.d.ts +2 -0
  20. package/lib/dev/cell-dev-server.spec.d.ts.map +1 -0
  21. package/lib/dev/cell-dev-server.spec.js +188 -0
  22. package/lib/dev/cell-dev-server.spec.js.map +1 -0
  23. package/lib/expression/expression-evaluator.d.ts +24 -0
  24. package/lib/expression/expression-evaluator.d.ts.map +1 -0
  25. package/lib/expression/expression-evaluator.js +97 -0
  26. package/lib/expression/expression-evaluator.js.map +1 -0
  27. package/lib/expression/expression-evaluator.spec.d.ts +2 -0
  28. package/lib/expression/expression-evaluator.spec.d.ts.map +1 -0
  29. package/lib/expression/expression-evaluator.spec.js +23 -0
  30. package/lib/expression/expression-evaluator.spec.js.map +1 -0
  31. package/lib/generator/config-generator.d.ts +18 -0
  32. package/lib/generator/config-generator.d.ts.map +1 -0
  33. package/lib/generator/config-generator.js +45 -0
  34. package/lib/generator/config-generator.js.map +1 -0
  35. package/lib/generator/container-generator.d.ts +13 -0
  36. package/lib/generator/container-generator.d.ts.map +1 -0
  37. package/lib/generator/container-generator.js +41 -0
  38. package/lib/generator/container-generator.js.map +1 -0
  39. package/lib/generator/generator.spec.d.ts +2 -0
  40. package/lib/generator/generator.spec.d.ts.map +1 -0
  41. package/lib/generator/generator.spec.js +61 -0
  42. package/lib/generator/generator.spec.js.map +1 -0
  43. package/lib/generator/html-generator.d.ts +13 -0
  44. package/lib/generator/html-generator.d.ts.map +1 -0
  45. package/lib/generator/html-generator.js +42 -0
  46. package/lib/generator/html-generator.js.map +1 -0
  47. package/lib/index.d.ts +21 -0
  48. package/lib/index.d.ts.map +1 -0
  49. package/lib/index.js +21 -0
  50. package/lib/index.js.map +1 -0
  51. package/lib/output/asset-embedder.d.ts +19 -0
  52. package/lib/output/asset-embedder.d.ts.map +1 -0
  53. package/lib/output/asset-embedder.js +35 -0
  54. package/lib/output/asset-embedder.js.map +1 -0
  55. package/lib/output/externals-copier.d.ts +20 -0
  56. package/lib/output/externals-copier.d.ts.map +1 -0
  57. package/lib/output/externals-copier.js +62 -0
  58. package/lib/output/externals-copier.js.map +1 -0
  59. package/lib/output/externals-copier.spec.d.ts +2 -0
  60. package/lib/output/externals-copier.spec.d.ts.map +1 -0
  61. package/lib/output/externals-copier.spec.js +30 -0
  62. package/lib/output/externals-copier.spec.js.map +1 -0
  63. package/lib/output/output-writer.d.ts +39 -0
  64. package/lib/output/output-writer.d.ts.map +1 -0
  65. package/lib/output/output-writer.js +102 -0
  66. package/lib/output/output-writer.js.map +1 -0
  67. package/lib/output/output-writer.spec.d.ts +2 -0
  68. package/lib/output/output-writer.spec.d.ts.map +1 -0
  69. package/lib/output/output-writer.spec.js +34 -0
  70. package/lib/output/output-writer.spec.js.map +1 -0
  71. package/lib/plugin/cell-plugin.d.ts +134 -0
  72. package/lib/plugin/cell-plugin.d.ts.map +1 -0
  73. package/lib/plugin/cell-plugin.js +429 -0
  74. package/lib/plugin/cell-plugin.js.map +1 -0
  75. package/lib/plugin/cell-plugin.spec.d.ts +2 -0
  76. package/lib/plugin/cell-plugin.spec.d.ts.map +1 -0
  77. package/lib/plugin/cell-plugin.spec.js +74 -0
  78. package/lib/plugin/cell-plugin.spec.js.map +1 -0
  79. package/lib/plugin/node-polyfills.d.ts +13 -0
  80. package/lib/plugin/node-polyfills.d.ts.map +1 -0
  81. package/lib/plugin/node-polyfills.js +55 -0
  82. package/lib/plugin/node-polyfills.js.map +1 -0
  83. package/lib/plugin/node-polyfills.spec.d.ts +2 -0
  84. package/lib/plugin/node-polyfills.spec.d.ts.map +1 -0
  85. package/lib/plugin/node-polyfills.spec.js +16 -0
  86. package/lib/plugin/node-polyfills.spec.js.map +1 -0
  87. package/lib/project/component-collector.d.ts +32 -0
  88. package/lib/project/component-collector.d.ts.map +1 -0
  89. package/lib/project/component-collector.js +93 -0
  90. package/lib/project/component-collector.js.map +1 -0
  91. package/lib/project/config-loader.d.ts +16 -0
  92. package/lib/project/config-loader.d.ts.map +1 -0
  93. package/lib/project/config-loader.js +76 -0
  94. package/lib/project/config-loader.js.map +1 -0
  95. package/lib/project/env-overrides.d.ts +23 -0
  96. package/lib/project/env-overrides.d.ts.map +1 -0
  97. package/lib/project/env-overrides.js +48 -0
  98. package/lib/project/env-overrides.js.map +1 -0
  99. package/lib/project/env-overrides.spec.d.ts +2 -0
  100. package/lib/project/env-overrides.spec.d.ts.map +1 -0
  101. package/lib/project/env-overrides.spec.js +41 -0
  102. package/lib/project/env-overrides.spec.js.map +1 -0
  103. package/lib/project/module-resolver.d.ts +15 -0
  104. package/lib/project/module-resolver.d.ts.map +1 -0
  105. package/lib/project/module-resolver.js +32 -0
  106. package/lib/project/module-resolver.js.map +1 -0
  107. package/lib/project/project-loader.d.ts +54 -0
  108. package/lib/project/project-loader.d.ts.map +1 -0
  109. package/lib/project/project-loader.js +117 -0
  110. package/lib/project/project-loader.js.map +1 -0
  111. package/lib/project/project-loader.spec.d.ts +2 -0
  112. package/lib/project/project-loader.spec.d.ts.map +1 -0
  113. package/lib/project/project-loader.spec.js +178 -0
  114. package/lib/project/project-loader.spec.js.map +1 -0
  115. package/lib/test/fixture-project.d.ts +17 -0
  116. package/lib/test/fixture-project.d.ts.map +1 -0
  117. package/lib/test/fixture-project.js +43 -0
  118. package/lib/test/fixture-project.js.map +1 -0
  119. package/lib/testing/memory-http.d.ts +82 -0
  120. package/lib/testing/memory-http.d.ts.map +1 -0
  121. package/lib/testing/memory-http.js +210 -0
  122. package/lib/testing/memory-http.js.map +1 -0
  123. package/lib/testing/test-app.d.ts +54 -0
  124. package/lib/testing/test-app.d.ts.map +1 -0
  125. package/lib/testing/test-app.js +132 -0
  126. package/lib/testing/test-app.js.map +1 -0
  127. package/lib/testing/test-app.spec.d.ts +2 -0
  128. package/lib/testing/test-app.spec.d.ts.map +1 -0
  129. package/lib/testing/test-app.spec.js +115 -0
  130. package/lib/testing/test-app.spec.js.map +1 -0
  131. package/lib/utils/config-util.d.ts +6 -0
  132. package/lib/utils/config-util.d.ts.map +1 -0
  133. package/lib/utils/config-util.js +25 -0
  134. package/lib/utils/config-util.js.map +1 -0
  135. package/lib/utils/config-util.spec.d.ts +2 -0
  136. package/lib/utils/config-util.spec.d.ts.map +1 -0
  137. package/lib/utils/config-util.spec.js +22 -0
  138. package/lib/utils/config-util.spec.js.map +1 -0
  139. package/lib/utils/tsconfig-util.d.ts +5 -0
  140. package/lib/utils/tsconfig-util.d.ts.map +1 -0
  141. package/lib/utils/tsconfig-util.js +21 -0
  142. package/lib/utils/tsconfig-util.js.map +1 -0
  143. package/package.json +50 -4
  144. package/src/builder/cell-builder.ts +72 -0
  145. package/src/constants.ts +30 -0
  146. package/src/dev/cell-dev-server.ts +387 -0
  147. package/src/expression/expression-evaluator.ts +106 -0
  148. package/src/generator/config-generator.ts +45 -0
  149. package/src/generator/container-generator.ts +41 -0
  150. package/src/generator/html-generator.ts +43 -0
  151. package/src/index.ts +20 -0
  152. package/src/output/asset-embedder.ts +42 -0
  153. package/src/output/externals-copier.ts +64 -0
  154. package/src/output/output-writer.ts +113 -0
  155. package/src/plugin/cell-plugin.ts +481 -0
  156. package/src/plugin/node-polyfills.ts +57 -0
  157. package/src/project/component-collector.ts +107 -0
  158. package/src/project/config-loader.ts +77 -0
  159. package/src/project/env-overrides.ts +55 -0
  160. package/src/project/module-resolver.ts +39 -0
  161. package/src/project/project-loader.ts +153 -0
  162. package/src/test/fixture-project.ts +50 -0
  163. package/src/testing/memory-http.ts +238 -0
  164. package/src/testing/test-app.ts +155 -0
  165. package/src/utils/config-util.ts +21 -0
  166. package/src/utils/tsconfig-util.ts +19 -0
@@ -0,0 +1,155 @@
1
+ import { CellError, type ProjectModel } from '@celljs/kit';
2
+ import { CellDevServer, type CellDevServerOptions } from '../dev/cell-dev-server.js';
3
+ import { MemoryRequest, MemoryResponse } from './memory-http.js';
4
+
5
+ export interface TestAppOptions extends Omit<CellDevServerOptions, 'listen' | 'port' | 'host' | 'onRestart' | 'onReload'> {
6
+ /** Active modes. Default `['test']` (reads cell.test.yml). */
7
+ mode?: string[];
8
+ }
9
+
10
+ /** What a test needs from a DI container: `get` by identifier (an inversify container). */
11
+ export interface TestContainer {
12
+ get<T>(id: unknown): T;
13
+ getAll<T>(id: unknown): T[];
14
+ isBound(id: unknown): boolean;
15
+ }
16
+
17
+ /**
18
+ * The whole application in the test process (docs/design/agent-first.md R11): the DI container and
19
+ * the HTTP chain of the backend, loaded from source like `cell dev` does, without a port. Requests go
20
+ * through in-memory request and response objects; `fetch` returns a standard `Response`.
21
+ *
22
+ * ```ts
23
+ * const app = await createTestApp();
24
+ * expect(await (await app.fetch('/users')).json()).toEqual([...]);
25
+ * await app.close();
26
+ * ```
27
+ */
28
+ export class TestApp {
29
+ protected server?: CellDevServer;
30
+
31
+ constructor(protected readonly options: TestAppOptions = {}) {}
32
+
33
+ get project(): ProjectModel {
34
+ return this.started.project;
35
+ }
36
+
37
+ /** The DI container of the backend. */
38
+ get container(): TestContainer {
39
+ const container = this.started.application?.container as TestContainer | undefined;
40
+ if (!container) {
41
+ throw new CellError({
42
+ code: 'CELL_TEST_APP_NOT_STARTED',
43
+ message: 'The test application has no running backend.',
44
+ hint: 'Await createTestApp() before using it.',
45
+ });
46
+ }
47
+ return container;
48
+ }
49
+
50
+ /**
51
+ * A service of the container. The application runs in its own module instances (Vite's module
52
+ * runner), so a class or `Symbol('X')` the test imports from the project's sources is not the one
53
+ * the container knows: such identifiers are matched by class name or symbol description. For the exact
54
+ * identifiers, take them from {@link import}.
55
+ */
56
+ get<T>(id: unknown): T {
57
+ return this.container.get<T>(this.identifier(id));
58
+ }
59
+
60
+ protected identifier(id: unknown): unknown {
61
+ const container = this.container as TestContainer & { _bindingDictionary?: { getMap(): Map<unknown, unknown> } };
62
+ if (container.isBound(id) || (typeof id !== 'symbol' && typeof id !== 'function')) {
63
+ return id;
64
+ }
65
+ const name = TestApp.nameOf(id);
66
+ const bound = [...(container._bindingDictionary?.getMap().keys() ?? [])];
67
+ const same = bound.filter(other => typeof other === typeof id && TestApp.nameOf(other) === name);
68
+ if (same.length === 1) {
69
+ return same[0];
70
+ }
71
+ throw new CellError({
72
+ code: same.length === 0 ? 'CELL_DI_NO_BINDING' : 'CELL_DI_AMBIGUOUS',
73
+ message:
74
+ same.length === 0
75
+ ? `The application has no binding for ${name}.`
76
+ : `${same.length} bindings of the application are named ${name}; app.get() cannot tell which one the test means.`,
77
+ hint:
78
+ same.length === 0
79
+ ? 'Check that the file with the class is imported by a DI module (src/node/module.ts) and has @Component() or @Rpc().'
80
+ : 'Use Symbol.for() identifiers or distinct class names.',
81
+ });
82
+ }
83
+
84
+ protected static nameOf(id: unknown): string {
85
+ return typeof id === 'symbol' ? (id.description ?? '') : typeof id === 'function' ? id.name : String(id);
86
+ }
87
+
88
+ /**
89
+ * Imports a module the way the backend loaded it (`/src/user-service.ts` from the project root, or a
90
+ * package such as `@celljs/core`), so its classes and symbols identify the backend's components:
91
+ * `app.get((await app.import('/src/user-service.ts')).UserService)`.
92
+ */
93
+ import<T = Record<string, any>>(specifier: string): Promise<T> {
94
+ return this.started.import<T>(specifier);
95
+ }
96
+
97
+ async start(): Promise<this> {
98
+ const server = new CellDevServer({ logLevel: 'silent', ...this.options, mode: this.options.mode ?? ['test'], listen: false });
99
+ await server.start();
100
+ if (server.error) {
101
+ await server.close();
102
+ throw server.error;
103
+ }
104
+ this.server = server;
105
+ return this;
106
+ }
107
+
108
+ /** Sends a request through the HTTP chain in memory; `input` is a path such as `/users?page=2`. */
109
+ async fetch(input: string, init: RequestInit = {}): Promise<Response> {
110
+ const handler = this.started.handler;
111
+ if (!this.started.application?.handler) {
112
+ throw new CellError({
113
+ code: 'CELL_TEST_APP_NO_HTTP',
114
+ message: `The backend entry ${this.project.backend?.entry} has no HTTP handler.`,
115
+ hint: 'Depend on @celljs/express-adapter for an HTTP backend, or test the services through app.get(...).',
116
+ });
117
+ }
118
+ const headers = new Headers(init.headers);
119
+ const body = init.body === undefined || init.body === null ? undefined : Buffer.from(await new Response(init.body).arrayBuffer());
120
+ if (body && !headers.has('content-length')) {
121
+ headers.set('content-length', String(body.length));
122
+ }
123
+ if (!headers.has('host')) {
124
+ headers.set('host', 'localhost');
125
+ }
126
+ const req = new MemoryRequest((init.method ?? 'GET').toUpperCase(), input, headers, body);
127
+ const res = new MemoryResponse(req);
128
+ return new Promise<Response>((resolve, reject) => {
129
+ res.on('finish', () => resolve(res.toResponse()));
130
+ res.on('error', reject);
131
+ handler(req as never, res as never);
132
+ });
133
+ }
134
+
135
+ async close(): Promise<void> {
136
+ await this.server?.close();
137
+ this.server = undefined;
138
+ }
139
+
140
+ protected get started(): CellDevServer {
141
+ if (!this.server) {
142
+ throw new CellError({
143
+ code: 'CELL_TEST_APP_NOT_STARTED',
144
+ message: 'The test application is not started.',
145
+ hint: 'Await createTestApp() (or start()) first.',
146
+ });
147
+ }
148
+ return this.server;
149
+ }
150
+ }
151
+
152
+ /** Starts the backend of the project in the test process; see {@link TestApp}. */
153
+ export function createTestApp(options: TestAppOptions = {}): Promise<TestApp> {
154
+ return new TestApp(options).start();
155
+ }
@@ -0,0 +1,21 @@
1
+ export namespace ConfigUtil {
2
+ export function isPlainObject(value: unknown): value is Record<string, any> {
3
+ return typeof value === 'object' && value !== null && !Array.isArray(value);
4
+ }
5
+
6
+ /** Deep merge into `target`: nested objects merge, arrays and scalars of `source` replace. */
7
+ export function merge(target: Record<string, any>, source: Record<string, any>): Record<string, any> {
8
+ for (const [key, value] of Object.entries(source)) {
9
+ if (value === undefined) {
10
+ continue;
11
+ }
12
+ const current = target[key];
13
+ if (isPlainObject(value) && isPlainObject(current)) {
14
+ merge(current, value);
15
+ } else {
16
+ target[key] = isPlainObject(value) ? merge({}, value) : value;
17
+ }
18
+ }
19
+ return target;
20
+ }
21
+ }
@@ -0,0 +1,19 @@
1
+ import { existsSync, readFileSync } from 'node:fs';
2
+ import { dirname, join, resolve } from 'node:path';
3
+
4
+ export namespace TsconfigUtil {
5
+ /** JSX runtime implied by the project's tsconfig (following relative `extends`). */
6
+ export function jsxRuntime(root: string): 'classic' | 'automatic' {
7
+ let file: string | undefined = join(root, 'tsconfig.json');
8
+ for (let depth = 0; file && existsSync(file) && depth < 8; depth++) {
9
+ const text: string = readFileSync(file, 'utf8');
10
+ const jsx = /"jsx"\s*:\s*"([^"]+)"/.exec(text)?.[1];
11
+ if (jsx) {
12
+ return jsx === 'react' ? 'classic' : 'automatic';
13
+ }
14
+ const parent: string | undefined = /"extends"\s*:\s*"([^"]+)"/.exec(text)?.[1];
15
+ file = parent?.startsWith('.') ? resolve(dirname(file), parent.endsWith('.json') ? parent : `${parent}.json`) : undefined;
16
+ }
17
+ return 'automatic';
18
+ }
19
+ }