Skip to content

Commit

Permalink
fix(build): do not export default as a type
Browse files Browse the repository at this point in the history
resolves #291
  • Loading branch information
danielroe committed Jun 18, 2024
1 parent ced56f6 commit d29337c
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
8 changes: 4 additions & 4 deletions src/commands/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,11 @@ async function writeTypes(distDir: string) {
// Read generated module types
const moduleTypesFile = resolve(distDir, 'module.d.ts')
const moduleTypes = await fsp.readFile(moduleTypesFile, 'utf8').catch(() => '')
const typeExports = findExports(
const moduleReExports = findExports(
// Replace `export { type Foo }` with `export { Foo }`
moduleTypes
.replace(/export\s*\{.*?\}/gs, match =>
match.replace(/\btype\b/g, ''),
match.replace(/\b(type|interface)\b/g, ''),
),
)
const isStub = moduleTypes.includes('export *')
Expand All @@ -201,7 +201,7 @@ async function writeTypes(distDir: string) {
const schemaImports: string[] = []
const moduleExports: string[] = []

const hasTypeExport = (name: string) => isStub || typeExports.find(exp => exp.names.includes(name))
const hasTypeExport = (name: string) => isStub || moduleReExports.find(exp => exp.names.includes(name))

if (!hasTypeExport('ModuleOptions')) {
schemaImports.push('NuxtModule')
Expand Down Expand Up @@ -243,7 +243,7 @@ ${appShims.length ? `declare module '#app' {\n${appShims.join('\n')}\n}\n` : ''}
${schemaShims.length ? `declare module '@nuxt/schema' {\n${schemaShims.join('\n')}\n}\n` : ''}
${schemaShims.length ? `declare module 'nuxt/schema' {\n${schemaShims.join('\n')}\n}\n` : ''}
${moduleExports.length ? `\n${moduleExports.join('\n')}` : ''}
${typeExports[0] ? `\nexport type { ${typeExports[0].names.join(', ')} } from './module'` : ''}
${moduleReExports[0] ? `\nexport { ${moduleReExports[0].names.map(n => (n === 'default' ? '' : 'type ') + n).join(', ')} } from './module'` : ''}
`.trim().replace(/[\n\r]{3,}/g, '\n\n') + '\n'

await fsp.writeFile(dtsFile, dtsContents, 'utf8')
Expand Down
4 changes: 2 additions & 2 deletions test/build.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ describe('module builder', () => {
interface PublicRuntimeConfig extends ModulePublicRuntimeConfig {}
}
export type { ModuleHooks, ModuleOptions, ModulePublicRuntimeConfig, ModuleRuntimeConfig, ModuleRuntimeHooks, default } from './module'
export { type ModuleHooks, type ModuleOptions, type ModulePublicRuntimeConfig, type ModuleRuntimeConfig, type ModuleRuntimeHooks, default } from './module'
"
`)
})
Expand Down Expand Up @@ -107,7 +107,7 @@ describe('module builder', () => {
export type ModuleOptions = typeof Module extends NuxtModule<infer O> ? Partial<O> : Record<string, any>
export type { ModuleHooks, ModulePublicRuntimeConfig, ModuleRuntimeConfig, ModuleRuntimeHooks, default } from './module'
export { type ModuleHooks, type ModulePublicRuntimeConfig, type ModuleRuntimeConfig, type ModuleRuntimeHooks, default } from './module'
"
`)
})
Expand Down

0 comments on commit d29337c

Please sign in to comment.