()
| 10 | * A tiny JSX loader. |
| 11 | */ |
| 12 | export function createLoader() { |
| 13 | return {load, getFormat, transformSource} |
| 14 | |
| 15 | // Node version 17. |
| 16 | /** |
| 17 | * @param {string} href |
| 18 | * @param {unknown} context |
| 19 | * @param {Function} defaultLoad |
| 20 | */ |
| 21 | async function load(href, context, defaultLoad) { |
| 22 | const url = new URL(href) |
| 23 | |
| 24 | if (!url.pathname.endsWith('.jsx')) { |
| 25 | return defaultLoad(href, context, defaultLoad) |
| 26 | } |
| 27 | |
| 28 | const {code, warnings} = await transform(String(await fs.readFile(url)), { |
| 29 | format: 'esm', |
| 30 | loader: 'jsx', |
| 31 | sourcefile: fileURLToPath(url), |
| 32 | sourcemap: 'both', |
| 33 | target: 'esnext' |
| 34 | }) |
| 35 | |
| 36 | if (warnings) { |
| 37 | for (const warning of warnings) { |
| 38 | console.log(warning.location) |
| 39 | console.log(warning.text) |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | return {format: 'module', shortCircuit: true, source: code} |
| 44 | } |
| 45 | |
| 46 | // Pre version 17. |
| 47 | /** |
| 48 | * @param {string} href |
| 49 | * @param {unknown} context |
| 50 | * @param {Function} defaultGetFormat |
| 51 | */ |
| 52 | function getFormat(href, context, defaultGetFormat) { |
| 53 | const url = new URL(href) |
| 54 | |
| 55 | return url.pathname.endsWith('.jsx') |
| 56 | ? {format: 'module'} |
| 57 | : defaultGetFormat(href, context, defaultGetFormat) |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * @param {Buffer} value |
| 62 | * @param {{url: string, [x: string]: unknown}} context |
| 63 | * @param {Function} defaultTransformSource |
| 64 | */ |
| 65 | async function transformSource(value, context, defaultTransformSource) { |
| 66 | const url = new URL(context.url) |
| 67 | |
| 68 | if (!url.pathname.endsWith('.jsx')) { |
| 69 | return defaultTransformSource(value, context, defaultTransformSource) |
no outgoing calls
no test coverage detected
searching dependent graphs…