-
-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
Draft
IonianPlayboy
wants to merge
17
commits into
nuxt:main
Choose a base branch
from
IonianPlayboy:fix/unused-layout-page-warning
base: main
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
e89240c
fix(nuxt): avoid warning about unused page/layout components when mouβ¦
IonianPlayboy dc46308
Revert "fix(nuxt): avoid warning about unused page/layout components β¦
IonianPlayboy e94e25e
feat(nuxt): add _hasLayouts property on NuxtApp
IonianPlayboy b8ea5de
refactor(nuxt): emit component usage warnings at compile time
IonianPlayboy cefa4c0
refactor(nuxt): remove plugins with runtime component usage checks
IonianPlayboy 488f2bb
[autofix.ci] apply automated fixes
autofix-ci[bot] 6f54ebb
refactor(nuxt): use addBuildPlugin to add DetectComponentUsagePlugin
IonianPlayboy 2330bc9
chore: remove old code
danielroe 10fbc4c
fix: use `app.layouts` as source of truth
danielroe 8163fe4
refactor(nuxt): use check-component-usage plugin to log warnings at rβ¦
IonianPlayboy 06ff4df
refactor(nuxt): use absolute path with distDir for detect-component-uβ¦
IonianPlayboy aa4eda9
refactor(nuxt): hardcode sep character
IonianPlayboy 9e58137
test: fix runtime-compiler fixture
IonianPlayboy 0f986b0
test: fix basic fixture
IonianPlayboy 4a9609d
Revert "test: fix runtime-compiler fixture" and "test: fix basic fixtβ¦
IonianPlayboy 4132d38
fix(nuxt): include the generated runtime app.vue for component usage β¦
IonianPlayboy 8145b40
refactor: simplify template
danielroe File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 0 additions & 30 deletions
30
packages/nuxt/src/pages/runtime/plugins/check-if-page-unused.ts
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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?