@celljs/vite 4.3.0 → 4.4.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 (105) hide show
  1. package/README.md +2 -1
  2. package/lib/builder/cell-builder.spec.js +27 -21
  3. package/lib/builder/cell-builder.spec.js.map +1 -1
  4. package/lib/builder/pages.spec.d.ts +2 -0
  5. package/lib/builder/pages.spec.d.ts.map +1 -0
  6. package/lib/builder/pages.spec.js +96 -0
  7. package/lib/builder/pages.spec.js.map +1 -0
  8. package/lib/constants.d.ts +0 -2
  9. package/lib/constants.d.ts.map +1 -1
  10. package/lib/constants.js +0 -2
  11. package/lib/constants.js.map +1 -1
  12. package/lib/dev/cell-dev-server.d.ts +4 -2
  13. package/lib/dev/cell-dev-server.d.ts.map +1 -1
  14. package/lib/dev/cell-dev-server.js +32 -2
  15. package/lib/dev/cell-dev-server.js.map +1 -1
  16. package/lib/dev/cell-dev-server.spec.js +1 -1
  17. package/lib/dev/cell-dev-server.spec.js.map +1 -1
  18. package/lib/expression/expression-evaluator.d.ts +11 -2
  19. package/lib/expression/expression-evaluator.d.ts.map +1 -1
  20. package/lib/expression/expression-evaluator.js +6 -2
  21. package/lib/expression/expression-evaluator.js.map +1 -1
  22. package/lib/expression/expression-evaluator.spec.js +3 -3
  23. package/lib/expression/expression-evaluator.spec.js.map +1 -1
  24. package/lib/generator/config-generator.d.ts +4 -3
  25. package/lib/generator/config-generator.d.ts.map +1 -1
  26. package/lib/generator/config-generator.js +7 -5
  27. package/lib/generator/config-generator.js.map +1 -1
  28. package/lib/generator/generator.spec.js +20 -5
  29. package/lib/generator/generator.spec.js.map +1 -1
  30. package/lib/generator/html-generator.js +3 -3
  31. package/lib/generator/html-generator.js.map +1 -1
  32. package/lib/generator/route-generator.d.ts +13 -0
  33. package/lib/generator/route-generator.d.ts.map +1 -0
  34. package/lib/generator/route-generator.js +34 -0
  35. package/lib/generator/route-generator.js.map +1 -0
  36. package/lib/index.d.ts +1 -0
  37. package/lib/index.d.ts.map +1 -1
  38. package/lib/index.js +1 -0
  39. package/lib/index.js.map +1 -1
  40. package/lib/output/output-writer.d.ts +4 -0
  41. package/lib/output/output-writer.d.ts.map +1 -1
  42. package/lib/output/output-writer.js +10 -0
  43. package/lib/output/output-writer.js.map +1 -1
  44. package/lib/plugin/cell-plugin.d.ts +11 -1
  45. package/lib/plugin/cell-plugin.d.ts.map +1 -1
  46. package/lib/plugin/cell-plugin.js +49 -3
  47. package/lib/plugin/cell-plugin.js.map +1 -1
  48. package/lib/plugin/class-name-util.d.ts +14 -0
  49. package/lib/plugin/class-name-util.d.ts.map +1 -0
  50. package/lib/plugin/class-name-util.js +82 -0
  51. package/lib/plugin/class-name-util.js.map +1 -0
  52. package/lib/plugin/class-name-util.spec.d.ts +2 -0
  53. package/lib/plugin/class-name-util.spec.d.ts.map +1 -0
  54. package/lib/plugin/class-name-util.spec.js +37 -0
  55. package/lib/plugin/class-name-util.spec.js.map +1 -0
  56. package/lib/project/config-loader.d.ts +6 -6
  57. package/lib/project/config-loader.d.ts.map +1 -1
  58. package/lib/project/config-loader.js +16 -22
  59. package/lib/project/config-loader.js.map +1 -1
  60. package/lib/project/config-schema.d.ts +107 -0
  61. package/lib/project/config-schema.d.ts.map +1 -0
  62. package/lib/project/config-schema.js +522 -0
  63. package/lib/project/config-schema.js.map +1 -0
  64. package/lib/project/config-schema.spec.d.ts +2 -0
  65. package/lib/project/config-schema.spec.d.ts.map +1 -0
  66. package/lib/project/config-schema.spec.js +248 -0
  67. package/lib/project/config-schema.spec.js.map +1 -0
  68. package/lib/project/env-overrides.d.ts +14 -10
  69. package/lib/project/env-overrides.d.ts.map +1 -1
  70. package/lib/project/env-overrides.js +28 -19
  71. package/lib/project/env-overrides.js.map +1 -1
  72. package/lib/project/env-overrides.spec.js +23 -13
  73. package/lib/project/env-overrides.spec.js.map +1 -1
  74. package/lib/project/page-scanner.d.ts +26 -0
  75. package/lib/project/page-scanner.d.ts.map +1 -0
  76. package/lib/project/page-scanner.js +102 -0
  77. package/lib/project/page-scanner.js.map +1 -0
  78. package/lib/project/page-scanner.spec.d.ts +2 -0
  79. package/lib/project/page-scanner.spec.d.ts.map +1 -0
  80. package/lib/project/page-scanner.spec.js +68 -0
  81. package/lib/project/page-scanner.spec.js.map +1 -0
  82. package/lib/project/project-loader.d.ts +32 -3
  83. package/lib/project/project-loader.d.ts.map +1 -1
  84. package/lib/project/project-loader.js +70 -15
  85. package/lib/project/project-loader.js.map +1 -1
  86. package/lib/project/project-loader.spec.js +78 -56
  87. package/lib/project/project-loader.spec.js.map +1 -1
  88. package/lib/testing/test-app.spec.js +3 -3
  89. package/lib/testing/test-app.spec.js.map +1 -1
  90. package/package.json +7 -7
  91. package/src/constants.ts +0 -3
  92. package/src/dev/cell-dev-server.ts +39 -5
  93. package/src/expression/expression-evaluator.ts +16 -3
  94. package/src/generator/config-generator.ts +7 -5
  95. package/src/generator/html-generator.ts +3 -3
  96. package/src/generator/route-generator.ts +34 -0
  97. package/src/index.ts +1 -0
  98. package/src/output/output-writer.ts +12 -0
  99. package/src/plugin/cell-plugin.ts +52 -3
  100. package/src/plugin/class-name-util.ts +85 -0
  101. package/src/project/config-loader.ts +18 -23
  102. package/src/project/config-schema.ts +596 -0
  103. package/src/project/env-overrides.ts +33 -22
  104. package/src/project/page-scanner.ts +105 -0
  105. package/src/project/project-loader.ts +82 -17
