feat!(nuxt3): extends support for composables/ directory (#3423)

Co-authored-by: pooya parsa <pyapar@gmail.com>
This commit is contained in:
Kevin Marrec 2022-03-09 12:03:36 +01:00 committed by GitHub
parent 13dd2b98ea
commit 6c083f3d12
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 24 additions and 0 deletions

View File

@ -1,5 +1,7 @@
<script setup> <script setup>
const themeConfig = useRuntimeConfig().theme const themeConfig = useRuntimeConfig().theme
const foo = useFoo()
const bar = getBar()
</script> </script>
<template> <template>
@ -8,6 +10,9 @@ const themeConfig = useRuntimeConfig().theme
<pre>{{ JSON.stringify(themeConfig, null, 2) }}</pre> <pre>{{ JSON.stringify(themeConfig, null, 2) }}</pre>
<BaseButton>Base Button</BaseButton> <BaseButton>Base Button</BaseButton>
<FancyButton>Fancy Button</FancyButton> <FancyButton>Fancy Button</FancyButton>
<br>
{{ foo }} {{ bar }}
<br>
{{ $myPlugin() }} {{ $myPlugin() }}
</NuxtExampleLayout> </NuxtExampleLayout>
</template> </template>

View File

@ -0,0 +1,3 @@
import { useState } from '#app'
export const useFoo = () => useState('foo', () => 'foo')

View File

@ -1,6 +1,9 @@
import { defineNuxtConfig } from 'nuxt3' import { defineNuxtConfig } from 'nuxt3'
export default defineNuxtConfig({ export default defineNuxtConfig({
autoImports: {
dirs: ['utils']
},
publicRuntimeConfig: { publicRuntimeConfig: {
theme: { theme: {
primaryColor: 'base_primary', primaryColor: 'base_primary',

View File

@ -0,0 +1 @@
export const getBar = () => 'bar'

View File

@ -41,6 +41,9 @@ export default defineNuxtModule({
nuxtCtx.set(nuxt) nuxtCtx.set(nuxt)
} }
// Mock _extends
nuxt.options._extends = nuxt.options._extends || []
if (opts.nitro) { if (opts.nitro) {
await setupNitroBridge() await setupNitroBridge()
} }

View File

@ -36,6 +36,15 @@ export default defineNuxtModule<AutoImportsOptions>({
join(nuxt.options.srcDir, 'composables'), join(nuxt.options.srcDir, 'composables'),
...options.dirs ...options.dirs
] ]
// Extend with layers
for (const layer of nuxt.options._extends) {
composablesDirs.push(resolve(layer.config.srcDir, 'composables'))
for (const dir of (layer.config.autoImports?.dirs ?? [])) {
composablesDirs.push(resolve(layer.config.srcDir, dir))
}
}
await nuxt.callHook('autoImports:dirs', composablesDirs) await nuxt.callHook('autoImports:dirs', composablesDirs)
composablesDirs = composablesDirs.map(dir => normalize(dir)) composablesDirs = composablesDirs.map(dir => normalize(dir))