-
Notifications
You must be signed in to change notification settings - Fork 1
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
chore(test): refactor tests to use ESM syntax #2
Conversation
…mmon Fully resolves highlightjs#2848. Related highlightjs#2558.
Related highlightjs#2923. Work towards highlightjs#2558.
It's too specific to CSS to be a general rule.
- Use `delphi` instead or add your own aliases
Builds the browser and CDN `highlight.js` as a single unit, as in Rollup sees the entire dependency tree and can optimize it most effectively. Previously we compiled core separately and each languages separately. The final distributable was then a concatenation of all these separate builds - meaning that often it could include repeated code from grammars pulling in the same dependencies (regex libs, etc.) The common build (minified) is now 20kb smaller which results in a 4-5kb reduction in the gzip size.
@joshgoebel note that I haven't updated |
No idea, is it an async issue? |
Hum apparently that was because I was using |
Oh wait because you included both... is that intentional? I thought we only needed the CJS one for the one thing? |
It was intentional, I think we want to make sure both ESM and CJS users are able to run the library. It's more an integration test than a unit test spirit. |
|
||
describe('no highlighting', () => { | ||
describe('no highlighting', function() { |
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.
Was this (and the other =>
=> function
changes) necessary or is this just a style change?
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.
Yes, because this
is undefined in an ES module, so the test throws when arriving at this.blocks =
. Another way of fixing it would be to declare a variable blocks
in the function scope and do a s/this.blocks/blocks/
replacement.
But when I merge our work (and rename the files back to .js) I get errors: https://github.com/joshgoebel/highlight.js/tree/esm-tests-more-work I find the error entirely opaque:
|
I agree the error message is unclear. From what I understand, this is due to syntax errors in the
It seems the |
You need to replace highlight.js/tools/build_node.js Line 21 in 22ec6ff
With return `${require};hljs.registerLanguage('${lang.name}', ${importName});`; But then we run into the issue that the module doesn't have default export… |
highlight.js/tools/build_node.js Line 15 in 22ec6ff
This is not very robust, it replaces only the first dash. It would be worth doing instead: const importName = "L_" + lang.name.replace(/\W/g,"_"); |
If I import first then use the identifier as you suggest then I just hit:
|
Yes, |
I thought the only reason Changing it takes you right back to
|
I thought you said this "just worked"... how can we publish an ES6 library if it can't be required by people? I feel like I'm missing something. I also thought I tried it before and it worked... so ugh. |
CJS users would need to |
Hmmm... If I remove the async/await I'm stuck with:
So does this mean users must also be forced to make sure their call site is async? |
They can either use a bundler, switch to ESM, or make their API async. |
Ugh. I believe somewhere along the line I got confused (and maybe I didn't do enough testing) that since ES6 modules were "fully supported" now this would be more of a drop-in change... that sounds potentially like a LOT of work for a lot of people. It seems like a lot less effort for the ES6 people to deal with a library being CJS than for CJS people to deal with a library switching to ES6. I'm not sure I see the advantage of making this change for Node users on the server-side (is there any??) - and seems to force a lot of work on them. ES6 modules in the browser was never the goal here, that was only a possible side-benefit that came for free if the ecosystem was truly ready for a "painless" upgrade from CJS to module, but it's starting to feel like it isn't. Is there something here I'm missing? |
Sorry I failed to set the correct expectations for the move to ESM. It is indeed a limitation with
Let me disagree, working with CJS in the browser is a pain in my experience, you have to load a bunch of code to make it work somehow, or to use a tool that do that for you, or use the UMD bundle – either way you're pushing more JS to your users and have a (subjective) inferior developer experience.
The two systems are very different, I think it's very subjective which are advantages and which are limitations. The key feature for me is the ability for me to use the same syntax/module loading mechanisms for both browser and server development without setting up any tool.
I tried to find surveys that would help us finding out how spread out ESM is in the ecosystem, but couldn't find anything recent enough. If I was following my guts, I'd say we should do it, CJS is clearly a legacy system that is probably going to fade out. Anyway, if we are not happy with the
I feel quite strongly that ESM is the correct way forward, however I realize I'm the only one who actually requested it, so I would understand if highlight.js as a project chose a different path. However I'll still try to use my humble influence of mere contributor of the project to guide it towards the light path of ESM 😉 |
Link please, I would take a look at what that looks like. |
Note that it's not exactly the same thing, as the main API is promise-based, and I'm providing a different synchronous API, transpiled to CJS. |
Yeah...
I was looking for an example of a library that's taken this approach that I could have a look at. Do you know of any? |
Rollup does something like that, they use I've created #3 to demonstrate what it would look like for this library. |
78a0453
to
fbdbc7b
Compare
Changes
Checklist
CHANGES.md