feat(nuxt): allow configuring default <NuxtLink> options (#23724)

This commit is contained in:
Daniel Roe 2023-10-18 13:43:42 +01:00 committed by GitHub
parent 2cd0b59f56
commit 9dd304628a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 24 additions and 2 deletions

View File

@ -9,6 +9,9 @@ import { navigateTo, useRouter } from '../composables/router'
import { useNuxtApp } from '../nuxt' import { useNuxtApp } from '../nuxt'
import { cancelIdleCallback, requestIdleCallback } from '../compat/idle-callback' import { cancelIdleCallback, requestIdleCallback } from '../compat/idle-callback'
// @ts-expect-error virtual file
import { nuxtLinkDefaults } from '#build/nuxt.config.mjs'
const firstNonUndefined = <T> (...args: (T | undefined)[]) => args.find(arg => arg !== undefined) const firstNonUndefined = <T> (...args: (T | undefined)[]) => args.find(arg => arg !== undefined)
const DEFAULT_EXTERNAL_REL_ATTRIBUTE = 'noopener noreferrer' const DEFAULT_EXTERNAL_REL_ATTRIBUTE = 'noopener noreferrer'
@ -332,7 +335,7 @@ export function defineNuxtLink (options: NuxtLinkOptions) {
}) as unknown as DefineComponent<NuxtLinkProps> }) as unknown as DefineComponent<NuxtLinkProps>
} }
export default defineNuxtLink({ componentName: 'NuxtLink' }) export default defineNuxtLink(nuxtLinkDefaults)
// --- Prefetching utils --- // --- Prefetching utils ---
type CallbackFn = () => void type CallbackFn = () => void

View File

@ -347,6 +347,7 @@ export const nuxtConfigTemplate = {
`export const remoteComponentIslands = ${ctx.nuxt.options.experimental.componentIslands === 'local+remote'}`, `export const remoteComponentIslands = ${ctx.nuxt.options.experimental.componentIslands === 'local+remote'}`,
`export const devPagesDir = ${ctx.nuxt.options.dev ? JSON.stringify(ctx.nuxt.options.dir.pages) : 'null'}`, `export const devPagesDir = ${ctx.nuxt.options.dev ? JSON.stringify(ctx.nuxt.options.dir.pages) : 'null'}`,
`export const devRootDir = ${ctx.nuxt.options.dev ? JSON.stringify(ctx.nuxt.options.rootDir) : 'null'}`, `export const devRootDir = ${ctx.nuxt.options.dev ? JSON.stringify(ctx.nuxt.options.rootDir) : 'null'}`,
`export const nuxtLinkDefaults = ${JSON.stringify(ctx.nuxt.options.experimental.defaults.nuxtLink)}`,
`export const vueAppRootContainer = ${ctx.nuxt.options.app.rootId ? `'#${ctx.nuxt.options.app.rootId}'` : `'body > ${ctx.nuxt.options.app.rootTag}'`}` `export const vueAppRootContainer = ${ctx.nuxt.options.app.rootId ? `'#${ctx.nuxt.options.app.rootId}'` : `'body > ${ctx.nuxt.options.app.rootTag}'`}`
].join('\n\n') ].join('\n\n')
} }

View File

@ -20,6 +20,7 @@ export default defineBuildConfig({
], ],
externals: [ externals: [
// Type imports // Type imports
'#app/components/nuxt-link',
'vue-router', 'vue-router',
'@nuxt/telemetry', '@nuxt/telemetry',
'vue-bundle-renderer', 'vue-bundle-renderer',

View File

@ -246,6 +246,19 @@ export default defineUntypedSchema({
* For more control, such as if you are using a custom `path` or `alias` set in the page's `definePageMeta`, you * For more control, such as if you are using a custom `path` or `alias` set in the page's `definePageMeta`, you
* should set `routeRules` directly within your `nuxt.config`. * should set `routeRules` directly within your `nuxt.config`.
*/ */
inlineRouteRules: false inlineRouteRules: false,
/**
* This allows specifying the default options for core Nuxt components and composables.
*
* These options will likely be moved elsewhere in the future, such as into `app.config` or into the
* `app/` directory.
*/
defaults: {
/** @type {typeof import('#app/components/nuxt-link')['NuxtLinkOptions']} */
nuxtLink: {
componentName: 'NuxtLink'
}
}
} }
}) })

View File

@ -0,0 +1,3 @@
export const nuxtLinkDefaults = {
componentName: 'NuxtLink'
}

View File

@ -6,6 +6,7 @@ import { isWindows } from 'std-env'
export default defineConfig({ export default defineConfig({
resolve: { resolve: {
alias: { alias: {
'#build/nuxt.config.mjs': resolve('./test/mocks/nuxt-config'),
'#app': resolve('./packages/nuxt/dist/app/index'), '#app': resolve('./packages/nuxt/dist/app/index'),
'@nuxt/test-utils/experimental': resolve('./packages/test-utils/src/experimental.ts'), '@nuxt/test-utils/experimental': resolve('./packages/test-utils/src/experimental.ts'),
'@nuxt/test-utils': resolve('./packages/test-utils/src/index.ts') '@nuxt/test-utils': resolve('./packages/test-utils/src/index.ts')