feat(nuxt3): extends support for app/router.options (#3939)

This commit is contained in:
Kevin Marrec 2022-04-04 10:23:11 +02:00 committed by GitHub
parent 01db83032a
commit dda0cebc76
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 31 additions and 3 deletions

View File

@ -83,15 +83,20 @@ export default defineNuxtModule({
filename: 'router.options.mjs', filename: 'router.options.mjs',
getContents: async () => { getContents: async () => {
// Check for router options // Check for router options
const routerOptionsFile = await findPath('~/app/router.options') const routerOptionsFiles = (await Promise.all(nuxt.options._layers.map(
async layer => await findPath(resolve(layer.config.srcDir, 'app/router.options'))
))).filter(Boolean)
const configRouterOptions = genObjectFromRawEntries(Object.entries(nuxt.options.router.options) const configRouterOptions = genObjectFromRawEntries(Object.entries(nuxt.options.router.options)
.map(([key, value]) => [key, genString(value as string)])) .map(([key, value]) => [key, genString(value as string)]))
return [ return [
routerOptionsFile ? genImport(routerOptionsFile, 'routerOptions') : '', ...routerOptionsFiles.map((file, index) => genImport(file, `routerOptions${index}`)),
`const configRouterOptions = ${configRouterOptions}`, `const configRouterOptions = ${configRouterOptions}`,
'export default {', 'export default {',
'...configRouterOptions,', '...configRouterOptions,',
routerOptionsFile ? '...routerOptions' : '', // We need to reverse spreading order to respect layers priority
...routerOptionsFiles.map((_, index) => `...routerOptions${index},`).reverse(),
'}' '}'
].join('\n') ].join('\n')
} }

View File

@ -239,6 +239,15 @@ describe('extends support', () => {
expect(headers.get('injected-header')).toEqual('foo') expect(headers.get('injected-header')).toEqual('foo')
}) })
}) })
describe('app', () => {
it('extends foo/app/router.options & bar/app/router.options', async () => {
const html: string = await $fetch('/')
const routerLinkClasses = html.match(/href="\/" class="([^"]*)"/)[1].split(' ')
expect(routerLinkClasses).toContain('foo-active-class')
expect(routerLinkClasses).toContain('bar-exact-active-class')
})
})
}) })
describe('dynamic paths', () => { describe('dynamic paths', () => {

View File

@ -0,0 +1,5 @@
import type { RouterOptions } from '@nuxt/schema'
export default <RouterOptions>{
linkExactActiveClass: 'bar-exact-active-class'
}

View File

@ -0,0 +1,6 @@
import type { RouterOptions } from '@nuxt/schema'
export default <RouterOptions>{
linkActiveClass: 'foo-active-class',
linkExactActiveClass: 'foo-exact-active-class'
}

View File

@ -7,6 +7,9 @@
<div>RuntimeConfig | testConfig: {{ config.testConfig }}</div> <div>RuntimeConfig | testConfig: {{ config.testConfig }}</div>
<div>Composable | foo: {{ foo }}</div> <div>Composable | foo: {{ foo }}</div>
<div>Composable | bar: {{ bar }}</div> <div>Composable | bar: {{ bar }}</div>
<NuxtLink to="/">
Link
</NuxtLink>
<SugarCounter :count="12" /> <SugarCounter :count="12" />
<CustomComponent /> <CustomComponent />
</div> </div>