mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-14 18:13:54 +00:00
refactor(nuxt): use addTypeTemplate in more places
This commit is contained in:
parent
8c07493782
commit
33ce71dd12
@ -1,6 +1,6 @@
|
|||||||
import fs, { statSync } from 'node:fs'
|
import fs, { statSync } from 'node:fs'
|
||||||
import { join, normalize, relative, resolve } from 'pathe'
|
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 type { Component, ComponentsDir, ComponentsOptions } from 'nuxt/schema'
|
||||||
|
|
||||||
import { distDir } from '../dirs'
|
import { distDir } from '../dirs'
|
||||||
@ -115,7 +115,7 @@ export default defineNuxtModule<ComponentsOptions>({
|
|||||||
})
|
})
|
||||||
|
|
||||||
// components.d.ts
|
// components.d.ts
|
||||||
addTemplate(componentsTypeTemplate)
|
addTypeTemplate(componentsTypeTemplate)
|
||||||
// components.plugin.mjs
|
// components.plugin.mjs
|
||||||
addPluginTemplate(componentsPluginTemplate)
|
addPluginTemplate(componentsPluginTemplate)
|
||||||
// component-names.mjs
|
// component-names.mjs
|
||||||
@ -188,9 +188,8 @@ export default defineNuxtModule<ComponentsOptions>({
|
|||||||
app.components = newComponents
|
app.components = newComponents
|
||||||
})
|
})
|
||||||
|
|
||||||
nuxt.hook('prepare:types', ({ references, tsConfig }) => {
|
nuxt.hook('prepare:types', ({ tsConfig }) => {
|
||||||
tsConfig.compilerOptions!.paths['#components'] = [resolve(nuxt.options.buildDir, 'components')]
|
tsConfig.compilerOptions!.paths['#components'] = [resolve(nuxt.options.buildDir, 'components')]
|
||||||
references.push({ path: resolve(nuxt.options.buildDir, 'components.d.ts') })
|
|
||||||
})
|
})
|
||||||
|
|
||||||
// Watch for changes
|
// Watch for changes
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import { existsSync } from 'node:fs'
|
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 { isAbsolute, join, normalize, relative, resolve } from 'pathe'
|
||||||
import type { Import, Unimport } from 'unimport'
|
import type { Import, Unimport } from 'unimport'
|
||||||
import { createUnimport, scanDirExports, toExports } from 'unimport'
|
import { createUnimport, scanDirExports, toExports } from 'unimport'
|
||||||
@ -124,12 +124,6 @@ export default defineNuxtModule<Partial<ImportsOptions>>({
|
|||||||
// Generate types
|
// Generate types
|
||||||
addDeclarationTemplates(ctx, options)
|
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
|
// Watch composables/ directory
|
||||||
nuxt.hook('builder:watch', async (_, relativePath) => {
|
nuxt.hook('builder:watch', async (_, relativePath) => {
|
||||||
const path = resolve(nuxt.options.srcDir, 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',
|
filename: 'imports.d.ts',
|
||||||
getContents: async ({ nuxt }) => toExports(await ctx.getImports(), nuxt.options.buildDir, true)
|
getContents: async ({ nuxt }) => toExports(await ctx.getImports(), nuxt.options.buildDir, true)
|
||||||
})
|
})
|
||||||
|
|
||||||
addTemplate({
|
addTypeTemplate({
|
||||||
filename: 'types/imports.d.ts',
|
filename: 'types/imports.d.ts',
|
||||||
getContents: async () => {
|
getContents: async () => {
|
||||||
const imports = await ctx.getImports()
|
const imports = await ctx.getImports()
|
||||||
|
Loading…
Reference in New Issue
Block a user