-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Possibility to use single index file (barrel file) w/o repeating/duplicating component names #13
Comments
I think the way to go here might be to support |
@delucis Have you given this any more thought? It would be nice to have a way to import namespaces. The syntax could look something like: AutoImport({
imports: [
{
// generates:
// import * as Components from '@components/index.js';
'@components/index.js': [['*', 'Components']],
},
],
}), The current workaround is to use a default export instead of a named export. - export {
+ export default {
ComponentA,
ComponentB
} or use another file that re-exports the namespace object as a default export. import * as Components from '@components/index.js';
export default Components; The downside still is that the exports need to be done manually, i.e., the "barrel" file still needs to exist. It's verbose and can slow things down. But the solution to that is already tracked by a separate issue (#20). |
Haven't had time to consider it much, but I like your syntax I think! I guess it's a little bit magic, but no more than the rest of the API design really. Are you interested in implementing this? |
Thanks for the suggestion. How would you then load this file with |
The usual way you’d do a default import: AutoImport({
imports: [
{
// generates:
// import { default as Components } from '@components/index.js';
'@components/index.js': [['default', 'Components']],
},
],
}), Then you can use them like: <Components.ComponentA />
<Components.ComponentB /> |
Maybe! I was looking through the source code and couldn't quite understand how it works 😅 |
Actually, it looks like this approach breaks hydration. Not sure if it's a bug in astro or astro-auto-import. See repro |
It’s actually stupider than it looks 😅 I think we’d need to change this function: astro-auto-import/packages/astro-auto-import/src/index.ts Lines 46 to 61 in 555c5e3
Right now that collects a bunch of So if we’re lucky, we can just special case import * as Namespace, { something } from 'module'; or would it have to be? import * as Namespace from 'module';
import { something } from 'module'; |
Looks like an Astro bug. Even importing the barrel file directly breaks hydration: import Barrel from '../components';
import Counter from '../components/Counter.tsx';
# Hello
<Barrel.Counter client:load />
<Counter client:load /> |
i think it needs to be the latter. in which case, maybe the below syntax would make more sense? AutoImport({
imports: [
{
// generates:
// import * as Namespace from 'module';
'module': 'Namespace',
},
],
}),
Opened withastro/compiler#899 |
@delucis @mayank99 As far as I can see, withastro/astro#32 still only allows to use the imported components via namespace, e.g. |
No. This would require The prefix can be as short as you like, so if it’s just the authoring noise your concerned about, I’d recommend choosing something shorter like e.g. For now, if you need the unprefixed name, you’d need to use the named import approach and tell |
Thanks for the detailed explanation! |
In my project I have a single
index.ts
file that just imports all of my separate components and then re-exports them as named exports (barrel file).This way, I used to be able to use all my components through a single import statements (possibly with a TS alias), like this:
Now I want to use
astro-auto-import
, and I was wondering if there is any way to auto-import all the files that get exported from my index file, without repeating/duplicating all the module names, and without prefixing them with a namespace.I'd love to have a single source of truth for all the usable components, and I'd love it to be my index file rather than the Astro config.
The text was updated successfully, but these errors were encountered: