From 13ba46bec3d0ed3b70497c0a6ea5407c3fce3933 Mon Sep 17 00:00:00 2001 From: Michael Brevard Date: Thu, 27 Jun 2024 16:16:17 +0300 Subject: [PATCH] feat(nuxt): warn when user components' names begin with `Lazy` (#27838) --- packages/nuxt/src/components/loader.ts | 2 +- packages/nuxt/src/components/scan.ts | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/packages/nuxt/src/components/loader.ts b/packages/nuxt/src/components/loader.ts index eaa4614ba5..b8e5d38b62 100644 --- a/packages/nuxt/src/components/loader.ts +++ b/packages/nuxt/src/components/loader.ts @@ -43,7 +43,7 @@ export const loaderPlugin = createUnplugin((options: LoaderOptions) => { const s = new MagicString(code) // replace `_resolveComponent("...")` to direct import - s.replace(/(?<=[ (])_?resolveComponent\(\s*["'](lazy-|Lazy)?([^'"]*)["'][^)]*\)/g, (full: string, lazy: string, name: string) => { + s.replace(/(?<=[ (])_?resolveComponent\(\s*["'](lazy-|Lazy(?=[A-Z]))?([^'"]*)["'][^)]*\)/g, (full: string, lazy: string, name: string) => { const component = findComponent(components, name, options.mode) if (component) { // @ts-expect-error TODO: refactor to nuxi diff --git a/packages/nuxt/src/components/scan.ts b/packages/nuxt/src/components/scan.ts index a09413991e..f37bea63bb 100644 --- a/packages/nuxt/src/components/scan.ts +++ b/packages/nuxt/src/components/scan.ts @@ -96,6 +96,10 @@ export async function scanComponents (dirs: ComponentsDir[], srcDir: string): Pr const componentNameSegments = resolveComponentNameSegments(fileName.replace(/["']/g, ''), prefixParts) const pascalName = pascalCase(componentNameSegments) + if (LAZY_COMPONENT_NAME_REGEX.test(pascalName)) { + logger.warn(`The component \`${pascalName}\` (in \`${filePath}\`) is using the reserved "Lazy" prefix used for dynamic imports, which may cause it to break at runtime.`) + } + if (resolvedNames.has(pascalName + suffix) || resolvedNames.has(pascalName)) { warnAboutDuplicateComponent(pascalName, filePath, resolvedNames.get(pascalName) || resolvedNames.get(pascalName + suffix)!) continue @@ -166,3 +170,5 @@ function warnAboutDuplicateComponent (componentName: string, filePath: string, d `\n - ${duplicatePath}`, ) } + +const LAZY_COMPONENT_NAME_REGEX = /^Lazy(?=[A-Z])/