-
Notifications
You must be signed in to change notification settings - Fork 7
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
fix(#9): Module error when using Javascript #12
Conversation
fix: Removed my username from author
Wow !!! Thank you @m-t-a97 . Great work! I need to go through the code one more time but this looks great to go. I'll merge ASAP. |
Thank you very much @elevatebart, really appreciate it 👍. Can't wait to see it merged in! |
@elevatebart can you hold out for a moment actually, I need to make a quick update to the code to add some type annotations for JavaScript as the Kestra function methods don't have any intellisense for some reason. It works fine with |
I have only one worry about backwards compatibility. I need to check. If you do this before this PR you get the following output. const Kestra = require('@kestra-io/libs')
console.log(Kestra.toString())
// output: function Kestra() {} with your change you get this because now we const Kestra = require('@kestra-io/libs')
console.log(Kestra.toString())
// output: [Object object] I'll get back to you ASAP |
OK I lied, looks good to me |
No worries. Thanks for checking it out 👍. I'm still looking into how to get intellisense and types to show up for CJS. I'll get back to you with another update once I've figured it out. |
Ok so I figured out what the issue was yesterday. Basically when bundling the code with rollup and you use For esm it does this:
// Minified output
export { t as default };
import { KestraFunction } from '../types/kestra.mjs';
declare const Kestra: KestraFunction;
export { Kestra as default }; For cjs:
import r from './kestra/kestra.mjs';
export { r as default };
import Kestra from './kestra/kestra.js';
export { Kestra as default }; The problem with this is that it adds a
import Kestra from './kestra/kestra.mjs';
export default Kestra;
import Kestra from './kestra/kestra.js';
export = Kestra; You can actually see an example of how rollup works here in their repl regarding default exports and what it generates: https://rollupjs.org/repl |
Do you want me to check it out? |
I think I'm quite close to getting this working. I'll push my code later today so you can check it out, hopefully by then I've got it all working. |
@elevatebart, I managed to implement it, it should all be working now. I'm pretty happy with the result. Please check it out and test it out too and let me know if you have any feedback 👍. |
What changes are being made and why?
This PR aims to address CJS and ESM modules compatibility as the current version of the project is broken and no longer usable. The current version of the project along with another PR attempted to introduce ESM and CJS compatibility but introduced several issues, including increased complexity, potential for inconsistencies, and breaking changes for existing users. To resolve these issues comprehensively and provide a future-proof solution, significant improvements and changes to this project were made:
@rollup/plugin-typescript
to handle.ts
files and much more.*.d.ts
files providing full type safety for users running TypeScript too..cjs
and.es6.js
file extensions, adding complexity and maintenance overhead.package.json
:dist
to the files field to control what gets published to npm..nvmrc
setting project node version to current LTS (at the time of writing it islts/jod
How the changes have been QAed?
These are the steps required to reproduce the tests:
javascript
folder as it outlines how you can test this library.The general gist of testing all this is as follows:
@kestra-io/libs
package as a dependency@kestra-io/libs
project.package.json
andindex.js
file, you can simply use CJS and it will import the CJS bundle from our Kestra library. I'd recommend creating a simple express.js server using the following tutorial: https://expressjs.com/en/starter/hello-world.html. This will give you an insight into how this library works in a real use case."type": "module"
in package.json and your project will be changed to ESM now and you are not required to change the file extension at all, everything will work seamlessly. But you will need to change the syntax to use ESM utilisingimport
andexport
etc instead of therequire
syntax.*.d.ts
files.And there you have it, full support for CJS, ESM and TypeScript with this library without any breaking changes 🎉.
P.S. I would like some feedback and more people to test this so that everything works as mentioned so feel free to post in the comments if it all works for you.
Closes issue #9