@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.
- package/lib/builder/cell-builder.spec.js +26 -18
- package/lib/builder/cell-builder.spec.js.map +1 -1
- package/lib/dev/cell-dev-server.d.ts +3 -1
- package/lib/dev/cell-dev-server.d.ts.map +1 -1
- package/lib/dev/cell-dev-server.js +14 -3
- package/lib/dev/cell-dev-server.js.map +1 -1
- package/lib/dev/cell-dev-server.spec.js +21 -5
- package/lib/dev/cell-dev-server.spec.js.map +1 -1
- package/lib/expression/expression-evaluator.d.ts +18 -14
- package/lib/expression/expression-evaluator.d.ts.map +1 -1
- package/lib/expression/expression-evaluator.js +61 -67
- package/lib/expression/expression-evaluator.js.map +1 -1
- package/lib/expression/expression-evaluator.spec.js +25 -0
- package/lib/expression/expression-evaluator.spec.js.map +1 -1
- package/lib/generator/container-generator.js +3 -3
- package/lib/generator/container-generator.js.map +1 -1
- package/lib/generator/generator.spec.js +1 -1
- package/lib/generator/generator.spec.js.map +1 -1
- package/lib/project/project-loader.spec.js +2 -1
- package/lib/project/project-loader.spec.js.map +1 -1
- package/lib/test/fixture-project.d.ts.map +1 -1
- package/lib/test/fixture-project.js +2 -1
- package/lib/test/fixture-project.js.map +1 -1
- package/lib/testing/test-app.d.ts +2 -1
- package/lib/testing/test-app.d.ts.map +1 -1
- package/lib/testing/test-app.js +1 -1
- package/lib/testing/test-app.js.map +1 -1
- package/package.json +9 -9
- package/src/dev/cell-dev-server.ts +15 -4
- package/src/expression/expression-evaluator.ts +64 -71
- package/src/generator/container-generator.ts +3 -3
- package/src/test/fixture-project.ts +2 -1
- 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
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
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
|
|
13
|
-
* runtime evaluation
|
|
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
|
|
17
|
+
protected static readonly utils = new Map<string, ExpressionUtil>();
|
|
17
18
|
|
|
18
|
-
protected readonly
|
|
19
|
+
protected readonly util: ExpressionUtil;
|
|
19
20
|
|
|
20
21
|
constructor(root: string) {
|
|
21
|
-
this.
|
|
22
|
+
this.util = ExpressionEvaluator.utilFor(root);
|
|
22
23
|
}
|
|
23
24
|
|
|
24
|
-
protected static
|
|
25
|
-
let
|
|
26
|
-
if (!
|
|
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
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
ExpressionEvaluator.
|
|
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
|
|
36
|
+
return util;
|
|
36
37
|
}
|
|
37
38
|
|
|
38
|
-
|
|
39
|
-
|
|
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
|
-
|
|
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
|
|
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
|
-
|
|
85
|
+
return this.util.evaluateText(text, ctx);
|
|
56
86
|
} catch (cause) {
|
|
57
|
-
throw
|
|
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
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
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 {
|
|
18
|
+
"import { Container } from './container.js';",
|
|
19
19
|
...statics,
|
|
20
20
|
'',
|
|
21
|
-
'const instance =
|
|
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 '
|
|
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
|
-
|
|
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
|
}
|
package/src/testing/test-app.ts
CHANGED
|
@@ -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
|
|
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
|
|
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 = [...
|
|
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];
|