From 52d22feaea6008392ed42f4dd43c63e4da21d87e Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Mon, 28 Feb 2022 10:07:20 +0000 Subject: [PATCH] fix(nuxt3): share scanned components with loader (#3396) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix(nuxt3): share scanned components with loader * refactor: remove `src` type from input * fix: remove old `src: ''` option * fix: use shared context for extending components too Co-authored-by: Sébastien Chopin --- packages/bridge/src/vite/templates.ts | 2 -- packages/kit/src/plugin.ts | 14 ++++++-------- packages/nuxt3/src/auto-imports/module.ts | 1 - packages/nuxt3/src/components/module.ts | 10 ++++------ 4 files changed, 10 insertions(+), 17 deletions(-) diff --git a/packages/bridge/src/vite/templates.ts b/packages/bridge/src/vite/templates.ts index ac5971bdce..0c7b6675c2 100644 --- a/packages/bridge/src/vite/templates.ts +++ b/packages/bridge/src/vite/templates.ts @@ -12,7 +12,6 @@ type TemplateContext = { // TODO: Use an alias export const middlewareTemplate = { filename: 'middleware.js', - src: '', getContents (ctx: TemplateContext) { const { dir, router: { middleware }, srcDir } = ctx.nuxt.options const _middleware = ((typeof middleware !== 'undefined' && middleware) || []).map((m) => { @@ -33,7 +32,6 @@ export default middleware` export const storeTemplate = { filename: 'store.js', - src: '', getContents (ctx: TemplateContext) { const { dir, srcDir } = ctx.nuxt.options const { templateVars: { storeModules = [] } } = ctx.app diff --git a/packages/kit/src/plugin.ts b/packages/kit/src/plugin.ts index 01bce37f39..2ed1e635f1 100644 --- a/packages/kit/src/plugin.ts +++ b/packages/kit/src/plugin.ts @@ -68,13 +68,11 @@ export function addPlugin (_plugin: NuxtPlugin | string, opts: AddPluginOptions /** * Adds a template and registers as a nuxt plugin. */ -export function addPluginTemplate (plugin: NuxtPluginTemplate | string, opts: AddPluginOptions = {}): NuxtPluginTemplate { - if (typeof plugin === 'string') { - plugin = { src: plugin } - } +export function addPluginTemplate (plugin: Omit | string, opts: AddPluginOptions = {}): NuxtPluginTemplate { + const normalizedPlugin: NuxtPluginTemplate = typeof plugin === 'string' + ? { src: plugin } + // Update plugin src to template destination + : { ...plugin, src: addTemplate(plugin).dst } - // Update plugin src to template destination - plugin.src = addTemplate(plugin).dst - - return addPlugin(plugin, opts) + return addPlugin(normalizedPlugin, opts) } diff --git a/packages/nuxt3/src/auto-imports/module.ts b/packages/nuxt3/src/auto-imports/module.ts index 17ac56d289..73c9163ac4 100644 --- a/packages/nuxt3/src/auto-imports/module.ts +++ b/packages/nuxt3/src/auto-imports/module.ts @@ -52,7 +52,6 @@ export default defineNuxtModule({ // Add all imports to globalThis in development mode addPluginTemplate({ filename: 'auto-imports.mjs', - src: '', getContents: () => { const imports = toImports(ctx.autoImports) const globalThisSet = ctx.autoImports.map(i => `globalThis.${i.as} = ${i.as};`).join('\n') diff --git a/packages/nuxt3/src/components/module.ts b/packages/nuxt3/src/components/module.ts index c421383406..61745795ff 100644 --- a/packages/nuxt3/src/components/module.ts +++ b/packages/nuxt3/src/components/module.ts @@ -1,6 +1,6 @@ import { statSync } from 'fs' import { resolve, basename } from 'pathe' -import { defineNuxtModule, resolveAlias, addVitePlugin, addWebpackPlugin, addTemplate, addPlugin } from '@nuxt/kit' +import { defineNuxtModule, resolveAlias, addVitePlugin, addWebpackPlugin, addTemplate, addPluginTemplate } from '@nuxt/kit' import type { Component, ComponentsDir, ComponentsOptions } from '@nuxt/schema' import { componentsTemplate, componentsTypeTemplate } from './templates' import { scanComponents } from './scan' @@ -106,17 +106,15 @@ export default defineNuxtModule({ options }) - addTemplate({ + addPluginTemplate({ ...componentsTemplate, options }) - addPlugin({ src: '#build/components' }) - // Scan components and add to plugin nuxt.hook('app:templates', async () => { options.components = await scanComponents(componentDirs, nuxt.options.srcDir!) - await nuxt.callHook('components:extend', components) + await nuxt.callHook('components:extend', options.components) await nuxt.callHook('builder:generateApp') }) @@ -135,7 +133,7 @@ export default defineNuxtModule({ } }) - const loaderOptions = { getComponents: () => components } + const loaderOptions = { getComponents: () => options.components } addWebpackPlugin(loaderPlugin.webpack(loaderOptions)) addVitePlugin(loaderPlugin.vite(loaderOptions)) }