New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(nuxt): use build plugin to detect usage of <NuxtPage>
and <NuxtLayout>
#26289
base: main
Are you sure you want to change the base?
Changes from all commits
e89240c
dc46308
e94e25e
b8ea5de
cefa4c0
488f2bb
6f54ebb
2330bc9
10fbc4c
8163fe4
06ff4df
aa4eda9
9e58137
0f986b0
4a9609d
4132d38
8145b40
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { defineNuxtPlugin } from '../nuxt' | ||
// @ts-expect-error virtual file | ||
import { hasPages, isNuxtLayoutUsed, isNuxtPageUsed } from '#build/detected-component-usage.mjs' | ||
// @ts-expect-error virtual file | ||
import layouts from '#build/layouts' | ||
|
||
export default defineNuxtPlugin({ | ||
name: 'nuxt:check-component-usage', | ||
setup (nuxtApp) { | ||
const cache = new Set<string>() | ||
|
||
nuxtApp.hook('app:mounted', () => { | ||
if (Object.keys(layouts).length > 0 && !isNuxtLayoutUsed && !cache.has('NuxtLayout')) { | ||
console.warn('[nuxt] Your project has layouts but the `<NuxtLayout />` component has not been used.') | ||
cache.add('NuxtLayout') | ||
} | ||
|
||
if (hasPages && !isNuxtPageUsed && !cache.has('NuxtPage')) { | ||
console.warn('[nuxt] Your project has pages but the `<NuxtPage />` component has not been used.' + | ||
' You might be using the `<RouterView />` component instead, which will not work correctly in Nuxt.' + | ||
' You can set `pages: false` in `nuxt.config` if you do not wish to use the Nuxt `vue-router` integration.') | ||
cache.add('NuxtPage') | ||
} | ||
}) | ||
} | ||
}) |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { createUnplugin } from 'unplugin' | ||
import { join, resolve } from 'pathe' | ||
import { updateTemplates } from '@nuxt/kit' | ||
import { distDir } from '../../dirs' | ||
|
||
interface DetectComponentUsageOptions { | ||
rootDir: string | ||
exclude?: Array<RegExp | string> | ||
include?: Array<RegExp | string> | ||
detectedComponents: Set<string> | ||
} | ||
|
||
export const DetectComponentUsagePlugin = (options: DetectComponentUsageOptions) => createUnplugin(() => { | ||
const importersToExclude = options?.exclude || [] | ||
const importersToInclude = options?.include || [] | ||
|
||
const detectComponentUsagePatterns: Array<[importPattern: string | RegExp, name: string]> = [ | ||
[resolve(distDir, 'pages/runtime/page'), 'NuxtPage'], | ||
[resolve(distDir, 'app/components/nuxt-layout'), 'NuxtLayout'] | ||
] | ||
|
||
return { | ||
name: 'nuxt:detect-component-usage', | ||
enforce: 'pre', | ||
resolveId (id, importer) { | ||
if (!importer) { return } | ||
if (id[0] === '.') { | ||
id = join(importer, '..', id) | ||
} | ||
const isExcludedImporter = importersToExclude.some(p => typeof p === 'string' ? importer === p : p.test(importer)) | ||
const isIncludedImporter = importersToInclude.some(p => typeof p === 'string' ? importer === p : p.test(importer)) | ||
if (isExcludedImporter && !isIncludedImporter) { return } | ||
Comment on lines
+30
to
+32
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we need the include/exclude pattern? (If we do need it for some reason, we would probably need to add quite a few more directories, including all layer root directories, and the paths to all auto-imported components.) |
||
|
||
for (const [pattern, name] of detectComponentUsagePatterns) { | ||
if (pattern instanceof RegExp ? pattern.test(id) : pattern === id) { | ||
options.detectedComponents.add(name) | ||
updateTemplates({ filter: template => template.filename === 'detected-component-usage.mjs' }) | ||
} | ||
} | ||
return null | ||
} | ||
} | ||
}) |
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This logic doesn't quite look right... What about directories beginning with
.
- and if it's a relative path surely we could just dojoin(importer, id)
rather than inserting an extra level up?