Skip to content

Commit 3c1647e

Browse files
authored
Merge pull request #965 from zeux/js-esm
js: Remove CJS modules and rename ESM modules to .js
2 parents 67b720d + 56bc72d commit 3c1647e

31 files changed

Lines changed: 84 additions & 1338 deletions

‎.github/workflows/build.yml‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,8 @@ jobs:
9292
- name: check es5
9393
run: |
9494
npm install -g es-check@7.2.1
95-
npx es-check es5 js/meshopt_decoder.js js/meshopt_encoder.js js/meshopt_simplifier.js js/meshopt_clusterizer.js
96-
npx es-check --module es5 js/meshopt_decoder.module.js js/meshopt_encoder.module.js js/meshopt_simplifier.module.js js/meshopt_clusterizer.module.js
97-
npx es-check es5 gltf/library.js
95+
npx es-check --module es5 js/meshopt_decoder.js js/meshopt_encoder.js js/meshopt_simplifier.js js/meshopt_clusterizer.js
96+
npx es-check --module es2020 gltf/library.js
9897
9998
gltfpack:
10099
runs-on: ubuntu-latest

‎Makefile‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ format:
137137
formatjs:
138138
prettier -w js/*.js gltf/*.js demo/*.html js/*.ts
139139

140-
js: js/meshopt_decoder.js js/meshopt_decoder.module.js js/meshopt_encoder.js js/meshopt_encoder.module.js js/meshopt_simplifier.js js/meshopt_simplifier.module.js js/meshopt_clusterizer.js js/meshopt_clusterizer.module.js
140+
js: js/meshopt_decoder.js js/meshopt_encoder.js js/meshopt_simplifier.js js/meshopt_clusterizer.js
141141

142142
symbols: $(BUILD)/amalgamated.so
143143
nm $< -U -g
@@ -196,10 +196,12 @@ js/meshopt_encoder.js js/meshopt_simplifier.js js/meshopt_clusterizer.js:
196196
sed -i "s#Built from meshoptimizer .*#Built from meshoptimizer $$(cat src/meshoptimizer.h | grep -Po '(?<=version )[0-9.]+')#" $@
197197
python3 tools/wasmpack.py patch $@ wasm <$<
198198

199-
js/%.module.js: js/%.js
200-
sed '\#// export!#q' <$< >$@
201-
sed -i "/use strict.;/d" $@
202-
sed -i "s#// export! \(.*\)#export { \\1 };#" $@
199+
js/meshopt_decoder.cjs: js/meshopt_decoder.js
200+
sed '/export {/d' <$< >$@
201+
echo "if (typeof exports === 'object' && typeof module === 'object') module.exports = MeshoptDecoder;" >>$@
202+
echo "else if (typeof define === 'function' && define['amd']) define([], function () { return MeshoptDecoder; });" >>$@
203+
echo "else if (typeof exports === 'object') exports['MeshoptDecoder'] = MeshoptDecoder;" >>$@
204+
echo "else (typeof self !== 'undefined' ? self : this).MeshoptDecoder = MeshoptDecoder;" >>$@
203205

204206
$(DEMO): $(DEMO_OBJECTS) $(LIBRARY)
205207
$(CXX) $^ $(LDFLAGS) -o $@

‎demo/index.html‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
<script type="module">
5050
import * as THREE from 'three';
5151
import { GLTFLoader } from 'three-examples/loaders/GLTFLoader.js';
52-
import { MeshoptDecoder } from '../js/meshopt_decoder.module.js';
52+
import { MeshoptDecoder } from '../js/meshopt_decoder.js';
5353

5454
var container;
5555

‎demo/simplify.html‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,8 @@
127127
import { OBJLoader } from 'three-examples/loaders/OBJLoader.js';
128128
import { OrbitControls } from 'three-examples/controls/OrbitControls.js';
129129
import { mergeVertices } from 'three-examples/utils/BufferGeometryUtils.js';
130-
import { MeshoptDecoder } from '../js/meshopt_decoder.module.js';
131-
import { MeshoptSimplifier } from '../js/meshopt_simplifier.module.js';
130+
import { MeshoptDecoder } from '../js/meshopt_decoder.js';
131+
import { MeshoptSimplifier } from '../js/meshopt_simplifier.js';
132132
import { GUI } from 'lil-gui';
133133

134134
var container, gridContainer;
@@ -817,7 +817,7 @@
817817
}
818818

819819
function reload() {
820-
var simp = import('/js/meshopt_simplifier.module.js?x=' + Date.now());
820+
var simp = import('/js/meshopt_simplifier.js?x=' + Date.now());
821821
MeshoptSimplifier.ready = simp.then(function (s) {
822822
return s.MeshoptSimplifier.ready.then(function () {
823823
for (var prop in s.MeshoptSimplifier) {
@@ -832,7 +832,7 @@
832832
function autoReload() {
833833
if (!settings.autoUpdate) return;
834834

835-
fetch('/js/meshopt_simplifier.module.js?x=' + Date.now(), { method: 'HEAD' })
835+
fetch('/js/meshopt_simplifier.js?x=' + Date.now(), { method: 'HEAD' })
836836
.then(function (r) {
837837
var last = r.headers.get('Last-Modified');
838838
if (last != moduleLastModified) {

‎gltf/README.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ When working with glTF files that contain point clouds, gltfpack automatically p
2828

2929
## Decompression
3030

31-
When using compressed files, [js/meshopt_decoder.js](https://github.com/zeux/meshoptimizer/blob/master/js/meshopt_decoder.js) or `js/meshopt_decoder.module.js` needs to be loaded to provide the WebAssembly decoder module like this:
31+
When using compressed files, [js/meshopt_decoder.js](https://github.com/zeux/meshoptimizer/blob/master/js/meshopt_decoder.js) needs to be loaded to provide the WebAssembly decoder module like this:
3232

3333
```js
34-
import { MeshoptDecoder } from './meshopt_decoder.module.js';
34+
import { MeshoptDecoder } from './meshopt_decoder.js';
3535

3636
...
3737

‎gltf/cli.js‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
#!/usr/bin/env node
22
// This file is part of gltfpack and is distributed under the terms of MIT License.
3-
var gltfpack = require('./library.js');
4-
5-
var fs = require('fs');
3+
import { pack } from './library.js';
4+
import fs from 'fs';
65

76
var args = process.argv.slice(2);
87

9-
var interface = {
8+
var iface = {
109
read: function (path) {
1110
return fs.readFileSync(path);
1211
},
@@ -15,8 +14,7 @@ var interface = {
1514
},
1615
};
1716

18-
gltfpack
19-
.pack(args, interface)
17+
pack(args, iface)
2018
.then(function (log) {
2119
process.stdout.write(log);
2220
process.exit(0);

‎gltf/library.js‎

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -345,28 +345,12 @@ function uploadArgv(argv) {
345345
}
346346

347347
// Automatic initialization for node.js
348-
if (typeof window === 'undefined' && typeof process !== 'undefined' && process.release.name === 'node') {
349-
var fs = require('fs');
350-
var util = require('util');
351-
352-
// Node versions before v12 don't support TextEncoder/TextDecoder natively, but util. provides compatible replacements
353-
if (typeof TextEncoder === 'undefined' && typeof TextDecoder === 'undefined') {
354-
TextEncoder = util.TextEncoder;
355-
TextDecoder = util.TextDecoder;
356-
}
357-
358-
init(fs.readFileSync(__dirname + '/library.wasm'));
348+
if (typeof process !== 'undefined' && process.release && process.release.name === 'node') {
349+
init(
350+
import('node:fs').then(function (fs) {
351+
return fs.readFileSync(new URL('./library.wasm', import.meta.url));
352+
})
353+
);
359354
}
360355

361-
// UMD
362-
(function (root, factory) {
363-
if (typeof define === 'function' && define.amd) {
364-
define([], factory);
365-
} else if (typeof module === 'object' && module.exports) {
366-
module.exports = factory();
367-
} else {
368-
root.gltfpack = factory();
369-
}
370-
})(typeof self !== 'undefined' ? self : this, function () {
371-
return { init: init, pack: pack };
372-
});
356+
export { init, pack };

‎gltf/package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"type": "git",
1414
"url": "https://github.com/zeux/meshoptimizer"
1515
},
16+
"type": "module",
1617
"bin": "./cli.js",
1718
"main": "./library.js",
1819
"files": [

‎js/README.md‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,7 @@ This folder contains JavaScript/WebAssembly modules that can be used to access p
44

55
## Structure
66

7-
Each component comes in two variants:
8-
9-
- `meshopt_component.js` uses a UMD-style module declaration and can be used by a wide variety of JavaScript module loaders, including node.js require(), AMD, Common.JS, and can also be loaded into the web page directly via a `<script>` tag which exposes the module as a global variable
10-
- `meshopt_component.module.js` uses ES6 module exports and can be imported from another ES6 module
11-
12-
In either case the export name is MeshoptComponent and is an object that has two fields:
7+
Each component comes in a separate file, `meshopt_component.js`, which uses ES6 module exports and can be imported from another ES6 module. The export name is MeshoptComponent and is an object that has two fields:
138

149
- `supported` is a boolean that can be checked to see if the component is supported by the current execution environment; it will generally be `false` when WebAssembly is not supported or enabled. To use these components on browsers without WebAssembly a polyfill library is recommended.
1510
- `ready` is a Promise that is resolved when WebAssembly compilation and initialization finishes; any functions are unsafe to call before that happens.
@@ -20,6 +15,8 @@ In addition to that, each component exposes a set of specific functions document
2015

2116
`MeshoptDecoder` (`meshopt_decoder.js`) implements high performance decompression of attribute and index buffers encoded using meshopt compression. This can be used to decompress glTF buffers encoded with `EXT_meshopt_compression` extension or for custom geometry compression pipelines. The module contains two implementations, scalar and SIMD, with the best performing implementation selected automatically. When SIMD is available, the decoders run at 1-3 GB/s on modern desktop computers.
2217

18+
> Note: for maximum compatibility, MeshoptDecoder is also available as CommonJS module via `meshopt_decoder.cjs`; it can be used by a wide variety of JavaScript module loaders, including node.js require(), AMD, Common.JS, and can also be loaded into the web page directly via a `<script>` tag which exposes the module as a global variable `MeshoptDecoder`.
19+
2320
To decode a buffer, one of the decoding functions should be called:
2421

2522
```ts

‎js/benchmark.js‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
var encoder = require('./meshopt_encoder.js');
2-
var decoder = require('./meshopt_decoder.js');
3-
var { performance } = require('perf_hooks');
1+
import { MeshoptEncoder as encoder } from './meshopt_encoder.js';
2+
import { MeshoptDecoder as decoder } from './meshopt_decoder.js';
3+
import { performance } from 'perf_hooks';
44

55
process.on('unhandledRejection', (error) => {
66
console.log('unhandledRejection', error);

0 commit comments

Comments
 (0)