import { compile } from 'coffeescript' ; import { transformAsync , TransformOptions } from '@babel/core' ; import { LoaderConfig } from '../worker' ; export const name = 'coffeescript' ; const options = { sourceMaps : 'inline' , plugins : [ ] , } ; export function prepare ( config ) { if ( config . jsxFactory ) { options . plugins . push ( [ '@babel/plugin-transform-react-jsx' , { pragma : config . jsxFactory , pragmaFrag : config . jsxFragment , useSpread : true , } ] ) ; } } export function extractScript ( expr ) { try { compile ( expr ) ; } catch ( e ) { if ( e . message !== 'unmatched }' ) throw e ; const location = e . location ; const sLines = expr . split ( '\n' ) ; const row = location . first_line ; const column = location . first_column - 1 ; return [ ... sLines . slice ( 0 , row - 1 ) , sLines [ row ] . slice ( 0 , column + 1 ) ] . join ( '\n' ) ; } } export async function transformScript ( expr ) { expr = `do -> (${ expr })` ; const raw = compile ( expr , { bare : true } ) ; const { code } = await transformAsync ( raw , options ) ; return code ; } export async function transformModule ( expr ) { const raw = compile ( expr , { bare : true } ) ; const { code } = await transformAsync ( raw , options ) ; return code ; }