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
3 changes: 3 additions & 0 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,9 @@ export default defineNuxtModule<ModuleOptions>({
// NUXT_PUBLIC_SCRIPTS_<SCRIPT>_<KEY> works without manual runtimeConfig
const registryWithDefaults: Record<string, any> = {}
for (const [key, value] of Object.entries(config.registry)) {
if (value === false) {
continue
}
if (value && REGISTRY_ENV_DEFAULTS[key]) {
const envDefaults = REGISTRY_ENV_DEFAULTS[key]
if (value === true || value === 'mock') {
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/registry/bluesky-embed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export interface BlueskyEmbedPostData {
labels: Array<{ val: string }>
}

export type BlueskyEmbedInput = RegistryScriptInput<typeof BlueskyEmbedOptions, false, false, false>
export type BlueskyEmbedInput = RegistryScriptInput<typeof BlueskyEmbedOptions, false, false>

const BSKY_POST_URL_RE = /bsky\.app\/profile\/([^/]+)\/post\/([^/?]+)/

Expand Down
2 changes: 1 addition & 1 deletion src/runtime/registry/crisp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { CrispOptions } from './schemas'

export { CrispOptions }

export type CrispInput = RegistryScriptInput<typeof CrispOptions, false, false, false>
export type CrispInput = RegistryScriptInput<typeof CrispOptions, false, false>

export interface CrispApi {
push: (...args: any[]) => void
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/registry/fathom-analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { FathomAnalyticsOptions } from './schemas'

export { FathomAnalyticsOptions }

export type FathomAnalyticsInput = RegistryScriptInput<typeof FathomAnalyticsOptions, false, false, false>
export type FathomAnalyticsInput = RegistryScriptInput<typeof FathomAnalyticsOptions, false, false>

export interface FathomAnalyticsApi {
beacon: (ctx: { url: string, referrer?: string }) => void
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/registry/google-adsense.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { GoogleAdsenseOptions } from './schemas'

export { GoogleAdsenseOptions }

export type GoogleAdsenseInput = RegistryScriptInput<typeof GoogleAdsenseOptions, true, false, false>
export type GoogleAdsenseInput = RegistryScriptInput<typeof GoogleAdsenseOptions, true, false>

export interface GoogleAdsenseApi {
/**
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/registry/hotjar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ declare global {
}
}

export type HotjarInput = RegistryScriptInput<typeof HotjarOptions, true, false, false>
export type HotjarInput = RegistryScriptInput<typeof HotjarOptions, true, false>

export function useScriptHotjar<T extends HotjarApi>(_options?: HotjarInput) {
return useRegistryScript<T, typeof HotjarOptions>('hotjar', options => ({
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/registry/instagram-embed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { InstagramEmbedOptions } from './schemas'

export { InstagramEmbedOptions }

export type InstagramEmbedInput = RegistryScriptInput<typeof InstagramEmbedOptions, false, false, false>
export type InstagramEmbedInput = RegistryScriptInput<typeof InstagramEmbedOptions, false, false>

const INSTAGRAM_SHORTCODE_RE = /instagram\.com\/(?:p|reel|tv)\/([^/?]+)/

Expand Down
2 changes: 1 addition & 1 deletion src/runtime/registry/intercom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { IntercomOptions } from './schemas'

export { IntercomOptions }

export type IntercomInput = RegistryScriptInput<typeof IntercomOptions, true, false, false>
export type IntercomInput = RegistryScriptInput<typeof IntercomOptions, true, false>

export interface IntercomApi {
Intercom: ((event: 'boot', data?: InferInput<typeof IntercomOptions>) => void)
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/registry/matomo-analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { MatomoAnalyticsOptions } from './schemas'

export { MatomoAnalyticsOptions }

export type MatomoAnalyticsInput = RegistryScriptInput<typeof MatomoAnalyticsOptions, false, false, false>
export type MatomoAnalyticsInput = RegistryScriptInput<typeof MatomoAnalyticsOptions, false, false>

interface MatomoAnalyticsApi {
_paq: unknown[]
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/registry/meta-pixel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ declare global {
}

export { MetaPixelOptions }
export type MetaPixelInput = RegistryScriptInput<typeof MetaPixelOptions, true, false, false>
export type MetaPixelInput = RegistryScriptInput<typeof MetaPixelOptions, true, false>

export function useScriptMetaPixel<T extends MetaPixelApi>(_options?: MetaPixelInput) {
return useRegistryScript<T, typeof MetaPixelOptions>('metaPixel', options => ({
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/registry/npm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export { NpmOptions }

type Provider = 'jsdelivr' | 'cdnjs' | 'unpkg'

export type NpmInput = RegistryScriptInput<typeof NpmOptions, true, true, false>
export type NpmInput = RegistryScriptInput<typeof NpmOptions, true, true>

export function useScriptNpm<T extends Record<string | symbol, any>>(_options: NpmInput) {
return useRegistryScript<T, typeof NpmOptions>(`${_options.packageName}-npm`, (options) => {
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/registry/reddit-pixel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ declare global {
}

export { RedditPixelOptions }
export type RedditPixelInput = RegistryScriptInput<typeof RedditPixelOptions, true, false, false>
export type RedditPixelInput = RegistryScriptInput<typeof RedditPixelOptions, true, false>

export function useScriptRedditPixel<T extends RedditPixelApi>(_options?: RedditPixelInput) {
return useRegistryScript<T, typeof RedditPixelOptions>('redditPixel', (options) => {
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/registry/snapchat-pixel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export interface SnapPixelApi {
declare global {
interface Window extends SnapPixelApi {}
}
export type SnapTrPixelInput = RegistryScriptInput<typeof SnapTrPixelOptions, true, false, false>
export type SnapTrPixelInput = RegistryScriptInput<typeof SnapTrPixelOptions, true, false>

export function useScriptSnapchatPixel<T extends SnapPixelApi>(_options?: SnapTrPixelInput) {
return useRegistryScript<T, typeof SnapTrPixelOptions>('snapchatPixel', options => ({
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/registry/tiktok-pixel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ declare global {

export { TikTokPixelOptions }

export type TikTokPixelInput = RegistryScriptInput<typeof TikTokPixelOptions, true, false, false>
export type TikTokPixelInput = RegistryScriptInput<typeof TikTokPixelOptions, true, false>

export function useScriptTikTokPixel<T extends TikTokPixelApi>(_options?: TikTokPixelInput) {
return useRegistryScript<T, typeof TikTokPixelOptions>('tiktokPixel', options => ({
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/registry/vercel-analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export interface BeforeSendEvent {

export type BeforeSend = (event: BeforeSendEvent) => BeforeSendEvent | null

export type VercelAnalyticsInput = RegistryScriptInput<typeof VercelAnalyticsOptions, false, false, false> & {
export type VercelAnalyticsInput = RegistryScriptInput<typeof VercelAnalyticsOptions, false, false> & {
beforeSend?: BeforeSend
}

Expand Down
2 changes: 1 addition & 1 deletion src/runtime/registry/x-embed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export interface XEmbedTweetData {
}
}

export type XEmbedInput = RegistryScriptInput<typeof XEmbedOptions, false, false, false>
export type XEmbedInput = RegistryScriptInput<typeof XEmbedOptions, false, false>

/**
* Proxy an X/Twitter image URL through the server
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/registry/x-pixel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ declare global {
}

export { XPixelOptions }
export type XPixelInput = RegistryScriptInput<typeof XPixelOptions, true, false, false>
export type XPixelInput = RegistryScriptInput<typeof XPixelOptions, true, false>

export function useScriptXPixel<T extends XPixelApi>(_options?: XPixelInput) {
return useRegistryScript<T, typeof XPixelOptions>('xPixel', (options) => {
Expand Down
32 changes: 11 additions & 21 deletions src/runtime/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ export interface ScriptRegistry {
[key: `${string}-npm`]: NpmInput
}

export type NuxtConfigScriptRegistryEntry<T> = true | 'mock' | T | [T, NuxtUseScriptOptionsSerializable]
export type NuxtConfigScriptRegistryEntry<T> = true | false | 'mock' | T | [T, NuxtUseScriptOptionsSerializable]
export type NuxtConfigScriptRegistry<T extends keyof ScriptRegistry = keyof ScriptRegistry> = Partial<{
[key in T]: NuxtConfigScriptRegistryEntry<ScriptRegistry[key]>
}> & Record<string & {}, NuxtConfigScriptRegistryEntry<any>>
Expand All @@ -212,30 +212,20 @@ export type EmptyOptionsSchema = typeof _emptyOptions
type ScriptInput = Script

export type InferIfSchema<T> = T extends ObjectSchema<any, any> | UnionSchema<any, any> ? InferInput<T> : T
export interface RegistryScriptInputExtras<Bundelable extends boolean = true, Usable extends boolean = false> {
/**
* 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'>
}

export type RegistryScriptInput<
T = EmptyOptionsSchema,
Bundelable extends boolean = true,
Usable extends boolean = false,
CanBypassOptions extends boolean = true,
>
= (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>> & (
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.
*/
key?: string
scriptInput: Required<Pick<ScriptInput, 'src'>> & ScriptInput
scriptOptions?: Omit<NuxtUseScriptOptions, Bundelable extends true ? '' : 'bundle' | Usable extends true ? '' : 'use'>
} : never)
> = Partial<InferIfSchema<T>> & RegistryScriptInputExtras<Bundelable, Usable>

export interface RegistryScriptServerHandler {
route: string
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export function requireRegistryEndpoint(componentName: string, registryKey: stri

export function useRegistryScript<T extends Record<string | symbol, any>, O = EmptyOptionsSchema>(registryKey: keyof ScriptRegistry | string, optionsFn: OptionsFn<O>, _userOptions?: RegistryScriptInput<O>): UseScriptContext<UseFunctionType<NuxtUseScriptOptions<T>, T>> {
const scriptConfig = scriptRuntimeConfig(registryKey as keyof ScriptRegistry)
const userOptions = Object.assign(_userOptions || {}, typeof scriptConfig === 'object' ? scriptConfig : {})
const userOptions = defu(_userOptions || {}, typeof scriptConfig === 'object' ? scriptConfig : {})
const options = optionsFn(userOptions as InferIfSchema<O>, { scriptInput: userOptions.scriptInput as UseScriptInput & { src?: string } })

// NEW: Handle NPM-only scripts differently
Expand Down
2 changes: 2 additions & 0 deletions src/templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ export function templatePlugin(config: Partial<ModuleOptions>, registry: Require

// for global scripts, we can initialise them script away
for (const [k, c] of Object.entries(config.registry || {})) {
if (c === false)
continue
const importDefinition = registry.find(i => i.import.name.toLowerCase() === `usescript${k.toLowerCase()}`)
if (importDefinition) {
resolvedRegistryKeys.push(k)
Expand Down
Loading