weifuwu 0.18.14 → 0.18.17

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 = [
@@ -5192,8 +5197,8 @@ function clearCompileCache() {
5192
5197
  _alias = null;
5193
5198
  }
5194
5199
  async function compileTsx(path2) {
5195
- if (cache.has(path2)) return cache.get(path2);
5196
5200
  const absPath = resolve3(path2);
5201
+ if (cache.has(absPath)) return cache.get(absPath);
5197
5202
  mkdirSync(OUT_DIR, { recursive: true });
5198
5203
  const hash = id(absPath);
5199
5204
  const outPath = join2(OUT_DIR, hash + ".js");
@@ -5211,12 +5216,40 @@ async function compileTsx(path2) {
5211
5216
  allowOverwrite: true
5212
5217
  });
5213
5218
  const mod = await import(pathToFileURL(outPath).href);
5214
- cache.set(path2, mod);
5219
+ cache.set(absPath, mod);
5220
+ return mod;
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
+ const absPath = resolve3(path2);
5233
+ if (cache.has(absPath)) return cache.get(absPath);
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(absPath, mod);
5215
5248
  return mod;
5216
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 = "";
@@ -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.14",
3
+ "version": "0.18.17",
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",