@@ -2,21 +2,26 @@ import { hash } from 'ohash'
22import type { ModuleOptions } from './module'
33import { logger } from './logger'
44import type { RegistryScript } from '#nuxt-scripts/types'
5+ import { useNuxt } from "@nuxt/kit" ;
6+ import { relative } from "pathe" ;
57
68export function templatePlugin ( config : Partial < ModuleOptions > , registry : Required < RegistryScript > [ ] ) {
79 if ( Array . isArray ( config . globals ) ) {
810 // convert to object
911 config . globals = Object . fromEntries ( config . globals . map ( i => [ hash ( i ) , i ] ) )
1012 logger . warn ( 'The `globals` array option is deprecated, please convert to an object.' )
1113 }
12- const imports = [ 'useScript' , 'defineNuxtPlugin' ]
14+ const nuxt = useNuxt ( )
15+ const buildDir = nuxt . options . buildDir
16+ const imports = [ ]
1317 const inits = [ ]
1418 // for global scripts, we can initialise them script away
1519 for ( const [ k , c ] of Object . entries ( config . registry || { } ) ) {
1620 const importDefinition = registry . find ( i => i . import . name === `useScript${ k . substring ( 0 , 1 ) . toUpperCase ( ) + k . substring ( 1 ) } ` )
1721 if ( importDefinition ) {
1822 // 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 ) } '` )
2025 const args = ( typeof c !== 'object' ? { } : c ) || { }
2126 if ( c === 'mock' )
2227 args . scriptOptions = { trigger : 'manual' , skipValidation : true }
@@ -35,7 +40,9 @@ export function templatePlugin(config: Partial<ModuleOptions>, registry: Require
3540 }
3641 }
3742 return [
38- `import { ${ imports . join ( ', ' ) } } from '#imports'` ,
43+ `import { useScript } from '#nuxt-scripts/composables/useScript'` ,
44+ `import { defineNuxtPlugin } from 'nuxt/app'` ,
45+ ...imports ,
3946 '' ,
4047 `export default defineNuxtPlugin({` ,
4148 ` name: "scripts:init",` ,
0 commit comments