fix(nuxt): allow overriding lower layer composables (#10017)

This commit is contained in:
Daniel Roe 2023-01-14 01:14:24 +00:00 committed by GitHub
parent 0b1cdcc70d
commit bf649fe576
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 18 additions and 1 deletions

View File

@ -72,11 +72,17 @@ export default defineNuxtModule<Partial<ImportsOptions>>({
addVitePlugin(TransformPlugin.vite({ ctx, options, sourcemap: nuxt.options.sourcemap.server || nuxt.options.sourcemap.client })) addVitePlugin(TransformPlugin.vite({ ctx, options, sourcemap: nuxt.options.sourcemap.server || nuxt.options.sourcemap.client }))
addWebpackPlugin(TransformPlugin.webpack({ ctx, options, sourcemap: nuxt.options.sourcemap.server || nuxt.options.sourcemap.client })) addWebpackPlugin(TransformPlugin.webpack({ ctx, options, sourcemap: nuxt.options.sourcemap.server || nuxt.options.sourcemap.client }))
const priorities = nuxt.options._layers.map((layer, i) => [layer.config.srcDir, -i] as const).sort(([a], [b]) => b.length - a.length)
const regenerateImports = async () => { const regenerateImports = async () => {
ctx.clearDynamicImports() ctx.clearDynamicImports()
await ctx.modifyDynamicImports(async (imports) => { await ctx.modifyDynamicImports(async (imports) => {
// Scan composables/ // Scan composables/
imports.push(...await scanDirExports(composablesDirs)) const composableImports = await scanDirExports(composablesDirs)
for (const i of composableImports) {
i.priority = i.priority || priorities.find(([dir]) => i.from.startsWith(dir))?.[1]
}
imports.push(...composableImports)
// Modules extending // Modules extending
await nuxt.callHook('imports:extend', imports) await nuxt.callHook('imports:extend', imports)
}) })

View File

@ -487,6 +487,10 @@ describe('extends support', () => {
const html = await $fetch('/foo') const html = await $fetch('/foo')
expect(html).toContain('Composable | useExtendsFoo: foo') expect(html).toContain('Composable | useExtendsFoo: foo')
}) })
it('allows overriding composables', async () => {
const html = await $fetch('/extends')
expect(html).toContain('test from project')
})
}) })
describe('plugins', () => { describe('plugins', () => {

View File

@ -0,0 +1 @@
export const useOverrideableComposable = () => 'test from project'

View File

@ -0,0 +1 @@
export const useOverrideableComposable = () => 'test from layer'

5
test/fixtures/basic/pages/extends.vue vendored Normal file
View File

@ -0,0 +1,5 @@
<template>
<div>
{{ useOverrideableComposable() }}
</div>
</template>