Skip to content

Instantly share code, notes, and snippets.

@sibelius
Created March 9, 2025 14:50
Show Gist options
  • Save sibelius/851fd7cc47b94d604fd11c3ec331ba89 to your computer and use it in GitHub Desktop.
Save sibelius/851fd7cc47b94d604fd11c3ec331ba89 to your computer and use it in GitHub Desktop.
tsx with node
import { transform } from '@swc/core';
import fs from 'fs/promises';
import path from 'path';
import { fileURLToPath } from 'url';
const cwd = process.cwd();
export async function load(url, context, defaultLoad) {
if (url.endsWith('.tsx')) {
const filePath = fileURLToPath(url);
const source = await fs.readFile(filePath, 'utf8');
const { code } = await transform(source, {
filename: filePath,
jsc: {
parser: {
syntax: 'typescript',
tsx: url.endsWith('.tsx'),
},
target: 'esnext',
},
module: {
type: 'es6',
},
});
return {
format: 'module',
source: code,
shortCircuit: true,
};
}
return defaultLoad(url, context, defaultLoad);
}
node --experimental-strip-types --disable-warning='ExperimentalWarning' --import ./register.js
import { register } from 'node:module';
import { pathToFileURL } from 'node:url';
register('./file-loader.js', pathToFileURL('./'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment