feat(nuxt): warn when user components' names begin with Lazy (#27838)

This commit is contained in:
Michael Brevard 2024-06-27 16:16:17 +03:00 committed by Daniel Roe
parent 56092503b4
commit 13ba46bec3
No known key found for this signature in database
GPG Key ID: 3714AB03996F442B
2 changed files with 7 additions and 1 deletions

View File

@ -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

View File

@ -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])/