Skip to content

Commit 91964f7

Browse files
committed
refinement
1 parent 97cb80f commit 91964f7

5 files changed

Lines changed: 933 additions & 177 deletions

File tree

book/playground/public/kawaiko.png

89.8 KB
Loading

book/playground/scripts/generate-chapters.ts

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,28 @@ function loadChapters(): Chapter[] {
189189
content: f.content,
190190
}));
191191

192-
// Fix vite.config.ts to use the correct path for WebContainer
192+
// Console hook script to inject into index.html
193+
const consoleHookScript = `<script>
194+
(function() {
195+
const originalConsole = { log: console.log, info: console.info, warn: console.warn, error: console.error };
196+
['log', 'info', 'warn', 'error'].forEach(level => {
197+
console[level] = function(...args) {
198+
originalConsole[level].apply(console, args);
199+
try {
200+
window.parent.postMessage({ type: 'console', level, args: args.map(a => {
201+
try { return typeof a === 'object' ? JSON.stringify(a) : String(a); }
202+
catch { return String(a); }
203+
})}, '*');
204+
} catch {}
205+
};
206+
});
207+
window.onerror = (msg, src, line, col, err) => {
208+
window.parent.postMessage({ type: 'console', level: 'error', args: [msg + ' at ' + src + ':' + line] }, '*');
209+
};
210+
})();
211+
</script>`;
212+
213+
// Fix vite.config.ts and inject console hook into index.html
193214
const fixedPlaygroundFiles = playgroundFiles.map((f) => {
194215
if (f.path === "vite.config.ts") {
195216
// Replace relative paths to packages with the flat structure path
@@ -201,14 +222,19 @@ function loadChapters(): Chapter[] {
201222
// Fix import paths for @extensions
202223
.replace(
203224
/from\s+["']\.\.\/\.\.\/packages\/@extensions\/([^"']+)["']/g,
204-
'from "packages/@extensions/$1"',
225+
'from "./packages/@extensions/$1"',
205226
)
206227
.replace(
207228
/import\s+(\w+)\s+from\s+["']\.\.\/\.\.\/packages\/@extensions\/([^"']+)["']/g,
208229
'import $1 from "./packages/@extensions/$2"',
209230
);
210231
return { ...f, content: fixedContent };
211232
}
233+
// Inject console hook into index.html
234+
if (f.path === "index.html") {
235+
const fixedContent = f.content.replace(/<head>/i, `<head>\n${consoleHookScript}`);
236+
return { ...f, content: fixedContent };
237+
}
212238
return f;
213239
});
214240

0 commit comments

Comments
 (0)