-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmodule.ts
61 lines (53 loc) · 1.44 KB
/
module.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import { type VueGtmUseOptions } from '@gtm-support/vue-gtm'
import { defineNuxtModule, addPlugin, createResolver, addImports } from '@nuxt/kit'
import { defu } from 'defu'
import { setupDevToolsUI } from './devtools'
// Module options TypeScript interface definition
export interface ModuleOptions extends Omit<VueGtmUseOptions, 'vueRouter'> {
enableRouterSync?: boolean
/**
* Enable Nuxt Devtools integration
*
* @default true
*/
devtools?: boolean
}
declare module '@nuxt/schema' {
interface PublicRuntimeConfig {
gtm: ModuleOptions
}
interface NuxtConfig {
gtm?: ModuleOptions
}
interface NuxtOptions {
gtm?: ModuleOptions
}
}
export default defineNuxtModule<ModuleOptions>({
meta: {
name: '@zadigetvoltaire/nuxt-gtm',
configKey: 'gtm',
compatibility: {
nuxt: '^3.0.0'
}
},
// @ts-ignore
defaults: {
devtools: true
},
setup (options, nuxt) {
const resolver = createResolver(import.meta.url)
const moduleOptions: ModuleOptions = defu(nuxt.options.runtimeConfig.public.gtm, options)
nuxt.options.runtimeConfig.public.gtm = moduleOptions
// Do not add the extension since the `.ts` will be transpiled to `.mjs` after `npm run prepack`
addPlugin(resolver.resolve('./runtime/plugin'))
addImports({
name: 'useGtm',
as: 'useGtm',
from: '@gtm-support/vue-gtm'
})
if (options.devtools) {
setupDevToolsUI(nuxt, resolver)
}
}
})