@@ -24,37 +24,53 @@ export async function generateTpcContent(input: TpcDescriptor) {
2424 'import { withQuery } from \'ufo\'' ,
2525 'import { useRegistryScript } from \'#nuxt-scripts-utils\'' ,
2626 'import type { RegistryScriptInput } from \'#nuxt-scripts\'' ,
27- `import type { ${ input . tpcTypeImport } } from 'third-party-capital'` ,
2827 ] )
28+ const tpcTypes = new Set < string > ( )
2929
3030 const chunks : string [ ] = [ ]
3131 const functionBody : string [ ] = [ ]
3232
33+ if ( input . tpcTypeAugmentation ) {
34+ tpcTypes . add ( input . tpcTypeAugmentation )
35+
36+ chunks . push ( `
37+ declare global {
38+ interface Window extends ${ input . tpcTypeAugmentation } {}
39+ }` )
40+ }
41+
42+ if ( input . tpcTypesImport ) {
43+ for ( const typeImport of input . tpcTypesImport ) {
44+ tpcTypes . add ( typeImport )
45+ }
46+ }
47+
48+ if ( tpcTypes . size ) {
49+ imports . add ( genImport ( 'third-party-capital' , [ ...tpcTypes ] ) )
50+ }
51+
3352 if ( input . defaultOptions ) {
3453 imports . add ( genImport ( 'defu' , [ 'defu' ] ) )
3554 functionBody . push ( `_options = defu(_options, ${ JSON . stringify ( input . defaultOptions ) } )` )
3655 }
3756
3857 const params = [ ...new Set ( input . tpcData . scripts ?. map ( s => s . params || [ ] ) . flat ( ) || [ ] ) ]
58+ const optionalParams = [ ...new Set ( input . tpcData . scripts ?. map ( s => Object . keys ( s . optionalParams ) || [ ] ) . flat ( ) || [ ] ) ]
3959
40- if ( params . length ) {
60+ if ( params . length || optionalParams . length ) {
4161 const validatorImports = new Set < string > ( [ 'object' , 'string' ] )
62+ if ( optionalParams . length ) {
63+ validatorImports . add ( 'optional' )
64+ }
65+
66+ const properties = params . filter ( p => ! optionalParams . includes ( p ) ) . map ( p => `${ p } : string()` ) . concat ( optionalParams . map ( o => `${ o } : optional(string())` ) )
4267 // need schema validation from tpc
43- chunks . push ( `export const ${ titleKey } Options = object({${ params . map ( ( p ) => {
44- if ( input . defaultOptions && p in input . defaultOptions ) {
45- validatorImports . add ( 'optional' )
46- return `${ p } : optional(string())`
47- }
48- return `${ p } : string()`
49- } ) } })`)
68+ chunks . push ( `export const ${ titleKey } Options = object({
69+ ${ properties . join ( ',\n' ) }
70+ })` )
5071 imports . add ( genImport ( '#nuxt-scripts-validator' , [ ...validatorImports ] ) )
5172 }
5273
53- chunks . push ( `
54- declare global {
55- interface Window extends ${ input . tpcTypeImport } {}
56- }` )
57-
5874 const clientInitCode : string [ ] = [ ]
5975
6076 if ( input . tpcData . stylesheets ) {
@@ -66,7 +82,7 @@ declare global {
6682
6783 for ( const script of input . tpcData . scripts ) {
6884 if ( 'code' in script )
69- clientInitCode . push ( replaceTokenToRuntime ( script . code ) )
85+ clientInitCode . push ( replaceTokenToRuntime ( script . code , script . optionalParams ) )
7086
7187 if ( script === mainScript )
7288 continue
@@ -78,21 +94,33 @@ declare global {
7894
7995 chunks . push ( `export type ${ titleKey } Input = RegistryScriptInput${ params . length ? `<typeof ${ titleKey } Options>` : '' } ` )
8096
97+ if ( input . useBody ) {
98+ chunks . push ( `
99+ function use(options: ${ titleKey } Input) {
100+ ${ input . useBody }
101+ }
102+ ` )
103+ }
104+
105+ const srcQueries = [ ...new Set < string > ( [ ...mainScript . params , ...Object . keys ( mainScript . optionalParams ) ] ) ] . map ( p => `${ p } : options?.${ p } ` )
106+
81107 chunks . push ( `
82- export function ${ input . registry . import ! . name } <T extends ${ input . tpcTypeImport } > (_options?: ${ titleKey } Input) {
108+ export function ${ input . registry . import ! . name } (_options?: ${ titleKey } Input) {
83109${ functionBody . join ( '\n' ) }
84- return useRegistryScript${ params . length ? `<T, typeof ${ titleKey } Options> ` : '' } (_options?.key || '${ input . key } ', options => ({
110+ return useRegistryScript< ${ input . useBody ? `ReturnType<typeof use>` : `Record<string | symbol, any>` } , ${ params . length ? ` typeof ${ titleKey } Options` : '' } > (_options?.key || '${ input . key } ', options => ({
85111 scriptInput: {
86- src: withQuery('${ mainScript . url } ', {${ mainScript . params ?. map ( p => ` ${ p } : options?. ${ p } ` ) } })
112+ src: withQuery('${ mainScript . url } ', {${ srcQueries . join ( ', ' ) } }),
87113 },
88114 schema: import.meta.dev ? ${ titleKey } Options : undefined,
89115 scriptOptions: {
90- use: () => { return ${ input . returnUse } },
116+ ${ input . useBody ? ` use: () => use(options),` : '' }
91117 stub: import.meta.client ? undefined : ({fn}) => { return ${ input . returnStub } },
92118 ${ input . performanceMarkFeature ? `performanceMarkFeature: ${ JSON . stringify ( input . performanceMarkFeature ) } ,` : '' }
93119 ${ mainScriptOptions ? `...(${ JSON . stringify ( mainScriptOptions ) } )` : '' }
94120 },
95121 // eslint-disable-next-line
122+ // @ts-ignore
123+ // eslint-disable-next-line
96124 ${ clientInitCode . length ? `clientInit: import.meta.server ? undefined : () => {${ clientInitCode . join ( '\n' ) } },` : '' }
97125 }), _options)
98126}` )
@@ -102,8 +130,10 @@ ${functionBody.join('\n')}
102130 return chunks . join ( '\n' )
103131}
104132
105- function replaceTokenToRuntime ( code : string ) {
106- return code . split ( ';' ) . map ( c => c . replaceAll ( / ' ? \{ \{ ( .* ?) \} \} ' ? / g, 'options.$1!' ) ) . join ( ';' )
133+ function replaceTokenToRuntime ( code : string , defaultValues ?: Record < string , string | number | undefined > ) {
134+ return code . split ( ';' ) . map ( c => c . replaceAll ( / ' ? \{ \{ ( .* ?) \} \} ' ? / g, ( _ , token ) => {
135+ return `options?.${ token } ${ defaultValues ?. [ token ] ? `?? ${ JSON . stringify ( defaultValues ?. [ token ] ) } ` : '' } `
136+ } ) ) . join ( ';' )
107137}
108138
109139function getScriptInputOption ( script : Script ) : HeadEntryOptions | undefined {
0 commit comments