Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update dependency third-party-capital to v3 #318

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
"shiki": "1.24.0",
"sirv": "^3.0.0",
"std-env": "^3.8.0",
"third-party-capital": "2.3.0",
"third-party-capital": "3.0.0",
"ufo": "^1.5.4",
"unimport": "^3.14.4",
"unplugin": "^2.0.0",
Expand Down
10 changes: 5 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 10 additions & 4 deletions scripts/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export async function generateTpcContent(input: TpcDescriptor) {

chunks.push(`
declare global {
type Window = ${input.tpcTypeAugmentation}
interface Window extends ${input.tpcTypeAugmentation} {}
}`)
}

Expand All @@ -58,12 +58,12 @@ export async function generateTpcContent(input: TpcDescriptor) {
const optionalParams = [...new Set(input.tpcData.scripts?.map(s => Object.keys(s.optionalParams) || []).flat() || [])]

if (params.length || optionalParams.length) {
const validatorImports = new Set<string>(['object', 'string'])
const validatorImports = new Set<string>(['object', 'any'])
if (optionalParams.length) {
validatorImports.add('optional')
}

const properties = params.filter(p => !optionalParams.includes(p)).map(p => `${p}: string()`).concat(optionalParams.map(o => `${o}: optional(string())`))
const properties = params.filter(p => !optionalParams.includes(p)).map(p => `${p}: any()`).concat(optionalParams.map(o => `${o}: optional(any())`))
// need schema validation from tpc
chunks.push(`export const ${titleKey}Options = object({
${properties.join(',\n')}
Expand Down Expand Up @@ -131,7 +131,13 @@ ${functionBody.join('\n')}
}

function replaceTokenToRuntime(code: string, defaultValues?: Record<string, string | number | undefined>) {
return code.split(';').map(c => c.replaceAll(/'?\{\{(.*?)\}\}'?/g, (_, token) => {
// replace all {{#token}}...{{/token}} with if(options?.token){...}
return code.replace(
/\{\{#([^{}]+)\}\}(.*)\{\{\/\1\}\}/g,
(_match, name, innerCode) => {
return `if(options?.${name}){${replaceTokenToRuntime(innerCode, defaultValues)}};`
},
).split(';').map(c => c.replaceAll(/'?\{\{(.*?)\}\}'?/g, (_, token) => {
return `(options?.${token} ${defaultValues?.[token] ? `?? ${JSON.stringify(defaultValues?.[token])}` : ''})`
})).join(';')
}
Expand Down
10 changes: 6 additions & 4 deletions src/runtime/registry/google-analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ import { withQuery } from 'ufo'
import type { DataLayer, GTag } from 'third-party-capital'
import { useRegistryScript } from '#nuxt-scripts/utils'
import type { RegistryScriptInput } from '#nuxt-scripts/types'
import { object, string, optional } from '#nuxt-scripts-validator'
import { object, any, optional } from '#nuxt-scripts-validator'

export const GoogleAnalyticsOptions = object({
id: string(),
l: optional(string()),
id: any(),
l: optional(any()),
consentType: optional(any()),
consentValues: optional(any()),
})
export type GoogleAnalyticsInput = RegistryScriptInput<typeof GoogleAnalyticsOptions>

Expand All @@ -34,6 +36,6 @@ export function useScriptGoogleAnalytics(_options?: GoogleAnalyticsInput) {
// eslint-disable-next-line
// @ts-ignore
// eslint-disable-next-line
clientInit: import.meta.server ? undefined : () => {window[(options?.l ?? "dataLayer")]=window[(options?.l ?? "dataLayer")]||[];window['gtag-'+(options?.l ?? "dataLayer")]=function (){window[(options?.l ?? "dataLayer")].push(arguments);};window['gtag-'+(options?.l ?? "dataLayer")]('js',new Date());window['gtag-'+(options?.l ?? "dataLayer")]('config',(options?.id ))},
clientInit: import.meta.server ? undefined : () => {window[(options?.l ?? "dataLayer")]=window[(options?.l ?? "dataLayer")]||[];window['gtag-'+(options?.l ?? "dataLayer")]=function (){window[(options?.l ?? "dataLayer")].push(arguments);};if(options?.consentValues){window['gtag-'+(options?.l ?? "dataLayer")]('consent', (options?.consentType ?? "default"), (options?.consentValues ));};window['gtag-'+(options?.l ?? "dataLayer")]('js',new Date());window['gtag-'+(options?.l ?? "dataLayer")]('config',(options?.id ))},
}), _options)
}
10 changes: 6 additions & 4 deletions src/runtime/registry/google-tag-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ import { withQuery } from 'ufo'
import type { GoogleTagManagerApi, DataLayer } from 'third-party-capital'
import { useRegistryScript } from '#nuxt-scripts/utils'
import type { RegistryScriptInput } from '#nuxt-scripts/types'
import { object, string, optional } from '#nuxt-scripts-validator'
import { object, any, optional } from '#nuxt-scripts-validator'

declare global {
interface Window extends GoogleTagManagerApi {}
}
export const GoogleTagManagerOptions = object({
id: string(),
l: optional(string()),
id: any(),
l: optional(any()),
consentType: optional(any()),
consentValues: optional(any()),
})
export type GoogleTagManagerInput = RegistryScriptInput<typeof GoogleTagManagerOptions>

Expand All @@ -33,6 +35,6 @@ export function useScriptGoogleTagManager(_options?: GoogleTagManagerInput) {
// eslint-disable-next-line
// @ts-ignore
// eslint-disable-next-line
clientInit: import.meta.server ? undefined : () => {window[(options?.l ?? "dataLayer")]=window[(options?.l ?? "dataLayer")]||[];window[(options?.l ?? "dataLayer")].push({'gtm.start':new Date().getTime(),event:'gtm.js'});},
clientInit: import.meta.server ? undefined : () => {window[(options?.l ?? "dataLayer")]=window[(options?.l ?? "dataLayer")]||[];if(options?.consentValues){(function () {window[(options?.l ?? "dataLayer")].push(arguments)})('consent', (options?.consentType ?? "default"), (options?.consentValues ));};window[(options?.l ?? "dataLayer")].push({'gtm.start':new Date().getTime(),event:'gtm.js'});},
}), _options)
}
Loading