@@ -119,12 +119,15 @@ export function templatePlugin(config: Partial<ModuleOptions>, registry: Require
119119 const scriptOptions = { ...c [ 1 ] }
120120 const triggerResolved = resolveTriggerForTemplate ( scriptOptions ?. trigger )
121121 if ( triggerResolved ) {
122- scriptOptions . trigger = `__TRIGGER_ ${ triggerResolved } __` as any
122+ scriptOptions . trigger = '__TRIGGER_PLACEHOLDER__' as any
123123 if ( triggerResolved . includes ( 'useScriptTriggerIdleTimeout' ) ) needsIdleTimeoutImport = true
124124 if ( triggerResolved . includes ( 'useScriptTriggerInteraction' ) ) needsInteractionImport = true
125125 }
126126 const args = { ...input , scriptOptions }
127- inits . push ( `const ${ k } = ${ importDefinition . import . name } (${ JSON . stringify ( args ) . replace ( / " _ _ T R I G G E R _ ( .* ?) _ _ " / g, '$1' ) } )` )
127+ const argsJson = triggerResolved
128+ ? JSON . stringify ( args ) . replace ( / " _ _ T R I G G E R _ P L A C E H O L D E R _ _ " / g, triggerResolved )
129+ : JSON . stringify ( args )
130+ inits . push ( `const ${ k } = ${ importDefinition . import . name } (${ argsJson } )` )
128131 }
129132 else {
130133 const args = ( typeof c !== 'object' ? { } : c ) || { }
@@ -142,8 +145,9 @@ export function templatePlugin(config: Partial<ModuleOptions>, registry: Require
142145 if ( triggerResolved ) {
143146 if ( triggerResolved . includes ( 'useScriptTriggerIdleTimeout' ) ) needsIdleTimeoutImport = true
144147 if ( triggerResolved . includes ( 'useScriptTriggerInteraction' ) ) needsInteractionImport = true
145- const resolvedOptions = { ...options , trigger : `__TRIGGER_${ triggerResolved } __` } as any
146- inits . push ( `const ${ k } = useScript(${ JSON . stringify ( { key : k , ...( typeof c [ 0 ] === 'string' ? { src : c [ 0 ] } : c [ 0 ] ) } ) } , { ...${ JSON . stringify ( resolvedOptions ) . replace ( / " _ _ T R I G G E R _ ( .* ?) _ _ " / g, '$1' ) } , use: () => ({ ${ k } : window.${ k } }) })` )
148+ const resolvedOptions = { ...options , trigger : '__TRIGGER_PLACEHOLDER__' } as any
149+ const optionsJson = JSON . stringify ( resolvedOptions ) . replace ( / " _ _ T R I G G E R _ P L A C E H O L D E R _ _ " / g, triggerResolved )
150+ inits . push ( `const ${ k } = useScript(${ JSON . stringify ( { key : k , ...( typeof c [ 0 ] === 'string' ? { src : c [ 0 ] } : c [ 0 ] ) } ) } , { ...${ optionsJson } , use: () => ({ ${ k } : window.${ k } }) })` )
147151 }
148152 else {
149153 inits . push ( `const ${ k } = useScript(${ JSON . stringify ( { key : k , ...( typeof c [ 0 ] === 'string' ? { src : c [ 0 ] } : c [ 0 ] ) } ) } , { ...${ JSON . stringify ( c [ 1 ] ) } , use: () => ({ ${ k } : window.${ k } }) })` )
@@ -154,8 +158,9 @@ export function templatePlugin(config: Partial<ModuleOptions>, registry: Require
154158 if ( triggerResolved ) {
155159 if ( triggerResolved . includes ( 'useScriptTriggerIdleTimeout' ) ) needsIdleTimeoutImport = true
156160 if ( triggerResolved . includes ( 'useScriptTriggerInteraction' ) ) needsInteractionImport = true
157- const resolvedOptions = { ...c , trigger : `__TRIGGER_${ triggerResolved } __` } as any
158- inits . push ( `const ${ k } = useScript(${ JSON . stringify ( { key : k , ...resolvedOptions } ) . replace ( / " _ _ T R I G G E R _ ( .* ?) _ _ " / g, '$1' ) } , { use: () => ({ ${ k } : window.${ k } }) })` )
161+ const resolvedOptions = { ...c , trigger : '__TRIGGER_PLACEHOLDER__' } as any
162+ const argsJson = JSON . stringify ( { key : k , ...resolvedOptions } ) . replace ( / " _ _ T R I G G E R _ P L A C E H O L D E R _ _ " / g, triggerResolved )
163+ inits . push ( `const ${ k } = useScript(${ argsJson } , { use: () => ({ ${ k } : window.${ k } }) })` )
159164 }
160165 else {
161166 inits . push ( `const ${ k } = useScript(${ JSON . stringify ( { key : k , ...c } ) } , { use: () => ({ ${ k } : window.${ k } }) })` )
0 commit comments