-
Notifications
You must be signed in to change notification settings - Fork 112
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
Ensure ESM support. Fixes #149 #150
Conversation
I removed the It causes multiple warnings about not linter not being able to resolve the imports, like
but it also compiles and works well. I added it as alternative to using the flag, IMO should be reverted (the last commit), but wanted to provide possible outcome. I don't think there is any good way of making it work without any of those solutions. I was reading online whole morning about using ES modules, and it seems TS devs/JS community still don't know (or still argue) which approach would be the best. Some of the stuff I found interesting:
Have to say, it's weird stuff Typescript is doing around ES modules. It feels like Typescript is stuck few years ago because holding to compatibly, or some part of community/devs are stubborn. |
Interesting stuff! Just a thought, you can use typescript-transform-paths with the Steps:
I think that would do what you want. I'm still not as knowledgeable about esm, but I'm getting there in having to deal with various support tickets for it in the repos I maintain. If this is a solution, maybe it would make sense to add a config option to |
I knew I could use transformers for that, problem is using I think I will try to explore other options, like maybe custom transformer just to add the There is also another issue apart from the import { decode, encode } from 'he'; because some exports can't be deduced (there are tools for it, but not perfect; even in this project there is Anyway, ESM with Typescript is messy, no official solutions provided as the standards aren't yet fully created. Yet, having |
It's probably best to leave the syntax unchanged and use a transformer to convert it on output. Although verbose, even the comment method is less likely to produce any side-effects later. As you mentioned, ESM in TS is very messy. That said, a transformer which added the The logic in the transformer you posted looks sound, however it does seem to be a bit outdated. Does not incorporate factory changes in the compiler API, so the methods it's using are deprecated. It will likely need an update in a few TS versions. |
The problem discussed a few years ago, until now has not been resolved, really fuck |
Thanks for all your work, It is a big change. I didn't know much about this. But I think we should take of this carefully. |
This should be addressed in the following PR: Would appreciate feedback to ensure it's working for your use cases @AgainPsychoX @apprat |
Corrected in v5 |
Fixes #149.
Tests included are passed okay (after fixing paths and adding
experimental-specifier-resolution=node
). I noticed the benchmarks on the README are "a bit" outdated, but it isn't my concern today.Why currently included tests were passing despite they were using ES modules?
There seems to be files in
test
folder using ES modules, among themcompare-node-html-parser.mjs
.Despite first impression, it wasn't using ES modules version from
./dist/esm/*
, but it was using./dist/index.js
; that's why the benchmark was running fine before.Additional testing
I tested proper the ESM support myself using simple project like:
package.json
(changing type to
commonjs
(when not added at all, it's alsocommonjs
))main.js
(I had the
node-html-parser
linked bynpm link
for the test)Results:
import
approach, it works only usingrequire
(commonjs).Notes
experimental-specifier-resolution=node
is used because generated files inesm
version don't have.js
/.mjs
extensions. It could be done by adding.js
extension in Typescript sources (yes, in.ts
adding.js
when importing), but it was more work and I wasn't sure should I really edit sources for it. The choice itself don't have to be final, and even node devs still don't know what they want to finally do with the directory name resolution in ES modules.