Skip to content
This repository has been archived by the owner on Apr 6, 2023. It is now read-only.

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

Merged
merged 2 commits into from
Jan 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion packages/nuxt/src/imports/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,17 @@ export default defineNuxtModule<Partial<ImportsOptions>>({
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 }))

const priorities = nuxt.options._layers.map((layer, i) => [layer.config.srcDir, -i] as const).sort(([a], [b]) => b.length - a.length)

const regenerateImports = async () => {
ctx.clearDynamicImports()
await ctx.modifyDynamicImports(async (imports) => {
// 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]
pi0 marked this conversation as resolved.
Show resolved Hide resolved
}
imports.push(...composableImports)
// Modules extending
await nuxt.callHook('imports:extend', imports)
})
Expand Down
4 changes: 4 additions & 0 deletions test/basic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,10 @@ describe('extends support', () => {
const html = await $fetch('/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', () => {
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/basic/composables/override-base.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const useOverrideableComposable = () => 'test from project'
1 change: 1 addition & 0 deletions test/fixtures/basic/extends/bar/composables/base.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const useOverrideableComposable = () => 'test from layer'
5 changes: 5 additions & 0 deletions test/fixtures/basic/pages/extends.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<template>
<div>
{{ useOverrideableComposable() }}
</div>
</template>