@@ -10,6 +10,7 @@ import { anyOf, createRegExp } from 'magic-regexp'
1010import { consola } from 'consola'
1111import type { NuxtModule } from '@nuxt/schema'
1212import { findExports , resolvePath } from 'mlly'
13+ import type { ESMExport } from 'mlly'
1314import { defineCommand } from 'citty'
1415
1516import { name , version } from '../../package.json'
@@ -169,31 +170,32 @@ export default defineCommand({
169170 await fsp . writeFile ( metaFile , JSON . stringify ( moduleMeta , null , 2 ) , 'utf8' )
170171
171172 // Generate types
172- await writeTypes ( ctx . options . outDir )
173+ await writeTypes ( ctx . options . outDir , ctx . options . stub )
173174 } ,
174175 } ,
175176 } )
176177 } ,
177178} )
178179
179- async function writeTypes ( distDir : string ) {
180+ async function writeTypes ( distDir : string , isStub : boolean ) {
180181 const dtsFile = resolve ( distDir , 'types.d.ts' )
181182 const dtsFileMts = resolve ( distDir , 'types.d.mts' )
182183 if ( existsSync ( dtsFile ) ) {
183184 return
184185 }
185186
186- // Read generated module types
187- const moduleTypesFile = resolve ( distDir , 'module.d.ts' )
188- const moduleTypes = await fsp . readFile ( moduleTypesFile , 'utf8' ) . catch ( ( ) => '' )
189- const moduleReExports = findExports (
190- // Replace `export { type Foo }` with `export { Foo }`
191- moduleTypes
192- . replace ( / e x p o r t \s * \{ .* ?\} / gs, match =>
193- match . replace ( / \b ( t y p e | i n t e r f a c e ) \b / g, '' ) ,
194- ) ,
195- )
196- const isStub = moduleTypes . includes ( 'export *' )
187+ const moduleReExports : ESMExport [ ] = [ ]
188+ if ( ! isStub ) {
189+ // Read generated module types
190+ const moduleTypesFile = resolve ( distDir , 'module.d.ts' )
191+ const moduleTypes = await fsp . readFile ( moduleTypesFile , 'utf8' ) . catch ( ( ) => '' )
192+ const normalisedModuleTypes = moduleTypes
193+ // Replace `export { type Foo }` with `export { Foo }`
194+ . replace ( / e x p o r t \s * \{ .* ?\} / gs, match => match . replace ( / \b ( t y p e | i n t e r f a c e ) \b / g, '' ) )
195+ for ( const e of findExports ( normalisedModuleTypes ) ) {
196+ moduleReExports . push ( e )
197+ }
198+ }
197199
198200 const appShims : string [ ] = [ ]
199201 const schemaShims : string [ ] = [ ]
@@ -243,6 +245,7 @@ ${appShims.length ? `declare module '#app' {\n${appShims.join('\n')}\n}\n` : ''}
243245${ schemaShims . length ? `declare module '@nuxt/schema' {\n${ schemaShims . join ( '\n' ) } \n}\n` : '' }
244246${ schemaShims . length ? `declare module 'nuxt/schema' {\n${ schemaShims . join ( '\n' ) } \n}\n` : '' }
245247${ moduleExports . length ? `\n${ moduleExports . join ( '\n' ) } ` : '' }
248+ ${ isStub ? 'export * from "./module"' : '' }
246249${ moduleReExports [ 0 ] ? `\nexport { ${ moduleReExports [ 0 ] . names . map ( n => ( n === 'default' ? '' : 'type ' ) + n ) . join ( ', ' ) } } from './module'` : '' }
247250` . trim ( ) . replace ( / [ \n \r ] { 3 , } / g, '\n\n' ) + '\n'
248251
0 commit comments