Skip to content

Commit 738703a

Browse files
committed
fix: safer resolve of triggers via nuxt.config
1 parent b67c9a3 commit 738703a

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

src/templates.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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(/"__TRIGGER_(.*?)__"/g, '$1')})`)
127+
const argsJson = triggerResolved
128+
? JSON.stringify(args).replace(/"__TRIGGER_PLACEHOLDER__"/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(/"__TRIGGER_(.*?)__"/g, '$1')}, use: () => ({ ${k}: window.${k} }) })`)
148+
const resolvedOptions = { ...options, trigger: '__TRIGGER_PLACEHOLDER__' } as any
149+
const optionsJson = JSON.stringify(resolvedOptions).replace(/"__TRIGGER_PLACEHOLDER__"/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(/"__TRIGGER_(.*?)__"/g, '$1')}, { use: () => ({ ${k}: window.${k} }) })`)
161+
const resolvedOptions = { ...c, trigger: '__TRIGGER_PLACEHOLDER__' } as any
162+
const argsJson = JSON.stringify({ key: k, ...resolvedOptions }).replace(/"__TRIGGER_PLACEHOLDER__"/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} }) })`)

test/unit/templates.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ describe('template plugin file', () => {
184184
},
185185
}, [])
186186
expect(res).toContain('import { useScriptTriggerInteraction }')
187-
expect(res).toContain('useScriptTriggerInteraction({ events: [\\"scroll\\",\\"click\\"] })')
187+
expect(res).toContain('useScriptTriggerInteraction({ events: ["scroll","click"] })')
188188
})
189189

190190
// Test registry with idleTimeout trigger

0 commit comments

Comments
 (0)