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

🐛 BUG: Astro.glob with path aliases won't work #3835

Closed
1 task
fabien opened this issue Jul 6, 2022 · 3 comments · Fixed by #4010
Closed
1 task

🐛 BUG: Astro.glob with path aliases won't work #3835

fabien opened this issue Jul 6, 2022 · 3 comments · Fixed by #4010
Assignees
Labels
- P3: minor bug An edge case that only affects very specific usage (priority)

Comments

@fabien
Copy link

fabien commented Jul 6, 2022

What version of astro are you using?

1.0.0-beta.63

Are you using an SSR adapter? If so, which one?

None

What package manager are you using?

npm

What operating system are you using?

Mac

Describe the Bug

After setting up aliases in tsconfig.js I was expecting these to be available to Astro.glob() as well:

{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "@components/*": [
        "src/components/*"
      ],
      "@layouts/*": [
        "src/layouts/*"
      ],
      "@assets/*": [
        "src/assets/*"
      ],
    }
  }
}
import { Debug } from 'astro/components';
import Layout from '@layouts/Layout.astro';
import Card from '@components/Card.astro';

// Works:
// const components = await Astro.glob('../components/*.astro');

// Fails
const components = await Astro.glob('@components/*.astro');

A workaround in astro.config.mjs - which is what I expected to be done automatically under the hood:

// https://astro.build/config
export default defineConfig({
  integrations: [],
  vite: {
    resolve: {
      alias: {
        '@components': path.resolve('./src/components/')
      }
    }
  }
});

See /src/pages/index.astro in the Sandbox below.

Link to Minimal Reproducible Example

https://codesandbox.io/s/flamboyant-parm-hmhud7

Participation

  • I am willing to submit a pull request for this issue.
@fabien
Copy link
Author

fabien commented Jul 6, 2022

Even with the Vite resolve alias workaround, building for production fails:

[vite:build-import-analysis] Invalid glob import syntax: pattern must start with "." or "/" (relative to project root) or alias path

@natemoo-re
Copy link
Member

Hmm, this does look like an Astro-specific issue. :(

We are automatically setting vite.resolve.alias but Astro.glob doesn't seem to be passing these through to import.meta.glob properly.

See plain Vite repro (it works) https://stackblitz.com/edit/vitejs-vite-mktopt?file=vite.config.ts,main.js&terminal=dev

@natemoo-re natemoo-re added - P3: minor bug An edge case that only affects very specific usage (priority) s1-small labels Jul 8, 2022
@jeremydw
Copy link

FYI The workaround in this issue was working previously, but I believe that the fix from #3933 broke the workaround.

I can't seem to use Astro.glob with a monorepo at all anymore.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
- P3: minor bug An edge case that only affects very specific usage (priority)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants