Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions packages/script/src/module.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -346,18 +346,26 @@ export default defineNuxtModule<ModuleOptions>({
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<string, any> = {}
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<string, string> = {}
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<string, string> = {}
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(
Expand Down
12 changes: 11 additions & 1 deletion packages/script/src/plugins/transform.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -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<string, any> = {}
// Compute registryKey for proxy config lookup
let registryKey: string | undefined
if (fnName !== 'useScript') {
Expand DownExpand Up@@ -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<string, any> = {}

Expand DownExpand Up@@ -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]
Expand All@@ -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
Expand DownExpand Up@@ -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 } })
Expand Down
Loading