import { transformSync } from '@builder/pack/deps/@babel/core' ; import { Plugin } from 'vite' ; const vitePluginLazy = ( routesPath ) : Plugin => { const routesRegExp = /src\/routes\.(j|t)s(x)?$/ ; return { name : 'vite-plugin-lazy' , enforce : 'pre' , transform ( code , id ) { if ( ! id . match ( routesRegExp ) ) { return ; } const parserPlugins = [ 'jsx' , 'importMeta' , 'topLevelAwait' , 'classProperties' , 'classPrivateMethods' , ] ; if ( /\.tsx?$/ . test ( id ) ) { parserPlugins . push ( 'typescript' ) ; parserPlugins . push ( 'decorators-legacy' ) ; } const result = transformSync ( code , { babelrc : false , configFile : false , filename : id , parserOpts : { sourceType : 'module' , allowAwaitOutsideFunction : true , plugins : parserPlugins , } , generatorOpts : { decoratorsBeforeExport : true } , plugins : [ [ require . resolve ( './babelPluginLazy' ) , { routesPath } ] ] , sourceFileName : id , } ) ; return result ; } } ; } ; export default vitePluginLazy ;