delucis / astro-auto-import

Auto-import components in Astro projects
84 stars 2 forks source link

astro-auto-import not working #17

Closed Boston343 closed 1 year ago

Boston343 commented 1 year ago

The package seems to be non-functional at the moment. The error I am getting makes me think the package is going to the base project folder, then taking into account any ../ in the file path, then removing all the slashes. So it's trying to access a non-existent folder structure.

Basic repo with issue shown is here https://github.com/Boston343/astro-auto-import-test

For this repo, the mdx file in question is using-mdx.mdx which is the example file from astros blog starter. I removed the include that was in the file, and instead am using the AutoImport to try and add the component import into the mdx file. This throws errors.

astro.config.mjs

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

import sitemap from "@astrojs/sitemap";

// https://astro.build/config
export default defineConfig({
  site: "https://example.com",
  integrations: [
    AutoImport({
      imports: ["../../components/HeaderLink.astro"],
    }),
    mdx(),
    sitemap(),
  ],
});

The error looks like:

 Cannot find module 'filePathToRepoWithoutSlashesastro-auto-import-testsrccomponentsHeaderLink.astro' imported from 'file/Path/To/Repo/With/Slashes/astro-auto-import-test/src/content/blog/using-mdx.mdx'
  File:
    file/Path/To/Repo/With/Slashes/astro-auto-import-test/src/content/blog/using-mdx.mdx?astroPropagatedAssets=true
brianwisti commented 1 year ago

Try using an absolute path with $PROJECT_ROOT acting as /.

Given a component src/components/Note.astro:

export default defineConfig({
  // ...
  integrations: [
    // ...
    AutoImport({
      imports: ["/src/components/Note.astro"],
    )},
    mdx(),
  ],
  // ...
});

Mind you this has been tested for exactly one usage of one component on one page. I just came across the same problem, saw this issue, and tried an idea. So far it works.

delucis commented 1 year ago

Ah, yes — sorry I somehow missed the original bug report — as @brianwisti says, the intended usage is with a path building up from / as the root of your project.

I think that should work and therefore this is not really a bug, so I'll close this. Feel free to comment or reopen if you run into difficulty though!