ipywidgets icon indicating copy to clipboard operation
ipywidgets copied to clipboard

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

Open thomasjm opened this issue 2 years ago • 0 comments
trafficstars

Description

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

https://github.com/jupyter-widgets/ipywidgets/blob/52663ac472c38ba12575dfb4979fa2d250e79bc3/packages/controls/src/index.ts#L27

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.

thomasjm avatar Oct 23 '23 10:10 thomasjm