@@ -10,6 +10,7 @@ import { anyOf, createRegExp } from 'magic-regexp'
10
10
import { consola } from 'consola'
11
11
import type { NuxtModule } from '@nuxt/schema'
12
12
import { findExports , resolvePath } from 'mlly'
13
+ import type { ESMExport } from 'mlly'
13
14
import { defineCommand } from 'citty'
14
15
15
16
import { name , version } from '../../package.json'
@@ -169,31 +170,32 @@ export default defineCommand({
169
170
await fsp . writeFile ( metaFile , JSON . stringify ( moduleMeta , null , 2 ) , 'utf8' )
170
171
171
172
// Generate types
172
- await writeTypes ( ctx . options . outDir )
173
+ await writeTypes ( ctx . options . outDir , ctx . options . stub )
173
174
} ,
174
175
} ,
175
176
} )
176
177
} ,
177
178
} )
178
179
179
- async function writeTypes ( distDir : string ) {
180
+ async function writeTypes ( distDir : string , isStub : boolean ) {
180
181
const dtsFile = resolve ( distDir , 'types.d.ts' )
181
182
const dtsFileMts = resolve ( distDir , 'types.d.mts' )
182
183
if ( existsSync ( dtsFile ) ) {
183
184
return
184
185
}
185
186
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
+ }
197
199
198
200
const appShims : string [ ] = [ ]
199
201
const schemaShims : string [ ] = [ ]
@@ -243,6 +245,7 @@ ${appShims.length ? `declare module '#app' {\n${appShims.join('\n')}\n}\n` : ''}
243
245
${ schemaShims . length ? `declare module '@nuxt/schema' {\n${ schemaShims . join ( '\n' ) } \n}\n` : '' }
244
246
${ schemaShims . length ? `declare module 'nuxt/schema' {\n${ schemaShims . join ( '\n' ) } \n}\n` : '' }
245
247
${ moduleExports . length ? `\n${ moduleExports . join ( '\n' ) } ` : '' }
248
+ ${ isStub ? 'export * from "./module"' : '' }
246
249
${ moduleReExports [ 0 ] ? `\nexport { ${ moduleReExports [ 0 ] . names . map ( n => ( n === 'default' ? '' : 'type ' ) + n ) . join ( ', ' ) } } from './module'` : '' }
247
250
` . trim ( ) . replace ( / [ \n \r ] { 3 , } / g, '\n\n' ) + '\n'
248
251
0 commit comments