@@ -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 / f r o m \s + [ " ' ] \. \. \/ \. \. \/ p a c k a g e s \/ @ e x t e n s i o n s \/ ( [ ^ " ' ] + ) [ " ' ] / g,
204- 'from "packages/@extensions/$1"' ,
225+ 'from "./ packages/@extensions/$1"' ,
205226 )
206227 . replace (
207228 / i m p o r t \s + ( \w + ) \s + f r o m \s + [ " ' ] \. \. \/ \. \. \/ p a c k a g e s \/ @ e x t e n s i o n s \/ ( [ ^ " ' ] + ) [ " ' ] / 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 ( / < h e a d > / i, `<head>\n${ consoleHookScript } ` ) ;
236+ return { ...f , content : fixedContent } ;
237+ }
212238 return f ;
213239 } ) ;
214240
0 commit comments