Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
13 changes: 13 additions & 0 deletions src/runtime/composables/useFeatureDetection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { onMounted } from 'vue'

export function useFeatureDetection(id: string) {
onMounted(() => {
onMounted(() => {
performance?.mark?.('mark_feature_usage', {
detail: {
feature: `nuxt-third-parties-${id}`,
},
})
})
})
}
2 changes: 1 addition & 1 deletion src/tpc/google-analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export default function googleAnalitycsRegistry() {
},
tpcKey: 'gtag',
tpcTypeImport: 'GoogleAnalyticsApi',
augmentWindowTypes: true,
featureDetection: 'ga',
})
},
filename: 'nuxt-scripts/tpc/google-analytics.ts',
Expand Down
2 changes: 1 addition & 1 deletion src/tpc/google-tag-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export default function googleTagManagerRegistry() {
},
tpcKey: 'gtm',
tpcTypeImport: 'GoogleTagManagerApi',
augmentWindowTypes: true,
featureDetection: 'gtm',
})
},
filename: 'nuxt-scripts/tpc/google-tag-manager.ts',
Expand Down
11 changes: 6 additions & 5 deletions src/tpc/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ export interface ScriptContentOpts {
data: Output
scriptFunctionName: string
tpcTypeImport: string
augmentWindowTypes?: boolean
tpcKey: string
/**
* This will be stringified. The function must be pure.
Expand All @@ -17,6 +16,7 @@ export interface ScriptContentOpts {
* This will be stringified. The function must be pure.
*/
stub: (params: { fn: string }) => any
featureDetection: string
}

const HEAD_VAR = '__head'
Expand All @@ -38,10 +38,13 @@ export function getTpcScriptContent(input: ScriptContentOpts) {
'import { withQuery } from \'ufo\'',
'import { useRegistryScript } from \'#nuxt-scripts-utils\'',
'import type { RegistryScriptInput } from \'#nuxt-scripts\'',
'import { useFeatureDetection } from \'#imports\'',
])

const chunks: string[] = []
const functionBody: string[] = []
const functionBody: string[] = [
`useFeatureDetection('${input.featureDetection}')`,
]

const hasParams = mainScript.params?.length

Expand All @@ -56,12 +59,10 @@ export function getTpcScriptContent(input: ScriptContentOpts) {
chunks.push(`const OptionSchema = object({${mainScript.params?.map(p => `${p}: any()`)}})`)
}

if (input.augmentWindowTypes) {
chunks.push(`
chunks.push(`
declare global {
interface Window extends ${input.tpcTypeImport} {}
}`)
}

const clientInitCode: string[] = []

Expand Down
9 changes: 5 additions & 4 deletions test/unit/tpc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ describe.each([
},
tpcKey: 'google-analytics',
tpcTypeImport: 'GoogleAnalyticsInput',
augmentWindowTypes: true,
scriptFunctionName: 'useScriptGoogleAnalytics',
use: () => { },
stub: () => { },
featureDetection: 'ga',
})).toThrowError('no main script found for google-analytics in third-party-capital')
})

Expand All @@ -60,14 +60,14 @@ describe.each([
},
tpcKey: 'google-analytics',
tpcTypeImport: 'GoogleAnalyticsInput',
augmentWindowTypes: true,
scriptFunctionName: 'useScriptGoogleAnalytics',
use: () => {
return { dataLayer: window.dataLayer, gtag: window.gtag }
},
stub: () => {
return []
},
featureDetection: 'google-analytics',
}

it(`expect to${isDev ? '' : ' not'} add the schema to the script options`, () => {
Expand Down Expand Up @@ -113,12 +113,13 @@ describe.each([
expect(getCodeFromAst(result, stubFn)).toContain('return []')
})

it('expect to augment window types', () => {
it('expect to augment window types and useFeature detection', () => {
const result = getTpcScriptContent(input)
const ast = parse(result, { loc: true, range: true })
const augmentWindowTypes = ast.body.find((node): node is TSESTree.TSModuleDeclaration => node.type === TSESTree.AST_NODE_TYPES.TSModuleDeclaration)
expect(augmentWindowTypes).toBeTruthy()
expect(getCodeFromAst(result, augmentWindowTypes!)).toContain('interface Window extends GoogleAnalyticsInput {}')
expect(result).toContain('useFeatureDetection(\'google-analytics\')')
})
})
})
Expand All @@ -141,10 +142,10 @@ describe('script content generation with head positioning', () => {
},
tpcKey: 'google-analytics',
tpcTypeImport: 'GoogleAnalyticsInput',
augmentWindowTypes: true,
scriptFunctionName: 'useScriptGoogleAnalytics',
use: () => { },
stub: () => { },
featureDetection: 'google-analytics',
}

describe('main script', () => {
Expand Down