@@ -34,6 +34,11 @@ export interface AssetBundlerTransformerOptions {
34
34
defaultBundle ?: boolean | 'force'
35
35
assetsBaseURL ?: string
36
36
scripts ?: Required < RegistryScript > [ ]
37
+ /**
38
+ * Merged configuration from both scripts.registry and runtimeConfig.public.scripts
39
+ * Used to provide default options to script bundling functions when no arguments are provided
40
+ */
41
+ registryConfig ?: Record < string , any >
37
42
fallbackOnSrcOnBundleFail ?: boolean
38
43
fetchOptions ?: FetchOptions
39
44
cacheMaxAge ?: number
@@ -211,28 +216,42 @@ export function NuxtScriptBundleTransformer(options: AssetBundlerTransformerOpti
211
216
return
212
217
213
218
// integration case
219
+ // Get registry key from function name (e.g., useScriptGoogleTagManager -> googleTagManager)
220
+ const baseName = fnName . replace ( / ^ u s e S c r i p t / , '' )
221
+ const registryKey = baseName . length > 0 ? baseName . charAt ( 0 ) . toLowerCase ( ) + baseName . slice ( 1 ) : ''
222
+
223
+ // Get registry config for this script
224
+ const registryConfig = options . registryConfig ?. [ registryKey ] || { }
225
+
226
+ const fnArg0 = { }
227
+
214
228
// extract the options as the first argument that we'll use to reconstruct the src
215
229
if ( node . arguments [ 0 ] ?. type === 'ObjectExpression' ) {
216
230
const optionsNode = node . arguments [ 0 ] as ObjectExpression
217
- const fnArg0 = { }
218
231
// extract literal values from the object to reconstruct the options
219
232
for ( const prop of optionsNode . properties ) {
220
- if ( prop . type === 'Property' && prop . value . type === 'Literal' )
233
+ if ( prop . type === 'Property' && prop . value . type === 'Literal' && prop . key && 'name' in prop . key )
221
234
// @ts -expect-error untyped
222
235
fnArg0 [ prop . key . name ] = prop . value . value
223
236
}
237
+
224
238
const srcProperty = node . arguments [ 0 ] . properties . find (
225
239
( p : any ) => ( p . key ?. name === 'src' || p . key ?. value === 'src' ) && p ?. value . type === 'Literal' && p . type === 'Property' ,
226
240
) as Property | undefined
227
241
if ( ( srcProperty ?. value as Literal ) ?. value ) {
228
242
scriptSrcNode = srcProperty ?. value as Literal & { start : number , end : number }
229
243
}
230
- else {
231
- src = registryNode . scriptBundling && registryNode . scriptBundling ( fnArg0 as any as InferInput < any > )
232
- // not supported
233
- if ( src === false )
234
- return
235
- }
244
+ }
245
+
246
+ // If no src was found from function arguments, try to generate from registry config
247
+ if ( ! scriptSrcNode ) {
248
+ // Merge registry config with function arguments (function args take precedence)
249
+ const mergedOptions = { ...registryConfig , ...fnArg0 }
250
+
251
+ src = registryNode . scriptBundling && registryNode . scriptBundling ( mergedOptions as InferInput < any > )
252
+ // not supported
253
+ if ( src === false )
254
+ return
236
255
}
237
256
}
238
257
@@ -296,7 +315,7 @@ export function NuxtScriptBundleTransformer(options: AssetBundlerTransformerOpti
296
315
}
297
316
}
298
317
// @ts -expect-error untyped
299
- const scriptOptions = node . arguments [ 0 ] . properties ?. find (
318
+ const scriptOptions = node . arguments [ 0 ] ? .properties ?. find (
300
319
( p : any ) => ( p . key ?. name === 'scriptOptions' ) ,
301
320
) as Property | undefined
302
321
// we need to check if scriptOptions contains bundle: true/false/'force', if it exists
@@ -335,28 +354,37 @@ export function NuxtScriptBundleTransformer(options: AssetBundlerTransformerOpti
335
354
s . overwrite ( scriptSrcNode . start , scriptSrcNode . end , `'${ url } '` )
336
355
}
337
356
else {
338
- const optionsNode = node . arguments [ 0 ] as ObjectExpression
339
- // check if there's a scriptInput property
340
- const scriptInputProperty = optionsNode . properties . find (
341
- ( p : any ) => p . key ?. name === 'scriptInput' || p . key ?. value === 'scriptInput' ,
342
- )
343
- // see if there is a script input on it
344
- if ( scriptInputProperty ) {
345
- // @ts -expect-error untyped
346
- const scriptInput = scriptInputProperty . value
347
- if ( scriptInput . type === 'ObjectExpression' ) {
348
- const srcProperty = scriptInput . properties . find (
349
- ( p : any ) => p . key ?. name === 'src' || p . key ?. value === 'src' ,
350
- )
351
- if ( srcProperty )
352
- s . overwrite ( srcProperty . value . start , srcProperty . value . end , `'${ url } '` )
353
- else
354
- s . appendRight ( scriptInput . end , `, src: '${ url } '` )
357
+ // Handle case where we need to add scriptInput
358
+ if ( node . arguments [ 0 ] ) {
359
+ // There's at least one argument
360
+ const optionsNode = node . arguments [ 0 ] as ObjectExpression
361
+ // check if there's a scriptInput property
362
+ const scriptInputProperty = optionsNode . properties . find (
363
+ ( p : any ) => p . key ?. name === 'scriptInput' || p . key ?. value === 'scriptInput' ,
364
+ )
365
+ // see if there is a script input on it
366
+ if ( scriptInputProperty ) {
367
+ // @ts -expect-error untyped
368
+ const scriptInput = scriptInputProperty . value
369
+ if ( scriptInput . type === 'ObjectExpression' ) {
370
+ const srcProperty = scriptInput . properties . find (
371
+ ( p : any ) => p . key ?. name === 'src' || p . key ?. value === 'src' ,
372
+ )
373
+ if ( srcProperty )
374
+ s . overwrite ( srcProperty . value . start , srcProperty . value . end , `'${ url } '` )
375
+ else
376
+ s . appendRight ( scriptInput . end , `, src: '${ url } '` )
377
+ }
378
+ }
379
+ else {
380
+ // @ts -expect-error untyped
381
+ s . appendRight ( node . arguments [ 0 ] . start + 1 , ` scriptInput: { src: '${ url } ' }, ` )
355
382
}
356
383
}
357
384
else {
385
+ // No arguments at all, need to create the first argument
358
386
// @ts -expect-error untyped
359
- s . appendRight ( node . arguments [ 0 ] . start + 1 , ` scriptInput: { src: '${ url } ' }, ` )
387
+ s . appendRight ( node . callee . end , `({ scriptInput: { src: '${ url } ' } }) ` )
360
388
}
361
389
}
362
390
}
0 commit comments