diff --git a/packages/script/src/module.ts b/packages/script/src/module.ts index 9d9295bf6..460b382ab 100644 --- a/packages/script/src/module.ts +++ b/packages/script/src/module.ts @@ -346,18 +346,26 @@ export default defineNuxtModule({ if (entry === false) continue const input = (entry as any[])[0] + const scriptOptions = (entry as any[])[1] const envDefaults = scripts.find(s => s.registryKey === key)?.envDefaults + const base: Record = {} if (!envDefaults || !Object.keys(envDefaults).length) { - registryWithDefaults[key] = input - continue + Object.assign(base, input) + } + else { + // Read process.env for each field, falling back to the static default + const envResolved: Record = {} + for (const [field, defaultValue] of Object.entries(envDefaults)) { + const envKey = `NUXT_PUBLIC_SCRIPTS_${toScreamingSnake(key)}_${toScreamingSnake(field)}` + envResolved[field] = process.env[envKey] || defaultValue + } + Object.assign(base, defu(input, envResolved)) } - // Read process.env for each field, falling back to the static default - const envResolved: Record = {} - for (const [field, defaultValue] of Object.entries(envDefaults)) { - const envKey = `NUXT_PUBLIC_SCRIPTS_${toScreamingSnake(key)}_${toScreamingSnake(field)}` - envResolved[field] = process.env[envKey] || defaultValue + // Include scriptOptions so composable instances inherit registry-level settings (e.g. bundle) + if (scriptOptions && Object.keys(scriptOptions).length > 0) { + base.scriptOptions = scriptOptions } - registryWithDefaults[key] = defu(input, envResolved) + registryWithDefaults[key] = base } nuxt.options.runtimeConfig.public.scripts = defu( diff --git a/packages/script/src/plugins/transform.ts b/packages/script/src/plugins/transform.ts index 14b4427f7..384484d2d 100644 --- a/packages/script/src/plugins/transform.ts +++ b/packages/script/src/plugins/transform.ts @@ -263,6 +263,7 @@ export function NuxtScriptBundleTransformer(options: AssetBundlerTransformerOpti const node = _node as any let scriptSrcNode: { start: number, end: number, value: any } | undefined let src: false | string | undefined + let registryConfig: Record = {} // Compute registryKey for proxy config lookup let registryKey: string | undefined if (fnName !== 'useScript') { @@ -295,7 +296,7 @@ export function NuxtScriptBundleTransformer(options: AssetBundlerTransformerOpti // integration case // Get registry config for this script - const registryConfig = options.registryConfig?.[registryKey || ''] || {} + registryConfig = options.registryConfig?.[registryKey || ''] || {} const fnArg0: Record = {} @@ -365,6 +366,7 @@ export function NuxtScriptBundleTransformer(options: AssetBundlerTransformerOpti : undefined let canBundle = !!registryScript?.bundle let forceDownload = false + let explicitBundleInCall = false // useScript if (node.arguments[1]?.type === 'ObjectExpression') { const scriptOptionsArg = node.arguments[1] @@ -373,6 +375,7 @@ export function NuxtScriptBundleTransformer(options: AssetBundlerTransformerOpti (p: any) => (p.key?.name === 'bundle' || p.key?.value === 'bundle') && p.type === 'Property', ) if (bundleProperty && bundleProperty.value.type === 'Literal') { + explicitBundleInCall = true const bundleValue = bundleProperty.value.value if (bundleValue !== true && bundleValue !== 'force' && String(bundleValue) !== 'true') { canBundle = false @@ -400,10 +403,17 @@ export function NuxtScriptBundleTransformer(options: AssetBundlerTransformerOpti return prop.type === 'Property' && prop.key?.name === 'bundle' && prop.value.type === 'Literal' }) if (bundleOption) { + explicitBundleInCall = true const bundleValue = bundleOption.value.value canBundle = bundleValue === true || bundleValue === 'force' || String(bundleValue) === 'true' forceDownload = bundleValue === 'force' } + // Inherit bundle setting from registry config (nuxt.config) when not explicitly set in the call + if (!explicitBundleInCall && registryConfig?.scriptOptions?.bundle !== undefined) { + const bundleValue = registryConfig.scriptOptions.bundle + canBundle = bundleValue === true || bundleValue === 'force' || String(bundleValue) === 'true' + forceDownload = bundleValue === 'force' + } // Check for per-script proxy opt-out // Check in three locations: // 1. In scriptOptions (nested) - useScriptGA({ scriptOptions: { proxy: false } })