-
Notifications
You must be signed in to change notification settings - Fork 501
Migrate package to ES6 Module #579
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
Conversation
I'm not a fan of dropping those functions. If we really have to, let's at least go for your suggestion:
What about a different approach: Use dynamic imports and declare |
Seems like the best solution so far, but unlike synchronous may as well drop the loadSync API for 2.0.0 and only keep .load() / .download() ? |
Yeah, I found that funny too. :) but the function still works kind of synchronously itself using await, it just had to be declared as async to be able to use the await keyword. |
I'll also take a look at import { createRequire } from 'module'; Tomorrow, not sure about the node version requirements and browser compatibility yet. |
That's what I tested If we HAVE to keep those user-side function, can we at least drop the But I'd like to remind that all those functions are a mess to maintain, they rely on vendors API and I was hoping to drop them all for 2.x |
- use last mocha: 0 vulnerability - remove reify deps: unsupported in last mocha version
26a7385
to
f74be7f
Compare
@Connum Here is what I did:
Tell me if you have any improvement ideas |
Heads up: Attempting to use this library within my ESM app run through vite-node, and it encounters a runtime error due to the lack of |
Heads up: switching to ESM entirely will completely break support for a ton of libraries already built on opentype.js, including mine. |
@ILOVEPIE are you sure about that breakage ? because in this PR, I took the effort to make it backward compatible. so all previous path are still there and there is now a new opentype.module.js artefact IIRC |
Definitely not suggesting to remove CJS support, just to get ESM working in conjunction! |
Shall I rebase/resolve this PR or can it still wait ? |
I think we should wait until some of the larger PRs are merged. It's easier to then make the required code changes here than getting all the different PR authors do so. |
Understood, I'll be ready :) |
This has been resolved. |
Motivation and Context
Opentype.js codebase is structured as modules (see:
import
/export
lines), but it is not declared as such (type:module
in package.json) because we rely on commonJsrequire()
syntax to import node-specific function (ex:fs
). This "hybrid" usage forced us to usereify
in out tests order to makemocha<=8
work (but mocha 8 also has at least 2 known vulnerabilities)Sadly this combo don't work anymore with latest mocha v10, so we are stuck on a vulnerable version.
So here are 4 possibles structure we could use:
commonjs
.js
commonjs
js
=>.mjs
module
.js
require(*.js)
: .js are now considered ESMmodule
.cjs
+.js
I used the less breaking options (n°4) :
<script src="//cdn.example/opentypejs/dist/opentype.js"></script>
import * as opentype from "//cdn.example/opentypejs/dist/opentype.module.js"
require('opentype.js')
which load the.cjs
version (disambiguation required)import * as opentype from 'opentype.js'
which load the.module.js
versionBut declaring as
type:module
mean thatrequire()
must be migrated toimport
/import()
.How Has This Been Tested?
by releasing my 2.0.1 fork on NPM and using it
with a node script
main.js
with a node script
main.mjs
with an index.html testing both cjs/jsm cases
Types of changes
.load()
/.download()
functionsChecklist:
npm run test
and all tests passed green (including code styling checks).