@@ -2,21 +2,26 @@ import { hash } from 'ohash'
2
2
import type { ModuleOptions } from './module'
3
3
import { logger } from './logger'
4
4
import type { RegistryScript } from '#nuxt-scripts/types'
5
+ import { useNuxt } from "@nuxt/kit" ;
6
+ import { relative } from "pathe" ;
5
7
6
8
export function templatePlugin ( config : Partial < ModuleOptions > , registry : Required < RegistryScript > [ ] ) {
7
9
if ( Array . isArray ( config . globals ) ) {
8
10
// convert to object
9
11
config . globals = Object . fromEntries ( config . globals . map ( i => [ hash ( i ) , i ] ) )
10
12
logger . warn ( 'The `globals` array option is deprecated, please convert to an object.' )
11
13
}
12
- const imports = [ 'useScript' , 'defineNuxtPlugin' ]
14
+ const nuxt = useNuxt ( )
15
+ const buildDir = nuxt . options . buildDir
16
+ const imports = [ ]
13
17
const inits = [ ]
14
18
// for global scripts, we can initialise them script away
15
19
for ( const [ k , c ] of Object . entries ( config . registry || { } ) ) {
16
20
const importDefinition = registry . find ( i => i . import . name === `useScript${ k . substring ( 0 , 1 ) . toUpperCase ( ) + k . substring ( 1 ) } ` )
17
21
if ( importDefinition ) {
18
22
// title case
19
- imports . unshift ( importDefinition . import . name )
23
+ imports . unshift ( `import { ${ importDefinition . import . name } } from '${ relative ( buildDir , importDefinition . import . from ) } '` )
24
+ console . log ( `import { ${ importDefinition . import . name } } from '${ relative ( buildDir , importDefinition . import . from ) } '` )
20
25
const args = ( typeof c !== 'object' ? { } : c ) || { }
21
26
if ( c === 'mock' )
22
27
args . scriptOptions = { trigger : 'manual' , skipValidation : true }
@@ -35,7 +40,9 @@ export function templatePlugin(config: Partial<ModuleOptions>, registry: Require
35
40
}
36
41
}
37
42
return [
38
- `import { ${ imports . join ( ', ' ) } } from '#imports'` ,
43
+ `import { useScript } from '#nuxt-scripts/composables/useScript'` ,
44
+ `import { defineNuxtPlugin } from 'nuxt/app'` ,
45
+ ...imports ,
39
46
'' ,
40
47
`export default defineNuxtPlugin({` ,
41
48
` name: "scripts:init",` ,
0 commit comments