Skip to content

Commit 6b1970d

Browse files
committed
fix(build): export all types in stub mode
1 parent 40469ce commit 6b1970d

File tree

1 file changed

+16
-13
lines changed

1 file changed

+16
-13
lines changed

src/commands/build.ts

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { anyOf, createRegExp } from 'magic-regexp'
1010
import { consola } from 'consola'
1111
import type { NuxtModule } from '@nuxt/schema'
1212
import { findExports, resolvePath } from 'mlly'
13+
import type { ESMExport } from 'mlly'
1314
import { defineCommand } from 'citty'
1415

1516
import { 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(/export\s*\{.*?\}/gs, match =>
193-
match.replace(/\b(type|interface)\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(/export\s*\{.*?\}/gs, match => match.replace(/\b(type|interface)\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

Comments
 (0)