import type { Plugin } from 'vite' ; import { transformSync , PluginItem } from '@babel/core' ; import BabelPluginKirei from 'babel-plugin-kirei' ; import TestExclude from 'test-exclude' ; interface KireiPluginOptions { include ? : string | string [ ] ; exclude ? : string | string [ ] ; extension ? : string | string [ ] ; cwd ? : string ; plugins ? : PluginItem [ ] ; } param opts returns export default function kireiPlugin ( opts = { } ) { const cwd = process . cwd ( ) ; const exclude = new TestExclude ( { cwd : opts . cwd , include : opts . include , exclude : opts . exclude , extension : opts . extension , excludeNodeModules : true , } ) ; const plugins = [ [ BabelPluginKirei , { harmony : true , guard : false , } ] , ] ; if ( Array . isArray ( opts . plugins ) ) { plugins . push ( ... opts . plugins ) ; } return { name : 'vite:kirei' , transform ( srcCode , id ) { if ( ! __DEV__ || id . startsWith ( '/@modules/' ) ) { return ; } if ( exclude . shouldInstrument ( id ) ) { const { code , map } = transformSync ( srcCode , { plugins , cwd , filename : id , ast : false , sourceMaps : true , } ) ; return { code , map } ; } } , } ; }