@kbach/react 0.5.21 → 0.5.23
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/vite-plugin.js +56 -20
- package/dist/vite-plugin.mjs +56 -20
- package/package.json +1 -1
package/dist/vite-plugin.js
CHANGED
|
@@ -3438,32 +3438,68 @@ function kbach(userConfig) {
|
|
|
3438
3438
|
scanDir((0, import_path.join)(root, dir), (code) => extractFromCode(code));
|
|
3439
3439
|
}
|
|
3440
3440
|
},
|
|
3441
|
-
// ── DEV: intercept every
|
|
3442
|
-
// React Router v8
|
|
3443
|
-
//
|
|
3441
|
+
// ── DEV: intercept every HTML response and inject <style data-kbach> ──────
|
|
3442
|
+
// React Router v8 uses chunked streaming SSR — </head> comes in an early
|
|
3443
|
+
// res.write() chunk, not res.end(). Buffer all writes, concatenate at end,
|
|
3444
|
+
// inject CSS into the full HTML, then send in one go.
|
|
3444
3445
|
configureServer(s) {
|
|
3445
3446
|
server = s;
|
|
3446
|
-
s.middlewares.use((
|
|
3447
|
+
s.middlewares.use((req, res, next) => {
|
|
3448
|
+
const url = req.url ?? "";
|
|
3449
|
+
if (/\.(js|ts|jsx|tsx|css|png|jpg|svg|ico|woff2?|ttf|json|map)(\?|$)/.test(url) || url.startsWith("/@") || url.startsWith("/__")) {
|
|
3450
|
+
return next();
|
|
3451
|
+
}
|
|
3452
|
+
const origWrite = res.write.bind(res);
|
|
3447
3453
|
const origEnd = res.end.bind(res);
|
|
3448
|
-
|
|
3449
|
-
|
|
3450
|
-
|
|
3451
|
-
|
|
3452
|
-
|
|
3453
|
-
|
|
3454
|
-
|
|
3455
|
-
|
|
3456
|
-
|
|
3457
|
-
|
|
3458
|
-
|
|
3459
|
-
|
|
3460
|
-
|
|
3461
|
-
|
|
3454
|
+
const chunks = [];
|
|
3455
|
+
let done = false;
|
|
3456
|
+
function toBuffer(chunk, enc) {
|
|
3457
|
+
if (chunk == null) return Buffer.alloc(0);
|
|
3458
|
+
if (typeof chunk === "string") {
|
|
3459
|
+
return Buffer.from(chunk, typeof enc === "string" ? enc : "utf-8");
|
|
3460
|
+
}
|
|
3461
|
+
if (ArrayBuffer.isView(chunk)) {
|
|
3462
|
+
return Buffer.from(chunk.buffer, chunk.byteOffset, chunk.byteLength);
|
|
3463
|
+
}
|
|
3464
|
+
if (chunk instanceof ArrayBuffer) return Buffer.from(chunk);
|
|
3465
|
+
return Buffer.from(String(chunk));
|
|
3466
|
+
}
|
|
3467
|
+
res.write = function(chunk, enc, cb) {
|
|
3468
|
+
if (done) return origWrite(chunk, enc, cb);
|
|
3469
|
+
if (chunk != null) chunks.push(toBuffer(chunk, enc));
|
|
3470
|
+
if (typeof enc === "function") enc();
|
|
3471
|
+
else if (typeof cb === "function") cb();
|
|
3472
|
+
return true;
|
|
3473
|
+
};
|
|
3474
|
+
res.end = function(chunk, enc, cb) {
|
|
3475
|
+
done = true;
|
|
3476
|
+
if (typeof chunk === "function") {
|
|
3477
|
+
cb = chunk;
|
|
3478
|
+
chunk = null;
|
|
3479
|
+
enc = null;
|
|
3480
|
+
} else if (typeof enc === "function") {
|
|
3481
|
+
cb = enc;
|
|
3482
|
+
enc = null;
|
|
3483
|
+
}
|
|
3484
|
+
if (chunk != null) chunks.push(toBuffer(chunk, enc));
|
|
3485
|
+
if (chunks.length > 0) {
|
|
3486
|
+
const html = Buffer.concat(chunks).toString("utf-8");
|
|
3487
|
+
if (html.includes("</head>")) {
|
|
3488
|
+
const injected = injectIntoHtml(html);
|
|
3489
|
+
const buf = Buffer.from(injected, "utf-8");
|
|
3490
|
+
try {
|
|
3491
|
+
res.removeHeader("transfer-encoding");
|
|
3492
|
+
} catch {
|
|
3493
|
+
}
|
|
3494
|
+
try {
|
|
3495
|
+
res.setHeader("content-length", buf.length);
|
|
3496
|
+
} catch {
|
|
3462
3497
|
}
|
|
3498
|
+
return origEnd(buf, cb);
|
|
3463
3499
|
}
|
|
3464
|
-
|
|
3500
|
+
return origEnd(Buffer.concat(chunks), enc, cb);
|
|
3465
3501
|
}
|
|
3466
|
-
return origEnd(
|
|
3502
|
+
return origEnd(void 0, enc, cb);
|
|
3467
3503
|
};
|
|
3468
3504
|
next();
|
|
3469
3505
|
});
|
package/dist/vite-plugin.mjs
CHANGED
|
@@ -96,32 +96,68 @@ function kbach(userConfig) {
|
|
|
96
96
|
scanDir(join(root, dir), (code) => extractFromCode(code));
|
|
97
97
|
}
|
|
98
98
|
},
|
|
99
|
-
// ── DEV: intercept every
|
|
100
|
-
// React Router v8
|
|
101
|
-
//
|
|
99
|
+
// ── DEV: intercept every HTML response and inject <style data-kbach> ──────
|
|
100
|
+
// React Router v8 uses chunked streaming SSR — </head> comes in an early
|
|
101
|
+
// res.write() chunk, not res.end(). Buffer all writes, concatenate at end,
|
|
102
|
+
// inject CSS into the full HTML, then send in one go.
|
|
102
103
|
configureServer(s) {
|
|
103
104
|
server = s;
|
|
104
|
-
s.middlewares.use((
|
|
105
|
+
s.middlewares.use((req, res, next) => {
|
|
106
|
+
const url = req.url ?? "";
|
|
107
|
+
if (/\.(js|ts|jsx|tsx|css|png|jpg|svg|ico|woff2?|ttf|json|map)(\?|$)/.test(url) || url.startsWith("/@") || url.startsWith("/__")) {
|
|
108
|
+
return next();
|
|
109
|
+
}
|
|
110
|
+
const origWrite = res.write.bind(res);
|
|
105
111
|
const origEnd = res.end.bind(res);
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
112
|
+
const chunks = [];
|
|
113
|
+
let done = false;
|
|
114
|
+
function toBuffer(chunk, enc) {
|
|
115
|
+
if (chunk == null) return Buffer.alloc(0);
|
|
116
|
+
if (typeof chunk === "string") {
|
|
117
|
+
return Buffer.from(chunk, typeof enc === "string" ? enc : "utf-8");
|
|
118
|
+
}
|
|
119
|
+
if (ArrayBuffer.isView(chunk)) {
|
|
120
|
+
return Buffer.from(chunk.buffer, chunk.byteOffset, chunk.byteLength);
|
|
121
|
+
}
|
|
122
|
+
if (chunk instanceof ArrayBuffer) return Buffer.from(chunk);
|
|
123
|
+
return Buffer.from(String(chunk));
|
|
124
|
+
}
|
|
125
|
+
res.write = function(chunk, enc, cb) {
|
|
126
|
+
if (done) return origWrite(chunk, enc, cb);
|
|
127
|
+
if (chunk != null) chunks.push(toBuffer(chunk, enc));
|
|
128
|
+
if (typeof enc === "function") enc();
|
|
129
|
+
else if (typeof cb === "function") cb();
|
|
130
|
+
return true;
|
|
131
|
+
};
|
|
132
|
+
res.end = function(chunk, enc, cb) {
|
|
133
|
+
done = true;
|
|
134
|
+
if (typeof chunk === "function") {
|
|
135
|
+
cb = chunk;
|
|
136
|
+
chunk = null;
|
|
137
|
+
enc = null;
|
|
138
|
+
} else if (typeof enc === "function") {
|
|
139
|
+
cb = enc;
|
|
140
|
+
enc = null;
|
|
141
|
+
}
|
|
142
|
+
if (chunk != null) chunks.push(toBuffer(chunk, enc));
|
|
143
|
+
if (chunks.length > 0) {
|
|
144
|
+
const html = Buffer.concat(chunks).toString("utf-8");
|
|
145
|
+
if (html.includes("</head>")) {
|
|
146
|
+
const injected = injectIntoHtml(html);
|
|
147
|
+
const buf = Buffer.from(injected, "utf-8");
|
|
148
|
+
try {
|
|
149
|
+
res.removeHeader("transfer-encoding");
|
|
150
|
+
} catch {
|
|
151
|
+
}
|
|
152
|
+
try {
|
|
153
|
+
res.setHeader("content-length", buf.length);
|
|
154
|
+
} catch {
|
|
120
155
|
}
|
|
156
|
+
return origEnd(buf, cb);
|
|
121
157
|
}
|
|
122
|
-
|
|
158
|
+
return origEnd(Buffer.concat(chunks), enc, cb);
|
|
123
159
|
}
|
|
124
|
-
return origEnd(
|
|
160
|
+
return origEnd(void 0, enc, cb);
|
|
125
161
|
};
|
|
126
162
|
next();
|
|
127
163
|
});
|