-
Notifications
You must be signed in to change notification settings - Fork 29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: convert HTMLNano to Es Modules #260
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not a fan of pure ESM package, as well as the existing community. htmlnano is still used by toolchain/bundler that uses CommonJS. Would you like to make it a dual ESM/CJS?
If you notice, I have kept the package backwards compatible. The main entry still points to the In fact, I added some commonjs compatibility tests for that: |
Could you approve the CI? |
Sure. Just did. |
Thank you. This PR is ready. Let me know if there is anything else to be done here. |
@SukkaW what do you think, can it be merged now? |
This pull request is ready, but I made some changes to the Babel plugin responsible for silane/babel-plugin-replace-import-extension#9 Depending on the timing of the merges, I'll either update this PR or make a new PR to htmlnano with the improvement. |
This allows the external bundlers to work properly and optimize the imports
I published my fork of the Babel plugin to unblock us. Now, the dynamic imports can be processed by bundlers (e.g. for usage as an Astro plugin) and the Now, my astro-htmlnano plugin finally works: |
Thanks! @aminya I guess this could be released as a minor version or patch as it doesn't contain any backward incompatible changes? |
You are welcome! I'd go with a minor version as the addition is a new feature. But it should be backwards compatible. |
Fixes #259
This removes all the usage of commonjs from HTMLNano replacing it with standard ES modules. The optional dependencies are now imported using dynamic imports. So, the functionality will be preserved.
All the current tests pass. I added commonjs compatibility tests to make sure nothing breaks.
The minify function returned by htmlnano is now async due to the dynamic import.
posthtml
should await this promise. Based on the tests, posthtml already does this, so there should not be an issue.This change allows adoption of HTMLNano to new projects that only work with pure ES modules.
Tests passing:
![image](https://private-user-images.githubusercontent.com/16418197/274339214-ff7fc8a9-5f85-4199-8771-27c6a44ea29e.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjQ4MDIyNzcsIm5iZiI6MTcyNDgwMTk3NywicGF0aCI6Ii8xNjQxODE5Ny8yNzQzMzkyMTQtZmY3ZmM4YTktNWY4NS00MTk5LTg3NzEtMjdjNmE0NGVhMjllLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA4MjclMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwODI3VDIzMzkzN1omWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPWExNTdhYjUyMWFmM2E5N2M0YzQzNzUyZGIwMDU1NzdjZjdhMTRkMGZjYWI5YmM5MmEyZDVhM2MyMTUyMGJlODcmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.umTxWxjKSzWVh3AJEnRKxA3xaK_LWFZ41zP9MoD4nsw)
Backwards compatibility tests:
![image](https://private-user-images.githubusercontent.com/16418197/274339390-290d1077-6c67-4c98-9a2e-3fc8abdc2b7f.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjQ4MDIyNzcsIm5iZiI6MTcyNDgwMTk3NywicGF0aCI6Ii8xNjQxODE5Ny8yNzQzMzkzOTAtMjkwZDEwNzctNmM2Ny00Yzk4LTlhMmUtM2ZjOGFiZGMyYjdmLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA4MjclMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwODI3VDIzMzkzN1omWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTkwOTlmMjM5NjAyYjY3YTQyM2YzN2YxMzk5MGVjN2JkNmY4ZGNhNjFiOWZlNWQ4NThiZGUwYTgzNGEyNDc2YWImWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.0mjSWeOEEver7s7DamxSaHPSTDCIUB0rZ3kDYlBxr9c)