66 templateStringParse
77} from "pulsjs-template" ;
88import fs from "node:fs" ;
9+ import { Project } from "ts-morph" ;
910
1011class RealValue {
1112 constructor ( public value ?: any ) {
@@ -91,12 +92,31 @@ export async function compile(r: string) {
9192
9293 const promises : Promise < void > [ ] = [ ]
9394
95+ let generic : string | undefined = undefined
96+
9497 templateParser . filterElements = ( e : ParserTag ) => {
9598 if ( e . tag === 'script' ) {
9699 promises . push ( new Promise < void > ( async ( res ) => {
97100 const lang = e . attributes . find ( ( [ k ] ) => k === 'lang' ) ?. [ 1 ]
98101
102+ generic = e . attributes . find ( ( [ k ] ) => k === 'generics' ) ?. [ 1 ]
103+
99104 let scriptValue = ( e . body [ 0 ] as ParserText ) . value
105+
106+
107+ const proj = new Project ( {
108+ useInMemoryFileSystem : true ,
109+ } )
110+ const sourceFile = proj . createSourceFile ( 'example.ts' , scriptValue )
111+ sourceFile . getImportDeclarations ( ) . forEach ( ( imp ) => {
112+ hasHtmlImport = hasHtmlImport || ! ! imp . getNamedImports ( ) . find ( n => n . getName ( ) === 'pulsInstance' )
113+ extractedImports += imp . getFullText ( ) + '\n'
114+ imp . remove ( )
115+ } )
116+
117+ scriptValue = sourceFile . getFullText ( ) ;
118+
119+
100120 if ( lang === 'ts' ) {
101121 const ts = await import ( 'typescript' )
102122 const { outputText } = ts . transpileModule ( scriptValue , {
@@ -106,20 +126,15 @@ export async function compile(r: string) {
106126 target : ts . ScriptTarget . ESNext ,
107127 moduleResolution : ts . ModuleResolutionKind . NodeJs ,
108128 esModuleInterop : true ,
129+ noUnusedLocals : false ,
130+ noUnusedParameters : false ,
131+ allowUnusedLabels : true
109132 }
110133 } )
111134 scriptValue = outputText ;
112135 }
113136 scriptTag = scriptValue ;
114137
115- const importRegex = / i m p o r t \s + ( ( (?: t y p e \s + ) ? (?: \* \s + a s \s + \w + | { [ ^ } ] + } | \w + ) \s + f r o m \s + [ ' " ] [ ^ ' " ] + [ ' " ] (?: \s + w i t h \s + { [ ^ } ] + } ) ? \s * ) | ( [ ' " ] [ ^ ' " ] + [ ' " ] ) ) ; ? / g;
116- const imports = scriptTag . match ( importRegex ) || [ ] ;
117-
118- hasHtmlImport = imports . some ( i => / i m p o r t \s + .* \b h t m l \b .* f r o m \s + [ ' " ] p u l s I n s t a n c e [ ' " ] / . test ( i ) ) ;
119-
120- extractedImports = imports . join ( '\n' ) ;
121- scriptTag = scriptTag . replace ( importRegex , '' ) . trim ( ) ;
122-
123138 other = r . substring ( 0 , e . from ) + r . substring ( e . to ) ;
124139 res ( )
125140 } ) )
@@ -134,7 +149,7 @@ export async function compile(r: string) {
134149 const [ strings , values ] = parseTemplateString ( other )
135150 const second = templateStringParse ( new TemplateParser ( ) , strings as any , ...values ) . parse ( ) ;
136151
137- return `${ extractedImports } ${ hasHtmlImport ? '' : "\nimport { pulsInstance } from 'pulsjs';" } \n\nexport default ($props = {}) => {
152+ return `${ extractedImports } ${ hasHtmlImport ? '' : "\nimport { pulsInstance } from 'pulsjs';" } \n/* ${ generic ? `@template ${ generic } ` : '' } */ \nexport default ($props = {}) => {
138153 \n ${ scriptTag } \n \n return (new pulsInstance.adapter(${ exportOutput ( second ) } )).render();
139154}` ;
140155}
0 commit comments