Skip to content

Commit

Permalink
feat: global script registration
Browse files Browse the repository at this point in the history
  • Loading branch information
harlan-zw committed Mar 27, 2024
1 parent b73281c commit 7dfc5aa
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,23 @@ addConfetti({ emojis: ['🌈', '⚡️', '💥', '✨', '💫', '🌸'] })

## Guides

### Loading Scripts Globally

If you prefer a config based approach, you can load scripts globally by defining them in your `nuxt.config.ts`.

```ts
export default defineNuxtConfig({
scripts: {
globals: [
'https://cdn.jsdelivr.net/npm/js-confetti@latest/dist/js-confetti.browser.js',
{
assetStrategy: 'bundle'
}
]
}
})
```

### Bundling Scripts

Bundling scripts can allow you to serve them from your own server, improving privacy and performance. It
Expand Down
3 changes: 3 additions & 0 deletions playground/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ export default defineNuxtConfig({
],
devtools: { enabled: true },
scripts: {
globals: [
'https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.global.prod.js',
],
overrides: {
'cloudflare-turnstile': {
assetStrategy: 'bundle',
Expand Down
31 changes: 28 additions & 3 deletions src/module.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
import { addBuildPlugin, addImportsDir, createResolver, defineNuxtModule } from '@nuxt/kit'
import { addBuildPlugin, addImportsDir, addPlugin, addTemplate, createResolver, defineNuxtModule } from '@nuxt/kit'
import { readPackageJSON } from 'pkg-types'
import type { NuxtUseScriptOptions } from './runtime/types'
import type { NuxtUseScriptInput, NuxtUseScriptOptions } from './runtime/types'
import { setupDevToolsUI } from './devtools'
import { NuxtScriptAssetBundlerTransformer } from './plugins/transform'
import { setupPublicAssetStrategy } from './assets'
import { logger } from './logger'

export interface ModuleOptions {
/**
* Register scripts that should be loaded globally on all pages.
*/
globals?: (NuxtUseScriptInput | [NuxtUseScriptInput, NuxtUseScriptOptions])[]
/**
* Override the static script options for specific scripts based on their provided `key` or `src`.
*/
overrides?: {
[key: string]: Pick<NuxtUseScriptOptions, 'assetStrategy' | 'skipEarlyConnections'>
[key: string]: Pick<NuxtUseScriptOptions, 'assetStrategy'>
}
/** Configure the way scripts assets are exposed */
assets?: {
Expand Down Expand Up @@ -70,6 +74,27 @@ export default defineNuxtModule<ModuleOptions>({
nuxt.options.runtimeConfig['nuxt-scripts'] = { version }
addImportsDir(resolve('./runtime/composables'))

if (config.globals?.length) {
// create a virtual plugin
const template = addTemplate({
filename: 'modules/nuxt-scripts/plugin.client.mjs',
getContents() {
return `import { defineNuxtPlugin, useScript } from '#imports'
export default defineNuxtPlugin({
setup() {
${config.globals?.map(g => typeof g === 'string'
? ` useScript("${g}")`
: ` useScript(${JSON.stringify(g[0])}, ${JSON.stringify(g[1])} })`).join('\n')}
}
})`
},
})
addPlugin({
src: template.dst,
mode: 'client',
})
}

const scriptMap = new Map<string, string>()
const { normalizeScriptData } = setupPublicAssetStrategy(config.assets)

Expand Down

0 comments on commit 7dfc5aa

Please sign in to comment.