Created
March 9, 2025 14:50
-
-
Save sibelius/851fd7cc47b94d604fd11c3ec331ba89 to your computer and use it in GitHub Desktop.
tsx with node
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
node --experimental-strip-types --disable-warning='ExperimentalWarning' --import ./register.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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