@@ -0,0 +1,105 @@
1
+ import { existsSync, readdirSync } from 'node:fs';
2
+ import { isAbsolute, join, relative } from 'node:path';
3
+ import { CellError, type PageRoute } from '@celljs/kit';
4
+
5
+ /**
6
+ * Turns the files of `src/browser/pages` into routes, as `docs/guide/frontend.md` describes:
7
+ *
8
+ * - `index.tsx` is the path of its directory, `about.tsx` is `/about`;
9
+ * - `[id].tsx` is a parameter (`:id`), `[...rest].tsx` matches the rest of the path (`*`);
10
+ * - `_layout.tsx` wraps the pages of its directory and below; other names starting with `_` (files
11
+ * or directories) and tests are not pages, so components can live next to the pages that use them.
12
+ */
13
+ export class PageScanner {
14
+ static readonly DIR = join('src', 'browser', 'pages');
15
+ static readonly PAGE = /\.(tsx|jsx|ts|js)$/;
16
+ static readonly LAYOUT = /^_layout\.(tsx|jsx|ts|js)$/;
17
+ static readonly IGNORED = /\.(spec|test)\.[^.]+$|\.d\.ts$/;
18
+
19
+ constructor(protected readonly root: string) {}
20
+
21
+ get dir(): string {
22
+ return join(this.root, PageScanner.DIR);
23
+ }
24
+
25
+ /** The routes, or none when the project has no pages directory. */
26
+ scan(): PageRoute[] {
27
+ if (!existsSync(this.dir)) {
28
+ return [];
29
+ }
30
+ const routes: PageRoute[] = [];
31
+ this.scanDir(this.dir, [], [], routes);
32
+ const byPath = new Map<string, PageRoute>();
33
+ for (const route of routes) {
34
+ const other = byPath.get(route.path);
35
+ if (other) {
36
+ throw new CellError({
37
+ code: 'CELL_ROUTE_CONFLICT',
38
+ message: `${other.file} and ${route.file} are both the page of ${route.path}.`,
39
+ file: join(this.root, route.file),
40
+ hint: 'Keep one of them: about.tsx and about/index.tsx are the same path.',
41
+ });
42
+ }
43
+ byPath.set(route.path, route);
44
+ }
45
+ return routes;
46
+ }
47
+
48
+ /** Whether a file is one the routes depend on: a page or layout file, or a directory, under the pages directory. */
49
+ affects(file: string): boolean {
50
+ const rel = relative(this.dir, file);
51
+ return rel === '' || (!rel.startsWith('..') && !isAbsolute(rel));
52
+ }
53
+
54
+ protected scanDir(dir: string, segments: string[], layouts: string[], routes: PageRoute[]): void {
55
+ const entries = readdirSync(dir, { withFileTypes: true }).sort((a, b) => a.name.localeCompare(b.name));
56
+ const layout = entries.find(e => e.isFile() && PageScanner.LAYOUT.test(e.name));
57
+ const around = layout ? [...layouts, this.relative(join(dir, layout.name))] : layouts;
58
+ for (const entry of entries) {
59
+ if (entry.name.startsWith('_') || entry.name.startsWith('.')) {
60
+ continue;
61
+ }
62
+ const file = join(dir, entry.name);
63
+ if (entry.isDirectory()) {
64
+ const segment = this.segment(entry.name, file);
65
+ if (segment === '*') {
66
+ throw new CellError({
67
+ code: 'CELL_ROUTE_INVALID',
68
+ message: `${this.relative(file)}: [...name] matches the rest of the path, so it can only be a file.`,
69
+ file,
70
+ hint: 'Rename the directory, or make it a file: [...rest].tsx.',
71
+ });
72
+ }
73
+ this.scanDir(file, [...segments, segment], around, routes);
74
+ } else if (entry.isFile() && PageScanner.PAGE.test(entry.name) && !PageScanner.IGNORED.test(entry.name)) {
75
+ const name = entry.name.replace(PageScanner.PAGE, '');
76
+ const path = name === 'index' ? segments : [...segments, this.segment(name, file)];
77
+ routes.push({ path: `/${path.join('/')}`, file: this.relative(file), layouts: around });
78
+ }
79
+ }
80
+ }
81
+
82
+ protected segment(name: string, file: string): string {
83
+ const catchAll = /^\[\.\.\.([A-Za-z_$][\w$]*)\]$/.exec(name);
84
+ if (catchAll) {
85
+ return '*';
86
+ }
87
+ const param = /^\[([A-Za-z_$][\w$]*)\]$/.exec(name);
88
+ if (param) {
89
+ return `:${param[1]}`;
90
+ }
91
+ if (/[[\]:*?]/.test(name)) {
92
+ throw new CellError({
93
+ code: 'CELL_ROUTE_INVALID',
94
+ message: `${this.relative(file)}: "${name}" is not a path segment.`,
95
+ file,
96
+ hint: 'Use a plain name (about), a parameter ([id]) or the rest of the path ([...rest]).',
97
+ });
98
+ }
99
+ return name;
100
+ }
101
+
102
+ protected relative(file: string): string {
103
+ return relative(this.root, file).split('\\').join('/');
104
+ }
105
+ }
@@ -6,8 +6,10 @@ import { ExpressionEvaluator } from '../expression/expression-evaluator.js';
6
6
  import type { Component } from './component-collector.js';
