Skip to content

Commit f1423d2

Browse files
committed
fix: tree shake registry script src resolvers
1 parent 6e60571 commit f1423d2

File tree

6 files changed

+22
-36
lines changed

6 files changed

+22
-36
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"bugs": {
1818
"url": "https://github.com/nuxt/scripts/issues"
1919
},
20+
"sideEffects": false,
2021
"exports": {
2122
".": {
2223
"types": "./dist/types.d.ts",

src/registry.ts

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,6 @@ import type { RegistryScripts } from './runtime/types'
99

1010
// avoid nuxt/kit dependency here so we can use in docs
1111

12-
export function GoogleAnalyticsScriptResolver(options?: GoogleAnalyticsInput) {
13-
return withQuery('https://www.googletagmanager.com/gtag/js', {
14-
id: options?.id,
15-
})
16-
}
17-
18-
export function PlausibleAnalyticsScriptResolver(options: PlausibleAnalyticsInput) {
19-
const extensions = Array.isArray(options?.extension) ? options.extension.join('.') : [options?.extension]
20-
return options?.extension ? `https://plausible.io/js/script.${extensions}.js` : 'https://plausible.io/js/script.js'
21-
}
22-
23-
export function CloudflareWebAnalyticsScriptResolver() {
24-
return 'https://static.cloudflareinsights.com/beacon.min.js'
25-
}
26-
27-
export function SegmentScriptResolver(options: SegmentInput) {
28-
return joinURL('https://cdn.segment.com/analytics.js/v1', options?.writeKey || '', 'analytics.min.js')
29-
}
30-
31-
export function MetaPixelScriptResolver() {
32-
return 'https://connect.facebook.net/en_US/fbevents.js'
33-
}
34-
3512
export const registry: (resolve?: (s: string) => string) => RegistryScripts = (resolve?: (s: string) => string) => {
3613
resolve = resolve || ((s: string) => s)
3714
return [
@@ -40,7 +17,9 @@ export const registry: (resolve?: (s: string) => string) => RegistryScripts = (r
4017
label: 'Google Analytics',
4118
category: 'analytics',
4219
logo: `<svg xmlns="http://www.w3.org/2000/svg" width="28.85" height="32" viewBox="0 0 256 284"><path fill="#F9AB00" d="M256.003 247.933a35.224 35.224 0 0 1-39.376 35.161c-18.044-2.67-31.266-18.371-30.826-36.606V36.845C185.365 18.591 198.62 2.881 216.687.24a35.221 35.221 0 0 1 39.316 35.16z"/><path fill="#E37400" d="M35.101 213.193c19.386 0 35.101 15.716 35.101 35.101c0 19.386-15.715 35.101-35.101 35.101S0 267.68 0 248.295c0-19.386 15.715-35.102 35.101-35.102m92.358-106.387c-19.477 1.068-34.59 17.406-34.137 36.908v94.285c0 25.588 11.259 41.122 27.755 44.433a35.161 35.161 0 0 0 42.146-34.56V142.089a35.222 35.222 0 0 0-35.764-35.282"/></svg>`,
43-
scriptBundling: GoogleAnalyticsScriptResolver,
20+
scriptBundling: (options?: GoogleAnalyticsInput) => withQuery('https://www.googletagmanager.com/gtag/js', {
21+
id: options?.id,
22+
}),
4423
import: {
4524
name: 'useScriptGoogleAnalytics',
4625
from: resolve('./runtime/registry/google-analytics'),
@@ -49,7 +28,10 @@ export const registry: (resolve?: (s: string) => string) => RegistryScripts = (r
4928
{
5029
label: 'Plausible Analytics',
5130
category: 'analytics',
52-
scriptBundling: PlausibleAnalyticsScriptResolver,
31+
scriptBundling: (options?: PlausibleAnalyticsInput) => {
32+
const extensions = Array.isArray(options?.extension) ? options.extension.join('.') : [options?.extension]
33+
return options?.extension ? `https://plausible.io/js/script.${extensions}.js` : 'https://plausible.io/js/script.js'
34+
},
5335
logo: `<svg height="32" id="Layer_2" viewBox="0 0 46 60" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><defs><linearGradient id="New_Gradient_Swatch_1" x1="14.841" y1="22.544" x2="27.473" y2="44.649" gradientUnits="userSpaceOnUse"><stop offset="0" stop-color="#909cf7"/><stop offset="1" stop-color="#4b38d8"/></linearGradient><linearGradient id="New_Gradient_Swatch_1-2" x1="7.984" y1="-1.358" x2="21.001" y2="21.422" xlink:href="#New_Gradient_Swatch_1"/><style>.cls-3{stroke-width:0;fill:#1f2961}</style></defs><g id="Plausible_-_Branding"><g id="Gradient_Logo_-_Purple_Gradient_on_White"><g id="Symbol_-_Purple_Gradient"><path d="M45.246 22.603C44.155 33.059 35.013 40.83 24.5 40.83h-4.047v9.57a9.6 9.6 0 0 1-9.6 9.6H3.36A3.36 3.36 0 0 1 0 56.64V36.938l5.038-7.07a3.362 3.362 0 0 1 4.037-1.149l2.866 1.2a3.353 3.353 0 0 0 4.025-1.145l6.717-9.417a3.34 3.34 0 0 1 4.014-1.14l5.52 2.32a3.347 3.347 0 0 0 4.022-1.142l6.46-9.063c2.025 3.56 3.014 7.789 2.547 12.27Z" style="stroke-width:0;fill:url(#New_Gradient_Swatch_1)"/><path d="M3.292 28.873c.823-1.155 2.021-2.044 3.414-2.312a5.41 5.41 0 0 1 3.147.316l2.865 1.2a1.357 1.357 0 0 0 1.62-.464l6.594-9.245c.823-1.154 2.02-2.041 3.412-2.309a5.368 5.368 0 0 1 3.128.314l5.52 2.32a1.35 1.35 0 0 0 1.619-.46l6.919-9.707C37.827 3.364 31.78 0 24.945 0H3.36A3.36 3.36 0 0 0 0 3.36v30.132l3.292-4.62Z" style="fill:url(#New_Gradient_Swatch_1-2);stroke-width:0"/></g></g></g></svg>`,
5436
import: {
5537
name: 'useScriptPlausibleAnalytics',
@@ -58,7 +40,7 @@ export const registry: (resolve?: (s: string) => string) => RegistryScripts = (r
5840
},
5941
{
6042
label: 'Cloudflare Web Analytics',
61-
scriptBundling: CloudflareWebAnalyticsScriptResolver,
43+
src: 'https://static.cloudflareinsights.com/beacon.min.js',
6244
category: 'analytics',
6345
logo: `<svg xmlns="http://www.w3.org/2000/svg" width="70.02" height="32" viewBox="0 0 256 117"><path fill="#FBAD41" d="M205.52 50.813c-.858 0-1.705.03-2.551.058c-.137.007-.272.04-.398.094a1.424 1.424 0 0 0-.92.994l-3.628 12.672c-1.565 5.449-.983 10.48 1.646 14.174c2.41 3.416 6.42 5.421 11.289 5.655l19.679 1.194c.585.03 1.092.312 1.4.776a1.92 1.92 0 0 1 .2 1.692a2.496 2.496 0 0 1-2.134 1.662l-20.448 1.193c-11.11.515-23.062 9.58-27.255 20.633l-1.474 3.9a1.092 1.092 0 0 0 .967 1.49h70.425a1.872 1.872 0 0 0 1.81-1.365A51.172 51.172 0 0 0 256 101.828c0-28.16-22.582-50.984-50.449-50.984"/><path fill="#F6821F" d="m174.782 115.362l1.303-4.583c1.568-5.449.987-10.48-1.639-14.173c-2.418-3.417-6.424-5.422-11.296-5.656l-92.312-1.193a1.822 1.822 0 0 1-1.459-.776a1.919 1.919 0 0 1-.203-1.693a2.496 2.496 0 0 1 2.154-1.662l93.173-1.193c11.063-.511 23.015-9.58 27.208-20.633l5.313-14.04c.214-.596.27-1.238.156-1.86C191.126 20.51 166.91 0 137.96 0C111.269 0 88.626 17.403 80.5 41.596a26.996 26.996 0 0 0-19.156-5.359C48.549 37.524 38.25 47.946 36.979 60.88a27.905 27.905 0 0 0 .702 9.642C16.773 71.145 0 88.454 0 109.726c0 1.923.137 3.818.413 5.667c.115.897.879 1.57 1.783 1.568h170.48a2.223 2.223 0 0 0 2.106-1.63"/></svg>`,
6446
import: {
@@ -98,7 +80,9 @@ export const registry: (resolve?: (s: string) => string) => RegistryScripts = (r
9880
},
9981
{
10082
label: 'Segment',
101-
scriptBundling: SegmentScriptResolver,
83+
scriptBundling: (options?: SegmentInput) => {
84+
return joinURL('https://cdn.segment.com/analytics.js/v1', options?.writeKey || '', 'analytics.min.js')
85+
},
10286
logo: `<svg xmlns="http://www.w3.org/2000/svg" width="30.92" height="32" viewBox="0 0 256 265"><path fill="#4FB58B" d="m233.56 141.927l.17.013l17.892 1.87a4.927 4.927 0 0 1 3.225 1.707l.133.163l-.17.085a4.93 4.93 0 0 1 1.02 3.74a133.272 133.272 0 0 1-41.604 81.083a128.86 128.86 0 0 1-87.629 34.38a127.488 127.488 0 0 1-46.156-8.57l-.802-.312a4.716 4.716 0 0 1-2.686-2.533l-.077-.187a4.891 4.891 0 0 1-.083-3.66l7.062-17.23a4.846 4.846 0 0 1 6.118-2.799l.163.06c36.097 13.939 76.98 6.089 105.349-20.227a104.455 104.455 0 0 0 32.891-63.32a4.93 4.93 0 0 1 5.013-4.27zm-190.08 64.31l.251-.002l.253.002c8.12.093 14.658 6.659 14.746 14.749v.253c0 .084 0 .168-.002.252c-.141 8.284-6.97 14.886-15.254 14.745c-8.284-.141-14.885-6.97-14.745-15.254c.139-8.115 6.695-14.615 14.75-14.745M4.93 147.082h146.316a4.973 4.973 0 0 1 4.928 4.844l.002.171v18.316a4.974 4.974 0 0 1-4.76 5.01l-.17.005H4.93A4.975 4.975 0 0 1 0 170.584v-18.659a4.975 4.975 0 0 1 4.755-4.838zM169.56 7.311a4.974 4.974 0 0 1 2.848 2.635a5.096 5.096 0 0 1 0 3.867l-6.375 16.999a4.845 4.845 0 0 1-6.162 2.974A101.228 101.228 0 0 0 62.13 51.252a105.267 105.267 0 0 0-34.507 54.99a4.93 4.93 0 0 1-4.76 3.698h-1.105L4.25 105.733a4.886 4.886 0 0 1-3.103-2.295h-.085A4.929 4.929 0 0 1 .51 99.57a133.393 133.393 0 0 1 44.41-70.204C79.739.7 127.019-7.666 169.56 7.311m-64.807 73.434H251.07a4.972 4.972 0 0 1 4.922 4.67l.008.174v18.317a4.973 4.973 0 0 1-4.76 5.01l-.17.005H104.754a4.972 4.972 0 0 1-4.886-4.842l-.002-.173V85.759a4.972 4.972 0 0 1 4.715-5.008zm101.572-55.883l.252-.002l.253.002c8.12.093 14.658 6.659 14.746 14.748v.253c0 .085 0 .17-.002.253c-.14 8.284-6.97 14.885-15.254 14.744c-8.284-.14-14.885-6.97-14.744-15.253c.138-8.116 6.694-14.616 14.749-14.745"/></svg>`,
10387
category: 'tracking',
10488
import: {
@@ -108,7 +92,7 @@ export const registry: (resolve?: (s: string) => string) => RegistryScripts = (r
10892
},
10993
{
11094
label: 'Meta Pixel',
111-
scriptBundling: MetaPixelScriptResolver,
95+
src: 'https://connect.facebook.net/en_US/fbevents.js',
11296
category: 'tracking',
11397
logo: `<svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 256 256"><path fill="#1877F2" d="M256 128C256 57.308 198.692 0 128 0C57.308 0 0 57.308 0 128c0 63.888 46.808 116.843 108 126.445V165H75.5v-37H108V99.8c0-32.08 19.11-49.8 48.348-49.8C170.352 50 185 52.5 185 52.5V84h-16.14C152.959 84 148 93.867 148 103.99V128h35.5l-5.675 37H148v89.445c61.192-9.602 108-62.556 108-126.445"/><path fill="#FFF" d="m177.825 165l5.675-37H148v-24.01C148 93.866 152.959 84 168.86 84H185V52.5S170.352 50 156.347 50C127.11 50 108 67.72 108 99.8V128H75.5v37H108v89.445A128.959 128.959 0 0 0 128 256a128.9 128.9 0 0 0 20-1.555V165z"/></svg>`,
11498
import: {

src/runtime/registry/google-analytics.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { GTag, GoogleAnalyticsApi } from 'third-party-capital'
2+
import { withQuery } from 'ufo'
23
import { useRegistryScript } from '../utils'
3-
import { GoogleAnalyticsScriptResolver } from '../../registry'
44
import { object, string } from '#nuxt-scripts-validator'
55
import type { RegistryScriptInput } from '#nuxt-scripts'
66

@@ -26,7 +26,9 @@ export function useScriptGoogleAnalytics<T extends GoogleAnalyticsApi>(_options?
2626
// Note: inputs.useScriptInput is not usable, needs to be normalized
2727
return useRegistryScript<T, typeof GoogleAnalyticsOptions>('googleAnalytics', options => ({
2828
scriptInput: {
29-
src: GoogleAnalyticsScriptResolver(options),
29+
src: withQuery('https://www.googletagmanager.com/gtag/js', {
30+
id: options?.id,
31+
}),
3032
},
3133
schema: import.meta.dev ? GoogleAnalyticsOptions : undefined,
3234
scriptOptions: {

src/runtime/registry/meta-pixel.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { useRegistryScript } from '../utils'
2-
import { MetaPixelScriptResolver } from '../../registry'
32
import { number, object, string, union } from '#nuxt-scripts-validator'
43
import type { RegistryScriptInput } from '#nuxt-scripts'
54

@@ -45,7 +44,7 @@ export type MetaPixelInput = RegistryScriptInput<typeof MetaPixelOptions>
4544
export function useScriptMetaPixel<T extends MetaPixelApi>(_options?: MetaPixelInput) {
4645
return useRegistryScript<T, typeof MetaPixelOptions>('metaPixel', options => ({
4746
scriptInput: {
48-
src: MetaPixelScriptResolver(),
47+
src: 'https://connect.facebook.net/en_US/fbevents.js',
4948
crossorigin: false,
5049
},
5150
schema: import.meta.dev ? MetaPixelOptions : undefined,

src/runtime/registry/plausible-analytics.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { useRegistryScript } from '../utils'
2-
import { PlausibleAnalyticsScriptResolver } from '../../registry'
32
import { array, literal, object, optional, string, union } from '#nuxt-scripts-validator'
43
import type { RegistryScriptInput } from '#nuxt-scripts'
54

@@ -38,9 +37,10 @@ declare global {
3837

3938
export function useScriptPlausibleAnalytics<T extends PlausibleAnalyticsApi>(_options?: PlausibleAnalyticsInput) {
4039
return useRegistryScript<T, typeof PlausibleAnalyticsOptions>('plausibleAnalytics', (options) => {
40+
const extensions = Array.isArray(options?.extension) ? options.extension.join('.') : [options?.extension]
4141
return {
4242
scriptInput: {
43-
'src': PlausibleAnalyticsScriptResolver(options),
43+
'src': options?.extension ? `https://plausible.io/js/script.${extensions}.js` : 'https://plausible.io/js/script.js',
4444
'data-domain': options?.domain,
4545
},
4646
schema: import.meta.dev ? PlausibleAnalyticsOptions : undefined,

src/runtime/registry/segment.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
import { joinURL } from 'ufo'
12
import { useRegistryScript } from '../utils'
2-
import { SegmentScriptResolver } from '../../registry'
33
import { object, optional, string } from '#nuxt-scripts-validator'
44
import type { RegistryScriptInput } from '#nuxt-scripts'
55

@@ -46,7 +46,7 @@ export function useScriptSegment<T extends SegmentApi>(_options?: SegmentInput)
4646
return {
4747
scriptInput: {
4848
'data-global-segment-analytics-key': k,
49-
'src': SegmentScriptResolver(options),
49+
'src': joinURL('https://cdn.segment.com/analytics.js/v1', options?.writeKey || '', 'analytics.min.js'),
5050
},
5151
clientInit: import.meta.server
5252
? undefined

0 commit comments

Comments
 (0)