Skip to content

REPL: show the console output of evaluated code - #3268

Open
officialasishkumar wants to merge 1 commit into
babel:mainfrom
officialasishkumar:repl-console-panel
Open

officialasishkumar wants to merge 1 commit into
babel:mainfrom
officialasishkumar:repl-console-panel

Conversation

@officialasishkumar

Copy link
Copy Markdown
Contributor

When "Evaluate" is enabled, the REPL runs the compiled code in a hidden iframe, but everything the code logs only ends up in the browser's developer tools. That makes quick experiments awkward and doesn't work at all on most mobile browsers. This adds a console tab next to "output" and "input AST" that shows what the evaluated code logs.

Changes:

  • scopedEval wraps the iframe's console.log/info/warn/error/debug and console.clear. Calls are still forwarded to the original methods, so the messages keep showing up in the developer tools too.
  • Errors thrown synchronously by the code, plus uncaught errors and unhandled promise rejections from callbacks (timers, promises), are reported as Uncaught ... messages. Warnings and errors use the same colors as the existing error/warning bars.
  • The console is cleared every time the code is evaluated again. Only the latest 1000 messages are kept, so logging in a loop can't freeze the page, and updates are batched in a microtask.
  • Logged values come from another realm (the iframe), so they are formatted by a small cross-realm inspect in lib/formatConsoleArgs.ts instead of JSON.stringify. It supports printf-style substitutions (%s %d %i %f %o %O %c %%), nested objects/arrays with a depth limit, Map/Set, typed arrays, errors, dates, functions/classes and circular references. Getters are shown as [Getter] and are not invoked, so formatting has no side effects. If a value can't be inspected (e.g. a revoked proxy), the user's console call still doesn't throw.
  • When "Evaluate" is disabled, the tab explains how to enable it.

Example: console.log("answer:", { value: 42, list: [1, "two"] }); console.warn("%s is deprecated", "foo"); shows answer: { value: 42, list: [ 1, "two" ] } followed by a highlighted foo is deprecated.

Testing:

  • Added tests/repl.spec.ts with Playwright tests for the logged messages (formatting, printf-style substitutions, sync and async errors) and for the placeholder shown when evaluation is disabled. Both fail on main and pass with this change. The existing REPL visual test still passes. The Argos repl screenshot will show a diff because of the new tab label.
  • Checked manually in light and dark mode: toggling "Evaluate", editing the code, console.clear() and switching tabs.

Fixes #1392

When "Evaluate" is enabled, the compiled code runs in a hidden iframe and
everything it logs only ends up in the browser's developer tools, which is
inconvenient (and not available at all on most mobile browsers).

Add a "console" tab next to "output" and "input AST" that shows what the
evaluated code logs:

- scopedEval now wraps the iframe's console.log/info/warn/error/debug and
  console.clear. Calls are still forwarded to the original methods, so the
  messages keep showing up in the developer tools too.
- Errors thrown synchronously by the code, as well as uncaught errors and
  unhandled promise rejections from callbacks, are reported as
  "Uncaught ..." messages.
- The console is cleared every time the code is evaluated again, and only
  the latest 1000 messages are kept so that logging in a loop can't freeze
  the page. Updates are batched in a microtask.
- Logged values are formatted by a small cross-realm `inspect`
  (formatConsoleArgs) that supports printf-style substitutions, nested
  objects/arrays with a depth limit, Map/Set, typed arrays, errors, dates,
  functions, classes and circular references. Getters are not invoked.
- When "Evaluate" is disabled, the tab explains how to enable it.

Add Playwright tests covering the logged messages (including
printf-style substitutions, sync and async errors) and the placeholder
shown when evaluation is disabled.
@netlify

netlify Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for babel-v9 ready!

Name Link
🔨 Latest commit 2e713de
🔍 Latest deploy log https://app.netlify.com/projects/babel-v9/deploys/6afca3ef2c63ad15b01eb46b
😎 Deploy Preview https://deploy-preview-3268--babel-v9.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@netlify

netlify Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for babel-v8 ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 2e713de
🔍 Latest deploy log https://app.netlify.com/projects/babel-v8/deploys/6afca3efed196867e572e81f
😎 Deploy Preview https://deploy-preview-3268--babel-v8.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@JLHwung JLHwung left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for opening a PR.

Per our AI policy, please disclose AI usage involved in this PR. And if you have used LLM to assist, please let the LLM delegate a new agent to review this PR before you are opening.

const contentWindow = getIframe().contentWindow;

// Every evaluation starts with an empty console.
clearConsole();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clearing console here doesn't stop old code from logging. The hidden sandbox iframe is reused in different runs, so any setInterval or pending promises will keep writing to the cleared console. For example, setInterval(() => console.log('tick'), 1500) keeps printing after user edits the code. Consider create a new iframe for each run.

) : (
entries.map((entry, index) => (
<pre
key={index}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When console entries reach the MAX_CONSOLE_ENTRIES limit, every key will points to different content on every batch. Because we have role="log" here, an accessibility screen reader may announce them again. Consider use a stable id for each message.


useEffect(() => {
// Keep the latest messages visible.
container.current.scrollTop = container.current.scrollHeight;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should only scroll down when the view is already at or near the bottom: Every time new messages arrive, this effect scrolls the panel to the very bottom. It doesn't check where user were looking, so it pulls down even if user had scrolled up.

};

_onConsoleChange = (consoleEntries: ReadonlyArray<ConsoleEntry>) => {
this.setState({ consoleEntries });

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment is from claude:

Consider use useSyncExternalStore to let ConsolePanel subscribes to itself rather than trigger the re-render of the REPL component whenever there is new console entries:

// scopedEval.ts
const listeners = new Set<() => void>();
let snapshot: ReadonlyArray<ConsoleEntry> = [];

function notifyConsoleListeners() {
  isNotifyScheduled = false;
  snapshot = consoleEntries.slice();
  listeners.forEach(l => l());
}

export function subscribeConsole(l: () => void) {
  listeners.add(l);
  return () => listeners.delete(l);
}
export const getConsoleSnapshot = () => snapshot;
// ConsolePanel.tsx
const entries = useSyncExternalStore(subscribeConsole, getConsoleSnapshot);

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

REPL: bring old console.log pane back

2 participants