7
7
  import { ComponentCollector } from './component-collector.js';
8
8
  import { ConfigLoader } from './config-loader.js';
9
+ import { type ConfigProblem, ConfigSchema } from './config-schema.js';
9
10
  import { EnvOverrides } from './env-overrides.js';
10
11
  import { ModuleResolver } from './module-resolver.js';
12
+ import { PageScanner } from './page-scanner.js';
11
13
 
12
14
  export interface ProjectLoaderOptions {
13
15
  root?: string;
@@ -25,7 +27,7 @@ export interface ProjectLoaderOptions {
25
27
  runtimeDisabled?: string[];
26
28
  /** DI modules added by toolchain modules, loaded after the components' own (`cell.addRuntimeModule`). */
27
29
  extraModules?: ExtraModule[];
28
- /** Environment for `${env.*}` in frontend configuration and `CELL_FRONTEND__` / `CELL_BACKEND__` overrides. Default: `process.env`. */
30
+ /** Environment for `${env.*}` in frontend configuration and `CELL_` overrides. Default: `process.env`. */
29
31
  env?: Record<string, string | undefined>;
30
32
  }
31
33
 
@@ -44,6 +46,7 @@ export interface ExtraModule {
44
46
  * is cheap enough for `cell info` and editor tooling (docs/design/agent-first.md R2).
45
47
  */
46
48
  export class ProjectLoader {
49
+ protected loadedSchema?: ConfigSchema;
47
50
  protected readonly root: string;
48
51
  protected readonly mode: string[];
49
52
  protected readonly env: Record<string, string | undefined>;
@@ -63,14 +66,41 @@ export class ProjectLoader {
63
66
  this.env = options.env ?? process.env;
64
67
  }
65
68
 
66
- load(): ProjectModel {
69
+ /**
70
+ * Reads the configuration files and the schemas and checks the files against the merged schema,
71
+ * without building the model: `cell check` reports each problem on its own.
72
+ */
73
+ validate(): { schema: ConfigSchema; problems: ConfigProblem[] } {
74
+ const { schema, problems } = this.read();
75
+ return { schema, problems };
76
+ }
77
+
78
+ protected read() {
67
79
  const collector = new ComponentCollector(this.root, new Set(this.options.disabled));
68
80
  const components = collector.collect();
69
81
  this.checkFilters(collector.excluded, components);
82
+ const configLoader = new ConfigLoader(this.mode);
83
+ const layers = configLoader.read(components);
84
+ const schema = ConfigSchema.load(components, layers);
85
+ this.loadedSchema = schema;
86
+ const problems = layers.flatMap(layer => schema.validate(layer.doc, layer.file, layer.text));
87
+ return { components, configLoader, layers, schema, problems };
88
+ }
89
+
90
+ /** The merged configuration schema; available after {@link load} or {@link validate}. */
91
+ get schema(): ConfigSchema {
92
+ if (!this.loadedSchema) {
93
+ throw new Error('ProjectLoader.schema is read before load() or validate().');
94
+ }
95
+ return this.loadedSchema;
96
+ }
97
+
98
+ load(): ProjectModel {
99
+ const { components, configLoader, layers, schema, problems } = this.read();
100
+ ProjectLoader.throwProblems(problems);
70
101
  const runtimeDisabled = new Set(this.options.runtimeDisabled);
71
102
  const runtime = components.filter(c => !runtimeDisabled.has(c.name));
72
- const configLoader = new ConfigLoader(this.mode);
73
- const merged = configLoader.load(components);
103
+ const merged = configLoader.merge(layers);
74
104
  const targets = this.targets(components);
75
105
  const model: ProjectModel = {
76
106
  schemaVersion: 1,
@@ -81,26 +111,66 @@ export class ProjectLoader {
81
111
  };
82
112
  const moduleResolver = new ModuleResolver();
83
113
  const overrides = new EnvOverrides(this.root, this.env);
114
+ // Where the server finds the frontend; `${paths.public}` in cell.yml (serve-static) refers to it.
115
+ merged.paths = { public: PUBLIC_DIR, ...(merged.paths as object) };
116
+ const types = schema.types(merged);
84
117
  for (const target of targets) {
85
- const config = configLoader.forTarget(merged, target);
86
- if (target === 'backend') {
87
- // Where the server finds the frontend; `${paths.public}` in cell.yml (serve-static) refers to it.
88
- config.paths = { public: PUBLIC_DIR, ...(config.paths as object) };
89
- }
90
- const envOverrides = target === 'frontend' ? overrides.apply(target, config) : overrides.preview(target, config);
118
+ const config =
119
+ target === 'frontend'
120
+ ? this.frontendConfig(merged, schema, overrides, types)
121
+ : { config: structuredClone(merged), envOverrides: overrides.preview(merged, types) };
91
122
  const targetModel: TargetModel = {
92
123
  target,
93
124
  ...this.entry(target, runtime),
94
125
  staticModules: [...runtime.flatMap(c => moduleResolver.resolve(c, target, 'staticModules')), ...this.extraModules(target, true)],
95
126
  modules: [...runtime.flatMap(c => moduleResolver.resolve(c, target, 'modules')), ...this.extraModules(target, false)],
96
- config: this.evaluate(target, config),
97
- envOverrides,
127
+ config: config.config,
128
+ ...(target === 'backend' ? { configTypes: types } : {}),
129
+ envOverrides: config.envOverrides,
98
130
  };
131
+ if (target === 'frontend') {
132
+ targetModel.routes = new PageScanner(this.root).scan();
133
+ }
99
134
  model[target] = targetModel;
100
135
  }
101
136
  return model;
102
137
  }
103
138
 
139
+ /**
140
+ * The browser sees only the keys the schemas mark public: the variables apply to the whole
141
+ * configuration (a public key may refer to another one), the public keys are evaluated, then picked.
142
+ */
143
+ protected frontendConfig(merged: Record<string, unknown>, schema: ConfigSchema, overrides: EnvOverrides, types: Record<string, string>) {
144
+ const config = structuredClone(merged);
145
+ delete config.paths;
146
+ const envOverrides = overrides.apply(config, types);
147
+ new ExpressionEvaluator(this.root).evaluate(
148
+ config,
149
+ { env: ExpressionEvaluator.definedEnv(this.env) },
150
+ { types, include: path => schema.isPublic(path) },
151
+ );
152
+ return { config: schema.project(config), envOverrides };
153
+ }
154
+
155
+ /** All problems of the configuration files in one error, the first one's file and line as its location. */
156
+ protected static throwProblems(problems: ConfigProblem[]): void {
157
+ if (problems.length === 0) {
158
+ return;
159
+ }
160
+ const [first] = problems;
161
+ const lines = problems.slice(0, 20).map(p => ` ${p.file}${p.line ? `:${p.line}` : ''} ${p.path}: ${p.message} ${p.hint}`);
162
+ throw new CellError({
163
+ code: 'CELL_CONFIG_INVALID',
164
+ message:
165
+ problems.length === 1
166
+ ? `${first.message}`
167
+ : `${problems.length} problems in the configuration files:\n${lines.join('\n')}${problems.length > 20 ? '\n ...' : ''}`,
168
+ file: first.file,
169
+ line: first.line,
170
+ hint: problems.length === 1 ? first.hint : 'Fix each reported key; the keys of every component are in .cell/schema/cell.yml.json.',
171
+ });
172
+ }
173
+
104
174
  protected extraModules(target: Target, isStatic: boolean): ModuleRef[] {
105
175
  const importer = join(this.root, 'package.json');
106
176
  return (this.options.extraModules ?? [])
@@ -145,9 +215,4 @@ export class ProjectLoader {
145
215
  });
146
216
  }
147
217
  }
148
-
149
- /** The browser has no runtime environment, so frontend expressions are evaluated at build time. */
150
- protected evaluate(target: Target, config: Record<string, unknown>): Record<string, unknown> {
151
- return target === 'frontend' ? new ExpressionEvaluator(this.root).evaluate(config, { env: { ...this.env } }) : config;
152
- }
153
218
  }