From 4e424d0d102f468f1a7ad1b68591a1c6b06a990b Mon Sep 17 00:00:00 2001 From: pooya parsa Date: Fri, 29 Oct 2021 13:36:55 +0200 Subject: [PATCH] feat(nuxt3): support components with `global: false` and always enable transform (#1578) --- packages/kit/src/types/components.ts | 10 +++++---- packages/nuxt3/src/components/module.ts | 8 +++---- packages/nuxt3/src/components/scan.ts | 26 +++++++++++----------- packages/nuxt3/src/components/templates.ts | 2 +- 4 files changed, 23 insertions(+), 23 deletions(-) diff --git a/packages/kit/src/types/components.ts b/packages/kit/src/types/components.ts index ac3fbf0c47..d3584241bd 100644 --- a/packages/kit/src/types/components.ts +++ b/packages/kit/src/types/components.ts @@ -8,14 +8,13 @@ export interface Component { level: number prefetch: boolean preload: boolean + global?: boolean /** @deprecated */ import?: string /** @deprecated */ asyncImport?: string /** @deprecated */ - global?: boolean - /** @deprecated */ async?: boolean } @@ -62,8 +61,11 @@ export interface ScanDir { extendComponent?: (component: Component) => Promise | (Component | void) - /** @deprecated */ - global?: boolean | 'dev' + /** + * If enabled, registers components to be globally available + * + */ + global?: boolean } export interface ComponentsDir extends ScanDir { diff --git a/packages/nuxt3/src/components/module.ts b/packages/nuxt3/src/components/module.ts index a7f4295483..d4676bd23a 100644 --- a/packages/nuxt3/src/components/module.ts +++ b/packages/nuxt3/src/components/module.ts @@ -93,10 +93,8 @@ export default defineNuxtModule({ } }) - if (!nuxt.options.dev) { - const options = { getComponents: () => components } - addWebpackPlugin(loaderPlugin.webpack(options)) - addVitePlugin(loaderPlugin.vite(options)) - } + const loaderOptions = { getComponents: () => components } + addWebpackPlugin(loaderPlugin.webpack(loaderOptions)) + addVitePlugin(loaderPlugin.vite(loaderOptions)) } }) diff --git a/packages/nuxt3/src/components/scan.ts b/packages/nuxt3/src/components/scan.ts index 0cea532fa1..23988911ce 100644 --- a/packages/nuxt3/src/components/scan.ts +++ b/packages/nuxt3/src/components/scan.ts @@ -18,11 +18,11 @@ export async function scanComponents (dirs: ScanDir[], srcDir: string): Promise< const filePaths = new Set() const scannedPaths: string[] = [] - for (const { path, pattern, ignore = [], prefix, extendComponent, pathPrefix, level, prefetch = false, preload = false } of dirs.sort(sortDirsByPathLength)) { + for (const dir of dirs.sort(sortDirsByPathLength)) { const resolvedNames = new Map() - for (const _file of await globby(pattern!, { cwd: path, ignore })) { - const filePath = join(path, _file) + for (const _file of await globby(dir.pattern!, { cwd: dir.path, ignore: dir.ignore })) { + const filePath = join(dir.path, _file) if (scannedPaths.find(d => filePath.startsWith(d))) { continue @@ -33,12 +33,12 @@ export async function scanComponents (dirs: ScanDir[], srcDir: string): Promise< // Resolve componentName const prefixParts = ([] as string[]).concat( - prefix ? splitByCase(prefix) : [], - (pathPrefix !== false) ? splitByCase(relative(path, dirname(filePath))) : [] + dir.prefix ? splitByCase(dir.prefix) : [], + (dir.pathPrefix !== false) ? splitByCase(relative(dir.path, dirname(filePath))) : [] ) let fileName = basename(filePath, extname(filePath)) if (fileName.toLowerCase() === 'index') { - fileName = pathPrefix === false ? basename(dirname(filePath)) : '' /* inherits from path */ + fileName = dir.pathPrefix === false ? basename(dirname(filePath)) : '' /* inherits from path */ } const fileNameParts = splitByCase(fileName) @@ -74,14 +74,14 @@ export async function scanComponents (dirs: ScanDir[], srcDir: string): Promise< chunkName, shortPath, export: 'default', - global: Boolean(global), - level: Number(level), - prefetch: Boolean(prefetch), - preload: Boolean(preload) + global: dir.global, + level: Number(dir.level), + prefetch: Boolean(dir.prefetch), + preload: Boolean(dir.preload) } - if (typeof extendComponent === 'function') { - component = (await extendComponent(component)) || component + if (typeof dir.extendComponent === 'function') { + component = (await dir.extendComponent(component)) || component } // Check if component is already defined, used to overwite if level is inferiour @@ -93,7 +93,7 @@ export async function scanComponents (dirs: ScanDir[], srcDir: string): Promise< } } - scannedPaths.push(path) + scannedPaths.push(dir.path) } return components diff --git a/packages/nuxt3/src/components/templates.ts b/packages/nuxt3/src/components/templates.ts index eb2af0af76..f19b5356be 100644 --- a/packages/nuxt3/src/components/templates.ts +++ b/packages/nuxt3/src/components/templates.ts @@ -28,7 +28,7 @@ export const componentsTemplate = { return `import { defineAsyncComponent } from 'vue' const components = { -${options.components.map((c) => { +${options.components.filter(c => c.global !== false).map((c) => { const exp = c.export === 'default' ? 'c.default || c' : `c['${c.export}']` const magicComments = createImportMagicComments(c)