refactor(nuxt): use addTypeTemplate in more places

This commit is contained in:
Daniel Roe 2024-03-16 15:20:29 -07:00
parent 8c07493782
commit 33ce71dd12
No known key found for this signature in database
GPG Key ID: CBC814C393D93268
2 changed files with 6 additions and 13 deletions

View File

@ -1,6 +1,6 @@
import fs, { statSync } from 'node:fs'
import { join, normalize, relative, resolve } from 'pathe'
import { addPluginTemplate, addTemplate, addVitePlugin, addWebpackPlugin, defineNuxtModule, logger, resolveAlias, updateTemplates } from '@nuxt/kit'
import { addPluginTemplate, addTemplate, addTypeTemplate, addVitePlugin, addWebpackPlugin, defineNuxtModule, logger, resolveAlias, updateTemplates } from '@nuxt/kit'
import type { Component, ComponentsDir, ComponentsOptions } from 'nuxt/schema'
import { distDir } from '../dirs'
@ -115,7 +115,7 @@ export default defineNuxtModule<ComponentsOptions>({
})
// components.d.ts
addTemplate(componentsTypeTemplate)
addTypeTemplate(componentsTypeTemplate)
// components.plugin.mjs
addPluginTemplate(componentsPluginTemplate)
// component-names.mjs
@ -188,9 +188,8 @@ export default defineNuxtModule<ComponentsOptions>({
app.components = newComponents
})
nuxt.hook('prepare:types', ({ references, tsConfig }) => {
nuxt.hook('prepare:types', ({ tsConfig }) => {
tsConfig.compilerOptions!.paths['#components'] = [resolve(nuxt.options.buildDir, 'components')]
references.push({ path: resolve(nuxt.options.buildDir, 'components.d.ts') })
})
// Watch for changes

View File

@ -1,5 +1,5 @@
import { existsSync } from 'node:fs'
import { addTemplate, addVitePlugin, addWebpackPlugin, defineNuxtModule, isIgnored, logger, resolveAlias, tryResolveModule, updateTemplates, useNuxt } from '@nuxt/kit'
import { addTemplate, addTypeTemplate, addVitePlugin, addWebpackPlugin, defineNuxtModule, isIgnored, logger, resolveAlias, tryResolveModule, updateTemplates, useNuxt } from '@nuxt/kit'
import { isAbsolute, join, normalize, relative, resolve } from 'pathe'
import type { Import, Unimport } from 'unimport'
import { createUnimport, scanDirExports, toExports } from 'unimport'
@ -124,12 +124,6 @@ export default defineNuxtModule<Partial<ImportsOptions>>({
// Generate types
addDeclarationTemplates(ctx, options)
// Add generated types to `nuxt.d.ts`
nuxt.hook('prepare:types', ({ references }) => {
references.push({ path: resolve(nuxt.options.buildDir, 'types/imports.d.ts') })
references.push({ path: resolve(nuxt.options.buildDir, 'imports.d.ts') })
})
// Watch composables/ directory
nuxt.hook('builder:watch', async (_, relativePath) => {
const path = resolve(nuxt.options.srcDir, relativePath)
@ -186,12 +180,12 @@ function addDeclarationTemplates (ctx: Unimport, options: Partial<ImportsOptions
}))
}
addTemplate({
addTypeTemplate({
filename: 'imports.d.ts',
getContents: async ({ nuxt }) => toExports(await ctx.getImports(), nuxt.options.buildDir, true)
})
addTemplate({
addTypeTemplate({
filename: 'types/imports.d.ts',
getContents: async () => {
const imports = await ctx.getImports()