embedded-react 0.21.0 → 0.22.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 (99) hide show
  1. package/README.md +2 -2
  2. package/aot/compile.mjs +1 -1
  3. package/aot/compiler/alerts.mjs +604 -0
  4. package/aot/compiler/check.mjs +1 -1
  5. package/aot/compiler/collect.mjs +1 -1
  6. package/aot/compiler/elements/button.mjs +3 -3
  7. package/aot/compiler/elements/image-background.mjs +1 -1
  8. package/aot/compiler/elements/props.mjs +10 -6
  9. package/aot/compiler/elements/section-list.mjs +1 -1
  10. package/aot/compiler/expressions.mjs +29 -2
  11. package/aot/compiler/handlers.mjs +73 -16
  12. package/aot/compiler/keyboard-dismiss.mjs +99 -0
  13. package/aot/compiler/keyboard.mjs +1 -1
  14. package/aot/compiler/lower.mjs +12 -0
  15. package/aot/compiler/nodes.mjs +17 -9
  16. package/aot/compiler/out.mjs +4 -0
  17. package/aot/compiler/package/codegen.mjs +36 -2
  18. package/aot/compiler/package/format.mjs +5 -3
  19. package/aot/compiler/package/lower.mjs +53 -19
  20. package/aot/compiler/scroll-to.mjs +112 -0
  21. package/aot/compiler/style-text.mjs +2 -2
  22. package/aot/compiler/svg.mjs +6 -3
  23. package/aot/compiler/updaters.mjs +1 -0
  24. package/aot/style-map.mjs +44 -1
  25. package/assets/bake-font.mjs +187 -244
  26. package/assets/bake-image.mjs +47 -71
  27. package/assets/bake-svg.mjs +465 -533
  28. package/assets/build-builtin-font.mjs +14 -27
  29. package/assets/emit-c.mjs +124 -204
  30. package/assets/emit-container.mjs +129 -197
  31. package/assets/emit-pack.mjs +92 -136
  32. package/assets/font-config.mjs +19 -27
  33. package/assets/font-sizes.mjs +432 -507
  34. package/assets/glyph-coverage.mjs +170 -214
  35. package/assets/index.mjs +52 -97
  36. package/assets/rasterize.mjs +173 -194
  37. package/assets/svg-loader.mjs +42 -48
  38. package/build.mjs +84 -101
  39. package/cli.mjs +482 -621
  40. package/compile-bin.mjs +24 -36
  41. package/flash.mjs +297 -401
  42. package/hotreload/dev-device.mjs +229 -270
  43. package/hotreload/pack-app.mjs +118 -159
  44. package/hotreload/pack-split.mjs +144 -200
  45. package/hotreload/package-transfer.mjs +128 -131
  46. package/hotreload/protocol.mjs +118 -118
  47. package/hotreload/uploader.mjs +132 -159
  48. package/hotreload/watch-tree.mjs +56 -65
  49. package/pack-container.mjs +93 -123
  50. package/package.json +19 -16
  51. package/persist-transform.mjs +89 -105
  52. package/qjsc-wasm.mjs +47 -72
  53. package/sim/embedded-react.wasm +0 -0
  54. package/sim/index.html +1 -1
  55. package/sim-server.mjs +260 -354
  56. package/src/embedded-react/Alert.mjs +112 -0
  57. package/src/embedded-react/Animated.mjs +412 -0
  58. package/src/embedded-react/{AppRegistry.js → AppRegistry.mjs} +26 -25
  59. package/src/embedded-react/{Button.js → Button.mjs} +29 -53
  60. package/src/embedded-react/{Dimensions.js → Dimensions.mjs} +12 -17
  61. package/src/embedded-react/Easing.mjs +34 -0
  62. package/src/embedded-react/{FlatList.js → FlatList.mjs} +25 -42
  63. package/src/embedded-react/{ImageBackground.js → ImageBackground.mjs} +11 -22
  64. package/src/embedded-react/Keyboard.mjs +82 -0
  65. package/src/embedded-react/{LayoutAnimation.js → LayoutAnimation.mjs} +17 -28
  66. package/src/embedded-react/PanResponder.mjs +256 -0
  67. package/src/embedded-react/{Platform.js → Platform.mjs} +8 -9
  68. package/src/embedded-react/{Pressable.js → Pressable.mjs} +12 -25
  69. package/src/embedded-react/SectionList.mjs +103 -0
  70. package/src/embedded-react/{StyleSheet.js → StyleSheet.mjs} +16 -20
  71. package/src/embedded-react/{TouchableOpacity.js → TouchableOpacity.mjs} +47 -83
  72. package/src/embedded-react/background-image.mjs +321 -0
  73. package/src/embedded-react/{components.js → components.mjs} +1 -3
  74. package/src/embedded-react/{imperative.js → imperative.mjs} +27 -38
  75. package/src/embedded-react/index.d.ts +155 -2
  76. package/src/embedded-react/index.js +26 -19
  77. package/src/embedded-react/layout-anim-config.mjs +85 -0
  78. package/src/embedded-react/{list-child.js → list-child.mjs} +11 -20
  79. package/src/embedded-react/{named-colors.js → named-colors.mjs} +2 -6
  80. package/src/embedded-react/nativeModule.mjs +371 -0
  81. package/src/embedded-react/split-style.mjs +57 -0
  82. package/src/embedded-react/svg-ops.mjs +1075 -0
  83. package/src/embedded-react/{useHostValue.js → useHostValue.mjs} +2 -4
  84. package/src/embedded-react/{usePersistentState.js → usePersistentState.mjs} +34 -38
  85. package/src/embedded-react/{warn-props.js → warn-props.mjs} +32 -39
  86. package/src/host-config.mjs +512 -0
  87. package/src/{native-ui.js → native-ui.mjs} +1 -8
  88. package/src/props.mjs +341 -0
  89. package/src/{renderer.js → renderer.mjs} +30 -44
  90. package/src/embedded-react/Animated.js +0 -440
  91. package/src/embedded-react/Easing.js +0 -39
  92. package/src/embedded-react/PanResponder.js +0 -330
  93. package/src/embedded-react/SectionList.js +0 -146
  94. package/src/embedded-react/layout-anim-config.js +0 -101
  95. package/src/embedded-react/nativeModule.js +0 -462
  96. package/src/embedded-react/split-style.js +0 -58
  97. package/src/embedded-react/svg-ops.js +0 -1295
  98. package/src/host-config.js +0 -548
  99. package/src/props.js +0 -306
