import { loader as webpackLoader } from 'webpack' ; import { getOptions } from 'loader-utils' ; import { resolve } from 'upath' ; import { matcher , scanComponents } from './util/scanComponents' ; import { PluginOptions , Component } from './types' ; export default async function loader ( this , source ) { this . cacheable ( ) ; if ( this . resourceQuery ) return source ; const options = getOptions ( this ) as unknown as PluginOptions ; const tags = options . extractor . call ( this , options ) ; if ( ! tags || tags . length <= 0 ) return source ; const scannedComponents = await scanComponents ( options , resolve ( './src' ) ) ; scannedComponents . forEach ( c => this . addDependency ( c . filePath ) ) ; const generateLazyLoadImport = ( c ) => { if ( options . vueVersion === 3 ) return `Lazy${ c . pascalName }: defineAsyncComponent(() => import('${ c . shortPath . replace ( ';' , '' ) }'))` ; return `Lazy${ c . pascalName }: () => import('${ c . shortPath . replace ( ';' , '' ) }')` ; } ; const componentsWithLazyImports = scannedComponents . map ( c => [ c , { ... c , pascalName : `Lazy${ c . pascalName }` , import : generateLazyLoadImport ( c ) , kebabName : `lazy-${ c . kebabName }` , lazy : true , } as Component ] ) . flat ( ) ; const components = matcher ( tags , componentsWithLazyImports ) ; if ( components . length <= 0 ) return source ; return options . injector . call ( this , source , components ) ; }