richtr / NoSleep.js

Prevent display sleep and enable wake lock in any Android or iOS web browser.
MIT License
2.23k stars 380 forks source link

ESM support not working #160

Closed codingjoe closed 7 months ago

codingjoe commented 8 months ago

Hi there 👋,

Thanks for maintaining such a wonderful package <3

We are transitioning to ESM over bundling. While doing so, I noticed that this package wasn't set up correctly.

The package.json module key points to src/index.js. https://github.com/richtr/NoSleep.js/blob/1d15cc88dd8c1b62fe5b5f910c6ff7396b280d68/package.json#L16

However, the index.js file is written in CommonJS at does a require call in the very first line. https://github.com/richtr/NoSleep.js/blob/1d15cc88dd8c1b62fe5b5f910c6ff7396b280d68/src/index.js#L1

Furthermore, the module key, is deprecated in favor of defining exports. Finally, both, module and exports should usually point to the distribution code, not the package sources.

I am happy to provide a fix that transforms this package to be fully ESM compatible. Please let me know if you'd be interested in a contribution.

Cheers! Joe

kungfooman commented 8 months ago

Ideally the library would just be rewritten into ESM and compiled down to UMD via rollup - easy enough. Then publish both versions in the npm package.

Considering that this library was last updated 4 years ago: the maintainer doesn't care obviously.

However, I care and please fork this repository and do what I just said. I will watch your fork and if you run into issues, you can ping me - I just happen to need this aswell.

If you don't now rollup good enough yet, you can take the build script of PlayCanvas as good/tested example.

While you are at it, you can also add proper JSDoc comments to document the code.

codingjoe commented 7 months ago

After a little research, why would anyone care, since WakeLock has been implemented?

kungfooman commented 7 months ago

Mainly as polyfill for testing e.g. Cardboard VR on old browsers.