@@ -13,218 +13,197 @@
13
13
  * See the License for the specific language governing permissions and
14
14
  * limitations under the License.
15
15
  */
16
-
17
- // Pure-JS glyph rasterizer — fills an opentype.js Path into a grayscale coverage bitmap with no
18
- // native dependencies. Curves are flattened to polylines, then each pixel's coverage is computed by
19
- // scanline-filling at SS× supersampling using the nonzero winding rule (TrueType's convention).
20
- //
21
- // The output convention matches what the engine's text renderer expects (see draw_glyph /
22
- // draw_glyph_aa in engine/text/text_renderer.c): coordinates come from glyph.getPath(0, baseline,
23
- // pixelSize), so xOffset is the glyph's left bearing from the pen and yOffset is the distance from
24
- // the line box top (y = 0) down to the top of the glyph ink.
25
-
26
16
  const DEFAULT_SUPERSAMPLE = 4;
27
17
  const DEFAULT_CURVE_STEPS = 12;
28
18
  const COVERAGE_MAX = 255;
29
19
  const SAMPLE_CENTER = 0.5;
30
-
31
20
  /**
32
21
  * Flattens opentype path commands into closed contours of {x, y} points.
33
22
  *
34
- * @param {Array} commands opentype.js Path commands (M/L/Q/C/Z).
35
- * @param {number} steps Subdivision steps per Bézier curve.
36
- * @returns {Array<Array<{x:number,y:number}>>} One point array per contour.
23
+ * @param commands opentype.js Path commands (M/L/Q/C/Z).
24
+ * @param steps Subdivision steps per Bézier curve.
25
+ * @returns One point array per contour.
37
26
  */
