Skip to content
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

Import globs? #20

Open
jonsage opened this issue Jun 22, 2023 · 5 comments
Open

Import globs? #20

jonsage opened this issue Jun 22, 2023 · 5 comments

Comments

@jonsage
Copy link

jonsage commented Jun 22, 2023

It would be great to be able to import using globs like so:

AutoImport({
  imports: [
    // '@components/Form.astro',
    // '@components/Radio.astro',
    '@components/*',
    // '@includes/Intro.md,
    // '@includes/About.astro',
    '@includes/*'
  ],
})

And still use the same way:

<Form>
  <Radio/>
</Form>

I attempted to create something using Vite's glob-import but couldn't get it work :/

@delucis
Copy link
Owner

delucis commented Jun 22, 2023

Interesting. A bit similar to the request in #13. I wonder how feasible this is. Currently this plug-in is pretty dumb: it doesn't do any resolution or anything, just maps config to injected import statements. For glob-style imports to work, we'd probably need have a way of discovering what is available at *. Not sure how we'd do that off the top of my head, but maybe someone has an idea!

@marfalkov
Copy link

@delucis

Interesting. A bit similar to the request in #13. I wonder how feasible this is. Currently this plug-in is pretty dumb: it doesn't do any resolution or anything, just maps config to injected import statements. For glob-style imports to work, we'd probably need have a way of discovering what is available at *. Not sure how we'd do that off the top of my head, but maybe someone has an idea!

There is ‘unjs/unimport’. They have presets for a couple of frameworks. Might be a good idea to just add an Astro preset.

@delucis
Copy link
Owner

delucis commented Aug 29, 2023

Interesting! Does that support MDX do you know? We need to inject import nodes into the MDX syntax tree. But even if they don't, their scanning behaviour could maybe be the basis for something like this feature?

I won't have much time to look into this but if anyone wants to try something, I'll be happy to look at a PR or answer any questions.

@marfalkov
Copy link

Interesting! Does that support MDX do you know? We need to inject import nodes into the MDX syntax tree. But even if they don't, their scanning behaviour could maybe be the basis for something like this feature?

I won't have much time to look into this but if anyone wants to try something, I'll be happy to look at a PR or answer any questions.

Unfortunately it does not: Doesn't work with Astro MDX #293

@delucis
Copy link
Owner

delucis commented May 3, 2024

Btw, just a thought for anyone coming across this thread, I think you can probably get a decent way towards this manually:

import { defineConfig } from 'astro/config';
import AutoImport from 'astro-auto-import';
import mdx from '@astrojs/mdx';

import { readdirSync } from 'node:fs';

// Get a list of all the components in a specific directory:
const componentDir = './src/components/';
const components = readdirSync(componentDir);

export default defineConfig({
  integrations: [
    AutoImport({
      imports: [
        // Add paths to each component to the auto-import array:
        ...components.map(filename => componentDir + filename),
      ],
    }),

    mdx(),
  ],
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants