embedded-react 0.19.0 → 0.21.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/NOTICE +5 -0
- package/README.md +8 -3
- package/aot/app-file-loader.mjs +116 -0
- package/aot/compile.mjs +180 -90
- package/aot/compiler/animations.mjs +568 -317
- package/aot/compiler/boot.mjs +97 -35
- package/aot/compiler/branch-merge.mjs +4 -3
- package/aot/compiler/check.mjs +70 -18
- package/aot/compiler/collect.mjs +787 -288
- package/aot/compiler/contexts.mjs +391 -0
- package/aot/compiler/control-flow.mjs +439 -268
- package/aot/compiler/custom-hooks.mjs +686 -0
- package/aot/compiler/diagnostics.mjs +22 -25
- package/aot/compiler/elements/activity-indicator.mjs +25 -24
- package/aot/compiler/elements/button.mjs +182 -0
- package/aot/compiler/elements/dial.mjs +91 -77
- package/aot/compiler/elements/flat-list.mjs +24 -22
- package/aot/compiler/elements/image-background.mjs +76 -0
- package/aot/compiler/elements/image.mjs +52 -50
- package/aot/compiler/elements/modal.mjs +31 -30
- package/aot/compiler/elements/props.mjs +61 -26
- package/aot/compiler/elements/rewrite.mjs +118 -0
- package/aot/compiler/elements/section-list.mjs +370 -0
- package/aot/compiler/elements/switch.mjs +42 -34
- package/aot/compiler/elements/text-input.mjs +34 -32
- package/aot/compiler/expressions.mjs +1685 -392
- package/aot/compiler/handlers.mjs +2029 -547
- package/aot/compiler/helper-values.mjs +312 -0
- package/aot/compiler/ir/functions.mjs +11 -1
- package/aot/compiler/ir/nodes.mjs +43 -4
- package/aot/compiler/ir/reads.mjs +223 -22
- package/aot/compiler/ir/text.mjs +5 -1
- package/aot/compiler/ir/types.mjs +26 -14
- package/aot/compiler/ir/vectors.mjs +5 -1
- package/aot/compiler/kept-states.mjs +177 -0
- package/aot/compiler/keyboard.mjs +17 -7
- package/aot/compiler/list-items.mjs +226 -0
- package/aot/compiler/list-order.mjs +586 -0
- package/aot/compiler/list-split.mjs +259 -0
- package/aot/compiler/loops.mjs +582 -0
- package/aot/compiler/lower.mjs +246 -111
- package/aot/compiler/native-files.mjs +439 -0
- package/aot/compiler/native-header.mjs +335 -0
- package/aot/compiler/native-modules.mjs +1173 -0
- package/aot/compiler/nodes.mjs +195 -172
- package/aot/compiler/options.mjs +37 -31
- package/aot/compiler/out.mjs +32 -23
- package/aot/compiler/package/animations.mjs +779 -0
- package/aot/compiler/package/arena.mjs +252 -0
- package/aot/compiler/package/assemble.mjs +175 -0
- package/aot/compiler/package/bytecode.mjs +315 -0
- package/aot/compiler/package/checks.mjs +1026 -0
- package/aot/compiler/package/codegen.mjs +2168 -0
- package/aot/compiler/package/enums.mjs +148 -0
- package/aot/compiler/package/format.mjs +771 -0
- package/aot/compiler/package/layouts.mjs +136 -0
- package/aot/compiler/package/lower.mjs +2103 -0
- package/aot/compiler/package/props.mjs +118 -0
- package/aot/compiler/package/string-caps.mjs +515 -0
- package/aot/compiler/package/tables.mjs +127 -0
- package/aot/compiler/package/vectors.mjs +502 -0
- package/aot/compiler/package/writer.mjs +686 -0
- package/aot/compiler/pan-responder.mjs +69 -61
- package/aot/compiler/parse.mjs +23 -21
- package/aot/compiler/record-shapes.mjs +246 -0
- package/aot/compiler/records.mjs +654 -0
- package/aot/compiler/reducers.mjs +272 -0
- package/aot/compiler/static-eval.mjs +340 -30
- package/aot/compiler/style-text.mjs +178 -174
- package/aot/compiler/svg.mjs +480 -251
- package/aot/compiler/switch-cases.mjs +198 -0
- package/aot/compiler/tagged-records.mjs +450 -0
- package/aot/compiler/typing.mjs +307 -0
- package/aot/compiler/updaters.mjs +320 -0
- package/aot/compiler/window-dimensions.mjs +94 -0
- package/aot/compiler/worked-values.mjs +319 -0
- package/aot/demo-file.mjs +30 -0
- package/aot/screenshot-smoke.mjs +54 -40
- package/aot/style-map.mjs +152 -92
- package/assets/bake-font.mjs +123 -77
- package/assets/bake-image.mjs +39 -22
- package/assets/bake-svg.mjs +361 -262
- package/assets/build-builtin-font.mjs +11 -7
- package/assets/emit-c.mjs +77 -58
- package/assets/emit-container.mjs +123 -37
- package/assets/emit-pack.mjs +77 -67
- package/assets/font-config.mjs +9 -6
- package/assets/font-sizes.mjs +163 -123
- package/assets/glyph-coverage.mjs +74 -54
- package/assets/index.mjs +31 -9
- package/assets/rasterize.mjs +126 -99
- package/assets/svg-loader.mjs +8 -8
- package/build.mjs +23 -13
- package/cli.mjs +402 -172
- package/compile-bin.mjs +3 -3
- package/flash.mjs +537 -0
- package/hotreload/dev-device.mjs +90 -33
- package/hotreload/pack-app.mjs +55 -43
- package/hotreload/pack-split.mjs +62 -64
- package/hotreload/package-transfer.mjs +175 -0
- package/hotreload/protocol.mjs +50 -37
- package/hotreload/uploader.mjs +38 -27
- package/hotreload/watch-tree.mjs +19 -19
- package/pack-container.mjs +37 -21
- package/package.json +7 -8
- package/persist-transform.mjs +34 -28
- package/qjsc-wasm.mjs +25 -14
- package/sim/embedded-react.cjs +1 -1
- package/sim/embedded-react.js +1 -1
- package/sim/embedded-react.wasm +0 -0
- package/sim/index.html +94 -85
- package/sim-server.mjs +113 -94
- package/src/embedded-react/Animated.js +24 -24
- package/src/embedded-react/Dimensions.js +37 -0
- package/src/embedded-react/Easing.js +1 -1
- package/src/embedded-react/FlatList.js +3 -3
- package/src/embedded-react/LayoutAnimation.js +8 -5
- package/src/embedded-react/PanResponder.js +75 -71
- package/src/embedded-react/Pressable.js +2 -8
- package/src/embedded-react/SectionList.js +7 -9
- package/src/embedded-react/StyleSheet.js +4 -3
- package/src/embedded-react/TouchableOpacity.js +13 -8
- package/src/embedded-react/index.d.ts +88 -8
- package/src/embedded-react/index.js +2 -0
- package/src/embedded-react/layout-anim-config.js +10 -4
- package/src/embedded-react/list-child.js +5 -5
- package/src/embedded-react/named-colors.js +1 -1
- package/src/embedded-react/nativeModule.js +462 -0
- package/src/embedded-react/split-style.js +11 -11
- package/src/embedded-react/svg-ops.js +760 -593
- package/src/embedded-react/useHostValue.js +3 -3
- package/src/embedded-react/usePersistentState.js +6 -6
- package/src/embedded-react/warn-props.js +1 -1
- package/src/host-config.js +60 -53
- package/src/props.js +49 -41
- package/src/renderer.js +7 -4
- package/aot/compiler/assemble.mjs +0 -944
- package/aot/compiler/c/animations.mjs +0 -436
- package/aot/compiler/c/boot.mjs +0 -127
- package/aot/compiler/c/expressions.mjs +0 -270
- package/aot/compiler/c/functions.mjs +0 -268
- package/aot/compiler/c/keyboard.mjs +0 -96
- package/aot/compiler/c/nodes.mjs +0 -303
- package/aot/compiler/c/pan-responder.mjs +0 -136
- package/aot/compiler/c/statements.mjs +0 -319
- package/aot/compiler/c/text.mjs +0 -83
- package/aot/compiler/c/vectors.mjs +0 -381
- package/aot/compiler/c-syntax.mjs +0 -127
package/assets/font-sizes.mjs
CHANGED
|
@@ -53,7 +53,10 @@ const MAX_DEPTH = 16;
|
|
|
53
53
|
// parser chokes on (a future syntax, a mangled bundle). Strictly weaker, never silently preferred.
|
|
54
54
|
const LITERAL_SIZE = /\bfontSize\s*:\s*(\d+(?:\.\d+)?)/g;
|
|
55
55
|
|
|
56
|
-
|
|
56
|
+
// Unresolved fontSize expressions are reported up to this many characters.
|
|
57
|
+
const SNIPPET_MAX_LENGTH = 48;
|
|
58
|
+
|
|
59
|
+
const PARSER_OPTIONS = {
|
|
57
60
|
sourceType: 'unambiguous', // an IIFE bundle is a script; an app entry is a module
|
|
58
61
|
errorRecovery: true,
|
|
59
62
|
// Same pair the AOT parser uses: the TS plugin is harmless on plain JS/JSX and lets a .tsx app
|
|
@@ -80,7 +83,7 @@ const NON_NODE_KEYS = new Set([
|
|
|
80
83
|
/** Depth-first visit of every AST node under `node`, in source order. */
|
|
81
84
|
function walk(node, visit) {
|
|
82
85
|
if (Array.isArray(node)) {
|
|
83
|
-
for (const
|
|
86
|
+
for (const item of node) walk(item, visit);
|
|
84
87
|
return;
|
|
85
88
|
}
|
|
86
89
|
if (!node || typeof node !== 'object' || typeof node.type !== 'string')
|
|
@@ -96,11 +99,11 @@ function walk(node, visit) {
|
|
|
96
99
|
/** Static name of a non-computed property key, or null when the key isn't a fixed string. */
|
|
97
100
|
function keyName(node) {
|
|
98
101
|
if (node.computed) return null;
|
|
99
|
-
const
|
|
100
|
-
if (!
|
|
101
|
-
if (
|
|
102
|
-
if (
|
|
103
|
-
if (
|
|
102
|
+
const key = node.key;
|
|
103
|
+
if (!key) return null;
|
|
104
|
+
if (key.type === 'Identifier') return key.name;
|
|
105
|
+
if (key.type === 'StringLiteral') return key.value;
|
|
106
|
+
if (key.type === 'NumericLiteral') return String(key.value);
|
|
104
107
|
return null;
|
|
105
108
|
}
|
|
106
109
|
|
|
@@ -108,9 +111,11 @@ function keyName(node) {
|
|
|
108
111
|
|
|
109
112
|
// A folded value is either a number or a Scale: an object/array whose properties are themselves
|
|
110
113
|
// candidate lists, so `TYPE.title` is a lookup and a ternary is a union.
|
|
111
|
-
const scale =
|
|
112
|
-
const isScale =
|
|
113
|
-
|
|
114
|
+
const scale = properties => ({properties});
|
|
115
|
+
const isScale = value =>
|
|
116
|
+
value !== null &&
|
|
117
|
+
typeof value === 'object' &&
|
|
118
|
+
value.properties instanceof Map;
|
|
114
119
|
|
|
115
120
|
/** Trims a candidate list to the fan-out budget, dropping duplicates first. */
|
|
116
121
|
const cap = values =>
|
|
@@ -119,20 +124,20 @@ const cap = values =>
|
|
|
119
124
|
: values;
|
|
120
125
|
|
|
121
126
|
/** The numbers in a candidate list (a Scale is not a size). */
|
|
122
|
-
const numbers = values => values.filter(
|
|
127
|
+
const numbers = values => values.filter(value => typeof value === 'number');
|
|
123
128
|
|
|
124
129
|
const ARITHMETIC = {
|
|
125
|
-
'+': (
|
|
126
|
-
'-': (
|
|
127
|
-
'*': (
|
|
128
|
-
'/': (
|
|
129
|
-
'%': (
|
|
130
|
-
'**': (
|
|
130
|
+
'+': (left, right) => left + right,
|
|
131
|
+
'-': (left, right) => left - right,
|
|
132
|
+
'*': (left, right) => left * right,
|
|
133
|
+
'/': (left, right) => left / right,
|
|
134
|
+
'%': (left, right) => left % right,
|
|
135
|
+
'**': (left, right) => left ** right,
|
|
131
136
|
};
|
|
132
137
|
|
|
133
138
|
// Rounding is how a ratio-based scale lands on whole pixels (Math.round(base * 1.25)); min/max is
|
|
134
139
|
// how it gets clamped. Anything else on Math is left unresolved rather than guessed.
|
|
135
|
-
const
|
|
140
|
+
const MATH_FUNCTIONS = {
|
|
136
141
|
round: Math.round,
|
|
137
142
|
floor: Math.floor,
|
|
138
143
|
ceil: Math.ceil,
|
|
@@ -151,7 +156,7 @@ const MATH_FNS = {
|
|
|
151
156
|
*/
|
|
152
157
|
function evaluate(node, scope, depth = 0) {
|
|
153
158
|
if (!node || depth > MAX_DEPTH) return [];
|
|
154
|
-
const
|
|
159
|
+
const evaluateChild = child => evaluate(child, scope, depth + 1);
|
|
155
160
|
|
|
156
161
|
switch (node.type) {
|
|
157
162
|
case 'NumericLiteral':
|
|
@@ -167,77 +172,84 @@ function evaluate(node, scope, depth = 0) {
|
|
|
167
172
|
case 'TSNonNullExpression':
|
|
168
173
|
case 'TSTypeAssertion':
|
|
169
174
|
case 'TypeCastExpression':
|
|
170
|
-
return
|
|
175
|
+
return evaluateChild(node.expression);
|
|
171
176
|
|
|
172
177
|
case 'UnaryExpression': {
|
|
173
178
|
if (node.operator !== '-' && node.operator !== '+') return [];
|
|
174
179
|
const sign = node.operator === '-' ? -1 : 1;
|
|
175
|
-
return numbers(
|
|
180
|
+
return numbers(evaluateChild(node.argument)).map(value => sign * value);
|
|
176
181
|
}
|
|
177
182
|
|
|
178
183
|
case 'BinaryExpression': {
|
|
179
|
-
const
|
|
180
|
-
if (!
|
|
181
|
-
const left = numbers(
|
|
182
|
-
const right = numbers(
|
|
183
|
-
const
|
|
184
|
-
for (const
|
|
185
|
-
|
|
184
|
+
const operation = ARITHMETIC[node.operator];
|
|
185
|
+
if (!operation) return [];
|
|
186
|
+
const left = numbers(evaluateChild(node.left));
|
|
187
|
+
const right = numbers(evaluateChild(node.right));
|
|
188
|
+
const results = [];
|
|
189
|
+
for (const leftValue of left)
|
|
190
|
+
for (const rightValue of right)
|
|
191
|
+
results.push(operation(leftValue, rightValue));
|
|
192
|
+
return cap(results);
|
|
186
193
|
}
|
|
187
194
|
|
|
188
195
|
// Both arms: which one a responsive app takes depends on the panel it boots on, and the same
|
|
189
196
|
// bundle runs on all of them.
|
|
190
197
|
case 'ConditionalExpression':
|
|
191
|
-
return cap([
|
|
198
|
+
return cap([
|
|
199
|
+
...evaluateChild(node.consequent),
|
|
200
|
+
...evaluateChild(node.alternate),
|
|
201
|
+
]);
|
|
192
202
|
|
|
193
203
|
// `a ?? b` / `a || b` are the same shape of choice; `&&` can only ever yield its right arm.
|
|
194
204
|
case 'LogicalExpression':
|
|
195
205
|
return node.operator === '&&'
|
|
196
|
-
?
|
|
197
|
-
: cap([...
|
|
206
|
+
? evaluateChild(node.right)
|
|
207
|
+
: cap([...evaluateChild(node.left), ...evaluateChild(node.right)]);
|
|
198
208
|
|
|
199
209
|
case 'ObjectExpression': {
|
|
200
|
-
const
|
|
201
|
-
for (const
|
|
202
|
-
if (
|
|
203
|
-
for (const base of
|
|
204
|
-
for (const [
|
|
210
|
+
const properties = new Map();
|
|
211
|
+
for (const property of node.properties) {
|
|
212
|
+
if (property.type === 'SpreadElement') {
|
|
213
|
+
for (const base of evaluateChild(property.argument).filter(isScale))
|
|
214
|
+
for (const [key, values] of base.properties)
|
|
215
|
+
properties.set(key, values);
|
|
205
216
|
continue;
|
|
206
217
|
}
|
|
207
|
-
if (
|
|
208
|
-
const name = keyName(
|
|
218
|
+
if (property.type !== 'ObjectProperty') continue; // a method is never a size
|
|
219
|
+
const name = keyName(property);
|
|
209
220
|
if (name === null) continue;
|
|
210
|
-
const values =
|
|
211
|
-
if (values.length)
|
|
221
|
+
const values = evaluateChild(property.value);
|
|
222
|
+
if (values.length) properties.set(name, values);
|
|
212
223
|
}
|
|
213
|
-
return [scale(
|
|
224
|
+
return [scale(properties)];
|
|
214
225
|
}
|
|
215
226
|
|
|
216
227
|
case 'ArrayExpression': {
|
|
217
|
-
const
|
|
218
|
-
node.elements.forEach((
|
|
219
|
-
if (!
|
|
220
|
-
const values =
|
|
221
|
-
if (values.length)
|
|
228
|
+
const properties = new Map();
|
|
229
|
+
node.elements.forEach((element, index) => {
|
|
230
|
+
if (!element || element.type === 'SpreadElement') return;
|
|
231
|
+
const values = evaluateChild(element);
|
|
232
|
+
if (values.length) properties.set(String(index), values);
|
|
222
233
|
});
|
|
223
|
-
return [scale(
|
|
234
|
+
return [scale(properties)];
|
|
224
235
|
}
|
|
225
236
|
|
|
226
237
|
case 'MemberExpression': {
|
|
227
|
-
const objects =
|
|
238
|
+
const objects = evaluateChild(node.object).filter(isScale);
|
|
228
239
|
if (!objects.length) return [];
|
|
229
240
|
const key = memberKey(node, scope, depth);
|
|
230
|
-
const
|
|
231
|
-
for (const
|
|
241
|
+
const results = [];
|
|
242
|
+
for (const object of objects) {
|
|
232
243
|
// An unresolvable key (`TYPE[variant]`) still narrows things to one scale, and every entry
|
|
233
244
|
// in it is something the app can reach — take the lot rather than nothing.
|
|
234
245
|
if (key === null) {
|
|
235
|
-
for (const
|
|
246
|
+
for (const values of object.properties.values())
|
|
247
|
+
results.push(...values);
|
|
236
248
|
} else {
|
|
237
|
-
|
|
249
|
+
results.push(...(object.properties.get(key) ?? []));
|
|
238
250
|
}
|
|
239
251
|
}
|
|
240
|
-
return cap(
|
|
252
|
+
return cap(results);
|
|
241
253
|
}
|
|
242
254
|
|
|
243
255
|
case 'CallExpression': {
|
|
@@ -250,12 +262,20 @@ function evaluate(node, scope, depth = 0) {
|
|
|
250
262
|
callee.property?.type !== 'Identifier'
|
|
251
263
|
)
|
|
252
264
|
return [];
|
|
253
|
-
const
|
|
254
|
-
if (!
|
|
265
|
+
const mathFunction = MATH_FUNCTIONS[callee.property.name];
|
|
266
|
+
if (!mathFunction) return [];
|
|
255
267
|
// Every argument must resolve, or the result isn't the one the app computes.
|
|
256
|
-
const
|
|
257
|
-
|
|
258
|
-
|
|
268
|
+
const argumentValues = node.arguments.map(argument =>
|
|
269
|
+
numbers(evaluateChild(argument)),
|
|
270
|
+
);
|
|
271
|
+
if (
|
|
272
|
+
!argumentValues.length ||
|
|
273
|
+
argumentValues.some(candidates => !candidates.length)
|
|
274
|
+
)
|
|
275
|
+
return [];
|
|
276
|
+
return cap(
|
|
277
|
+
combine(argumentValues).map(values => mathFunction(...values)),
|
|
278
|
+
);
|
|
259
279
|
}
|
|
260
280
|
|
|
261
281
|
default:
|
|
@@ -269,9 +289,9 @@ function combine(lists) {
|
|
|
269
289
|
for (const list of lists) {
|
|
270
290
|
const next = [];
|
|
271
291
|
for (const row of rows)
|
|
272
|
-
for (const
|
|
292
|
+
for (const value of list) {
|
|
273
293
|
if (next.length >= MAX_CANDIDATES) break;
|
|
274
|
-
next.push([...row,
|
|
294
|
+
next.push([...row, value]);
|
|
275
295
|
}
|
|
276
296
|
rows = next;
|
|
277
297
|
}
|
|
@@ -297,7 +317,7 @@ function memberKey(node, scope, depth) {
|
|
|
297
317
|
* names a bundle contains are only ever evaluated for the handful a fontSize actually reads.
|
|
298
318
|
*/
|
|
299
319
|
function lookup(name, scope, depth) {
|
|
300
|
-
const known = scope.
|
|
320
|
+
const known = scope.environment.get(name);
|
|
301
321
|
if (known !== undefined) return known; // a memoized "nothing" is an answer, not a miss
|
|
302
322
|
const bound = scope.bindings.get(name);
|
|
303
323
|
if (!bound || scope.resolving.has(name)) return [];
|
|
@@ -307,7 +327,7 @@ function lookup(name, scope, depth) {
|
|
|
307
327
|
bound.flatMap(node => evaluate(node, scope, depth + 1)).filter(isScale),
|
|
308
328
|
);
|
|
309
329
|
scope.resolving.delete(name);
|
|
310
|
-
scope.
|
|
330
|
+
scope.environment.set(name, values);
|
|
311
331
|
return values;
|
|
312
332
|
}
|
|
313
333
|
|
|
@@ -326,7 +346,7 @@ function lookup(name, scope, depth) {
|
|
|
326
346
|
* places resolves to all of them, which over-bakes at worst; declared names always win.
|
|
327
347
|
*
|
|
328
348
|
* @param {object} ast Parsed program.
|
|
329
|
-
* @returns {{
|
|
349
|
+
* @returns {{environment: Map<string,any[]>, bindings: Map<string,object[]>, resolving: Set<string>}}
|
|
330
350
|
*/
|
|
331
351
|
function buildScope(ast) {
|
|
332
352
|
const declarations = [];
|
|
@@ -339,25 +359,25 @@ function buildScope(ast) {
|
|
|
339
359
|
else bindings.set(name, [node]);
|
|
340
360
|
};
|
|
341
361
|
|
|
342
|
-
walk(ast.program,
|
|
343
|
-
if (
|
|
344
|
-
if (
|
|
345
|
-
declarations.push(
|
|
346
|
-
counts.set(
|
|
362
|
+
walk(ast.program, node => {
|
|
363
|
+
if (node.type === 'VariableDeclarator') {
|
|
364
|
+
if (node.id?.type === 'Identifier' && node.init) {
|
|
365
|
+
declarations.push(node);
|
|
366
|
+
counts.set(node.id.name, (counts.get(node.id.name) ?? 0) + 1);
|
|
347
367
|
}
|
|
348
368
|
return;
|
|
349
369
|
}
|
|
350
370
|
if (
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
371
|
+
node.type === 'JSXAttribute' &&
|
|
372
|
+
node.name?.type === 'JSXIdentifier' &&
|
|
373
|
+
node.value?.type === 'JSXExpressionContainer'
|
|
354
374
|
) {
|
|
355
|
-
bind(
|
|
375
|
+
bind(node.name.name, node.value.expression);
|
|
356
376
|
return;
|
|
357
377
|
}
|
|
358
|
-
if (
|
|
359
|
-
const name = keyName(
|
|
360
|
-
if (name !== null) bind(name,
|
|
378
|
+
if (node.type === 'ObjectProperty') {
|
|
379
|
+
const name = keyName(node);
|
|
380
|
+
if (name !== null) bind(name, node.value);
|
|
361
381
|
}
|
|
362
382
|
});
|
|
363
383
|
|
|
@@ -365,13 +385,17 @@ function buildScope(ast) {
|
|
|
365
385
|
// binding consulted mid-fold could cache an answer taken from a half-built environment. Two passes,
|
|
366
386
|
// so a scale that reads a constant declared below it still folds; a third would only help a chain
|
|
367
387
|
// that deep, which no type scale is.
|
|
368
|
-
const scope = {
|
|
388
|
+
const scope = {
|
|
389
|
+
environment: new Map(),
|
|
390
|
+
bindings: new Map(),
|
|
391
|
+
resolving: new Set(),
|
|
392
|
+
};
|
|
369
393
|
for (let pass = 0; pass < 2; pass++) {
|
|
370
|
-
for (const
|
|
371
|
-
const name =
|
|
394
|
+
for (const declaration of declarations) {
|
|
395
|
+
const name = declaration.id.name;
|
|
372
396
|
if (counts.get(name) > 1) continue;
|
|
373
|
-
const values = evaluate(
|
|
374
|
-
if (values.length) scope.
|
|
397
|
+
const values = evaluate(declaration.init, scope);
|
|
398
|
+
if (values.length) scope.environment.set(name, values);
|
|
375
399
|
}
|
|
376
400
|
}
|
|
377
401
|
// A declared name resolves to its declaration or to nothing; it never falls through to a binding.
|
|
@@ -384,24 +408,30 @@ function buildScope(ast) {
|
|
|
384
408
|
|
|
385
409
|
/** Rounds a folded value to a bakeable pixel size, or null when it isn't one. */
|
|
386
410
|
function toPixelSize(value) {
|
|
387
|
-
const
|
|
388
|
-
return Number.isFinite(
|
|
411
|
+
const pixelSize = Math.round(value);
|
|
412
|
+
return Number.isFinite(pixelSize) &&
|
|
413
|
+
pixelSize >= MIN_PX &&
|
|
414
|
+
pixelSize <= MAX_PX
|
|
415
|
+
? pixelSize
|
|
416
|
+
: null;
|
|
389
417
|
}
|
|
390
418
|
|
|
391
419
|
/** One-line, length-capped rendering of the expression behind an unresolved fontSize. */
|
|
392
420
|
function snippet(source, node) {
|
|
393
421
|
const raw = source.slice(node.start, node.end).replace(/\s+/g, ' ').trim();
|
|
394
|
-
return raw.length >
|
|
422
|
+
return raw.length > SNIPPET_MAX_LENGTH
|
|
423
|
+
? `${raw.slice(0, SNIPPET_MAX_LENGTH - 1)}…`
|
|
424
|
+
: raw;
|
|
395
425
|
}
|
|
396
426
|
|
|
397
427
|
/** Literal-only discovery, for a source the parser rejected. */
|
|
398
428
|
function scanLiterals(source) {
|
|
399
429
|
const sizes = new Set();
|
|
400
|
-
for (const
|
|
401
|
-
const
|
|
402
|
-
if (
|
|
430
|
+
for (const match of source.matchAll(LITERAL_SIZE)) {
|
|
431
|
+
const pixelSize = toPixelSize(Number(match[1]));
|
|
432
|
+
if (pixelSize !== null) sizes.add(pixelSize);
|
|
403
433
|
}
|
|
404
|
-
return {sizes: [...sizes].sort((
|
|
434
|
+
return {sizes: [...sizes].sort((left, right) => left - right), dynamic: []};
|
|
405
435
|
}
|
|
406
436
|
|
|
407
437
|
/**
|
|
@@ -416,7 +446,7 @@ export function analyzeFontSizes(source) {
|
|
|
416
446
|
if (!source) return {sizes: [], dynamic: []};
|
|
417
447
|
let ast;
|
|
418
448
|
try {
|
|
419
|
-
ast = parse(source,
|
|
449
|
+
ast = parse(source, PARSER_OPTIONS);
|
|
420
450
|
} catch {
|
|
421
451
|
return scanLiterals(source); // never worse than the literal scan this replaced
|
|
422
452
|
}
|
|
@@ -424,27 +454,30 @@ export function analyzeFontSizes(source) {
|
|
|
424
454
|
const scope = buildScope(ast);
|
|
425
455
|
const sizes = new Set();
|
|
426
456
|
const dynamic = new Map(); // expression text → occurrences
|
|
427
|
-
walk(ast.program,
|
|
428
|
-
if (
|
|
457
|
+
walk(ast.program, node => {
|
|
458
|
+
if (node.type !== 'ObjectProperty' || keyName(node) !== 'fontSize') return;
|
|
429
459
|
// A value that resolves but can't be baked (0, negative, past the engine's uint8 pixel_size) is
|
|
430
460
|
// an authoring slip, not a discovery failure: leave it out of the bake and out of the report.
|
|
431
|
-
const resolved = numbers(evaluate(
|
|
461
|
+
const resolved = numbers(evaluate(node.value, scope));
|
|
432
462
|
if (resolved.length) {
|
|
433
463
|
for (const value of resolved) {
|
|
434
|
-
const
|
|
435
|
-
if (
|
|
464
|
+
const pixelSize = toPixelSize(value);
|
|
465
|
+
if (pixelSize !== null) sizes.add(pixelSize);
|
|
436
466
|
}
|
|
437
467
|
return;
|
|
438
468
|
}
|
|
439
|
-
const
|
|
440
|
-
dynamic.set(
|
|
469
|
+
const expression = snippet(source, node.value);
|
|
470
|
+
dynamic.set(expression, (dynamic.get(expression) ?? 0) + 1);
|
|
441
471
|
});
|
|
442
472
|
|
|
443
473
|
return {
|
|
444
|
-
sizes: [...sizes].sort((
|
|
474
|
+
sizes: [...sizes].sort((left, right) => left - right),
|
|
445
475
|
dynamic: [...dynamic]
|
|
446
|
-
.map(([
|
|
447
|
-
.sort(
|
|
476
|
+
.map(([expression, count]) => ({expr: expression, count}))
|
|
477
|
+
.sort(
|
|
478
|
+
(left, right) =>
|
|
479
|
+
right.count - left.count || left.expr.localeCompare(right.expr),
|
|
480
|
+
),
|
|
448
481
|
};
|
|
449
482
|
}
|
|
450
483
|
|
|
@@ -462,7 +495,7 @@ export function analyzeFontSizes(source) {
|
|
|
462
495
|
*/
|
|
463
496
|
export function fontSizeSignature(used) {
|
|
464
497
|
const {sizes = [], dynamic = []} = used ?? {};
|
|
465
|
-
return `${sizes.join(',')}|${dynamic.map(
|
|
498
|
+
return `${sizes.join(',')}|${dynamic.map(entry => entry.expr).join('\u0000')}`;
|
|
466
499
|
}
|
|
467
500
|
|
|
468
501
|
/*--- Coverage check -----------------------------------------------------------------------------*/
|
|
@@ -470,8 +503,8 @@ export function fontSizeSignature(used) {
|
|
|
470
503
|
/** Pixel sizes a bakeFont() result — or the job that produced it — covers. */
|
|
471
504
|
function bakedSizes(font) {
|
|
472
505
|
return (font.sizes ?? [])
|
|
473
|
-
.map(
|
|
474
|
-
.filter(
|
|
506
|
+
.map(size => (typeof size === 'number' ? size : size?.pixelSize))
|
|
507
|
+
.filter(pixelSize => Number.isFinite(pixelSize));
|
|
475
508
|
}
|
|
476
509
|
|
|
477
510
|
/**
|
|
@@ -486,10 +519,14 @@ function bakedSizes(font) {
|
|
|
486
519
|
export function pickBakedSize(baked, wanted) {
|
|
487
520
|
const target = Math.min(Math.max(wanted, RENDER_MIN_PX), RENDER_MAX_PX);
|
|
488
521
|
let best = baked[0];
|
|
489
|
-
for (const
|
|
490
|
-
const
|
|
491
|
-
const
|
|
492
|
-
if (
|
|
522
|
+
for (const size of baked.slice(1)) {
|
|
523
|
+
const difference = Math.abs(target - size);
|
|
524
|
+
const bestDifference = Math.abs(target - best);
|
|
525
|
+
if (
|
|
526
|
+
difference < bestDifference ||
|
|
527
|
+
(difference === bestDifference && size > best)
|
|
528
|
+
)
|
|
529
|
+
best = size;
|
|
493
530
|
}
|
|
494
531
|
return best;
|
|
495
532
|
}
|
|
@@ -511,7 +548,7 @@ export function pickBakedSize(baked, wanted) {
|
|
|
511
548
|
*/
|
|
512
549
|
export function findSizeGaps({sizes = [], fonts = []}) {
|
|
513
550
|
const checked = fonts.length
|
|
514
|
-
? fonts.map(
|
|
551
|
+
? fonts.map(font => ({family: font.family, baked: bakedSizes(font)}))
|
|
515
552
|
: [{family: BUILTIN_FAMILY, baked: [...BUILTIN_SIZES]}];
|
|
516
553
|
|
|
517
554
|
const gaps = [];
|
|
@@ -535,13 +572,14 @@ export function findSizeGaps({sizes = [], fonts = []}) {
|
|
|
535
572
|
baked: live,
|
|
536
573
|
});
|
|
537
574
|
}
|
|
538
|
-
return {gaps: gaps.sort((
|
|
575
|
+
return {gaps: gaps.sort((left, right) => left.size - right.size), dropped};
|
|
539
576
|
}
|
|
540
577
|
|
|
541
578
|
const MAX_LISTED = 8;
|
|
542
579
|
|
|
543
580
|
/** `[a, b, c]` as an assets.config.js `sizes` line would spell it. */
|
|
544
|
-
const sizeList = sizes =>
|
|
581
|
+
const sizeList = sizes =>
|
|
582
|
+
`[${[...sizes].sort((left, right) => left - right).join(', ')}]`;
|
|
545
583
|
|
|
546
584
|
/** A family name as a valid object key (a family is a file basename — `Inter-Regular` needs quotes). */
|
|
547
585
|
const configKey = family =>
|
|
@@ -569,8 +607,8 @@ export function formatFontSizeReport({
|
|
|
569
607
|
const lines = gaps
|
|
570
608
|
.slice(0, MAX_LISTED)
|
|
571
609
|
.map(
|
|
572
|
-
|
|
573
|
-
` ${
|
|
610
|
+
gap =>
|
|
611
|
+
` ${gap.size}px → renders at ${gap.snapsTo}px in ${gap.family} (baked: ${gap.baked.join(', ')})`,
|
|
574
612
|
);
|
|
575
613
|
if (gaps.length > MAX_LISTED)
|
|
576
614
|
lines.push(` … and ${gaps.length - MAX_LISTED} more`);
|
|
@@ -581,10 +619,10 @@ export function formatFontSizeReport({
|
|
|
581
619
|
}
|
|
582
620
|
|
|
583
621
|
if (dynamic.length) {
|
|
584
|
-
const total = dynamic.reduce((
|
|
585
|
-
const lines = dynamic.slice(0, MAX_LISTED).map(
|
|
586
|
-
const times =
|
|
587
|
-
return ` fontSize: ${
|
|
622
|
+
const total = dynamic.reduce((sum, entry) => sum + entry.count, 0);
|
|
623
|
+
const lines = dynamic.slice(0, MAX_LISTED).map(entry => {
|
|
624
|
+
const times = entry.count > 1 ? ` (×${entry.count})` : '';
|
|
625
|
+
return ` fontSize: ${entry.expr}${times}`;
|
|
588
626
|
});
|
|
589
627
|
if (dynamic.length > MAX_LISTED)
|
|
590
628
|
lines.push(` … and ${dynamic.length - MAX_LISTED} more`);
|
|
@@ -598,12 +636,12 @@ export function formatFontSizeReport({
|
|
|
598
636
|
);
|
|
599
637
|
}
|
|
600
638
|
|
|
601
|
-
for (const
|
|
639
|
+
for (const droppedFamily of dropped) {
|
|
602
640
|
blocks.push(
|
|
603
|
-
`embedded-react: ${
|
|
604
|
-
`registers the first ${MAX_BAKED_SIZES} and ignores ${sizeList(
|
|
641
|
+
`embedded-react: ${droppedFamily.family} is baked at more than ${MAX_BAKED_SIZES} sizes — the engine ` +
|
|
642
|
+
`registers the first ${MAX_BAKED_SIZES} and ignores ${sizeList(droppedFamily.ignored)}, so text at ` +
|
|
605
643
|
`those sizes snaps to one that did register.\n fix: choose the ${MAX_BAKED_SIZES} that ` +
|
|
606
|
-
`matter — assets.config.js → fonts: {${configKey(
|
|
644
|
+
`matter — assets.config.js → fonts: {${configKey(droppedFamily.family)}: {sizes: ${sizeList(droppedFamily.registered)}}}`,
|
|
607
645
|
);
|
|
608
646
|
}
|
|
609
647
|
|
|
@@ -616,15 +654,17 @@ function gapFix(gaps, fonts) {
|
|
|
616
654
|
return (
|
|
617
655
|
` fix: the built-in font is baked at ${sizeList(BUILTIN_SIZES)} — use one of those sizes, or ` +
|
|
618
656
|
`import a .ttf/.otf\n and bake your own (assets.config.js → fonts: ` +
|
|
619
|
-
`{<Family>: {sizes: ${sizeList(new Set(gaps.map(
|
|
657
|
+
`{<Family>: {sizes: ${sizeList(new Set(gaps.map(gap => gap.size)))}}})`
|
|
620
658
|
);
|
|
621
659
|
}
|
|
622
660
|
const perFamily = fonts
|
|
623
|
-
.map(
|
|
624
|
-
const missing = gaps
|
|
661
|
+
.map(font => {
|
|
662
|
+
const missing = gaps
|
|
663
|
+
.filter(gap => gap.family === font.family)
|
|
664
|
+
.map(gap => gap.size);
|
|
625
665
|
if (!missing.length) return null;
|
|
626
|
-
const wanted = new Set([...bakedSizes(
|
|
627
|
-
return `${configKey(
|
|
666
|
+
const wanted = new Set([...bakedSizes(font), ...missing]);
|
|
667
|
+
return `${configKey(font.family)}: {sizes: ${sizeList(wanted)}}`;
|
|
628
668
|
})
|
|
629
669
|
.filter(Boolean);
|
|
630
670
|
return ` fix: assets.config.js → fonts: {${perFamily.join(', ')}}`;
|