@celljs/vite 4.0.0 → 4.2.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 (33) hide show
  1. package/lib/builder/cell-builder.spec.js +26 -18
  2. package/lib/builder/cell-builder.spec.js.map +1 -1
  3. package/lib/dev/cell-dev-server.d.ts +3 -1
  4. package/lib/dev/cell-dev-server.d.ts.map +1 -1
  5. package/lib/dev/cell-dev-server.js +14 -3
  6. package/lib/dev/cell-dev-server.js.map +1 -1
  7. package/lib/dev/cell-dev-server.spec.js +21 -5
  8. package/lib/dev/cell-dev-server.spec.js.map +1 -1
  9. package/lib/expression/expression-evaluator.d.ts +18 -14
  10. package/lib/expression/expression-evaluator.d.ts.map +1 -1
  11. package/lib/expression/expression-evaluator.js +61 -67
  12. package/lib/expression/expression-evaluator.js.map +1 -1
  13. package/lib/expression/expression-evaluator.spec.js +25 -0
  14. package/lib/expression/expression-evaluator.spec.js.map +1 -1
  15. package/lib/generator/container-generator.js +3 -3
  16. package/lib/generator/container-generator.js.map +1 -1
  17. package/lib/generator/generator.spec.js +1 -1
  18. package/lib/generator/generator.spec.js.map +1 -1
  19. package/lib/project/project-loader.spec.js +2 -1
  20. package/lib/project/project-loader.spec.js.map +1 -1
  21. package/lib/test/fixture-project.d.ts.map +1 -1
  22. package/lib/test/fixture-project.js +2 -1
  23. package/lib/test/fixture-project.js.map +1 -1
  24. package/lib/testing/test-app.d.ts +2 -1
  25. package/lib/testing/test-app.d.ts.map +1 -1
  26. package/lib/testing/test-app.js +1 -1
  27. package/lib/testing/test-app.js.map +1 -1
  28. package/package.json +9 -9
  29. package/src/dev/cell-dev-server.ts +15 -4
  30. package/src/expression/expression-evaluator.ts +64 -71
  31. package/src/generator/container-generator.ts +3 -3
  32. package/src/test/fixture-project.ts +2 -1
  33. package/src/testing/test-app.ts +4 -3
@@ -2,47 +2,78 @@ import { createRequire } from 'node:module';
2
2
  import { join } from 'node:path';
3
3
  import { CellError } from '@celljs/kit';
4
4
 
