import * as htmlparser2 from 'htmlparser2' ; import fs from 'fs' ; import type { Options , Meta } from '@remax/types' ; import { getPath } from './helpers' ; export function walk ( platformConfig , filePath , templatePaths , options ) { if ( ! fs . existsSync ( filePath ) ) { console . error ( `文件 ${ filePath } 不存在` , options . notify ) ; return ; } const { tag , src } = platformConfig . template ; templatePaths . add ( filePath ) ; const content = fs . readFileSync ( filePath ) . toString ( ) ; const parser = new htmlparser2 . Parser ( { } ) ; ( parser as any ) . _cbs . onopentag = ( name , attrs ) => { if ( name === tag && attrs [ src ] ) { walk ( platformConfig , getPath ( filePath , attrs [ src ] ) , templatePaths , options ) ; } else if ( [ 'include' , 'import' ] . includes ( name ) && attrs . src ) { walk ( platformConfig , getPath ( filePath , attrs . src ) , templatePaths , options ) ; } } ; parser . reset ( ) ; parser . write ( content ) ; parser . end ( ) ; } export default function template ( platformConfig , options , id ) { const templatePaths = new Set ( ) ; const filePath = id . replace ( /\.js$/ , platformConfig . template . extension ) ; walk ( platformConfig , filePath , templatePaths , options ) ; return Array . from ( templatePaths ) ; }