import { Component } from '../types' ; function injectImports ( variable , components ) { return `${ variable }.components = Object.assign({}, { ${ components . map ( c => c . lazy ? c . import : c . pascalName ) . join ( ', ' ) } }, ${ variable }.components);` ; } export default function injectComponents ( source , components ) { const lazyCode = ! source . includes ( 'defineAsyncComponent' ) && components . filter ( c => c . lazy ) . length > 0 ? 'import { defineAsyncComponent } from \'vue\'' : '' ; let newContent = `/* vue-cli-plugin-import-components */\n${ lazyCode }\n${ components . filter ( c => ! c . lazy ) . map ( c => c . import ) . join ( '\n' ) }\n${ injectImports ( 'script' , components ) }` ; newContent += `if ('__vccOpts' in script) { script.__o = script.__o || {}; ${ injectImports ( 'script.__o' , components ) }}` ; const hotReload = source . indexOf ( '/* hot reload */' ) ; if ( hotReload > - 1 ) source = `${ source . slice ( 0 , hotReload ) + newContent }\n\n${ source . slice ( hotReload ) }` ; else source += `\n\n${ newContent }` ; return source ; }