jupyter-widgets / ipywidgets

Interactive Widgets for the Jupyter Notebook

Home Page:https://ipywidgets.readthedocs.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

@jupyter-widgets/controls: require() call breaks ES module

thomasjm opened this issue · comments

Description

There is a stray call to require in what is otherwise a perfectly good ES module here:

export const version = (require('../package.json') as any).version;

When this is published to NPM, it is transpiled from TypeScript but not bundled. So the production source looks the same: an ES module with a random require at the bottom. See here in lib/index.js.

If I try to bundle this package using Rollup into a library targeting ESM, and then use it in a web browser, that require remains there and causes a crash, because require doesn't exist in browsers. I'm doing this via Vite, which uses Rollup to compile for production.

There may be some advanced options for Rollup which will cause it to properly transform the require in a "mixed ES module" like this, which could serve as a workaround. But I think it would be more sensible to just remove the require. It seems its only purpose is to import the version string from package.json. I bet there's a way to insert it directly during the build process instead.