import-essex
Version:
Import JSX as essex components in NodeJS
42 lines (33 loc) • 901 B
JavaScript
import process from 'node:process';
import cachedTransform, { cacheKeyFromSource } from './cache.js';
import transform from './transform.js';
export const load = async (url, _context, nextLoad) => {
if (!url.endsWith('.js') || url.includes('node_modules')) {
return nextLoad(url);
}
const result = await nextLoad(url);
if (!result.source) {
return result;
}
const source = result.source.toString();
const useCache =
process.env.IMPORT_ESSEX_CACHE !== '0' &&
process.env.IMPORT_ESSEX_CACHE !== 'false';
const cacheKey = cacheKeyFromSource(source);
try {
const transformedSource = await cachedTransform(
() => transform(source, url),
{
enabled: useCache,
key: cacheKey,
}
);
return {
source: transformedSource,
format: 'module',
shortCircuit: true,
};
} catch {
return nextLoad(url);
}
};