-
-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathvite.config.ts
More file actions
464 lines (428 loc) · 16.6 KB
/
Copy pathvite.config.ts
File metadata and controls
464 lines (428 loc) · 16.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
import path from "path";
import { transformAsync } from "@babel/core";
import { defineConfig, loadEnv, type UserConfig } from "vite";
import react from "@vitejs/plugin-react";
import { viteStaticCopy } from "vite-plugin-static-copy";
import { nodePolyfills } from "vite-plugin-node-polyfills";
const projectRoot = __dirname;
const resolvePath = (...segments: string[]) => path.resolve(projectRoot, ...segments);
const normalizePath = (filePath: string) => filePath.replaceAll("\\", "/");
const parseEnvBoolean = (value: string | undefined, fallback: boolean) => {
if (value == null) {
return fallback;
}
const normalized = value.trim().toLowerCase();
if (["1", "true", "yes", "on"].includes(normalized)) {
return true;
}
if (["0", "false", "no", "off"].includes(normalized)) {
return false;
}
return fallback;
};
const transpileDependencyPatterns = [
/node_modules\/react-native-web\/src\//,
/node_modules\/react-native-easy-grid\/Components\//,
/node_modules\/react-native-animatable\//,
/node_modules\/react-native-drawer\//,
/node_modules\/react-native-qrcode-svg\//,
/node_modules\/@codler\/react-native-keyboard-aware-scroll-view\//,
];
const shouldTranspileDependency = (id: string) =>
transpileDependencyPatterns.some((pattern) => pattern.test(id));
const isEasyGridComponent = (id: string) =>
/node_modules\/react-native-easy-grid\/Components\//.test(id);
const isAnimatableModule = (id: string) => /node_modules\/react-native-animatable\//.test(id);
const isReactNativeDrawerModule = (id: string) => /node_modules\/react-native-drawer\//.test(id);
const isReactNativeQrCodeSvgModule = (id: string) =>
/node_modules\/react-native-qrcode-svg\//.test(id);
const isReactNativeElementDropdownModule = (id: string) =>
/node_modules\/react-native-element-dropdown\/lib\/module\/.*\.js$/.test(id);
const isReactNativeWebWebViewIndexModule = (id: string) =>
/node_modules\/react-native-web-webview\/(?:dist|src)\/index\.js$/.test(id);
const REACT_NATIVE_WEB_WEBVIEW_POSTMOCK_VIRTUAL_ID = "\0react-native-web-webview-postmock-html";
export default defineConfig(({ command, mode }) => {
const env = loadEnv(mode, process.cwd(), "");
const isProduction = mode === "production";
const isAppDev = parseEnvBoolean(env.BLIXT_DEV ?? env.VITE_BLIXT_DEV, !isProduction);
const disableHmrForProductionNodeEnv =
command === "serve" && process.env.NODE_ENV === "production";
const flavor = (env.FLAVOR ?? env.VITE_FLAVOR ?? "fakelnd").toLowerCase();
const isElectrobunTarget = (env.VITE_IS_ELECTROBUN ?? "false").toLowerCase() === "true";
const platformExtensions = isElectrobunTarget
? [".electrobun.tsx", ".electrobun.ts", ".electrobun.jsx", ".electrobun.js"]
: [];
const turboLndModuleReplacement =
flavor === "normal" ? "react-native-turbo-lnd/electrobun/view" : "react-native-turbo-lnd/mock";
return {
root: resolvePath("web"),
plugins: [
{
name: "react-native-web-webview-postmock-resolver",
enforce: "pre",
resolveId(source, importer) {
if (!importer) {
return null;
}
const cleanImporter = normalizePath(importer.split("?")[0]);
if (source === "./postMock.html" && isReactNativeWebWebViewIndexModule(cleanImporter)) {
return REACT_NATIVE_WEB_WEBVIEW_POSTMOCK_VIRTUAL_ID;
}
return null;
},
load(id) {
if (id === REACT_NATIVE_WEB_WEBVIEW_POSTMOCK_VIRTUAL_ID) {
return "export default '/postMock.html';";
}
return null;
},
},
{
name: "react-native-deps-babel",
enforce: "pre",
async transform(code, id) {
const cleanId = normalizePath(id.split("?")[0]);
if (!cleanId.endsWith(".js")) {
return null;
}
if (!cleanId.includes("/node_modules/")) {
return null;
}
if (isReactNativeElementDropdownModule(cleanId)) {
const patchedCode = code.replace(
/const\s+([A-Za-z_$][\w$]*)\s*=\s*require\((['"])([^'"]+\.(?:png|jpe?g|gif|webp|svg))\2\);/g,
"import $1 from '$3';",
);
if (patchedCode !== code) {
return {
code: patchedCode,
map: null,
};
}
}
if (!shouldTranspileDependency(cleanId)) {
if (!isReactNativeWebWebViewIndexModule(cleanId)) {
return null;
}
}
if (isReactNativeWebWebViewIndexModule(cleanId)) {
const patchedCode = code.replace(
/require\((['"])\\.\/postMock\.html\1\)/g,
'"/postMock.html"',
);
return {
code: patchedCode,
map: null,
};
}
const presets =
isEasyGridComponent(cleanId) ||
isAnimatableModule(cleanId) ||
isReactNativeDrawerModule(cleanId) ||
isReactNativeQrCodeSvgModule(cleanId)
? ["@babel/preset-react"]
: [["module:@react-native/babel-preset", { disableImportExportTransform: true }]];
const result = await transformAsync(code, {
filename: cleanId,
babelrc: false,
configFile: false,
presets,
sourceMaps: true,
});
if (!result?.code) {
return null;
}
return {
code: result.code,
map: result.map ?? null,
};
},
},
react({
babel: {
babelrc: false,
configFile: false,
},
}),
nodePolyfills({
protocolImports: true,
}),
viteStaticCopy({
targets: [
...(isElectrobunTarget
? []
: [
{
src: normalizePath(resolvePath("node_modules/sql.js/dist/sql-wasm.wasm")),
dest: ".",
},
{
src: normalizePath(resolvePath("node_modules/sql.js/dist/sql-wasm-browser.wasm")),
dest: ".",
},
]),
{
src: normalizePath(
resolvePath("node_modules/react-native-web-webview/dist/postMock.html"),
),
dest: ".",
},
],
}),
],
// Essential runtime replacements for web/Electrobun live in three layers:
// 1. Explicit package aliases in `resolve.alias`.
// - `react-native-turbo-lnd`
// - `flavor=normal`: upstream `react-native-turbo-lnd/electrobun/view`
// - those helpers are imported separately from
// `react-native-turbo-lnd/electrobun/custom-rpc` by
// `electrobun/src/shared/rpc-client.web.ts`
// - `flavor!=normal`: `react-native-turbo-lnd/mock`
// - `react-native-turbo-sqlite`
// - plain web: upstream package browser export; no alias needed
// - Electrobun: `electrobun/src/shims/react-native-turbo-sqlite.ts`, which forwards
// DB open/query/close calls over Electrobun RPC to the Bun-side SQLite implementation
// - `@react-native-async-storage/async-storage`
// - Electrobun only: `electrobun/src/shims/async-storage.js`, which forwards the
// AsyncStorage API over Electrobun RPC to the Bun-side KV store
// 2. Platform file resolution via `.electrobun.*` / `.web.*` because those extensions come
// first in `extensions`.
// - For the Electrobun renderer we prefer `.electrobun.*`, so files like
// `src/turbomodules/NativeBlixtTools.electrobun.ts` can stay Electrobun-specific without
// polluting the plain browser `*.web.*` implementations.
// - `src/storage/keystore.web.ts`: localStorage-backed replacement for `src/storage/keystore.ts`,
// so the native `react-native-keychain` dependency never enters the web/Electrobun bundle
// - `src/storage/database/sqlite.web.ts`: web DB entrypoint that imports
// `react-native-turbo-sqlite`; on plain web that resolves to the package browser backend,
// on Electrobun it resolves to the RPC-backed SQLite shim above
// - `src/turbomodules/NativeBlixtTools.web.ts`: plain browser fallback for native build/file
// helpers
// - `src/turbomodules/NativeBlixtTools.electrobun.ts`: Electrobun-specific NativeBlixtTools
// implementation that delegates into `electrobun/src/shims/native-blixt-tools.ts`
// - `src/turbomodules/NativeLndmobileTools.web.ts`,
// `src/turbomodules/NativeScheduledSyncTurbo.web.ts`, and
// `src/turbomodules/NativeSpeedloader.web.ts`: minimal web stand-ins for native TurboModules
// 3. Runtime globals outside aliasing.
// - `define.global = "globalThis"` here, plus `globalThis.global = globalThis` in
// `web/main.ts`, keeps React Native code that expects a Node-style `global` working
// - `web/main.ts` assigns `FLAVOR`, `CHAIN`, `APPLICATION_ID`, `VERSION_NAME`,
// `VERSION_CODE`, `BUILD_TYPE`, `DEBUG`, `__DEV__`, `BLIXT_WEB_DEMO`, and `IS_ELECTROBUN`
// before loading `index.web.js`; `NativeBlixtTools.web.ts` reads several of those directly
//
// The large `web/web-hacks/**` alias block below is a separate category: compatibility patches for
// React Native libraries. If a missing dependency is core app runtime surface area, document it in
// the inventory above; if it is just a broken RN package import path, keep it with the patch aliases.
resolve: {
extensions: [
...platformExtensions,
".web.tsx",
".web.ts",
".tsx",
".ts",
".web.jsx",
".web.js",
".jsx",
".js",
".json",
],
alias: [
{
find: /^react-native$/,
replacement: resolvePath("web/web-hacks/react-native-web-compat.js"),
},
{
find: /^sql\.js$/,
replacement: resolvePath("node_modules/sql.js/dist/sql-wasm.js"),
},
{
find: /^react-native\/Libraries\/Utilities\/codegenNativeComponent$/,
replacement: resolvePath(
"web/web-hacks/react-native-libraries/codegenNativeComponent.js",
),
},
{
find: /^react-native\/Libraries\/Utilities\/codegenNativeCommands$/,
replacement: resolvePath("web/web-hacks/react-native-libraries/codegenNativeCommands.js"),
},
{
find: /^react-native\/Libraries\/Image\/resolveAssetSource$/,
replacement: resolvePath("web/web-hacks/react-native-libraries/resolveAssetSource.js"),
},
{
find: /^react-native\/Libraries\/StyleSheet\/processColor$/,
replacement: resolvePath("web/web-hacks/react-native-libraries/processColor.js"),
},
{
find: /^react-native\/Libraries\/StyleSheet\/processColorArray$/,
replacement: resolvePath("web/web-hacks/react-native-libraries/processColorArray.js"),
},
{
find: /^react-native\/Libraries\/NativeComponent\/NativeComponentRegistry$/,
replacement: resolvePath(
"web/web-hacks/react-native-libraries/NativeComponentRegistry.js",
),
},
{
find: /^react-native\/Libraries\/NativeComponent\/ViewConfigIgnore$/,
replacement: resolvePath("web/web-hacks/react-native-libraries/ViewConfigIgnore.js"),
},
{
find: /^react-native\/Libraries\/ReactNative\/AppContainer$/,
replacement: resolvePath("web/web-hacks/react-native-libraries/AppContainer.js"),
},
{
find: /^react-native\/Libraries\/ReactNative\/RendererProxy$/,
replacement: resolvePath("web/web-hacks/react-native-libraries/RendererProxy.js"),
},
{
find: "react-native-linear-gradient",
replacement: "react-native-web-linear-gradient",
},
{
find: /^react-native-vector-icons$/,
replacement: resolvePath("node_modules/react-native-vector-icons/dist/index.js"),
},
{
find: /^react-native-vector-icons\/(.+)$/,
replacement: resolvePath("node_modules/react-native-vector-icons/dist/$1.js"),
},
{
find: /^react-native-element-dropdown$/,
replacement: resolvePath("web/web-hacks/react-native-element-dropdown.js"),
},
{
find: /^native-base-shoutem-theme$/,
replacement: resolvePath("node_modules/native-base-shoutem-theme/dist/index.js"),
},
{
find: /^@react-native-picker\/picker$/,
replacement: resolvePath("web/web-hacks/react-native-picker.js"),
},
{
find: /^@react-native-documents\/picker$/,
replacement: resolvePath("web/web-hacks/react-native-documents-picker.js"),
},
{
find: "react-native-maps",
replacement: resolvePath("web/web-hacks/react-native-maps.jsx"),
},
{
find: "react-native-svg",
replacement: "react-native-svg-web",
},
{
find: "react-native-webview",
replacement: "react-native-web-webview",
},
{
find: /^react-native-keyboard-controller$/,
replacement: resolvePath("web/web-hacks/react-native-keyboard-controller.js"),
},
{
find: /^react-native-turbo-lnd$/,
replacement: turboLndModuleReplacement,
},
{
find: "react-native-permissions",
replacement: resolvePath("web/web-hacks/react-native-permission.js"),
},
{
find: "react-native-dialogs",
replacement: resolvePath("web/web-hacks/react-native-dialogs.js"),
},
{
find: "react-native-fs",
replacement: resolvePath("web/web-hacks/react-native-fs.js"),
},
{
find: "react-native-icloudstore",
replacement: resolvePath("web/web-hacks/react-native-icloudstore.js"),
},
{
find: "react-native-nitro-tor",
replacement: resolvePath("web/web-hacks/react-native-nitro-tor.js"),
},
...(isElectrobunTarget
? [
{
find: /^react-native-turbo-sqlite$/,
replacement: resolvePath("electrobun/src/shims/react-native-turbo-sqlite.ts"),
},
{
find: /^react-native-turbo-sqlite\/mocks$/,
replacement: resolvePath("electrobun/src/shims/react-native-turbo-sqlite-mocks.ts"),
},
{
find: /^@react-native-async-storage\/async-storage$/,
replacement: resolvePath("electrobun/src/shims/async-storage.js"),
},
]
: []),
{
find: "@notifee/react-native",
replacement: resolvePath("web/web-hacks/notifee-react-native.js"),
},
],
},
define: {
global: "globalThis",
__DEV__: JSON.stringify(isAppDev),
"process.env.NODE_ENV": JSON.stringify(isProduction ? "production" : "development"),
"process.env.JEST_WORKER_ID": "null",
"process.env.ELECTROBUN_SAFE_STARTUP": JSON.stringify(
env.ELECTROBUN_SAFE_STARTUP ?? env.VITE_ELECTROBUN_SAFE_STARTUP ?? "",
),
},
optimizeDeps: {
entries: ["index.html"],
include: ["prop-types", "react-native-drawer", "qrcode"],
exclude: [
"react-native-easy-grid",
"react-native-animatable",
"react-native-qrcode-svg",
"@codler/react-native-keyboard-aware-scroll-view",
"react-native-web-webview",
],
rolldownOptions: {
transform: {
define: {
global: "globalThis",
},
},
moduleTypes: {
".html": "text",
},
plugins: [],
},
},
server: {
port: 8080,
hmr: disableHmrForProductionNodeEnv ? false : undefined,
fs: {
allow: [resolvePath(".")],
},
headers: {
// For react-native-turbo-sqlite web (uses OPFS):
"Cross-Origin-Opener-Policy": "same-origin",
"Cross-Origin-Embedder-Policy": "require-corp",
},
},
build: {
outDir: resolvePath("web/dist"),
emptyOutDir: true,
sourcemap: true,
target: "es2020",
rollupOptions: {
output: {
entryFileNames: "assets/[name].js",
chunkFileNames: "assets/[name].js",
assetFileNames: "assets/[name][extname]",
},
},
},
envPrefix: ["VITE_", "CHAIN", "FLAVOR", "APPLICATION_ID", "BLIXT_WEB_DEMO", "BLIXT_DEV"],
css: {
devSourcemap: true,
},
} satisfies UserConfig;
});