5
- type Section = string | { evalSync(ctx: object): unknown };
6
-
7
- interface ExpressionCompiler {
8
- compileSections(text: string, options: object): Section[];
5
+ /** What the build uses of `@celljs/core/el` (packages/core/src/common/el/expression-util.ts). */
6
+ interface ExpressionUtil {
7
+ evaluateText(text: string, context: object): unknown;
8
+ evaluateConfiguration(root: object): void;
9
9
  }
10
10
 
11
11
  /**
12
- * Evaluates `${...}` expressions with the project's own @celljs/core compiler, so build-time and
13
- * runtime evaluation have identical semantics. The context is the object itself plus `extra`.
12
+ * Evaluates `${...}` expressions with the project's own @celljs/core (`@celljs/core/el`), so build-time
13
+ * and runtime evaluation are the same code: references between keys resolve in any order, and what an
14
+ * expression returns (an environment variable, for instance) is never evaluated again.
14
15
  */
15
16
  export class ExpressionEvaluator {
16
- protected static readonly compilers = new Map<string, ExpressionCompiler>();
17
+ protected static readonly utils = new Map<string, ExpressionUtil>();
17
18
 
18
- protected readonly compiler: ExpressionCompiler;
19
+ protected readonly util: ExpressionUtil;
19
20
 
20
21
  constructor(root: string) {
21
- this.compiler = ExpressionEvaluator.compilerFor(root);
22
+ this.util = ExpressionEvaluator.utilFor(root);
22
23
  }
23
24
 
24
- protected static compilerFor(root: string): ExpressionCompiler {
25
- let compiler = ExpressionEvaluator.compilers.get(root);
26
- if (!compiler) {
25
+ protected static utilFor(root: string): ExpressionUtil {
26
+ let util = ExpressionEvaluator.utils.get(root);
27
+ if (!util) {
27
28
  const require = createRequire(join(root, 'package.json'));
28
- require('reflect-metadata');
29
- const { ExpressionCompilerImpl } = require('@celljs/core/lib/common/el/expression-compiler');
30
- const { JexlEngineProviderImpl } = require('@celljs/core/lib/common/el/jexl-engine-provider');
31
- compiler = new ExpressionCompilerImpl() as ExpressionCompiler;
32
- Object.defineProperty(compiler, 'jexlEngineProvider', { value: new JexlEngineProviderImpl() });
33
- ExpressionEvaluator.compilers.set(root, compiler);
29
+ try {
30
+ util = require('@celljs/core/el').ExpressionUtil as ExpressionUtil;
31
+ } catch (cause) {
32
+ throw ExpressionEvaluator.loadError(require, root, cause);
33
+ }
34
+ ExpressionEvaluator.utils.set(root, util);
34
35
  }
35
- return compiler;
36
+ return util;
36
37
  }
37
38
 
38
- /** Evaluates every string of `target` in place and returns it. */
39
- evaluate<T extends Record<string, unknown>>(target: T, extra: Record<string, unknown> = {}): T {
40
- const ctx: Record<string, unknown> = Object.assign(target, extra);
39
+ protected static loadError(require: NodeJS.Require, root: string, cause: unknown): CellError {
40
+ let version: string | undefined;
41
41
  try {
42
- this.walk(ctx, '', ctx, extra);
42
+ version = require('@celljs/core/package.json').version;
43
+ } catch {
44
+ return new CellError({
45
+ code: 'CELL_CORE_NOT_FOUND',
46
+ message: 'The project does not depend on @celljs/core.',
47
+ file: join(root, 'package.json'),
48
+ hint: 'Add @celljs/core to dependencies and install.',
49
+ cause,
50
+ });
51
+ }
52
+ return new CellError({
53
+ code: 'CELL_CORE_EL_MISSING',
54
+ message: `The @celljs/core ${version} of this project has no @celljs/core/el entry, which the build evaluates configuration expressions with.`,
55
+ file: join(root, 'package.json'),
56
+ hint: 'Upgrade @celljs/core and the other @celljs/* packages to 4.1.0 or later.',
57
+ cause,
58
+ });
59
+ }
60
+
61
+ /**
62
+ * Evaluates `target` in place and returns it. The objects of `extra` (e.g. `env`) are visible to the
63
+ * expressions as they are, without being evaluated, and are removed afterwards.
64
+ */
65
+ evaluate<T extends Record<string, unknown>>(target: T, extra: Record<string, object> = {}): T {
66
+ const root: Record<string, unknown> = target;
67
+ for (const [key, value] of Object.entries(extra)) {
68
+ root[key] = { ...value, _ignoreEl: true };
69
+ }
70
+ try {
71
+ this.util.evaluateConfiguration(root);
72
+ } catch (cause) {
73
+ throw this.error(cause, (cause as Error).message);
43
74
  } finally {
44
75
  for (const key of Object.keys(extra)) {
45
- delete ctx[key];
76
+ delete root[key];
46
77
  }
47
78
  }
48
79
  return target;
@@ -50,57 +81,19 @@ export class ExpressionEvaluator {
50
81
 
51
82
  /** Evaluates one text against `ctx`. */
52
83
  evaluateText(text: string, ctx: Record<string, unknown>, path = 'value'): unknown {
53
- let sections: Section[];
54
84
  try {
55
- sections = this.compiler.compileSections(text, {});
85
+ return this.util.evaluateText(text, ctx);
56
86
  } catch (cause) {
57
- throw new CellError({
58
- code: 'CELL_CONFIG_EXPRESSION',
59
- message: `Invalid expression at ${path}: ${text}`,
60
- hint: 'Expressions are jexl inside ${...}; check quotes and braces.',
61
- cause,
62
- });
63
- }
64
- if (!sections.some(s => typeof s !== 'string')) {
65
- return text;
87
+ throw this.error(cause, `Cannot evaluate ${path}: ${text} (${(cause as Error).message})`);
66
88
  }
67
- if (sections.length === 1) {
68
- return this.run(sections[0], ctx, text, path);
69
- }
70
- return sections.map(s => (typeof s === 'string' ? s : this.run(s, ctx, text, path))).join('');
71
- }
72
-
73
- protected run(section: Section, ctx: Record<string, unknown>, text: string, path: string): unknown {
74
- if (typeof section === 'string') {
75
- return section;
76
- }
77
- let value: unknown;
78
- try {
79
- value = section.evalSync(ctx);
80
- } catch (cause) {
81
- throw new CellError({
82
- code: 'CELL_CONFIG_EXPRESSION',
83
- message: `Cannot evaluate ${path}: ${text} (${(cause as Error).message})`,
84
- hint: 'Only configuration keys and `env` are available in expressions.',
85
- cause,
86
- });
87
- }
88
- return typeof value === 'string' ? this.evaluateText(value, ctx, path) : value;
89
89
  }
90
90
 
91
- protected walk(node: Record<string, unknown> | unknown[], path: string, ctx: Record<string, unknown>, extra: Record<string, unknown>): void {
92
- const record = node as Record<string, unknown>;
93
- for (const key of Object.keys(record)) {
94
- if (node === ctx && key in extra) {
95
- continue;
96
- }
97
- const value = record[key];
98
- const child = path ? `${path}.${key}` : key;
99
- if (typeof value === 'string') {
100
- record[key] = this.evaluateText(value, ctx, child);
101
- } else if (value && typeof value === 'object') {
102
- this.walk(value as Record<string, unknown>, child, ctx, extra);
103
- }
104
- }
91
+ protected error(cause: unknown, message: string): CellError {
92
+ return new CellError({
93
+ code: 'CELL_CONFIG_EXPRESSION',
94
+ message,
95
+ hint: 'Expressions inside ${...} (see ExpressionEngine of @celljs/core) see only configuration keys and `env`; check quotes, braces and key names.',
96
+ cause,
97
+ });
105
98
  }
106
99
  }
@@ -15,10 +15,10 @@ export class ContainerGenerator {
15
15
  return [
16
16
  `// Generated by @celljs/vite for the ${this.target}. Do not edit.`,
17
17
  ...this.prelude(),
18
- "import { ContainerFactory } from './container-factory.js';",
18
+ "import { Container } from './container.js';",
19
19
  ...statics,
20
20
  '',
21
- 'const instance = ContainerFactory.create();',
21
+ 'const instance = new Container();',
22
22
  ...loads,
23
23
  '',
24
24
  'function load(mod) {',
@@ -34,7 +34,7 @@ export class ContainerGenerator {
34
34
 
35
35
  protected prelude(): string[] {
36
36
  if (this.target === 'frontend') {
37
- return ["import 'reflect-metadata';", "import 'setimmediate';", "import '../../browser/index.js';"];
37
+ return ["import 'reflect-metadata';", "import '../../browser/index.js';"];
38
38
  }
39
39
  return ["import 'reflect-metadata';"];
40
40
  }
@@ -45,6 +45,7 @@ export class FixtureProject {
45
45
  }
46
46
 
47
47
  dispose(): void {
48
- rmSync(this.root, { recursive: true, force: true });
48
+ // Windows keeps the files of a process that was just killed busy for a moment.
49
+ rmSync(this.root, { recursive: true, force: true, maxRetries: 10, retryDelay: 200 });
49
50
  }
50
51
  }
@@ -7,11 +7,12 @@ export interface TestAppOptions extends Omit<CellDevServerOptions, 'listen' | 'p
7
7
  mode?: string[];
8
8
  }
9
9
 
10
- /** What a test needs from a DI container: `get` by identifier (an inversify container). */
10
+ /** What a test needs from a DI container (the @celljs/core Container). */
11
11
  export interface TestContainer {
12
12
  get<T>(id: unknown): T;
13
13
  getAll<T>(id: unknown): T[];
14
14
  isBound(id: unknown): boolean;
15
+ bindings(): ReadonlyMap<unknown, unknown>;
15
16
  }
16
17
 
17
18
  /**
@@ -58,12 +59,12 @@ export class TestApp {
58
59
  }
59
60
 
60
61
  protected identifier(id: unknown): unknown {
61
- const container = this.container as TestContainer & { _bindingDictionary?: { getMap(): Map<unknown, unknown> } };
62
+ const container = this.container;
62
63
  if (container.isBound(id) || (typeof id !== 'symbol' && typeof id !== 'function')) {
63
64
  return id;
64
65
  }
65
66
  const name = TestApp.nameOf(id);
66
- const bound = [...(container._bindingDictionary?.getMap().keys() ?? [])];
67
+ const bound = [...container.bindings().keys()];
67
68
  const same = bound.filter(other => typeof other === typeof id && TestApp.nameOf(other) === name);
68
69
  if (same.length === 1) {
69
70
  return same[0];