wrolpi/app/jest/babelTransform.js
lrnselfreliance e5987fdd7e
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>
2026-11-07 11:55:55 -07:00

30 lines
1.1 KiB
JavaScript

'use strict';
// Replaces CRA's JS transform so Jest can load three.js. three (and three-stdlib) ship ES modules
// with static class blocks, which babel-preset-react-app's class-properties plugin rejects at our
// pinned Babel version. Those files only need their imports converted to CommonJS; every other
// file goes through CRA's transform unchanged.
const babelJest = require('babel-jest').default;
const craTransform = require('react-scripts/config/jest/babelTransform');
const threeTransform = babelJest.createTransformer({
babelrc: false,
configFile: false,
plugins: [require.resolve('@babel/plugin-transform-modules-commonjs')],
});
const THREE_PATH = /[\\/]node_modules[\\/]three(-stdlib)?[\\/]/;
function pick(sourcePath) {
return THREE_PATH.test(sourcePath) ? threeTransform : craTransform;
}
module.exports = {
canInstrument: true,
getCacheKey(sourceText, sourcePath, options) {
return pick(sourcePath).getCacheKey(sourceText, sourcePath, options);
},
process(sourceText, sourcePath, options) {
return pick(sourcePath).process(sourceText, sourcePath, options);
},
};