Skip to content
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

simple ESM export and package.json #8

Merged
merged 3 commits into from
Mar 27, 2023
Merged

simple ESM export and package.json #8

merged 3 commits into from
Mar 27, 2023

Conversation

micahscopes
Copy link
Contributor

@micahscopes micahscopes commented Mar 16, 2023

Hey, I love this library and look forward to using it for more and more stuff! For my own project I'm using it as an ESM module and installing with npm.

(resolves #5)

@znah
Copy link
Collaborator

znah commented Mar 17, 2023

Hey @micahscopes, thank you for the pull request!
I have one concern which most web devs would probably find absurd:
I value the ability to simply open html file locally and have demos run without having to run a webserver, so that one needs nothing but chrome devtools to start experimenting. Do you think it's possible to preserve this capability somehow? CJS instead of ESM?

@micahscopes
Copy link
Contributor Author

@znah oh of course, I ought to make sure the demos work. ESM would be the one that would run without any build step or server, since the browsers support direct ESM imports. The demos might actually work as-is with this PR, but I'll double check. But yeah, with ESM all you'd need to get started is chrome dev tools, and I believe you could keep your original workflow too, there's no requirement to use ESM as far as I know. This just makes it easier to use it quickly in a larger project.

@znah
Copy link
Collaborator

znah commented Mar 17, 2023

I'm getting CORS errors when trying to import ESM module from "file://" (w/o local webserver)

@micahscopes
Copy link
Contributor Author

Ah, I see, I didn't realize ESM modules can only be loaded using CORS, I thought maybe it'd be possible to load them without type="module".

Do you think it's possible to preserve this capability somehow? CJS instead of ESM?

Well yes, I see now, it'd be nice to preserve the ability to use the library as is. The "conventional" way of doing this would be to include a build step that generates ESM and IIFE versions (for classic script import) from ESM or CJS sources. But then that would make it messier to make releases.

I think I have a workaround. It's possible to "import" ordinary JavaScript using import file.js, which just runs the file. What we can do is in swissgl.js save the main function to the global scope, then create an auxiliary swissgl.mjs that imports swissgl.js and exports the main function from the global scope. I'll try it now, I think it should work and it avoids a build step!

@micahscopes micahscopes force-pushed the main branch 2 times, most recently from f478037 to 48453b2 Compare March 17, 2023 12:29
@micahscopes
Copy link
Contributor Author

Okay I tried it and it seems to work while also preserving the demo functionality and avoiding a build step!

@arsiliath
Copy link

+1 for this functionality

would be nice to be able to import this

@znah
Copy link
Collaborator

znah commented Mar 25, 2023

Sure, I think I'm done with breaking API changes, will review and merge soon

@znah znah merged commit f252d92 into google:main Mar 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ESM package?
3 participants