import { transform } from 'esbuild' ; import jiti from 'jiti' ; import type { Loader , LoaderResult } from '../loader' ; export const jsLoader = async ( input , { options } ) => { if ( ! [ '.ts' , '.js' , '.mjs' ] . includes ( input . extension ) || input . path . endsWith ( '.d.ts' ) ) { return ; } const output = [ ] ; let contents = await input . getContents ( ) ; if ( options . declaration && ! input . srcPath ?. endsWith ( '.d.ts' ) ) { output . push ( { contents , srcPath : input . srcPath , path : input . path , extension : '.d.ts' , declaration : true } ) ; } if ( input . extension === '.ts' ) { contents = await transform ( contents , { loader : 'ts' } ) . then ( r => r . code ) ; } const isCjs = options . format === 'cjs' ; if ( isCjs ) { contents = jiti ( ) . transform ( { source : contents , retainLines : false } ) . replace ( /^exports.default = /mg , 'module.exports = ' ) ; } output . push ( { contents , path : input . path , extension : options . ext ? `.${ options . ext }` : ( isCjs ? '.js' : '.mjs' ) } ) ; return output ; } ;