#tsconfig's "paths" in astro.config.ts imports?

34 messages · Page 1 of 1 (latest)

coarse tide
#

Is there a way to get astro to support that? By registering something before calling astro/dist/cli myself maybe?

#

This is aesthetically unpleasant and itches me:

import readingStats from './local_modules/rehype-reading-stats'
import sectionize from './local_modules/remark-sectionize'
import captions from './local_modules/remark-captions'
import embeds from './local_modules/remark-embeds'
#

I want this instead:

import readingStats from '@local/rehype-reading-stats'
import sectionize from '@local/remark-sectionize'
import captions from '@local/remark-captions'
import embeds from '@local/remark-embeds'
#

I have

"paths": {
    "/*": ["src/*"],
    "@assets/*": ["src/assets/*"],
    "@local/*": ["local_modules/*"]
}
#

But I get no love:

9:50:16 AM [vite] Pre-transform error: Failed to load url @local/rehype-reading-stats (resolved id: @local/rehype-reading-stats) in astro.config.ts. Does the file exist?
9:50:16 AM [vite] Pre-transform error: Failed to load url @local/remark-sectionize (resolved id: @local/remark-sectionize) in astro.config.ts. Does the file exist?
9:50:16 AM [vite] Pre-transform error: Failed to load url @local/remark-captions (resolved id: @local/remark-captions) in astro.config.ts. Does the file exist?
9:50:16 AM [vite] Pre-transform error: Failed to load url @local/remark-embeds (resolved id: @local/remark-embeds) in astro.config.ts. Does the file exist?
#

I really don't want the overhead of a traditional monorepo with 50 tsconfig.jsons and package.jsons

runic cobalt
#
    "paths": {
      "@/*": ["./src/*"],
      "@/assets/*": ["./src/assets/*"],
      "@/components/*": ["./src/components/*"],
      "@/layouts/*": ["./src/layouts/*"],
      "@/lib/*": ["./src/lib/*"]
    },

This is what I have and it works just fine.

#

Where is your local_modules folder located? I just did another test like this

"@/local_modules/*": ["local_modules/*"]
export function localModuleFunction() {
  return "localModuleFunction";
}
import { localModuleFunction } from "@/local_modules/index";

localModuleFunction();

Works just fine for me

coarse tide
#

From astro.config.ts?

#

Paths work fine from any source file inside the Astro project. It's not the question.

runic cobalt
#

In my tsconfig.

#

Where in your Astro config are you putting paths?

coarse tide
#

I wrote all the information above.

runic cobalt
#

I guess I don’t understand why you are defining paths in your Astro Config?

coarse tide
#

While I appreciate you taking the time to answer, I think you completely misunderstood the problem and created a lot of noisy content that will just confuse others who may have the answer.

#

No I don't. In tsconfig.json, as written above.

#

path mappings defined in tsconfig.json are ignored when importing modules in astro.config.ts.

#

because that file is loaded in some vite pre-transform thing, apparently

stoic dawn
#

I don't think there's a way right now, unfortunately. There's kinda a chicken and egg problem, as you've noticed

coarse tide
#

Does it use a separate tsconfig to load astro.config.ts?

#

"it" being astro? vite? rollup?

stoic dawn
#

No, but the path resolution set in tsconfig.json does not actually directly control how Vite resolve paths

#

We have a Vite plugin that loads the tsconfig.json and transform the TypeScript path aliases into Vite path aliases

#

By the time that happens, it's too late because astro.config.ts is already loaded

coarse tide
#

Ah so I could maybe create a vite.config.mjs with those paths?

stoic dawn
#

There's a way to make it work (on our end), fwiw, but we've been a bit reluctant implementing it because it comes with other (mostly philosophical...) drawbacks

stoic dawn
coarse tide
#

Yeah I just confirmed it's not loaded 😕

#

Alright. I guess there's no choice but to scratch my arms when I see my imports, then.
Thanks @stoic dawn and @runic cobalt

stoic dawn
#

It happens, configs are rarely pretty, ha

runic cobalt
#

Sorry for not
Understanding the issue at first.

coarse tide
#

At least it's ts. Some people are still arguing against package.yaml.

#

@runic cobalt np!

#

Thanks for your time