#tsconfig's "paths" in astro.config.ts imports?
34 messages · Page 1 of 1 (latest)
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
"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
From astro.config.ts?
Paths work fine from any source file inside the Astro project. It's not the question.
I wrote all the information above.
I guess I don’t understand why you are defining paths in your Astro Config?
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
I don't think there's a way right now, unfortunately. There's kinda a chicken and egg problem, as you've noticed
Does it use a separate tsconfig to load astro.config.ts?
"it" being astro? vite? rollup?
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
Ah so I could maybe create a vite.config.mjs with those paths?
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
Perhaps? I know we disable loading vite.config.mjs in Astro itself, but not sure when we load the config
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
It happens, configs are rarely pretty, ha
Sorry for not
Understanding the issue at first.