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

Commit

Permalink
fix(nuxt): allow overriding lower layer composables (#10017)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed Jan 14, 2023
1 parent 0b1cdcc commit bf649fe
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 1 deletion.
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]
}
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 @@ -487,6 +487,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>

0 comments on commit bf649fe

Please sign in to comment.