Skip to content

js: Remove CJS modules and rename ESM modules to .js - #965

Merged
zeux merged 10 commits into
masterfrom
js-esm
Oct 8, 2025
Merged

zeux merged 10 commits into
masterfrom
js-esm

Conversation

@zeux

@zeux zeux commented Oct 6, 2025 •

Copy link
Copy Markdown
Owner

As of this PR, meshoptimizer and gltfpack NPM modules are ESM-only. All .module.js files are renamed to just .js.

Any use from Node that currently uses import 'meshoptimizer' should still work as is with no changes. Imports of specific files may need to be adjusted to remove the .module suffix.

Any use from Node via require needs to use --experimental-require-module on earlier Node versions, or switch to import.

Any use from the web browser needs to use import, with the exception of MeshoptDecoder which, for maximum compatibility, is still maintained as a non-ESM module (available via meshopt_decoder.cjs although it supports other legacy JS module loaders too).

In addition, meshoptimizer NPM package now provides exports that support importing individual modules e.g. import { MeshoptEncoder } from 'meshoptimizer/encoder'.

Why do this?

Maintaining two separate files for each component is becoming onerous. In node.js environment, currently most dependents (glTF-Transform, Cesium, etc.) are using ESM anyhow, but it gets routed to CJS module by Node's behavior - and fixing that is impossible without changing the file names or directory structure due to how Node determines the type of each file.

Why do this now?

This is a breaking change that's bound to happen at some point as the ecosystem migrates to ESM. meshoptimizer 1.0, the first major version, is around the corner. It's likely that meshoptimizer 2.0 won't ship for 5+ more years, and I don't want to wait for 5 more years to drop pre-ESM support.

On the flip side, node 18 and earlier are out of support, and node 20+ should even support the require-ESM feature which makes migration for existing node projects easy even if they don't use ESM.

Thanks to Don McCurdy for discussions and advice

zeux added 8 commits October 6, 2025 12:13
This is an initial commit for the migration towards ESM. meshoptimizer
will stop supporting CJS (except for meshopt_decoder.cjs which will be
added later) to simplify the build and deployment process.

As a first step, remove the CJS files and rename ESM files so that
js/meshopt_clusterizer.js et al is the canonical name for the module.
Now that the package is marked as "module", tests need to use import
instead of require.
This file can be require'd or used in non-ESM friendly environments
instead of the previously available js/meshopt_decoder.js which is now
ESM only. This is exclusive for the decoder; other modules will not have
a CJS-equivalent replacement.
We no longer have .module.js files and .js files are now ESM.
Instead of using UMD, use ESM for library.js.

Instead of automatically initializing the module in node, move the
initialization to cli.js which is node-specific. We don't need the
TextEncoder workaround anymore either.
We no longer use .module extension and the .js files are now ESM.
We now support exporting individual modules by name as well.
For decoder, we still maintain the .cjs file to ensure maximum
compatibility.
meshopt_decoder.js is now ESM, and .module.js doesn't exist.
@zeux
zeux force-pushed the js-esm branch 2 times, most recently from 51e205a to eb6c76d Compare October 8, 2025 20:30
This improves compatibility when the package is used through node; we
used to automatically init the package using synchronous file loads.

To do that, we need to dynamically import node's fs module to avoid
taking a bundle-time dependency on it in browser builds.
@zeux
zeux merged commit 3c1647e into master Oct 8, 2025
13 checks passed
@zeux
zeux deleted the js-esm branch October 8, 2025 20:41
@zeux zeux mentioned this pull request Dec 5, 2025
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.

1 participant