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.
- package/README.md +2 -2
- package/aot/compile.mjs +1 -1
- package/aot/compiler/alerts.mjs +604 -0
- package/aot/compiler/check.mjs +1 -1
- package/aot/compiler/collect.mjs +1 -1
- package/aot/compiler/elements/button.mjs +3 -3
- package/aot/compiler/elements/image-background.mjs +1 -1
- package/aot/compiler/elements/props.mjs +10 -6
- package/aot/compiler/elements/section-list.mjs +1 -1
- package/aot/compiler/expressions.mjs +29 -2
- package/aot/compiler/handlers.mjs +73 -16
- package/aot/compiler/keyboard-dismiss.mjs +99 -0
- package/aot/compiler/keyboard.mjs +1 -1
- package/aot/compiler/lower.mjs +12 -0
- package/aot/compiler/nodes.mjs +17 -9
- package/aot/compiler/out.mjs +4 -0
- package/aot/compiler/package/codegen.mjs +36 -2
- package/aot/compiler/package/format.mjs +5 -3
- package/aot/compiler/package/lower.mjs +53 -19
- package/aot/compiler/scroll-to.mjs +112 -0
- package/aot/compiler/style-text.mjs +2 -2
- package/aot/compiler/svg.mjs +6 -3
- package/aot/compiler/updaters.mjs +1 -0
- package/aot/style-map.mjs +44 -1
- package/assets/bake-font.mjs +187 -244
- package/assets/bake-image.mjs +47 -71
- package/assets/bake-svg.mjs +465 -533
- package/assets/build-builtin-font.mjs +14 -27
- package/assets/emit-c.mjs +124 -204
- package/assets/emit-container.mjs +129 -197
- package/assets/emit-pack.mjs +92 -136
- package/assets/font-config.mjs +19 -27
- package/assets/font-sizes.mjs +432 -507
- package/assets/glyph-coverage.mjs +170 -214
- package/assets/index.mjs +52 -97
- package/assets/rasterize.mjs +173 -194
- package/assets/svg-loader.mjs +42 -48
- package/build.mjs +84 -101
- package/cli.mjs +482 -621
- package/compile-bin.mjs +24 -36
- package/flash.mjs +297 -401
- package/hotreload/dev-device.mjs +229 -270
- package/hotreload/pack-app.mjs +118 -159
- package/hotreload/pack-split.mjs +144 -200
- package/hotreload/package-transfer.mjs +128 -131
- package/hotreload/protocol.mjs +118 -118
- package/hotreload/uploader.mjs +132 -159
- package/hotreload/watch-tree.mjs +56 -65
- package/pack-container.mjs +93 -123
- package/package.json +19 -16
- package/persist-transform.mjs +89 -105
- package/qjsc-wasm.mjs +47 -72
- package/sim/embedded-react.wasm +0 -0
- package/sim/index.html +1 -1
- package/sim-server.mjs +260 -354
- package/src/embedded-react/Alert.mjs +112 -0
- package/src/embedded-react/Animated.mjs +412 -0
- package/src/embedded-react/{AppRegistry.js → AppRegistry.mjs} +26 -25
- package/src/embedded-react/{Button.js → Button.mjs} +29 -53
- package/src/embedded-react/{Dimensions.js → Dimensions.mjs} +12 -17
- package/src/embedded-react/Easing.mjs +34 -0
- package/src/embedded-react/{FlatList.js → FlatList.mjs} +25 -42
- package/src/embedded-react/{ImageBackground.js → ImageBackground.mjs} +11 -22
- package/src/embedded-react/Keyboard.mjs +82 -0
- package/src/embedded-react/{LayoutAnimation.js → LayoutAnimation.mjs} +17 -28
- package/src/embedded-react/PanResponder.mjs +256 -0
- package/src/embedded-react/{Platform.js → Platform.mjs} +8 -9
- package/src/embedded-react/{Pressable.js → Pressable.mjs} +12 -25
- package/src/embedded-react/SectionList.mjs +103 -0
- package/src/embedded-react/{StyleSheet.js → StyleSheet.mjs} +16 -20
- package/src/embedded-react/{TouchableOpacity.js → TouchableOpacity.mjs} +47 -83
- package/src/embedded-react/background-image.mjs +321 -0
- package/src/embedded-react/{components.js → components.mjs} +1 -3
- package/src/embedded-react/{imperative.js → imperative.mjs} +27 -38
- package/src/embedded-react/index.d.ts +155 -2
- package/src/embedded-react/index.js +26 -19
- package/src/embedded-react/layout-anim-config.mjs +85 -0
- package/src/embedded-react/{list-child.js → list-child.mjs} +11 -20
- package/src/embedded-react/{named-colors.js → named-colors.mjs} +2 -6
- package/src/embedded-react/nativeModule.mjs +371 -0
- package/src/embedded-react/split-style.mjs +57 -0
- package/src/embedded-react/svg-ops.mjs +1075 -0
- package/src/embedded-react/{useHostValue.js → useHostValue.mjs} +2 -4
- package/src/embedded-react/{usePersistentState.js → usePersistentState.mjs} +34 -38
- package/src/embedded-react/{warn-props.js → warn-props.mjs} +32 -39
- package/src/host-config.mjs +512 -0
- package/src/{native-ui.js → native-ui.mjs} +1 -8
- package/src/props.mjs +341 -0
- package/src/{renderer.js → renderer.mjs} +30 -44
- package/src/embedded-react/Animated.js +0 -440
- package/src/embedded-react/Easing.js +0 -39
- package/src/embedded-react/PanResponder.js +0 -330
- package/src/embedded-react/SectionList.js +0 -146
- package/src/embedded-react/layout-anim-config.js +0 -101
- package/src/embedded-react/nativeModule.js +0 -462
- package/src/embedded-react/split-style.js +0 -58
- package/src/embedded-react/svg-ops.js +0 -1295
- package/src/host-config.js +0 -548
- package/src/props.js +0 -306
package/assets/rasterize.mjs
CHANGED
|
@@ -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
|
|
35
|
-
* @param
|
|
36
|
-
* @returns
|
|
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
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
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
|
-
|
|
100
|
-
|
|
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
|
|
107
|
-
* @
|
|
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
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
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
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
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
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
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
|
-
|
|
198
|
-
|
|
199
|
-
const
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
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
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
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
|
}
|
package/assets/svg-loader.mjs
CHANGED
|
@@ -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
|
|
36
|
-
* @param
|
|
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
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
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
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
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
|
}
|