diff --git a/test/loader.js b/test/loader.js index 1ad95863..66df2219 100644 --- a/test/loader.js +++ b/test/loader.js @@ -1,17 +1,51 @@ +import {promises as fs} from 'node:fs' import path from 'node:path' -import {fileURLToPath} from 'node:url' -import {transformSync} from 'esbuild' +import {URL, fileURLToPath} from 'node:url' +import {transform, transformSync} from 'esbuild' -const {getFormat, transformSource} = createLoader() +const {load, getFormat, transformSource} = createLoader() -export {getFormat, transformSource} +export {load, getFormat, transformSource} /** * A tiny JSX loader. */ export function createLoader() { - return {getFormat, transformSource} + return {load, getFormat, transformSource} + // Node version 17. + /** + * @param {string} url + * @param {unknown} context + * @param {Function} defaultLoad + */ + async function load(url, context, defaultLoad) { + if (path.extname(url) !== '.jsx') { + return defaultLoad(url, context, defaultLoad) + } + + const fp = fileURLToPath(new URL(url)) + const value = await fs.readFile(fp) + + const {code, warnings} = await transform(String(value), { + sourcefile: fp, + sourcemap: 'both', + loader: 'jsx', + target: 'esnext', + format: 'esm' + }) + + if (warnings && warnings.length > 0) { + for (const warning of warnings) { + console.log(warning.location) + console.log(warning.text) + } + } + + return {format: 'module', source: code} + } + + // Pre version 17. /** * @param {string} url * @param {unknown} context