embedded-react 0.20.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/NOTICE +5 -0
- package/README.md +3 -2
- package/aot/app-file-loader.mjs +116 -0
- package/aot/compile.mjs +17 -47
- package/aot/compiler/alerts.mjs +604 -0
- package/aot/compiler/animations.mjs +299 -57
- package/aot/compiler/boot.mjs +27 -2
- package/aot/compiler/check.mjs +62 -10
- package/aot/compiler/collect.mjs +517 -137
- package/aot/compiler/contexts.mjs +391 -0
- package/aot/compiler/control-flow.mjs +276 -117
- package/aot/compiler/custom-hooks.mjs +686 -0
- package/aot/compiler/elements/button.mjs +182 -0
- package/aot/compiler/elements/image-background.mjs +76 -0
- package/aot/compiler/elements/image.mjs +2 -1
- package/aot/compiler/elements/props.mjs +47 -8
- package/aot/compiler/elements/rewrite.mjs +118 -0
- package/aot/compiler/elements/section-list.mjs +370 -0
- package/aot/compiler/expressions.mjs +1378 -97
- package/aot/compiler/handlers.mjs +1807 -354
- package/aot/compiler/helper-values.mjs +312 -0
- package/aot/compiler/ir/nodes.mjs +40 -1
- package/aot/compiler/ir/reads.mjs +206 -5
- package/aot/compiler/ir/text.mjs +5 -1
- package/aot/compiler/ir/types.mjs +12 -0
- package/aot/compiler/kept-states.mjs +177 -0
- package/aot/compiler/keyboard-dismiss.mjs +99 -0
- package/aot/compiler/keyboard.mjs +1 -1
- 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 +203 -53
- 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 +45 -31
- package/aot/compiler/options.mjs +3 -2
- package/aot/compiler/out.mjs +18 -1
- package/aot/compiler/package/animations.mjs +133 -23
- package/aot/compiler/package/assemble.mjs +2 -4
- package/aot/compiler/package/bytecode.mjs +37 -8
- package/aot/compiler/package/checks.mjs +4 -0
- package/aot/compiler/package/codegen.mjs +906 -33
- package/aot/compiler/package/enums.mjs +10 -0
- package/aot/compiler/package/format.mjs +16 -3
- package/aot/compiler/package/lower.mjs +707 -176
- package/aot/compiler/package/string-caps.mjs +515 -0
- package/aot/compiler/package/tables.mjs +24 -0
- package/aot/compiler/package/writer.mjs +22 -0
- package/aot/compiler/parse.mjs +3 -1
- 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/scroll-to.mjs +112 -0
- package/aot/compiler/static-eval.mjs +315 -7
- package/aot/compiler/style-text.mjs +20 -5
- package/aot/compiler/svg.mjs +13 -7
- 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 +321 -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/style-map.mjs +94 -3
- 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 +85 -94
- package/cli.mjs +490 -586
- 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 +146 -213
- 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 +94 -116
- 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.mjs +32 -0
- 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 +238 -5
- package/src/embedded-react/index.js +26 -17
- 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 -148
- package/src/embedded-react/layout-anim-config.js +0 -101
- 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/sim-server.mjs
CHANGED
|
@@ -13,385 +13,291 @@
|
|
|
13
13
|
* See the License for the specific language governing permissions and
|
|
14
14
|
* limitations under the License.
|
|
15
15
|
*/
|
|
16
|
-
|
|
17
|
-
// sim-server.mjs — the shared WASM-simulator bundler + dev server.
|
|
16
|
+
// sim-server.mts — the shared WASM-simulator bundler + dev server.
|
|
18
17
|
//
|
|
19
18
|
// esbuild-bundles a JSX app → app.js, bakes its imported images/fonts → assets.pack, and either (a) serves
|
|
20
19
|
// the prebuilt embedded-react.{js,wasm} host page with live hot reload (runDevServer), or (b) produces those
|
|
21
20
|
// files once for a static export (buildApp). useState survives a hot reload via the Babel persist transform.
|
|
22
21
|
//
|
|
23
|
-
// Used by the consumer CLI (cli.
|
|
24
|
-
// (tools/web-sim/dev.
|
|
25
|
-
|
|
26
|
-
import {
|
|
27
|
-
import {
|
|
28
|
-
import {
|
|
29
|
-
import {
|
|
30
|
-
import {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
const HERE = dirname(
|
|
34
|
-
new URL(import.meta.url).pathname.replace(/^\/([A-Za-z]:)/, '$1'),
|
|
35
|
-
);
|
|
22
|
+
// Used by the consumer CLI (cli.mts → `npx embedded-react dev` / `export`) and the repo dev loop
|
|
23
|
+
// (tools/web-sim/dev.mts). The only differences are paths, passed in here.
|
|
24
|
+
import { createServer } from 'node:http';
|
|
25
|
+
import { readFile } from 'node:fs/promises';
|
|
26
|
+
import { existsSync, readFileSync, statSync } from 'node:fs';
|
|
27
|
+
import { createRequire } from 'node:module';
|
|
28
|
+
import { pathToFileURL } from 'node:url';
|
|
29
|
+
import { basename, dirname, extname, relative, resolve } from 'node:path';
|
|
30
|
+
const HERE = dirname(new URL(import.meta.url).pathname.replace(/^\/([A-Za-z]:)/, '$1'));
|
|
36
31
|
const require = createRequire(import.meta.url);
|
|
37
32
|
const esbuild = require('esbuild');
|
|
38
|
-
const {bakeAssetPack} = await import(
|
|
39
|
-
|
|
40
|
-
);
|
|
41
|
-
const {
|
|
42
|
-
|
|
43
|
-
);
|
|
44
|
-
const {
|
|
45
|
-
|
|
46
|
-
);
|
|
47
|
-
const {textSignature, warnMissingGlyphs} = await import(
|
|
48
|
-
pathToFileURL(resolve(HERE, 'assets/glyph-coverage.mjs')).href
|
|
49
|
-
);
|
|
50
|
-
const {registerSvgVectorLoader} = await import(
|
|
51
|
-
pathToFileURL(resolve(HERE, 'assets/svg-loader.mjs')).href
|
|
52
|
-
);
|
|
53
|
-
const {transformPersist, shouldPersist} = await import(
|
|
54
|
-
pathToFileURL(resolve(HERE, 'persist-transform.mjs')).href
|
|
55
|
-
);
|
|
56
|
-
|
|
57
|
-
const REACT_PIN = Object.fromEntries(
|
|
58
|
-
['react', 'react-reconciler', 'scheduler']
|
|
33
|
+
const { bakeAssetPack } = await import('./assets/index.mjs');
|
|
34
|
+
const { resolveFontJobs } = await import('./assets/font-config.mjs');
|
|
35
|
+
const { analyzeFontSizes, fontSizeSignature, warnFontSizes } = await import('./assets/font-sizes.mjs');
|
|
36
|
+
const { textSignature, warnMissingGlyphs } = await import('./assets/glyph-coverage.mjs');
|
|
37
|
+
const { registerSvgVectorLoader } = await import('./assets/svg-loader.mjs');
|
|
38
|
+
const { transformPersist, shouldPersist } = await import('./persist-transform.mjs');
|
|
39
|
+
const { loadAppFile, loadDependencyFile } = await import('./aot/app-file-loader.mjs');
|
|
40
|
+
const REACT_PIN = Object.fromEntries(['react', 'react-reconciler', 'scheduler']
|
|
59
41
|
.map(packageName => [
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
.filter(([, pinPath]) => existsSync(pinPath))
|
|
64
|
-
);
|
|
65
|
-
|
|
42
|
+
packageName,
|
|
43
|
+
resolve(HERE, 'node_modules', packageName),
|
|
44
|
+
])
|
|
45
|
+
.filter(([, pinPath]) => existsSync(pinPath)));
|
|
66
46
|
const MIME = {
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
47
|
+
'.html': 'text/html; charset=utf-8',
|
|
48
|
+
'.js': 'text/javascript; charset=utf-8',
|
|
49
|
+
'.wasm': 'application/wasm',
|
|
50
|
+
'.pack': 'application/octet-stream',
|
|
71
51
|
};
|
|
72
|
-
const assetName = filePath => basename(filePath).replace(/\.[^.]+$/, '');
|
|
73
|
-
const mtime = filePath => {
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
52
|
+
const assetName = (filePath) => basename(filePath).replace(/\.[^.]+$/, '');
|
|
53
|
+
const mtime = (filePath) => {
|
|
54
|
+
try {
|
|
55
|
+
return statSync(filePath).mtimeMs;
|
|
56
|
+
}
|
|
57
|
+
catch {
|
|
58
|
+
return 0;
|
|
59
|
+
}
|
|
79
60
|
};
|
|
80
|
-
|
|
81
61
|
/**
|
|
82
62
|
* Build the esbuild config + asset baker for one app bundle. Owns its own asset-discovery state, so it is
|
|
83
63
|
* safe to use for either a one-shot build or a watching context.
|
|
84
|
-
*
|
|
85
|
-
* @param {object} o
|
|
86
|
-
* @param {string} o.entry, o.projectRoot, o.libSrc, o.outDir
|
|
87
|
-
* @param {string[]} o.nodePaths
|
|
88
|
-
* @param {boolean} [o.persist] Apply the useState→persist transform (dev hot reload). Default true.
|
|
89
|
-
* @param {() => void} [o.onRebuilt] Called after each successful build + asset bake (e.g. broadcast reload).
|
|
90
|
-
* @returns {{ options: object, bundlePath: string, packPath: string }}
|
|
91
64
|
*/
|
|
92
|
-
function createBundle({
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
.
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
return;
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
try {
|
|
161
|
-
const summary = bakeAssetPack({
|
|
162
|
-
images: imageJobs,
|
|
163
|
-
fonts: fontJobs,
|
|
164
|
-
outPath: packPath,
|
|
165
|
-
source: bundleSource,
|
|
166
|
-
usedSizes,
|
|
167
|
-
});
|
|
168
|
-
console.log(
|
|
169
|
-
` assets → ${summary.images} image(s), ${summary.fonts} font size(s), ${summary.bytes} B`,
|
|
170
|
-
);
|
|
171
|
-
} catch (error) {
|
|
172
|
-
console.error(` asset bake failed: ${error.message}`);
|
|
65
|
+
function createBundle({ entry, projectRoot, libSrc, nodePaths, outDir, persist = true, onRebuilt, }) {
|
|
66
|
+
const bundlePath = resolve(outDir, 'app.js');
|
|
67
|
+
const packPath = resolve(outDir, 'assets.pack');
|
|
68
|
+
const projectRootNormalized = projectRoot.replace(/\\/g, '/');
|
|
69
|
+
const images = new Map();
|
|
70
|
+
const fonts = new Map();
|
|
71
|
+
let lastAssetSignature = null;
|
|
72
|
+
async function bakePack() {
|
|
73
|
+
const bundleSource = readFileSync(bundlePath, 'utf8');
|
|
74
|
+
const usedSizes = analyzeFontSizes(bundleSource);
|
|
75
|
+
let assetsConfig = {};
|
|
76
|
+
const assetsConfigPath = resolve(projectRoot, 'assets.config.js');
|
|
77
|
+
if (existsSync(assetsConfigPath))
|
|
78
|
+
assetsConfig =
|
|
79
|
+
(await import(`${pathToFileURL(assetsConfigPath).href}?t=${mtime(assetsConfigPath)}`)).default || {};
|
|
80
|
+
const fontConfig = assetsConfig.fonts || {};
|
|
81
|
+
const imageConfig = assetsConfig.images || {};
|
|
82
|
+
const fontJobs = resolveFontJobs(fonts, fontConfig, usedSizes.sizes);
|
|
83
|
+
const imageJobs = [...images.entries()].map(([name, path]) => ({
|
|
84
|
+
path,
|
|
85
|
+
name,
|
|
86
|
+
format: imageConfig[name]?.format,
|
|
87
|
+
}));
|
|
88
|
+
// The app's text is an asset input too: new characters and new font sizes must both be
|
|
89
|
+
// re-checked against the bake even when no font or image file changed.
|
|
90
|
+
const assetSignature = JSON.stringify({
|
|
91
|
+
text: textSignature(bundleSource),
|
|
92
|
+
fontSizes: fontSizeSignature(usedSizes),
|
|
93
|
+
images: imageJobs
|
|
94
|
+
.map(job => [job.name, job.path, mtime(job.path), job.format])
|
|
95
|
+
.sort(),
|
|
96
|
+
fonts: fontJobs
|
|
97
|
+
.map(job => [
|
|
98
|
+
job.family,
|
|
99
|
+
job.path,
|
|
100
|
+
mtime(job.path),
|
|
101
|
+
job.sizes,
|
|
102
|
+
job.bpp,
|
|
103
|
+
job.glyphs,
|
|
104
|
+
job.extraGlyphs,
|
|
105
|
+
])
|
|
106
|
+
.sort(),
|
|
107
|
+
});
|
|
108
|
+
if (assetSignature === lastAssetSignature)
|
|
109
|
+
return;
|
|
110
|
+
lastAssetSignature = assetSignature;
|
|
111
|
+
if (!imageJobs.length && !fontJobs.length) {
|
|
112
|
+
// Built-in font only → no pack, but its glyph and size coverage still bind.
|
|
113
|
+
warnMissingGlyphs({ source: bundleSource });
|
|
114
|
+
warnFontSizes({ used: usedSizes });
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
try {
|
|
118
|
+
const summary = bakeAssetPack({
|
|
119
|
+
images: imageJobs,
|
|
120
|
+
fonts: fontJobs,
|
|
121
|
+
outPath: packPath,
|
|
122
|
+
source: bundleSource,
|
|
123
|
+
usedSizes,
|
|
124
|
+
});
|
|
125
|
+
console.log(` assets → ${summary.images} image(s), ${summary.fonts} font size(s), ${summary.bytes} B`);
|
|
126
|
+
}
|
|
127
|
+
catch (error) {
|
|
128
|
+
console.error(` asset bake failed: ${error.message}`);
|
|
129
|
+
}
|
|
173
130
|
}
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
131
|
+
const options = {
|
|
132
|
+
entryPoints: [entry],
|
|
133
|
+
bundle: true,
|
|
134
|
+
format: 'iife',
|
|
135
|
+
outfile: bundlePath,
|
|
136
|
+
platform: 'neutral',
|
|
137
|
+
target: 'es2020',
|
|
138
|
+
jsx: 'automatic',
|
|
139
|
+
alias: { 'embedded-react': libSrc, ...REACT_PIN },
|
|
140
|
+
nodePaths,
|
|
141
|
+
define: { 'process.env.NODE_ENV': '"production"' },
|
|
142
|
+
legalComments: 'none',
|
|
143
|
+
logLevel: 'silent',
|
|
144
|
+
plugins: [
|
|
145
|
+
{
|
|
146
|
+
name: 'embedded-react-websim',
|
|
147
|
+
setup(pluginBuild) {
|
|
148
|
+
pluginBuild.onStart(() => {
|
|
149
|
+
images.clear();
|
|
150
|
+
fonts.clear();
|
|
151
|
+
});
|
|
152
|
+
pluginBuild.onLoad({ filter: /\.(jsx?|tsx?)$/ }, async (loadArgs) => {
|
|
153
|
+
// Only the app's own source — never dependencies (see shouldPersist: excluding node_modules is what
|
|
154
|
+
// stops the library's usePersistentState being rewritten to call itself). Native modules get their
|
|
155
|
+
// signatures and mocks, and with persist on, useState persists. A package's native modules get theirs too.
|
|
156
|
+
if (!shouldPersist(loadArgs.path, projectRootNormalized))
|
|
157
|
+
return loadDependencyFile(loadArgs.path, { mocks: true });
|
|
158
|
+
try {
|
|
159
|
+
return await loadAppFile(loadArgs.path, persist
|
|
160
|
+
? (source) => transformPersist(source, relative(projectRoot, loadArgs.path).replace(/\\/g, '/'))
|
|
161
|
+
: undefined, { mocks: true });
|
|
162
|
+
}
|
|
163
|
+
catch (error) {
|
|
164
|
+
return { errors: [{ text: `persist transform: ${error.message}` }] };
|
|
165
|
+
}
|
|
166
|
+
});
|
|
167
|
+
pluginBuild.onLoad({ filter: /\.(png|jpe?g|webp|gif|bmp)$/i }, loadArgs => {
|
|
168
|
+
images.set(assetName(loadArgs.path), loadArgs.path);
|
|
169
|
+
return {
|
|
170
|
+
contents: `module.exports = ${JSON.stringify(assetName(loadArgs.path))};`,
|
|
171
|
+
loader: 'js',
|
|
172
|
+
};
|
|
173
|
+
});
|
|
174
|
+
pluginBuild.onLoad({ filter: /\.(ttf|otf)$/i }, loadArgs => {
|
|
175
|
+
fonts.set(assetName(loadArgs.path), loadArgs.path);
|
|
176
|
+
return {
|
|
177
|
+
contents: `module.exports = ${JSON.stringify(assetName(loadArgs.path))};`,
|
|
178
|
+
loader: 'js',
|
|
179
|
+
};
|
|
180
|
+
});
|
|
181
|
+
registerSvgVectorLoader(pluginBuild, (name, svgPath) => images.set(name, svgPath)); // raster-fallback SVGs join the image pack
|
|
182
|
+
pluginBuild.onEnd(async (result) => {
|
|
183
|
+
if (result.errors.length) {
|
|
184
|
+
console.error(`✗ build failed (${result.errors.length} error(s))`);
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
await bakePack();
|
|
188
|
+
if (onRebuilt)
|
|
189
|
+
onRebuilt();
|
|
190
|
+
});
|
|
191
|
+
},
|
|
232
192
|
},
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
return {
|
|
237
|
-
contents: `module.exports = ${JSON.stringify(assetName(loadArgs.path))};`,
|
|
238
|
-
loader: 'js',
|
|
239
|
-
};
|
|
240
|
-
});
|
|
241
|
-
registerSvgVectorLoader(pluginBuild, (name, svgPath) =>
|
|
242
|
-
images.set(name, svgPath),
|
|
243
|
-
); // raster-fallback SVGs join the image pack
|
|
244
|
-
pluginBuild.onEnd(async result => {
|
|
245
|
-
if (result.errors.length) {
|
|
246
|
-
console.error(
|
|
247
|
-
`✗ build failed (${result.errors.length} error(s))`,
|
|
248
|
-
);
|
|
249
|
-
return;
|
|
250
|
-
}
|
|
251
|
-
await bakePack();
|
|
252
|
-
if (onRebuilt) onRebuilt();
|
|
253
|
-
});
|
|
254
|
-
},
|
|
255
|
-
},
|
|
256
|
-
],
|
|
257
|
-
};
|
|
258
|
-
|
|
259
|
-
return {options, bundlePath, packPath};
|
|
193
|
+
],
|
|
194
|
+
};
|
|
195
|
+
return { options, bundlePath, packPath };
|
|
260
196
|
}
|
|
261
|
-
|
|
262
197
|
/**
|
|
263
198
|
* One-shot bundle + asset bake into outDir (app.js [+ assets.pack]). For the static export.
|
|
264
199
|
* Persist transform is off (a static page has no hot reload to preserve state across).
|
|
265
200
|
*/
|
|
266
|
-
export async function buildApp({
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
libSrc,
|
|
277
|
-
nodePaths,
|
|
278
|
-
outDir,
|
|
279
|
-
persist: false,
|
|
280
|
-
});
|
|
281
|
-
await esbuild.build(options);
|
|
201
|
+
export async function buildApp({ entry, projectRoot, libSrc, nodePaths, outDir, }) {
|
|
202
|
+
const { options } = createBundle({
|
|
203
|
+
entry,
|
|
204
|
+
projectRoot,
|
|
205
|
+
libSrc,
|
|
206
|
+
nodePaths,
|
|
207
|
+
outDir,
|
|
208
|
+
persist: false,
|
|
209
|
+
});
|
|
210
|
+
await esbuild.build(options);
|
|
282
211
|
}
|
|
283
|
-
|
|
284
212
|
/**
|
|
285
213
|
* Run the simulator dev server: watch + bundle + bake + serve + hot reload.
|
|
286
|
-
*
|
|
287
|
-
* @param {object} o
|
|
288
|
-
* @param {string} o.entry, o.projectRoot, o.libSrc, o.indexHtml, o.simDir, o.outDir, o.label
|
|
289
|
-
* @param {string[]} o.nodePaths
|
|
290
|
-
* @param {number} o.port
|
|
291
214
|
*/
|
|
292
|
-
export async function runDevServer({
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
'cache-control': 'no-store',
|
|
365
|
-
})
|
|
366
|
-
.end(html);
|
|
367
|
-
} catch {
|
|
215
|
+
export async function runDevServer({ entry, projectRoot, libSrc, nodePaths, indexHtml, simDir, outDir, port, label, }) {
|
|
216
|
+
const clients = new Set();
|
|
217
|
+
const broadcastReload = () => {
|
|
218
|
+
for (const response of clients)
|
|
219
|
+
response.write('data: reload\n\n');
|
|
220
|
+
};
|
|
221
|
+
const { options } = createBundle({
|
|
222
|
+
entry,
|
|
223
|
+
projectRoot,
|
|
224
|
+
libSrc,
|
|
225
|
+
nodePaths,
|
|
226
|
+
outDir,
|
|
227
|
+
persist: true,
|
|
228
|
+
onRebuilt: () => {
|
|
229
|
+
console.log('↻ rebuilt → reloading');
|
|
230
|
+
broadcastReload();
|
|
231
|
+
},
|
|
232
|
+
});
|
|
233
|
+
const buildContext = await esbuild.context(options);
|
|
234
|
+
await buildContext.rebuild();
|
|
235
|
+
await buildContext.watch();
|
|
236
|
+
// Serve the host page; embedded-react.{js,wasm} from simDir, the freshly bundled app.js/assets.pack from
|
|
237
|
+
// outDir. The page references ./public/<name>. window.__erHot is injected so the page connects to /reload.
|
|
238
|
+
const send = async (response, file) => {
|
|
239
|
+
try {
|
|
240
|
+
const body = await readFile(file);
|
|
241
|
+
response
|
|
242
|
+
.writeHead(200, {
|
|
243
|
+
'content-type': MIME[extname(file)] || 'application/octet-stream',
|
|
244
|
+
'cache-control': 'no-store',
|
|
245
|
+
})
|
|
246
|
+
.end(body);
|
|
247
|
+
}
|
|
248
|
+
catch {
|
|
249
|
+
response.writeHead(404).end('not found');
|
|
250
|
+
}
|
|
251
|
+
};
|
|
252
|
+
const server = createServer(async (request, response) => {
|
|
253
|
+
const path = decodeURIComponent(new URL(request.url, 'http://localhost').pathname);
|
|
254
|
+
if (path === '/reload') {
|
|
255
|
+
response.writeHead(200, {
|
|
256
|
+
'content-type': 'text/event-stream',
|
|
257
|
+
'cache-control': 'no-cache',
|
|
258
|
+
connection: 'keep-alive',
|
|
259
|
+
});
|
|
260
|
+
response.write('retry: 1000\n\n');
|
|
261
|
+
clients.add(response);
|
|
262
|
+
request.on('close', () => clients.delete(response));
|
|
263
|
+
return;
|
|
264
|
+
}
|
|
265
|
+
if (path === '/' || path === '/index.html') {
|
|
266
|
+
try {
|
|
267
|
+
const html = (await readFile(indexHtml, 'utf8')).replace('</head>', '<script>window.__erHot=true</script></head>');
|
|
268
|
+
response
|
|
269
|
+
.writeHead(200, {
|
|
270
|
+
'content-type': MIME['.html'],
|
|
271
|
+
'cache-control': 'no-store',
|
|
272
|
+
})
|
|
273
|
+
.end(html);
|
|
274
|
+
}
|
|
275
|
+
catch {
|
|
276
|
+
response.writeHead(404).end('not found');
|
|
277
|
+
}
|
|
278
|
+
return;
|
|
279
|
+
}
|
|
280
|
+
if (path.startsWith('/public/')) {
|
|
281
|
+
const name = basename(path); // flat filename only
|
|
282
|
+
const dir = name === 'embedded-react.js' || name === 'embedded-react.wasm'
|
|
283
|
+
? simDir
|
|
284
|
+
: outDir;
|
|
285
|
+
return void send(response, resolve(dir, name));
|
|
286
|
+
}
|
|
368
287
|
response.writeHead(404).end('not found');
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
}
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
console.log(`embedded-react WASM sim → http://localhost:${port}/`);
|
|
385
|
-
console.log(`watching ${label} — edit & save to hot-reload. Ctrl-C to quit.`);
|
|
386
|
-
|
|
387
|
-
const shutdown = async () => {
|
|
388
|
-
try {
|
|
389
|
-
await buildContext.dispose();
|
|
390
|
-
} catch {}
|
|
391
|
-
server.close();
|
|
392
|
-
process.exit(0);
|
|
393
|
-
};
|
|
394
|
-
process.on('SIGINT', shutdown);
|
|
395
|
-
process.on('SIGTERM', shutdown);
|
|
396
|
-
return {ctx: buildContext, server};
|
|
288
|
+
});
|
|
289
|
+
await new Promise(onListening => server.listen(port, onListening));
|
|
290
|
+
console.log(`embedded-react WASM sim → http://localhost:${port}/`);
|
|
291
|
+
console.log(`watching ${label} — edit & save to hot-reload. Ctrl-C to quit.`);
|
|
292
|
+
const shutdown = async () => {
|
|
293
|
+
try {
|
|
294
|
+
await buildContext.dispose();
|
|
295
|
+
}
|
|
296
|
+
catch { }
|
|
297
|
+
server.close();
|
|
298
|
+
process.exit(0);
|
|
299
|
+
};
|
|
300
|
+
process.on('SIGINT', shutdown);
|
|
301
|
+
process.on('SIGTERM', shutdown);
|
|
302
|
+
return { ctx: buildContext, server };
|
|
397
303
|
}
|