2
2
addBuildPlugin ,
3
3
addComponentsDir ,
4
4
addImports ,
5
- addPluginTemplate ,
5
+ addPluginTemplate , addTypeTemplate ,
6
6
createResolver ,
7
7
defineNuxtModule ,
8
8
hasNuxtModule ,
@@ -13,7 +13,7 @@ import { setupDevToolsUI } from './devtools'
13
13
import { NuxtScriptBundleTransformer } from './plugins/transform'
14
14
import { setupPublicAssetStrategy } from './assets'
15
15
import { logger } from './logger'
16
- import { extendTypes , installNuxtModule } from './kit'
16
+ import { installNuxtModule } from './kit'
17
17
import { registry } from './registry'
18
18
import type {
19
19
NuxtConfigScriptRegistry ,
@@ -24,6 +24,7 @@ import type {
24
24
} from './runtime/types'
25
25
import { NuxtScriptsCheckScripts } from './plugins/check-scripts'
26
26
import { templatePlugin } from './templates'
27
+ import { relative , resolve } from 'pathe'
27
28
28
29
export interface ModuleOptions {
29
30
/**
@@ -171,9 +172,11 @@ export default defineNuxtModule<ModuleOptions>({
171
172
const registryScriptsWithImport = registryScripts . filter ( i => ! ! i . import ?. name ) as Required < RegistryScript > [ ]
172
173
const newScripts = registryScriptsWithImport . filter ( i => ! scripts . some ( r => r . import ?. name === i . import . name ) )
173
174
174
- // augment types to support the integrations registry
175
- extendTypes ( name ! , async ( { typesPath } ) => {
176
- let types = `
175
+ addTypeTemplate ( {
176
+ filename : 'module/nuxt-scripts.d.ts' ,
177
+ getContents : ( data ) => {
178
+ const typesPath = relative ( resolve ( data . nuxt ! . options . rootDir , data . nuxt ! . options . buildDir , 'module' ) , resolve ( 'runtime/types' ) )
179
+ let types = `
177
180
declare module '#app' {
178
181
interface NuxtApp {
179
182
$scripts: Record<${ [ ...Object . keys ( config . globals || { } ) , ...Object . keys ( config . registry || { } ) ] . map ( k => `'${ k } '` ) . concat ( [ 'string' ] ) . join ( ' | ' ) } , (import('#nuxt-scripts/types').UseScriptContext<any>)>
@@ -184,8 +187,8 @@ declare module '#app' {
184
187
}
185
188
}
186
189
`
187
- if ( newScripts . length ) {
188
- types = `${ types }
190
+ if ( newScripts . length ) {
191
+ types = `${ types }
189
192
declare module '#nuxt-scripts/types' {
190
193
type NuxtUseScriptOptions = Omit<import('${ typesPath } ').NuxtUseScriptOptions, 'use' | 'beforeInit'>
191
194
interface ScriptRegistry {
@@ -196,9 +199,13 @@ ${newScripts.map((i) => {
196
199
} ) . join ( '\n' ) }
197
200
}
198
201
}`
199
- return types
200
- }
201
- return types
202
+ return types
203
+ }
204
+ return `${ types }
205
+ export {}`
206
+ } ,
207
+ } , {
208
+ nuxt : true ,
202
209
} )
203
210
204
211
if ( Object . keys ( config . globals || { } ) . length || Object . keys ( config . registry || { } ) . length ) {
0 commit comments