diff --git a/docs/pages/index.vue b/docs/pages/index.vue index 194ab066..9630a185 100644 --- a/docs/pages/index.vue +++ b/docs/pages/index.vue @@ -81,11 +81,13 @@ const { $script } = useScript({ }, }) onMounted(() => { - confettiEl.value && useEventListener(confettiEl.value, 'mouseenter', () => { - $script.then(({ JSConfetti }) => { - new JSConfetti().addConfetti({ emojis: ['🎉', '🎊'] }) + if (confettiEl.value) { + useEventListener(confettiEl.value, 'mouseenter', () => { + $script.then(({ JSConfetti }) => { + new JSConfetti().addConfetti({ emojis: ['🎉', '🎊'] }) + }) }) - }) + } }) const links = [ diff --git a/playground/pages/npm/js-confetti.vue b/playground/pages/npm/js-confetti.vue index 51f7afe9..b7d995de 100644 --- a/playground/pages/npm/js-confetti.vue +++ b/playground/pages/npm/js-confetti.vue @@ -11,7 +11,7 @@ export interface JSConfettiApi { } declare global { - interface Window extends JSConfettiApi {} + type Window = JSConfettiApi } const { $script } = useScriptNpm({ diff --git a/playground/scripts/myCustomScript.ts b/playground/scripts/myCustomScript.ts index adcfd488..f207cbbd 100644 --- a/playground/scripts/myCustomScript.ts +++ b/playground/scripts/myCustomScript.ts @@ -3,8 +3,7 @@ import type { Input } from 'valibot' import { useScript } from '#imports' import type { NuxtUseScriptOptions } from '#nuxt-scripts' -export interface MyCustomScriptApi { -} +export type MyCustomScriptApi = Record export const MyCustomScriptOptions = object({ id: string(), diff --git a/scripts/utils.ts b/scripts/utils.ts index ee2e4040..e3dce36c 100644 --- a/scripts/utils.ts +++ b/scripts/utils.ts @@ -35,7 +35,7 @@ export async function generateTpcContent(input: TpcDescriptor) { chunks.push(` declare global { - interface Window extends ${input.tpcTypeAugmentation} {} + type Window = ${input.tpcTypeAugmentation} }`) } diff --git a/src/devtools.ts b/src/devtools.ts index ebc6fb62..ea367e1b 100644 --- a/src/devtools.ts +++ b/src/devtools.ts @@ -5,10 +5,6 @@ import { useNuxt } from '@nuxt/kit' import type { ModuleOptions } from './module' import { DEVTOOLS_UI_LOCAL_PORT, DEVTOOLS_UI_ROUTE } from './constants' -export interface ServerFunctions {} - -export interface ClientFunctions {} - export function setupDevToolsUI(options: ModuleOptions, resolve: Resolver['resolve'], nuxt: Nuxt = useNuxt()) { const clientPath = resolve('./client') const isProductionBuild = existsSync(clientPath) diff --git a/src/module.ts b/src/module.ts index 96796586..0e9cedd3 100644 --- a/src/module.ts +++ b/src/module.ts @@ -131,7 +131,7 @@ export default defineNuxtModule({ nuxt.hooks.hook('modules:done', async () => { const registryScripts = [...scripts] - // @ts-ignore nuxi prepare is broken to generate these types, possibly because of the runtime path + // @ts-expect-error nuxi prepare is broken to generate these types, possibly because of the runtime path await nuxt.hooks.callHook('scripts:registry', registryScripts) const registryScriptsWithImport = registryScripts.filter(i => !!i.import?.name) as Required[] addImports(registryScriptsWithImport.map((i) => { diff --git a/src/runtime/components/ScriptGoogleMaps.vue b/src/runtime/components/ScriptGoogleMaps.vue index 72b7bada..6401b130 100644 --- a/src/runtime/components/ScriptGoogleMaps.vue +++ b/src/runtime/components/ScriptGoogleMaps.vue @@ -135,7 +135,9 @@ defineExpose({ onMounted(() => { watch(ready, (v) => { - v && emits('ready', map) + if (v) { + emits('ready', map) + } }) watch($script.status, () => { if ($script.status.value === 'error') { diff --git a/src/runtime/components/ScriptVimeoPlayer.vue b/src/runtime/components/ScriptVimeoPlayer.vue index ba382ce7..8a5600c8 100644 --- a/src/runtime/components/ScriptVimeoPlayer.vue +++ b/src/runtime/components/ScriptVimeoPlayer.vue @@ -188,7 +188,9 @@ onMounted(() => { }) watch(() => props.id, (v) => { - v && player?.loadVideo(Number(v)) + if (v) { + player?.loadVideo(Number(v)) + } }) watch($script.status, (status) => { if (status === 'error') { diff --git a/src/runtime/composables/useScriptEventPage.ts b/src/runtime/composables/useScriptEventPage.ts index efe89d29..66312ce8 100644 --- a/src/runtime/composables/useScriptEventPage.ts +++ b/src/runtime/composables/useScriptEventPage.ts @@ -14,7 +14,7 @@ export function useScriptEventPage(onChange?: (payload: TrackedPage) => void) { return payload let lastPayload: TrackedPage = { path: '', title: '' } - let stopDomWatcher: () => void + let stopDomWatcher = () => {} // TODO make sure useAsyncData isn't running nuxt.hooks.hook('page:finish', () => { Promise.race([ @@ -23,18 +23,20 @@ export function useScriptEventPage(onChange?: (payload: TrackedPage) => void) { new Promise((resolve) => { stopDomWatcher = head.hooks.hook('dom:rendered', () => resolve()) }), - ]).finally(() => { - stopDomWatcher && stopDomWatcher() - }).then(() => { - payload.value = { - path: route.fullPath, - title: document.title, - } - if (lastPayload.path !== payload.value.path || lastPayload.title !== payload.value.title) { - onChange && onChange(payload.value) - lastPayload = payload.value - } - }) + ]) + .finally(stopDomWatcher) + .then(() => { + payload.value = { + path: route.fullPath, + title: document.title, + } + if (lastPayload.path !== payload.value.path || lastPayload.title !== payload.value.title) { + if (onChange) { + onChange(payload.value) + } + lastPayload = payload.value + } + }) }) return payload } diff --git a/src/runtime/registry/clarity.ts b/src/runtime/registry/clarity.ts index 2f780d34..9827b60f 100644 --- a/src/runtime/registry/clarity.ts +++ b/src/runtime/registry/clarity.ts @@ -33,7 +33,7 @@ export interface ClarityApi { } declare global { - interface Window extends ClarityApi {} + type Window = ClarityApi } export const ClarityOptions = object({ diff --git a/src/runtime/registry/cloudflare-web-analytics.ts b/src/runtime/registry/cloudflare-web-analytics.ts index 39c5a2a3..b7d50f05 100644 --- a/src/runtime/registry/cloudflare-web-analytics.ts +++ b/src/runtime/registry/cloudflare-web-analytics.ts @@ -18,7 +18,7 @@ export interface CloudflareWebAnalyticsApi { } declare global { - interface Window extends CloudflareWebAnalyticsApi {} + type Window = CloudflareWebAnalyticsApi } export const CloudflareWebAnalyticsOptions = object({ diff --git a/src/runtime/registry/google-adsense.ts b/src/runtime/registry/google-adsense.ts index 47f42146..d4f75453 100644 --- a/src/runtime/registry/google-adsense.ts +++ b/src/runtime/registry/google-adsense.ts @@ -21,7 +21,7 @@ export interface GoogleAdsenseApi { } declare global { - interface Window extends GoogleAdsenseApi {} + type Window = GoogleAdsenseApi } /** diff --git a/src/runtime/registry/google-maps.ts b/src/runtime/registry/google-maps.ts index 555cff71..faab7e4b 100644 --- a/src/runtime/registry/google-maps.ts +++ b/src/runtime/registry/google-maps.ts @@ -23,7 +23,7 @@ export const GoogleMapsOptions = object({ export type GoogleMapsInput = RegistryScriptInput -type MapsNamespace = typeof google.maps +type MapsNamespace = google.maps export interface GoogleMapsApi { maps: MapsNamespace | Promise } diff --git a/src/runtime/registry/google-tag-manager.ts b/src/runtime/registry/google-tag-manager.ts index 90ca6535..31bba99d 100644 --- a/src/runtime/registry/google-tag-manager.ts +++ b/src/runtime/registry/google-tag-manager.ts @@ -6,7 +6,7 @@ import type { RegistryScriptInput } from '#nuxt-scripts' import { object, string, optional } from '#nuxt-scripts-validator' declare global { - interface Window extends GoogleTagManagerApi {} + type Window = GoogleTagManagerApi } export const GoogleTagManagerOptions = object({ id: string(), diff --git a/src/runtime/registry/matomo-analytics.ts b/src/runtime/registry/matomo-analytics.ts index 4509dbdb..3539e5ff 100644 --- a/src/runtime/registry/matomo-analytics.ts +++ b/src/runtime/registry/matomo-analytics.ts @@ -17,7 +17,7 @@ interface MatomoAnalyticsApi { } declare global { - interface Window extends MatomoAnalyticsApi {} + type Window = MatomoAnalyticsApi } export function useScriptMatomoAnalytics(_options?: MatomoAnalyticsInput) { @@ -42,8 +42,12 @@ export function useScriptMatomoAnalytics(_options? ? undefined : () => { const _paq = window._paq = window._paq || [] - options?.trackPageView !== false && _paq.push(['trackPageView']) - options?.enableLinkTracking !== false && _paq.push(['enableLinkTracking']) + if (options?.trackPageView) { + _paq.push(['trackPageView']) + } + if (options?.enableLinkTracking) { + _paq.push(['enableLinkTracking']) + } _paq.push(['setTrackerUrl', withBase(`/matomo.php`, withHttps(options?.matomoUrl))]) _paq.push(['setSiteId', options?.siteId || '1']) }, diff --git a/src/runtime/registry/meta-pixel.ts b/src/runtime/registry/meta-pixel.ts index 8d4372fd..7f07240c 100644 --- a/src/runtime/registry/meta-pixel.ts +++ b/src/runtime/registry/meta-pixel.ts @@ -32,8 +32,7 @@ export interface MetaPixelApi { } declare global { - interface Window extends MetaPixelApi { - } + type Window = MetaPixelApi } export const MetaPixelOptions = object({ @@ -57,8 +56,12 @@ export function useScriptMetaPixel(_options?: MetaPixelI ? undefined : () => { const fbq: MetaPixelApi['fbq'] = window.fbq = function (...params: any[]) { - // @ts-expect-error untyped - fbq.callMethod ? fbq.callMethod(...params) : fbq.queue.push(params) + if (fbq.callMethod) { + fbq.callMethod(...params) + } + else { + fbq.queue.push(params) + } } as any as MetaPixelApi['fbq'] if (!window._fbq) window._fbq = fbq diff --git a/src/runtime/registry/segment.ts b/src/runtime/registry/segment.ts index c6b48203..50a2402d 100644 --- a/src/runtime/registry/segment.ts +++ b/src/runtime/registry/segment.ts @@ -31,11 +31,10 @@ interface AnalyticsApi { push: (args: any[]) => void } -export interface SegmentApi extends Pick { -} +export type SegmentApi = Pick declare global { - interface Window extends SegmentApi { } + type Window = SegmentApi } const methods = ['track', 'page', 'identify', 'group', 'alias', 'reset'] diff --git a/src/runtime/registry/stripe.ts b/src/runtime/registry/stripe.ts index 5a37d0d7..6f8aa4d5 100644 --- a/src/runtime/registry/stripe.ts +++ b/src/runtime/registry/stripe.ts @@ -15,7 +15,7 @@ export interface StripeApi { } declare global { - interface Window extends StripeApi {} + type Window = StripeApi } export function useScriptStripe(_options?: StripeInput) { diff --git a/src/runtime/registry/vimeo-player.ts b/src/runtime/registry/vimeo-player.ts index b491408f..b87a57ee 100644 --- a/src/runtime/registry/vimeo-player.ts +++ b/src/runtime/registry/vimeo-player.ts @@ -15,7 +15,7 @@ export interface VimeoPlayerApi { export type VimeoPlayerInput = RegistryScriptInput declare global { - interface Window extends VimeoPlayerApi {} + type Window = VimeoPlayerApi } export function useScriptVimeoPlayer(_options?: VimeoPlayerInput) { diff --git a/src/runtime/registry/x-pixel.ts b/src/runtime/registry/x-pixel.ts index 6b1bfa3e..956c82fe 100644 --- a/src/runtime/registry/x-pixel.ts +++ b/src/runtime/registry/x-pixel.ts @@ -36,8 +36,7 @@ export interface XPixelApi { } declare global { - interface Window extends XPixelApi { - } + type Window = XPixelApi } export const XPixelOptions = object({ @@ -57,10 +56,13 @@ export function useScriptXPixel(_options?: XPixelInput) { ? undefined : () => { // @ts-expect-error untyped - const s = window.twq = function () { - // @ts-expect-error untyped - // eslint-disable-next-line prefer-rest-params - s.exe ? s.exe(s, arguments) : s.queue.push(arguments) + const s = window.twq = function (...args) { + if (e.exe) { + s.exe(s, args) + } + else { + s.queue.push(args) + } } // @ts-expect-error untyped s.version = options?.version || '1.1' diff --git a/src/runtime/types.ts b/src/runtime/types.ts index 0ed42f77..7d54b7c3 100644 --- a/src/runtime/types.ts +++ b/src/runtime/types.ts @@ -132,9 +132,9 @@ export type NuxtConfigScriptRegistry }> -const emptyOptions = object({}) +const _emptyOptions = object({}) -export type EmptyOptionsSchema = typeof emptyOptions +export type EmptyOptionsSchema = typeof _emptyOptions type ScriptInput = ScriptBase & DataKeys & SchemaAugmentations['script']