38
27
  function flattenCommands(commands, steps) {
39
- const contours = [];
40
- let contour = null;
41
- let currentX = 0;
42
- let currentY = 0;
43
- let startX = 0;
44
- let startY = 0;
45
- for (const command of commands) {
46
- if (command.type === 'M') {
47
- if (contour && contour.length > 1) contours.push(contour);
48
- contour = [{x: command.x, y: command.y}];
49
- currentX = startX = command.x;
50
- currentY = startY = command.y;
51
- } else if (command.type === 'L') {
52
- contour.push({x: command.x, y: command.y});
53
- currentX = command.x;
54
- currentY = command.y;
55
- } else if (command.type === 'Q') {
56
- for (let step = 1; step <= steps; step++) {
57
- const t = step / steps;
58
- const oneMinusT = 1 - t;
59
- contour.push({
60
- x:
61
- oneMinusT * oneMinusT * currentX +
62
- 2 * oneMinusT * t * command.x1 +
63
- t * t * command.x,
64
- y:
65
- oneMinusT * oneMinusT * currentY +
66
- 2 * oneMinusT * t * command.y1 +
67
- t * t * command.y,
68
- });
69
- }
70
- currentX = command.x;
71
- currentY = command.y;
72
- } else if (command.type === 'C') {
73
- for (let step = 1; step <= steps; step++) {
74
- const t = step / steps;
75
- const oneMinusT = 1 - t;
76
- contour.push({
77
- x:
78
- oneMinusT * oneMinusT * oneMinusT * currentX +
79
- 3 * oneMinusT * oneMinusT * t * command.x1 +
80
- 3 * oneMinusT * t * t * command.x2 +
81
- t * t * t * command.x,
82
- y:
83
- oneMinusT * oneMinusT * oneMinusT * currentY +
84
- 3 * oneMinusT * oneMinusT * t * command.y1 +
85
- 3 * oneMinusT * t * t * command.y2 +
86
- t * t * t * command.y,
87
- });
88
- }
89
- currentX = command.x;
90
- currentY = command.y;
91
- } else if (command.type === 'Z') {
92
- if (contour) {
93
- contour.push({x: startX, y: startY}); // close the contour
94
- if (contour.length > 1) contours.push(contour);
95
- contour = null;
96
- }
28
+ const contours = [];
29
+ let contour = null;
30
+ let currentX = 0;
31
+ let currentY = 0;
32
+ let startX = 0;
33
+ let startY = 0;
34
+ for (const command of commands) {
35
+ if (command.type === 'M') {
36
+ if (contour && contour.length > 1)
37
+ contours.push(contour);
38
+ contour = [{ x: command.x, y: command.y }];
39
+ currentX = startX = command.x;
40
+ currentY = startY = command.y;
41
+ }
42
+ else if (command.type === 'L') {
43
+ contour.push({ x: command.x, y: command.y });
44
+ currentX = command.x;
45
+ currentY = command.y;
46
+ }
47
+ else if (command.type === 'Q') {
48
+ for (let step = 1; step <= steps; step++) {
49
+ const t = step / steps;
50
+ const oneMinusT = 1 - t;
51
+ contour.push({
52
+ x: oneMinusT * oneMinusT * currentX +
53
+ 2 * oneMinusT * t * command.x1 +
54
+ t * t * command.x,
55
+ y: oneMinusT * oneMinusT * currentY +
56
+ 2 * oneMinusT * t * command.y1 +
57
+ t * t * command.y,
58
+ });
59
+ }
60
+ currentX = command.x;
61
+ currentY = command.y;
62
+ }
63
+ else if (command.type === 'C') {
64
+ for (let step = 1; step <= steps; step++) {
65
+ const t = step / steps;
66
+ const oneMinusT = 1 - t;
67
+ contour.push({
68
+ x: oneMinusT * oneMinusT * oneMinusT * currentX +
69
+ 3 * oneMinusT * oneMinusT * t * command.x1 +
70
+ 3 * oneMinusT * t * t * command.x2 +
71
+ t * t * t * command.x,
72
+ y: oneMinusT * oneMinusT * oneMinusT * currentY +
73
+ 3 * oneMinusT * oneMinusT * t * command.y1 +
74
+ 3 * oneMinusT * t * t * command.y2 +
75
+ t * t * t * command.y,
76
+ });
77
+ }
78
+ currentX = command.x;
79
+ currentY = command.y;
80
+ }
81
+ else if (command.type === 'Z') {
82
+ if (contour) {
83
+ contour.push({ x: startX, y: startY }); // close the contour
84
+ if (contour.length > 1)
85
+ contours.push(contour);
86
+ contour = null;
87
+ }
88
+ }
97
89
  }
98
- }
99
- if (contour && contour.length > 1) contours.push(contour);
100
- return contours;
90
+ if (contour && contour.length > 1)
91
+ contours.push(contour);
92
+ return contours;
101
93
  }
102
-
103
94
  /**
104
95
  * Rasterizes a glyph path to an 8-bit coverage bitmap (0-255 per pixel).
105
96
  *
106
- * @param {object} path opentype.js Path (from glyph.getPath(0, baseline, pixelSize)).
107
- * @param {object} [opts]
108
- * @param {number} [opts.ss] Supersample factor per axis (coverage levels = ss*ss). Default 4.
109
- * @param {number} [opts.steps] Bézier subdivision steps. Default 12.
110
- * @returns {{width:number,height:number,xOffset:number,yOffset:number,coverage:Uint8Array}}
111
- * An empty (width 0, height 0) result for whitespace / glyphs with no ink.
97
+ * @param path opentype.js Path (from glyph.getPath(0, baseline, pixelSize)).
98
+ * @returns An empty (width 0, height 0) result for whitespace / glyphs with no ink.
112
99
  */
113
100
  export function rasterize(path, opts = {}) {
114
- const supersample = opts.ss || DEFAULT_SUPERSAMPLE;
115
- const steps = opts.steps || DEFAULT_CURVE_STEPS;
116
- const contours = flattenCommands(path.commands, steps);
117
-
118
- let minX = Infinity;
119
- let minY = Infinity;
120
- let maxX = -Infinity;
121
- let maxY = -Infinity;
122
- for (const contour of contours) {
123
- for (const point of contour) {
124
- if (point.x < minX) minX = point.x;
125
- if (point.x > maxX) maxX = point.x;
126
- if (point.y < minY) minY = point.y;
127
- if (point.y > maxY) maxY = point.y;
101
+ const supersample = opts.ss || DEFAULT_SUPERSAMPLE;
102
+ const steps = opts.steps || DEFAULT_CURVE_STEPS;
103
+ const contours = flattenCommands(path.commands, steps);
104
+ let minX = Infinity;
105
+ let minY = Infinity;
106
+ let maxX = -Infinity;
107
+ let maxY = -Infinity;
108
+ for (const contour of contours) {
109
+ for (const point of contour) {
110
+ if (point.x < minX)
111
+ minX = point.x;
112
+ if (point.x > maxX)
113
+ maxX = point.x;
114
+ if (point.y < minY)
115
+ minY = point.y;
116
+ if (point.y > maxY)
117
+ maxY = point.y;
118
+ }
128
119
  }
129
- }
130
- if (!Number.isFinite(minX)) {
131
- return {
132
- width: 0,
133
- height: 0,
134
- xOffset: 0,
135
- yOffset: 0,
136
- coverage: new Uint8Array(0),
137
- };
138
- }
139
-
140
- const originX = Math.floor(minX);
141
- const originY = Math.floor(minY);
142
- const width = Math.ceil(maxX) - originX;
143
- const height = Math.ceil(maxY) - originY;
144
- if (width <= 0 || height <= 0) {
145
- return {
146
- width: 0,
147
- height: 0,
148
- xOffset: 0,
149
- yOffset: 0,
150
- coverage: new Uint8Array(0),
151
- };
152
- }
153
-
154
- // Non-horizontal edges, normalized so yLow < yHigh; winding is the edge's winding contribution.
155
- const edges = [];
156
- for (const contour of contours) {
157
- for (let index = 0; index + 1 < contour.length; index++) {
158
- const from = contour[index];
159
- const to = contour[index + 1];
160
- if (from.y === to.y) continue;
161
- if (from.y < to.y)
162
- edges.push({
163
- yLow: from.y,
164
- yHigh: to.y,
165
- x: from.x,
166
- slope: (to.x - from.x) / (to.y - from.y),
167
- winding: 1,
168
- });
169
- else
170
- edges.push({
171
- yLow: to.y,
172
- yHigh: from.y,
173
- x: to.x,
174
- slope: (from.x - to.x) / (from.y - to.y),
175
- winding: -1,
176
- });
120
+ if (!Number.isFinite(minX)) {
121
+ return {
122
+ width: 0,
123
+ height: 0,
124
+ xOffset: 0,
125
+ yOffset: 0,
126
+ coverage: new Uint8Array(0),
127
+ };
177
128
  }
178
- }
179
-
180
- const counts = new Uint16Array(width * height); // covered subsamples per output pixel
181
- const subsampleWidth = width * supersample;
182
- for (
183
- let subsampleRow = 0;
184
- subsampleRow < height * supersample;
185
- subsampleRow++
186
- ) {
187
- const sampleY = originY + (subsampleRow + SAMPLE_CENTER) / supersample;
188
- const crossings = [];
189
- for (const edge of edges) {
190
- if (sampleY >= edge.yLow && sampleY < edge.yHigh) {
191
- crossings.push({
192
- x: edge.x + (sampleY - edge.yLow) * edge.slope,
193
- winding: edge.winding,
194
- });
195
- }
129
+ const originX = Math.floor(minX);
130
+ const originY = Math.floor(minY);
131
+ const width = Math.ceil(maxX) - originX;
132
+ const height = Math.ceil(maxY) - originY;
133
+ if (width <= 0 || height <= 0) {
134
+ return {
135
+ width: 0,
136
+ height: 0,
137
+ xOffset: 0,
138
+ yOffset: 0,
139
+ coverage: new Uint8Array(0),
140
+ };
196
141
  }
197
- if (crossings.length < 2) continue;
198
- crossings.sort((left, right) => left.x - right.x);
199
- const outputRow = (subsampleRow / supersample) | 0;
200
- let winding = 0;
201
- for (let index = 0; index + 1 < crossings.length; index++) {
202
- winding += crossings[index].winding;
203
- if (winding === 0) continue;
204
- // Interior span [crossings[index].x, crossings[index+1].x): tally the subsample columns whose
205
- // centre falls inside.
206
- let spanStart = Math.ceil(
207
- (crossings[index].x - originX) * supersample - SAMPLE_CENTER,
208
- );
209
- let spanEnd = Math.ceil(
210
- (crossings[index + 1].x - originX) * supersample - SAMPLE_CENTER,
211
- );
212
- if (spanStart < 0) spanStart = 0;
213
- if (spanEnd > subsampleWidth) spanEnd = subsampleWidth;
214
- for (
215
- let subsampleColumn = spanStart;
216
- subsampleColumn < spanEnd;
217
- subsampleColumn++
218
- ) {
219
- counts[outputRow * width + ((subsampleColumn / supersample) | 0)]++;
220
- }
142
+ // Non-horizontal edges, normalized so yLow < yHigh; winding is the edge's winding contribution.
143
+ const edges = [];
144
+ for (const contour of contours) {
145
+ for (let index = 0; index + 1 < contour.length; index++) {
146
+ const from = contour[index];
147
+ const to = contour[index + 1];
148
+ if (from.y === to.y)
149
+ continue;
150
+ if (from.y < to.y)
151
+ edges.push({
152
+ yLow: from.y,
153
+ yHigh: to.y,
154
+ x: from.x,
155
+ slope: (to.x - from.x) / (to.y - from.y),
156
+ winding: 1,
157
+ });
158
+ else
159
+ edges.push({
160
+ yLow: to.y,
161
+ yHigh: from.y,
162
+ x: to.x,
163
+ slope: (from.x - to.x) / (from.y - to.y),
164
+ winding: -1,
165
+ });
166
+ }
221
167
  }
222
- }
223
-
224
- const maxCoverage = supersample * supersample;
225
- const coverage = new Uint8Array(width * height);
226
- for (let index = 0; index < coverage.length; index++) {
227
- coverage[index] = Math.round((counts[index] * COVERAGE_MAX) / maxCoverage);
228
- }
229
- return {width, height, xOffset: originX, yOffset: originY, coverage};
168
+ const counts = new Uint16Array(width * height); // covered subsamples per output pixel
169
+ const subsampleWidth = width * supersample;
170
+ for (let subsampleRow = 0; subsampleRow < height * supersample; subsampleRow++) {
171
+ const sampleY = originY + (subsampleRow + SAMPLE_CENTER) / supersample;
172
+ const crossings = [];
173
+ for (const edge of edges) {
174
+ if (sampleY >= edge.yLow && sampleY < edge.yHigh) {
175
+ crossings.push({
176
+ x: edge.x + (sampleY - edge.yLow) * edge.slope,
177
+ winding: edge.winding,
178
+ });
179
+ }
180
+ }
181
+ if (crossings.length < 2)
182
+ continue;
183
+ crossings.sort((left, right) => left.x - right.x);
184
+ const outputRow = (subsampleRow / supersample) | 0;
185
+ let winding = 0;
186
+ for (let index = 0; index + 1 < crossings.length; index++) {
187
+ winding += crossings[index].winding;
188
+ if (winding === 0)
189
+ continue;
190
+ // Interior span [crossings[index].x, crossings[index+1].x): tally the subsample columns whose
191
+ // centre falls inside.
192
+ let spanStart = Math.ceil((crossings[index].x - originX) * supersample - SAMPLE_CENTER);
193
+ let spanEnd = Math.ceil((crossings[index + 1].x - originX) * supersample - SAMPLE_CENTER);
194
+ if (spanStart < 0)
195
+ spanStart = 0;
196
+ if (spanEnd > subsampleWidth)
197
+ spanEnd = subsampleWidth;
198
+ for (let subsampleColumn = spanStart; subsampleColumn < spanEnd; subsampleColumn++) {
199
+ counts[outputRow * width + ((subsampleColumn / supersample) | 0)]++;
200
+ }
201
+ }
202
+ }
203
+ const maxCoverage = supersample * supersample;
204
+ const coverage = new Uint8Array(width * height);
205
+ for (let index = 0; index < coverage.length; index++) {
206
+ coverage[index] = Math.round((counts[index] * COVERAGE_MAX) / maxCoverage);
207
+ }
208
+ return { width, height, xOffset: originX, yOffset: originY, coverage };
230
209
  }
@@ -13,7 +13,6 @@
13
13
  * See the License for the specific language governing permissions and
14
14
  * limitations under the License.
15
15
  */
16
-
17
16
  // esbuild loader for `import logo from './logo.svg'`. Two outcomes, decided at bake time:
18
17
  // - VECTOR: the SVG is fully representable as the engine's op-tape — bake it and INLINE a
19
18
  // {kind:'vector', ops, paints, gradients, width, height} artifact (small numeric data) into the bundle.
@@ -23,59 +22,54 @@
23
22
  // {kind:'raster', name, width, height} artifact. A <Svg source> renders kind:'vector' as a vector node
24
23
  // and kind:'raster' as an image node (host-config), so the fallback is transparent to the app.
25
24
  // Shared by every Flow A bundler so the .svg handling lives in one place.
26
- import {readFileSync} from 'node:fs';
27
- import {basename} from 'node:path';
28
- import {svgToVector, svgToRaster, writeRasterPng} from './bake-svg.mjs';
29
-
30
- const assetName = filePath => basename(filePath).replace(/\.[^.]+$/, '');
31
-
25
+ import { readFileSync } from 'node:fs';
26
+ import { basename } from 'node:path';
27
+ import { svgToVector, svgToRaster, writeRasterPng } from './bake-svg.mjs';
28
+ const assetName = (filePath) => basename(filePath).replace(/\.[^.]+$/, '');
32
29
  /**
33
30
  * Registers a `.svg` onLoad on an esbuild build.
34
31
  *
35
- * @param {import('esbuild').PluginBuild} build The esbuild build passed to a plugin's setup().
36
- * @param {(name:string, pngPath:string)=>void} [addRasterAsset] Registers a rasterized-SVG PNG into the
32
+ * @param build The esbuild build passed to a plugin's setup().
33
+ * @param addRasterAsset Registers a rasterized-SVG PNG into the
37
34
  * bundler's image collection (same mechanism as `import x from './x.png'`). When omitted, an SVG that
38
35
  * needs raster fallback is baked as a (lossy) vector and a warning notes the dropped content.
39
36
  */
40
37
  export function registerSvgVectorLoader(build, addRasterAsset) {
41
- build.onLoad({filter: /\.svg$/i}, async args => {
42
- try {
43
- const svg = readFileSync(args.path, 'utf8');
44
- const {dropped = [], ...vector} = await svgToVector(svg);
45
- if (dropped.length) {
46
- const features = dropped.join(', ');
47
- if (addRasterAsset) {
48
- console.warn(
49
- `embedded-react: ${basename(args.path)} uses unsupported SVG feature(s) [${features}] — rasterizing ` +
50
- `it as a fallback image. (Raster loses scalability and costs RAM; simplify the SVG to keep it a ` +
51
- `live vector.)`,
52
- );
53
- const {width, height, png} = await svgToRaster(svg);
54
- const name = assetName(args.path);
55
- addRasterAsset(name, writeRasterPng(name, png));
56
- return {
57
- contents: `module.exports = ${JSON.stringify({kind: 'raster', name, width, height})};`,
58
- loader: 'js',
59
- };
38
+ build.onLoad({ filter: /\.svg$/i }, async (args) => {
39
+ try {
40
+ const svg = readFileSync(args.path, 'utf8');
41
+ const { dropped = [], ...vector } = await svgToVector(svg);
42
+ if (dropped.length) {
43
+ const features = dropped.join(', ');
44
+ if (addRasterAsset) {
45
+ console.warn(`embedded-react: ${basename(args.path)} uses unsupported SVG feature(s) [${features}] — rasterizing ` +
46
+ `it as a fallback image. (Raster loses scalability and costs RAM; simplify the SVG to keep it a ` +
47
+ `live vector.)`);
48
+ const { width, height, png } = await svgToRaster(svg);
49
+ const name = assetName(args.path);
50
+ addRasterAsset(name, writeRasterPng(name, png));
51
+ return {
52
+ contents: `module.exports = ${JSON.stringify({ kind: 'raster', name, width, height })};`,
53
+ loader: 'js',
54
+ };
55
+ }
56
+ console.warn(`embedded-react: ${basename(args.path)} uses unsupported SVG feature(s) [${features}] and this build has ` +
57
+ `no raster fallback — that content will NOT render. Simplify the SVG, or import it via a bundler that ` +
58
+ `supports the image pipeline.`);
59
+ }
60
+ return {
61
+ contents: `module.exports = ${JSON.stringify({ kind: 'vector', ...vector })};`,
62
+ loader: 'js',
63
+ };
60
64
  }
61
- console.warn(
62
- `embedded-react: ${basename(args.path)} uses unsupported SVG feature(s) [${features}] and this build has ` +
63
- `no raster fallback — that content will NOT render. Simplify the SVG, or import it via a bundler that ` +
64
- `supports the image pipeline.`,
65
- );
66
- }
67
- return {
68
- contents: `module.exports = ${JSON.stringify({kind: 'vector', ...vector})};`,
69
- loader: 'js',
70
- };
71
- } catch (error) {
72
- return {
73
- errors: [
74
- {
75
- text: `embedded-react: failed to bake SVG ${args.path}: ${error.message}`,
76
- },
77
- ],
78
- };
79
- }
80
- });
65
+ catch (error) {
66
+ return {
67
+ errors: [
68
+ {
69
+ text: `embedded-react: failed to bake SVG ${args.path}: ${error.message}`,
70
+ },
71
+ ],
72
+ };
73
+ }
74
+ });
81
75
  }