Skip to content
Draft
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
1 change: 1 addition & 0 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ export {}`
addBuildPlugin(NuxtScriptBundleTransformer({
scripts: registryScriptsWithImport,
defaultBundle: config.defaultScriptOptions?.bundle,
defaultForceDownload: config.defaultScriptOptions?.forceDownload,
moduleDetected(module) {
if (nuxt.options.dev && module !== '@nuxt/scripts' && !moduleInstallPromises.has(module) && !hasNuxtModule(module))
moduleInstallPromises.set(module, () => installNuxtModule(module))
Expand Down
15 changes: 12 additions & 3 deletions src/plugins/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import type { RegistryScript } from '#nuxt-scripts/types'
export interface AssetBundlerTransformerOptions {
moduleDetected?: (module: string) => void
defaultBundle?: boolean
defaultForceDownload?: boolean
assetsBaseURL?: string
scripts?: Required<RegistryScript>[]
fallbackOnSrcOnBundleFail?: boolean
Expand Down Expand Up @@ -56,8 +57,9 @@ async function downloadScript(opts: {
src: string
url: string
filename?: string
forceDownload?: boolean
}, renderedScript: NonNullable<AssetBundlerTransformerOptions['renderedScript']>, fetchOptions?: FetchOptions) {
const { src, url, filename } = opts
const { src, url, filename, forceDownload } = opts
if (src === url || !filename) {
return
}
Expand All @@ -66,7 +68,7 @@ async function downloadScript(opts: {
let res: Buffer | undefined = scriptContent instanceof Error ? undefined : scriptContent?.content
if (!res) {
// Use storage to cache the font data between builds
if (await storage.hasItem(`bundle:${filename}`)) {
if (!forceDownload && await storage.hasItem(`bundle:${filename}`)) {
const res = await storage.getItemRaw<Buffer>(`bundle:${filename}`)
renderedScript.set(url, {
content: res!,
Expand Down Expand Up @@ -254,11 +256,18 @@ export function NuxtScriptBundleTransformer(options: AssetBundlerTransformerOpti
return prop.type === 'Property' && prop.key?.name === 'bundle' && prop.value.type === 'Literal'
})
canBundle = bundleOption ? bundleOption.value.value : canBundle

// check if scriptOptions contains forceDownload: true
// @ts-expect-error untyped
const forceDownloadOption = scriptOptions?.value.properties?.find((prop) => {
return prop.type === 'Property' && prop.key?.name === 'forceDownload' && prop.value.type === 'Literal'
})
const forceDownload = forceDownloadOption ? forceDownloadOption.value.value : (options.defaultForceDownload || false)
if (canBundle) {
const { url: _url, filename } = normalizeScriptData(src, options.assetsBaseURL)
let url = _url
try {
await downloadScript({ src, url, filename }, renderedScript, options.fetchOptions)
await downloadScript({ src, url, filename, forceDownload }, renderedScript, options.fetchOptions)
}
catch (e) {
if (options.fallbackOnSrcOnBundleFail) {
Expand Down
23 changes: 16 additions & 7 deletions src/runtime/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ export type NuxtUseScriptOptions<T extends Record<symbol | string, any> = {}> =
* - `false` - Do not bundle the script. (default)
*/
bundle?: boolean
/**
* Force download of the script even if it exists in cache. Useful for development workflows
* where you want to ensure the latest version is always downloaded.
* - `true` - Force download, bypass cache.
* - `false` - Use cached version if available. (default)
*
* Note: This may significantly increase build time as scripts will be re-downloaded on every build.
*/
forceDownload?: boolean
/**
* Skip any schema validation for the script input. This is useful for loading the script stubs for development without
* loading the actual script and not getting warnings.
Expand Down Expand Up @@ -173,16 +182,16 @@ export type RegistryScriptInput<
Usable extends boolean = false,
CanBypassOptions extends boolean = true,
>
= (InferIfSchema<T>
& {
= (InferIfSchema<T>
& {
/**
* A unique key to use for the script, this can be used to load multiple of the same script with different options.
*/
key?: string
scriptInput?: ScriptInput
scriptOptions?: Omit<NuxtUseScriptOptions, Bundelable extends true ? '' : 'bundle' | Usable extends true ? '' : 'use'>
})
| Partial<InferIfSchema<T>> & (
key?: string
scriptInput?: ScriptInput
scriptOptions?: Omit<NuxtUseScriptOptions, Bundelable extends true ? '' : 'bundle' | Usable extends true ? '' : 'use'>
})
| Partial<InferIfSchema<T>> & (
CanBypassOptions extends true ? {
/**
* A unique key to use for the script, this can be used to load multiple of the same script with different options.
Expand Down
Loading