ASCII STL files preview again (#948)

react-stl-viewer pins three-stdlib 2.17.2, whose STLLoader decodes ASCII STL
files with three's LoaderUtils.decodeText.  three removed that function, so
every ASCII STL preview failed while binary STLs (which never decode text)
still worked.  Override three-stdlib to 2.36.1, which ships its own decodeText.

Jest can now load three: a small transform converts three/three-stdlib ES
modules to CommonJS without babel-preset-react-app, whose class-properties
plugin rejects three's static class blocks at our pinned Babel version.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
lrnselfreliance 2026-11-07 11:55:55 -07:00 • committed by GitHub
parent 20439437f8
commit e5987fdd7e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 103 additions and 240 deletions

View file

@ -0,0 +1,41 @@
// react-stl-viewer parses STL files with three-stdlib's STLLoader against our top-level three.
// Both STL encodings must parse with the installed three/three-stdlib pair.
import {STLLoader} from 'three-stdlib/loaders/STLLoader.js';
const ASCII_STL = `solid ASCII
facet normal 0 0 1
outer loop
vertex 0 0 0
vertex 1 0 0
vertex 0 1 0
endloop
endfacet
endsolid
`;
function binaryStl() {
// 80-byte header, uint32 triangle count, then one 50-byte triangle.
const buffer = new ArrayBuffer(84 + 50);
const view = new DataView(buffer);
view.setUint32(80, 1, true);
const floats = [0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0];
floats.forEach((value, i) => view.setFloat32(84 + i * 4, value, true));
return buffer;
}
describe('STLLoader', () => {
it('parses an ASCII STL file', () => {
const buffer = new TextEncoder().encode(ASCII_STL).buffer;
const geometry = new STLLoader().parse(buffer);
expect(geometry.getAttribute('position').count).toBe(3);
expect(Array.from(geometry.getAttribute('position').array))
.toEqual([0, 0, 0, 1, 0, 0, 0, 1, 0]);
});
it('parses a binary STL file', () => {
const geometry = new STLLoader().parse(binaryStl());
expect(geometry.getAttribute('position').count).toBe(3);
expect(Array.from(geometry.getAttribute('position').array))
.toEqual([0, 0, 0, 1, 0, 0, 0, 1, 0]);
});
});