mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-22 05:35:13 +00:00
feat(nuxt3): extends support for app/router.options
(#3939)
This commit is contained in:
parent
01db83032a
commit
dda0cebc76
@ -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')
|
||||||
}
|
}
|
||||||
|
@ -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', () => {
|
||||||
|
5
test/fixtures/basic/extends/bar/app/router.options.ts
vendored
Normal file
5
test/fixtures/basic/extends/bar/app/router.options.ts
vendored
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
import type { RouterOptions } from '@nuxt/schema'
|
||||||
|
|
||||||
|
export default <RouterOptions>{
|
||||||
|
linkExactActiveClass: 'bar-exact-active-class'
|
||||||
|
}
|
6
test/fixtures/basic/extends/foo/app/router.options.ts
vendored
Normal file
6
test/fixtures/basic/extends/foo/app/router.options.ts
vendored
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
import type { RouterOptions } from '@nuxt/schema'
|
||||||
|
|
||||||
|
export default <RouterOptions>{
|
||||||
|
linkActiveClass: 'foo-active-class',
|
||||||
|
linkExactActiveClass: 'foo-exact-active-class'
|
||||||
|
}
|
3
test/fixtures/basic/pages/index.vue
vendored
3
test/fixtures/basic/pages/index.vue
vendored
@ -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>
|
||||||
|
Loading…
Reference in New Issue
Block a user