@@ -105,10 +105,10 @@ export default defineNuxtModule<ModuleOptions>({
105105 debug : false ,
106106 } ,
107107 async setup ( config , nuxt ) {
108- const { resolve } = createResolver ( import . meta. url )
109- const { version, name } = await readPackageJSON ( resolve ( '../package.json' ) )
110- nuxt . options . alias [ '#nuxt-scripts-validator' ] = resolve ( `./runtime/validation/${ ( nuxt . options . dev || nuxt . options . _prepare ) ? 'valibot' : 'mock' } ` )
111- nuxt . options . alias [ '#nuxt-scripts' ] = resolve ( './runtime' )
108+ const { resolvePath } = createResolver ( import . meta. url )
109+ const { version, name } = await readPackageJSON ( await resolvePath ( '../package.json' ) )
110+ nuxt . options . alias [ '#nuxt-scripts-validator' ] = await resolvePath ( `./runtime/validation/${ ( nuxt . options . dev || nuxt . options . _prepare ) ? 'valibot' : 'mock' } ` )
111+ nuxt . options . alias [ '#nuxt-scripts' ] = await resolvePath ( './runtime' )
112112 logger . level = ( config . debug || nuxt . options . debug ) ? 4 : 3
113113 if ( ! config . enabled ) {
114114 // TODO fallback to useHead?
@@ -129,23 +129,36 @@ export default defineNuxtModule<ModuleOptions>({
129129 defaultScriptOptions : config . defaultScriptOptions ,
130130 }
131131 addImportsDir ( [
132- resolve ( './runtime/composables' ) ,
132+ await resolvePath ( './runtime/composables' ) ,
133133 ] )
134134
135135 addComponentsDir ( {
136- path : resolve ( './runtime/components' ) ,
136+ path : await resolvePath ( './runtime/components' ) ,
137137 } )
138138
139- const scripts = registry ( resolve )
139+ const scripts = await registry ( resolvePath ) as ( RegistryScript & { _importRegistered ?: boolean } ) [ ]
140+
141+ for ( const script of scripts ) {
142+ if ( script . import ?. name ) {
143+ addImports ( { priority : 2 , ...script . import } )
144+ script . _importRegistered = true
145+ }
146+ }
147+
140148 nuxt . hooks . hook ( 'modules:done' , async ( ) => {
141149 const registryScripts = [ ...scripts ]
142150
143151 // @ts -expect-error nuxi prepare is broken to generate these types, possibly because of the runtime path
144152 await nuxt . hooks . callHook ( 'scripts:registry' , registryScripts )
145- const registryScriptsWithImport = registryScripts . filter ( i => ! ! i . import ?. name ) as Required < RegistryScript > [ ]
146- addImports ( registryScriptsWithImport . map ( i => i . import ) )
153+
154+ for ( const script of registryScripts ) {
155+ if ( script . import ?. name && ! script . _importRegistered ) {
156+ addImports ( { priority : 3 , ...script . import } )
157+ }
158+ }
147159
148160 // compare the registryScripts to the original registry to find new scripts
161+ const registryScriptsWithImport = registryScripts . filter ( i => ! ! i . import ?. name ) as Required < RegistryScript > [ ]
149162 const newScripts = registryScriptsWithImport . filter ( i => ! scripts . some ( r => r . import ?. name === i . import . name ) )
150163
151164 // augment types to support the integrations registry
@@ -215,6 +228,6 @@ ${newScripts.map((i) => {
215228 } )
216229
217230 if ( nuxt . options . dev )
218- setupDevToolsUI ( config , resolve )
231+ setupDevToolsUI ( config , resolvePath )
219232 } ,
220233} )
0 commit comments