Repository: pyscript/pyscript
File: pyscript.core/src/plugins/py-terminal.js (JavaScript)

Code before:
import { notify } from "./error.js";

const SELECTOR = [...TYPES.keys()]
    .map((type) => `script[type="${type}"][terminal],${type}-script[terminal]`)
    .join(",");

// show the error on main and
// stops the module from keep executing
const notifyAndThrow = (message) => {
    notify(message);
    throw new Error(message);
};

const pyTerminal = async () => {
    const terminals = document.querySelectorAll(SELECTOR);

    // no results will look further for runtime nodes
    if (!terminals.length) return;

    // if we arrived this far, let's drop the MutationObserver
    // as we only support one terminal per page (right now).
    mo.disconnect();

    // we currently support only one terminal as in "classic"
    if (terminals.length > 1)
        notifyAndThrow("Unable to address multiple terminals.");

    const [element] = terminals;
    // hopefully to be removed in the near future!
    if (element.matches('script[type="mpy"],mpy-script'))
        notifyAndThrow("Unsupported terminal.");

    // import styles lazily
    document.head.append(
        Object.assign(document.createElement("link"), {
            rel: "stylesheet",
            href: new URL("./xterm.css", import.meta.url),
        }),
    );

    // lazy load these only when a valid terminal is found
    const [{ Terminal }, { Readline }] = await Promise.all([
        import(/* webpackIgnore: true */ "../3rd-party/xterm.js"),
        import(/* webpackIgnore: true */ "../3rd-party/xterm-readline.js"),
    ]);

    const readline = new Readline();

    // common main thread initialization for both worker
    // or main case, bootstrapping the terminal on its target
    const init = (options) => {

Review comment (suggestion):
suggestion for a better error message, but feel tree to ignore

```suggestion
        notifyAndThrow("You can use at most 1 terminal");
```

Code after:
import { notify } from "./error.js";

const SELECTOR = [...TYPES.keys()]
    .map((type) => `script[type="${type}"][terminal],${type}-script[terminal]`)
    .join(",");

// show the error on main and
// stops the module from keep executing
const notifyAndThrow = (message) => {
    notify(message);
    throw new Error(message);
};

const pyTerminal = async () => {
    const terminals = document.querySelectorAll(SELECTOR);

    // no results will look further for runtime nodes
    if (!terminals.length) return;

    // if we arrived this far, let's drop the MutationObserver
    // as we only support one terminal per page (right now).
    mo.disconnect();

    // we currently support only one terminal as in "classic"
    if (terminals.length > 1) notifyAndThrow("You can use at most 1 terminal.");

    const [element] = terminals;
    // hopefully to be removed in the near future!
    if (element.matches('script[type="mpy"],mpy-script'))
        notifyAndThrow("Unsupported terminal.");

    // import styles lazily
    document.head.append(
        Object.assign(document.createElement("link"), {
            rel: "stylesheet",
            href: new URL("./xterm.css", import.meta.url),
        }),
    );

    // lazy load these only when a valid terminal is found
    const [{ Terminal }, { Readline }] = await Promise.all([
        import(/* webpackIgnore: true */ "../3rd-party/xterm.js"),
        import(/* webpackIgnore: true */ "../3rd-party/xterm-readline.js"),
    ]);

    const readline = new Readline();

    // common main thread initialization for both worker
    // or main case, bootstrapping the terminal on its target
    const init = (options) => {
        let target = element;