@celljs/vite 0.0.0-stage → 4.1.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 +28 -0
  24. package/lib/expression/expression-evaluator.d.ts.map +1 -0
  25. package/lib/expression/expression-evaluator.js +91 -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 +48 -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 +99 -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,45 @@
1
+ import type { Target } from '@celljs/kit';
2
+ import { PUBLIC_DIR } from '../constants.js';
3
+
4
+ /**
5
+ * Generates the code that replaces @celljs/core's `common/config/dynamic-config.js`. Backend
6
+ * `${...}` expressions stay as strings: @celljs/core evaluates them at runtime against the config
7
+ * and `process.env`, so one build serves every environment. `CELL_BACKEND__*` environment variables
8
+ * override keys at startup, then `paths.public` (relative to the output directory) becomes absolute.
9
+ */
10
+ export class ConfigGenerator {
11
+ constructor(
12
+ protected readonly target: Target,
13
+ protected readonly format: 'esm' | 'cjs' = 'esm',
14
+ /** Absolute output directory; by default the parent of the server bundle. `cell dev` has no bundle. */
15
+ protected readonly outputDir?: string,
16
+ ) {}
17
+
18
+ generate(config: Record<string, unknown>): string {
19
+ const lines = ['// Generated by @celljs/vite. Do not edit.'];
20
+ if (this.target === 'backend') {
21
+ if (this.format === 'esm' && !this.outputDir) {
22
+ lines.push("import { fileURLToPath } from 'node:url';");
23
+ }
24
+ lines.push("import { isAbsolute, join } from 'node:path';");
25
+ // This module replaces common/config/dynamic-config.js of @celljs/core, next to env-config-util.js.
26
+ lines.push("import { EnvConfigUtil } from './env-config-util.js';");
27
+ }
28
+ lines.push(`export const config = ${JSON.stringify(config)};`);
29
+ if (this.target === 'backend') {
30
+ const outputDir = this.outputDir
31
+ ? JSON.stringify(this.outputDir)
32
+ : this.format === 'esm'
33
+ ? "fileURLToPath(new URL('..', import.meta.url))"
34
+ : "join(__dirname, '..')";
35
+ lines.push(`config.paths = { public: '${PUBLIC_DIR}', ...config.paths };`);
36
+ // CELL_BACKEND__* variables override keys before any ${...} is evaluated.
37
+ lines.push('EnvConfigUtil.applyProcessEnv(config);');
38
+ // paths.public is relative to the output directory (the parent of server/), unless absolute or an expression.
39
+ lines.push(
40
+ `const publicDir = config.paths.public; if (typeof publicDir === 'string' && !isAbsolute(publicDir) && !publicDir.includes('$' + '{')) config.paths.public = join(${outputDir}, publicDir);`,
41
+ );
42
+ }
43
+ return `${lines.join('\n')}\n`;
44
+ }
45
+ }
@@ -0,0 +1,41 @@
1
+ import type { Target } from '@celljs/kit';
2
+
3
+ /**
4
+ * Generates the code that replaces @celljs/core's `common/container/dynamic-container.js`: static
5
+ * modules load synchronously, the other modules in order. Core-internal imports are relative, so
6
+ * they hit the very same module instances as the rest of the application.
7
+ */
8
+ export class ContainerGenerator {
9
+ constructor(protected readonly target: Target) {}
10
+
11
+ generate(staticIds: string[], moduleIds: string[]): string {
12
+ const statics = staticIds.map((id, i) => `import cellStatic${i} from ${JSON.stringify(id)};`);
13
+ const loads = staticIds.map((_, i) => `instance.load(cellStatic${i});`);
14
+ const chain = moduleIds.map(id => `\n .then(() => import(${JSON.stringify(id)})).then(load)`).join('');
15
+ return [
16
+ `// Generated by @celljs/vite for the ${this.target}. Do not edit.`,
17
+ ...this.prelude(),
18
+ "import { ContainerFactory } from './container-factory.js';",
19
+ ...statics,
20
+ '',
21
+ 'const instance = ContainerFactory.create();',
22
+ ...loads,
23
+ '',
24
+ 'function load(mod) {',
25
+ ' return Promise.resolve(mod.default).then(m => instance.load(m));',
26
+ '}',
27
+ '',
28
+ `export const container = Promise.resolve()${chain}`,
29
+ // A module that fails to load fails the entry: it stops the process or reports to the ApplicationHost.
30
+ ' .then(() => instance);',
31
+ '',
32
+ ].join('\n');
33
+ }
34
+
35
+ protected prelude(): string[] {
36
+ if (this.target === 'frontend') {
37
+ return ["import 'reflect-metadata';", "import '../../browser/index.js';"];
38
+ }
39
+ return ["import 'reflect-metadata';"];
40
+ }
41
+ }
@@ -0,0 +1,43 @@
1
+ import { ENTRY_URL } from '../constants.js';
2
+ import type { ExpressionEvaluator } from '../expression/expression-evaluator.js';
3
+
4
+ /**
5
+ * Generates the frontend HTML page: the project's `index.html` or a default page, with `${...}`
6
+ * evaluated against the frontend configuration and the entry script added.
7
+ */
8
+ export class HtmlGenerator {
9
+ static readonly DEFAULT_TEMPLATE = [
10
+ '<!DOCTYPE html>',
11
+ '<html>',
12
+ ' <head>',
13
+ ' <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">',
14
+ ' <meta http-equiv="X-UA-Compatible" content="edge">',
15
+ ' <meta name="renderer" content="webkit">',
16
+ ' <meta charset="UTF-8">',
17
+ ' <title>${cell.name}</title>',
18
+ ' <style>body { margin: 0; }</style>',
19
+ ' <base href="${cell.server.path ?: \'/\'}">',
20
+ ' <link rel="icon" href="favicon.ico" vite-ignore>',
21
+ ' </head>',
22
+ ' <body>',
23
+ ' <div id="${cell.hostDomId}"></div>',
24
+ ' </body>',
25
+ '</html>',
26
+ '',
27
+ ].join('\n');
28
+
29
+ constructor(protected readonly evaluator: ExpressionEvaluator) {}
30
+
31
+ generate(config: Record<string, unknown>, template = HtmlGenerator.DEFAULT_TEMPLATE): string {
32
+ const html = String(this.evaluator.evaluateText(template, structuredClone(config), 'index.html'));
33
+ return this.withEntryScript(html);
34
+ }
35
+
36
+ protected withEntryScript(html: string): string {
37
+ if (html.includes(ENTRY_URL)) {
38
+ return html;
39
+ }
40
+ const tag = `<script type="module" src="${ENTRY_URL}"></script>`;
41
+ return html.includes('</body>') ? html.replace('</body>', ` ${tag}\n </body>`) : `${html}\n${tag}\n`;
42
+ }
43
+ }
package/src/index.ts ADDED
@@ -0,0 +1,20 @@
1
+ export * from './builder/cell-builder.js';
2
+ export * from './constants.js';
3
+ export * from './dev/cell-dev-server.js';
4
+ export * from './expression/expression-evaluator.js';
5
+ export * from './generator/config-generator.js';
6
+ export * from './generator/container-generator.js';
7
+ export * from './generator/html-generator.js';
8
+ export * from './output/asset-embedder.js';
9
+ export * from './output/externals-copier.js';
10
+ export * from './output/output-writer.js';
11
+ export { CellPlugin, type CellPluginOptions, cell } from './plugin/cell-plugin.js';
12
+ export * from './plugin/node-polyfills.js';
13
+ export * from './project/component-collector.js';
14
+ export * from './project/config-loader.js';
15
+ export * from './project/env-overrides.js';
16
+ export * from './project/module-resolver.js';
17
+ export * from './project/project-loader.js';
18
+ export * from './testing/test-app.js';
19
+ export * from './utils/config-util.js';
20
+ export * from './utils/tsconfig-util.js';
@@ -0,0 +1,42 @@
1
+ import { createHash } from 'node:crypto';
2
+ import { readdirSync, readFileSync, statSync } from 'node:fs';
3
+ import { join, sep } from 'node:path';
4
+ import { EMBEDDED_ASSETS_KEY } from '../constants.js';
5
+
6
+ /** A frontend file inside a `--single` server bundle. */
7
+ export interface EmbeddedAsset {
8
+ /** Base64 of the content. */
9
+ body: string;
10
+ /** Strong ETag of the content. */
11
+ etag: string;
12
+ }
13
+
14
+ /**
15
+ * Turns the public directory into a module of the server bundle (`--single`, docs/design/architecture.md
16
+ * L2): it registers every file by URL path under `Symbol.for('@celljs/serve-static/assets')`, where
17
+ * @celljs/serve-static serves them from memory instead of the disk.
18
+ */
19
+ export class AssetEmbedder {
20
+ constructor(protected readonly dir: string) {}
21
+
22
+ files(): Map<string, EmbeddedAsset> {
23
+ const assets = new Map<string, EmbeddedAsset>();
24
+ for (const name of (readdirSync(this.dir, { recursive: true }) as string[]).sort()) {
25
+ const file = join(this.dir, name);
26
+ if (!statSync(file).isFile()) {
27
+ continue;
28
+ }
29
+ const content = readFileSync(file);
30
+ assets.set(`/${name.split(sep).join('/')}`, {
31
+ body: content.toString('base64'),
32
+ etag: `"${createHash('sha1').update(content).digest('hex').slice(0, 16)}"`,
33
+ });
34
+ }
35
+ return assets;
36
+ }
37
+
38
+ generate(): string {
39
+ const entries = JSON.stringify([...this.files()]);
40
+ return `globalThis[Symbol.for(${JSON.stringify(EMBEDDED_ASSETS_KEY)})] = new Map(${entries});\n`;
41
+ }
42
+ }
@@ -0,0 +1,64 @@
1
+ import { cpSync, existsSync, mkdirSync, readFileSync, realpathSync } from 'node:fs';
2
+ import { dirname, join, sep } from 'node:path';
3
+
4
+ /**
5
+ * Copies installed external packages, with their production dependencies, into
6
+ * `<outDir>/node_modules` so the server bundle runs from its own directory. Packages that are not
7
+ * installed are skipped: optional dependencies may legitimately be absent at runtime.
8
+ */
9
+ export class ExternalsCopier {
10
+ protected readonly found = new Map<string, string>();
11
+
12
+ constructor(
13
+ protected readonly root: string,
14
+ protected readonly outDir: string,
15
+ ) {}
16
+
17
+ /** Returns the names of the copied packages. */
18
+ copy(names: string[]): string[] {
19
+ for (const name of names) {
20
+ this.visit(name, this.root);
21
+ }
22
+ for (const [name, dir] of this.found) {
23
+ const target = join(this.outDir, 'node_modules', name);
24
+ if (existsSync(target)) {
25
+ continue;
26
+ }
27
+ mkdirSync(dirname(target), { recursive: true });
28
+ cpSync(dir, target, { recursive: true, dereference: true, filter: src => !src.slice(dir.length).split(sep).includes('node_modules') });
29
+ }
30
+ return [...this.found.keys()];
31
+ }
32
+
33
+ protected visit(name: string, from: string): void {
34
+ if (this.found.has(name)) {
35
+ return;
36
+ }
37
+ const manifest = this.findManifest(name, from);
38
+ if (!manifest) {
39
+ return;
40
+ }
41
+ const dir = realpathSync(dirname(manifest));
42
+ this.found.set(name, dir);
43
+ const pkg = JSON.parse(readFileSync(manifest, 'utf8')) as { dependencies?: Record<string, string>; optionalDependencies?: Record<string, string> };
44
+ for (const dep of Object.keys({ ...pkg.dependencies, ...pkg.optionalDependencies })) {
45
+ this.visit(dep, dir);
46
+ }
47
+ }
48
+
49
+ /**
50
+ * The package.json of `name` as Node finds the package from `from`. Looked up on disk rather than
51
+ * with `require.resolve('<name>/package.json')`, which fails for packages whose `exports` leave it out.
52
+ */
53
+ protected findManifest(name: string, from: string): string | undefined {
54
+ for (let dir = from; ; dir = dirname(dir)) {
55
+ const manifest = join(dir, 'node_modules', name, 'package.json');
56
+ if (existsSync(manifest)) {
57
+ return manifest;
58
+ }
59
+ if (dirname(dir) === dir) {
60
+ return undefined;
61
+ }
62
+ }
63
+ }
64
+ }
@@ -0,0 +1,113 @@
1
+ import { cpSync, existsSync, mkdirSync, rmSync, statSync, writeFileSync } from 'node:fs';
2
+ import { dirname, join } from 'node:path';
3
+ import { fileURLToPath } from 'node:url';
4
+ import type { OutputManifest, ProjectModel } from '@celljs/kit';
5
+ import { ExternalsCopier } from './externals-copier.js';
6
+
7
+ /** Kept for importers of `@celljs/vite`; the type lives in `@celljs/kit`. */
8
+ export type { OutputManifest };
9
+
10
+ /**
11
+ * Writes everything around the bundles: generated code under `.cell/generated` (so agents and humans
12
+ * can read what was built), frontend static files, server externals and `manifest.json`.
13
+ */
14
+ export class OutputWriter {
15
+ static readonly ASSET_DIRS = ['assets', 'src/assets', 'src/browser/assets'];
16
+ static readonly DEFAULT_FAVICON = fileURLToPath(new URL('../../assets/favicon.ico', import.meta.url));
17
+
18
+ constructor(
19
+ protected readonly model: ProjectModel,
20
+ protected readonly outDir: string,
21
+ protected readonly format: 'esm' | 'cjs',
22
+ protected readonly preset?: string,
23
+ ) {}
24
+
25
+ get publicDir(): string {
26
+ return join(this.outDir, 'public');
27
+ }
28
+
29
+ get serverDir(): string {
30
+ return join(this.outDir, 'server');
31
+ }
32
+
33
+ /**
34
+ * Before a build: a previous Cell output (it has manifest.json) is removed entirely, so files a
35
+ * preset wrote last time (a start script, a platform configuration) do not linger; in another
36
+ * directory only the server bundle is replaced.
37
+ */
38
+ clean(): void {
39
+ if (existsSync(join(this.outDir, 'manifest.json'))) {
40
+ rmSync(this.outDir, { recursive: true, force: true });
41
+ } else if (this.model.targets.includes('backend')) {
42
+ rmSync(this.serverDir, { recursive: true, force: true });
43
+ }
44
+ }
45
+
46
+ get serverEntry(): string {
47
+ return this.format === 'esm' ? 'index.mjs' : 'index.cjs';
48
+ }
49
+
50
+ get generatedDir(): string {
51
+ return join(this.model.root, '.cell', 'generated');
52
+ }
53
+
54
+ writeGenerated(relative: string, content: string): void {
55
+ this.write(join(this.generatedDir, relative), content);
56
+ }
57
+
58
+ /**
59
+ * The file that `copyFrontendFiles` puts at `path` of the public directory (`/favicon.ico`,
60
+ * `/assets/...`), for `cell dev`, which serves them from where they are. The first component
61
+ * that has the file wins, as in the copy.
62
+ */
63
+ frontendFile(path: string): string | undefined {
64
+ if (path === '/favicon.ico') {
65
+ return [join(this.model.root, 'favicon.ico'), OutputWriter.DEFAULT_FAVICON].find(existsSync);
66
+ }
67
+ const asset = /^\/assets\/(.+)$/.exec(path)?.[1];
68
+ if (!asset || asset.split('/').includes('..')) {
69
+ return undefined;
70
+ }
71
+ return this.model.components
72
+ .flatMap(component => OutputWriter.ASSET_DIRS.map(dir => join(component.dir, dir, asset)))
73
+ .find(file => existsSync(file) && statSync(file).isFile());
74
+ }
75
+
76
+ copyFrontendFiles(): void {
77
+ const favicon = [join(this.model.root, 'favicon.ico'), OutputWriter.DEFAULT_FAVICON].find(existsSync);
78
+ if (favicon) {
79
+ cpSync(favicon, join(this.publicDir, 'favicon.ico'));
80
+ }
81
+ for (const component of this.model.components) {
82
+ for (const dir of OutputWriter.ASSET_DIRS.map(d => join(component.dir, d)).filter(existsSync)) {
83
+ cpSync(dir, join(this.publicDir, 'assets'), { recursive: true, force: false });
84
+ }
85
+ }
86
+ }
87
+
88
+ copyExternals(names: string[]): string[] {
89
+ return new ExternalsCopier(this.model.root, this.serverDir).copy(names);
90
+ }
91
+
92
+ /** `single`: the frontend is inside the server bundle. */
93
+ writeManifest(single = false): OutputManifest {
94
+ const hasServer = this.model.targets.includes('backend');
95
+ const hasPublic = this.model.targets.includes('frontend') && !single;
96
+ const manifest: OutputManifest = {
97
+ schemaVersion: 1,
98
+ preset: this.preset,
99
+ format: this.format,
100
+ targets: this.model.targets,
101
+ server: hasServer ? `server/${this.serverEntry}` : undefined,
102
+ public: hasPublic ? 'public' : undefined,
103
+ ...(single ? { single: true as const } : {}),
104
+ };
105
+ this.write(join(this.outDir, 'manifest.json'), `${JSON.stringify(manifest, null, 2)}\n`);
106
+ return manifest;
107
+ }
108
+
109
+ protected write(file: string, content: string): void {
110
+ mkdirSync(dirname(file), { recursive: true });
111
+ writeFileSync(file, content);
112
+ }
113
+ }