Skip to content
Open
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
21 changes: 21 additions & 0 deletions docs/content/docs/3.api/5.nuxt-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -234,3 +234,24 @@
Generates a Subresource Integrity (SRI) hash for each bundled script and adds `integrity` with `crossorigin="anonymous"`.

Browsers compare the downloaded script with its declared hash before executing it; see MDN's [Subresource Integrity](https://developer.mozilla.org/en-US/docs/Web/Security/Defenses/Subresource_Integrity) guide.

## `assets.alwaysBundle`{lang="ts"}

- Type: `boolean | string[]`{lang="ts"}
- Default: `false`

Bundles a component's script even when the build cannot prove the component is used.

Check warning on line 243 in docs/content/docs/3.api/5.nuxt-config.md

View workflow job for this annotation

GitHub Actions / lint

Passive voice: "is used". Consider rewriting in active voice

A script component only bundles its script once the module graph shows a real importer, so an unused widget never downloads its SDK. A component used only through `nuxt-client` inside a server component has no importer in the client build, so its script falls back to loading from the third-party origin. Name the component here to bundle it anyway.

```ts [nuxt.config.ts]
export default defineNuxtConfig({
scripts: {
assets: {
alwaysBundle: ['ScriptCalendlyInlineWidget'],
},
},
})
```

Pass `true` to bundle every script component regardless of usage. If the build detects this case it warns and names the component to add.
19 changes: 18 additions & 1 deletion packages/script/src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,20 @@ export interface ModuleOptions {
* @default false
*/
integrity?: boolean | 'sha256' | 'sha384' | 'sha512'
/**
* Bundle a component's script even when the build cannot prove the component is used.
*
* Auto-registered components are only bundled once the module graph shows a real
* importer, so an unused widget never downloads its SDK. A component used only
* through `nuxt-client` inside a server component has no importer in the client
* graph, so it falls back to loading from the third-party origin. Name it here to
* bundle it anyway, or pass `true` to bundle every component's script.
*
* Values are component names, for example `'ScriptCalendlyInlineWidget'`.
*
* @default false
*/
alwaysBundle?: boolean | string[]
}
/**
* Enable standalone devtools mode.
Expand Down Expand Up @@ -523,8 +537,9 @@ export default defineNuxtModule<ModuleOptions>({
})
}

const runtimeComponentsDir = await resolvePath('./runtime/components')
addComponentsDir({
path: await resolvePath('./runtime/components'),
path: runtimeComponentsDir,
pathPrefix: false,
})

Expand Down Expand Up @@ -885,6 +900,7 @@ export default defineNuxtModule<ModuleOptions>({
addBuildPlugin(NuxtScriptsCheckScripts())
addBuildPlugin(NuxtScriptBundleTransformer({
nuxt,
componentDir: runtimeComponentsDir,
scripts: registryScriptsWithImport,
registryConfig: nuxt.options.runtimeConfig.public.scripts as Record<string, any> | undefined,
proxyConfigs,
Expand All @@ -900,6 +916,7 @@ export default defineNuxtModule<ModuleOptions>({
fetchOptions: config.assets?.fetchOptions,
cacheMaxAge: config.assets?.cacheMaxAge,
integrity: config.assets?.integrity,
alwaysBundle: config.assets?.alwaysBundle,
renderedScript,
}))

Expand Down
Loading
Loading