N
Nuxt4d ago
kylyi

Partial autoimport from layers

I am trying to build an app that uses multiple layers. For some layers, I would like to turn off the implicit autoimported dirs (./composables, ./utils) Let's have the following example:
File structure
- apps
- app1

- packages
- layer1
- composables
- useStuff1.ts
- layer2
- useStuff2.ts
- apps
- app1

- packages
- layer1
- composables
- useStuff1.ts
- layer2
- useStuff2.ts
I would like the useStuff1 to be autoimported, while useStuff2.ts not. Is that currently possible?
5 Replies
kapa.ai
kapa.ai4d ago
Hi @kylyi. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- Based on the knowledge sources provided, it appears that partially disabling auto-imports for specific layers in a Nuxt application is not directly supported in the way you're describing. However, there are some approaches you can consider to achieve a similar result: 1. Globally disabling auto-imports for composables and utils: You can disable auto-imports for composables and utils globally by setting imports.scan to false in your nuxt.config.ts file:
export default defineNuxtConfig({
imports: {
scan: false
}
})
export default defineNuxtConfig({
imports: {
scan: false
}
})
This will disable scanning of the composables/ and utils/ directories for auto-imports, as mentioned in the Nuxt documentation on auto-imports. 2. Manually adding specific directories for auto-imports: After disabling the global scan, you can manually specify directories to be auto-imported using the imports.dirs option:
export default defineNuxtConfig({
imports: {
scan: false,
dirs: ['packages/layer1/composables']
}
})
export default defineNuxtConfig({
imports: {
scan: false,
dirs: ['packages/layer1/composables']
}
})
This approach is mentioned in the Nuxt configuration documentation. However, it's important to note that this setup has limitations, especially when working with layers: 1. You will need to explicitly import composables from each layer that you want to use, rather than relying on auto-imports. 2. This approach breaks the layer system's override feature, as mentioned in the auto-imports documentation. Given these limitations, it may not be possible to achieve exactly what you're looking for in terms of selectively auto-importing from some layers but not others. The current Nuxt auto-import system doesn't seem to provide granular control at the layer level. If you need this level of control, you might need to consider alternative approaches, such as manually importing the composables you need from each layer, or restructuring your project to group auto-imported and manually imported utilities differently. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai4d ago
<@238577963338170370> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
kylyi
kylyiOP4d ago
@kapa.ai The first solution is not usable in my case - I want to autoimport the defaults in some cases. The second solution is not usable because if scan: false , the dirs property is ignored.
kapa.ai
kapa.ai4d ago
Let me check the knowledge base for you again. One moment! :books: ---------------------- I apologize for the confusion in my previous answer. You're correct that the solutions I provided don't address your specific needs. Let me provide a more accurate response based on the current Nuxt capabilities. Unfortunately, the knowledge sources don't provide a direct solution for selectively enabling auto-imports for specific layers while disabling them for others. The current auto-import system in Nuxt doesn't offer this level of granular control at the layer level. The imports.scan option is indeed a global setting, and when set to false, it disables scanning for all directories, including those specified in imports.dirs. This is confirmed in the Nuxt Configuration documentation:
scan: Whether to scan your `composables/` and `utils/` directories for composables to auto-import. Auto-imports registered by Nuxt or other modules, such as imports from `vue` or `nuxt`, will still be enabled.
scan: Whether to scan your `composables/` and `utils/` directories for composables to auto-import. Auto-imports registered by Nuxt or other modules, such as imports from `vue` or `nuxt`, will still be enabled.
Given these limitations, there isn't a built-in way to achieve what you're looking for with the current Nuxt auto-import system. The system is designed to work uniformly across all layers, and doesn't provide a mechanism to selectively enable or disable auto-imports for specific layers. For your use case, you might need to consider alternative approaches: 1. Manually import the composables from the layers where you don't want auto-imports. 2. Restructure your project to group auto-imported and manually imported utilities differently. 3. Create a custom Nuxt module to handle the selective auto-importing, although this would require significant development effort. If this functionality is crucial for your project, you might want to consider opening a feature request on the Nuxt GitHub repository to propose a more granular control over auto-imports in layered applications. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai4d ago
<@238577963338170370> Kapa.ai is still learning and improving, please let me know how I did by reacting below.

Did you find this page helpful?