fix(nuxt3): don't add superfluous templates (#3366)

This commit is contained in:
Daniel Roe 2022-02-25 10:16:24 +00:00 committed by GitHub
parent 271289c4d6
commit ac63401700
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,6 +1,6 @@
import { statSync } from 'fs'
import { resolve, basename } from 'pathe'
import { defineNuxtModule, resolveAlias, addVitePlugin, addWebpackPlugin } from '@nuxt/kit'
import { defineNuxtModule, resolveAlias, addVitePlugin, addWebpackPlugin, addTemplate, addPlugin } from '@nuxt/kit'
import type { Component, ComponentsDir, ComponentsOptions } from '@nuxt/schema'
import { componentsTemplate, componentsTypeTemplate } from './templates'
import { scanComponents } from './scan'
@ -22,7 +22,7 @@ export default defineNuxtModule<ComponentsOptions>({
},
setup (componentOptions, nuxt) {
let componentDirs = []
let components: Component[] = []
const components: Component[] = []
const normalizeDirs = (dir: any, cwd: string) => {
if (Array.isArray(dir)) {
@ -99,26 +99,25 @@ export default defineNuxtModule<ComponentsOptions>({
nuxt.options.build!.transpile!.push(...componentDirs.filter(dir => dir.transpile).map(dir => dir.path))
})
const options = { components, buildDir: nuxt.options.buildDir }
addTemplate({
...componentsTypeTemplate,
options
})
addTemplate({
...componentsTemplate,
options
})
addPlugin({ src: '#build/components' })
// Scan components and add to plugin
nuxt.hook('app:templates', async (app) => {
components = await scanComponents(componentDirs, nuxt.options.srcDir!)
nuxt.hook('app:templates', async () => {
options.components = await scanComponents(componentDirs, nuxt.options.srcDir!)
await nuxt.callHook('components:extend', components)
app.templates.push({
...componentsTypeTemplate,
options: { components, buildDir: nuxt.options.buildDir }
})
if (!components.length) {
return
}
app.templates.push({
...componentsTemplate,
options: { components }
})
app.plugins.push({ src: '#build/components' })
await nuxt.callHook('builder:generateApp')
})
nuxt.hook('prepare:types', ({ references }) => {