Konubinix' opinionated web of thoughts

To Transpile Data in Nodejs

Fleeting

to transpile data in nodejs

see also playwright/test + ts-node = wrong stack traces

see the code of ts-node

const old = require.extensions[ext];
require.extensions["myextension"] = function (m, filename) {

        const _compile = m._compile;
        m._compile = function (code, fileName) {
            const result = service.compile(code, fileName);
            return _compile.call(this, result, fileName);
        };
        return old(m, filename);
    };

Then, deal with the sourcemapsupport

const sourceMapSupport = require('@cspotcode/source-map-support') as typeof _sourceMapSupport;
sourceMapSupport.install({
    environment: 'node',
    retrieveSourceMap(pathOrUrl: location){
        ....
    },
    retrieveFile(pathOrUrl: string) {
        let path = pathOrUrl;
        // If it's a file URL, convert to local path
        // I could not find a way to handle non-URLs except to swallow an error
        if (path.startsWith('file://')) {
            try {
                path = fileURLToPath(path);
            } catch (e) {
                /* swallow error */
            }
        }
        path = normalizeSlashes(path);
        return outputCache.get(path)?.content || '';
    },