weifuwu 0.18.13 → 0.18.16

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/dist/compile.d.ts CHANGED
@@ -1,2 +1,4 @@
1
1
  export declare function clearCompileCache(): void;
2
2
  export declare function compileTsx(path: string): Promise<any>;
3
+ /** Dev hot-reload: CJS + in-memory + vm (faster than ESM + disk + import) */
4
+ export declare function compileTsxDev(path: string): Promise<any>;
package/dist/index.js CHANGED
@@ -508,6 +508,7 @@ var Router = class _Router {
508
508
  return await this.runChain(allMws, handler, req, ctxWithMatch);
509
509
  } catch (e) {
510
510
  const err = e instanceof Error ? e : new Error(String(e));
511
+ console.error(err);
511
512
  return this.errorHandler ? this.errorHandler(err, req, ctxWithMatch) : new Response("Internal Server Error", { status: 500 });
512
513
  }
513
514
  }
@@ -517,6 +518,7 @@ var Router = class _Router {
517
518
  return await this.runChain(this.globalMws, delegate, req, ctx);
518
519
  } catch (e) {
519
520
  const err = e instanceof Error ? e : new Error(String(e));
521
+ console.error(err);
520
522
  return this.errorHandler ? this.errorHandler(err, req, ctx) : new Response("Internal Server Error", { status: 500 });
521
523
  }
522
524
  }
@@ -5145,6 +5147,9 @@ import { existsSync as existsSync2, mkdirSync, readFileSync as readFileSync2 } f
5145
5147
  import { join as join2, resolve as resolve3, dirname } from "node:path";
5146
5148
  import { pathToFileURL } from "node:url";
5147
5149
  import { createHash as createHash2 } from "node:crypto";
5150
+ import vm from "node:vm";
5151
+ import { createRequire } from "node:module";
5152
+ var _cjsRequire = createRequire(import.meta.url);
5148
5153
  var OUT_DIR = ".weifuwu/ssr";
5149
5154
  var cache = /* @__PURE__ */ new Map();
5150
5155
  var externals = [
@@ -5214,9 +5219,37 @@ async function compileTsx(path2) {
5214
5219
  cache.set(path2, mod);
5215
5220
  return mod;
5216
5221
  }
5222
+ function loadSSRModule(code) {
5223
+ const ctx = vm.createContext(Object.create(globalThis));
5224
+ const mod = { exports: {} };
5225
+ ctx.require = (name) => _cjsRequire(name);
5226
+ ctx.module = mod;
5227
+ ctx.exports = mod.exports;
5228
+ new vm.Script(code).runInContext(ctx);
5229
+ return mod.exports;
5230
+ }
5231
+ async function compileTsxDev(path2) {
5232
+ if (cache.has(path2)) return cache.get(path2);
5233
+ const absPath = resolve3(path2);
5234
+ const result = await esbuild.build({
5235
+ entryPoints: { [id(absPath)]: absPath },
5236
+ format: "cjs",
5237
+ platform: "node",
5238
+ jsx: "automatic",
5239
+ jsxImportSource: "react",
5240
+ bundle: true,
5241
+ external: externals,
5242
+ alias: resolveAliases(),
5243
+ write: false
5244
+ });
5245
+ const code = new TextDecoder().decode(result.outputFiles[0].contents);
5246
+ const mod = loadSSRModule(code);
5247
+ cache.set(path2, mod);
5248
+ return mod;
5249
+ }
5217
5250
 
5218
5251
  // stream.ts
5219
- import { TextDecoder, TextEncoder as TextEncoder2 } from "node:util";
5252
+ import { TextDecoder as TextDecoder2, TextEncoder as TextEncoder2 } from "node:util";
5220
5253
  var _publicEnv = null;
5221
5254
  function getPublicEnv() {
5222
5255
  if (_publicEnv) return _publicEnv;
@@ -5272,7 +5305,7 @@ function buildBodyScripts(opts) {
5272
5305
  return parts.join("\n");
5273
5306
  }
5274
5307
  function streamResponse(reactStream, opts) {
5275
- const decoder = new TextDecoder();
5308
+ const decoder = new TextDecoder2();
5276
5309
  const encoder2 = new TextEncoder2();
5277
5310
  const headPayload = buildHeadPayload(opts);
5278
5311
  let buffer = "";
@@ -5319,7 +5352,7 @@ function streamResponse(reactStream, opts) {
5319
5352
  if (opts.isDev) {
5320
5353
  controller.enqueue(encoder2.encode(
5321
5354
  `
5322
- <script>(function(){var ws=new WebSocket((location.protocol==='https:'?'wss:':'ws:')+'//'+location.host+'${opts.base}/__weifuwu/livereload');ws.onmessage=function(e){if(e.data==='reload')location.reload()};ws.onclose=function(){setTimeout(function(){location.reload()},500)}})()</script>`
5355
+ <script>(function(){var ws=new WebSocket((location.protocol==='https:'?'wss:':'ws:')+'//'+location.host+'/__weifuwu/livereload');ws.onmessage=function(e){if(e.data==='reload')location.reload()};ws.onclose=function(){setTimeout(function(){location.reload()},500)}})()</script>`
5323
5356
  ));
5324
5357
  }
5325
5358
  } catch {
@@ -5508,10 +5541,14 @@ function liveReload(opts) {
5508
5541
  ignored: /(^|[/\\])\.|node_modules|[/\\]\.weifuwu[/\\]/,
5509
5542
  ignoreInitial: true
5510
5543
  });
5511
- watcher.on("change", (path2) => {
5512
- if (!/\.tsx?$/.test(path2)) return;
5544
+ watcher.on("change", async (filePath) => {
5545
+ if (!/\.tsx?$/.test(filePath)) return;
5513
5546
  clearCompileCache();
5514
- setTimeout(broadcastReload, 50);
5547
+ try {
5548
+ await compileTsxDev(filePath);
5549
+ } catch {
5550
+ }
5551
+ broadcastReload();
5515
5552
  });
5516
5553
  r.close = () => {
5517
5554
  watcher.close();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "weifuwu",
3
- "version": "0.18.13",
3
+ "version": "0.18.16",
4
4
  "description": "Web-standard HTTP framework for Node.js — (req, ctx) => Response",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",