Open rileychh opened 2 months ago
I don't think node supports that. You'll need to change it to "#*": "./*.js"
and use js for it to work.
esbuild doesn't transform that while importing. So, it's whatever node supports. Intermediate code:
\ bun and tsx have very lax resolution algorithms and support extension-less imports in ESM and TS.
I tried the following configurations:
import sharedData from '#sharedData.js'
with imports set to "#*": "./*.js"
:
Cannot find module '/home/projects/vitepress-subpath-imports/sharedData.js.js'
import sharedData from '#sharedData'
with imports set to "#*": "./*"
:
Cannot find module '/home/projects/vitepress-subpath-imports/sharedData.js'
import sharedData from '#sharedData.js'
with imports set to "#*": "./*"
:
Cannot find module '/home/projects/vitepress-subpath-imports/sharedData.js'
Unfortunately none of the configuration worked.
esbuild doesn't transform that while importing.
It's interesting that the second configuration throws an error with sharedData.js
, even when no extensions are specified. Why does this happen?
I meant try this:
import sharedData from '#sharedData'
"#*": "./*.js"
// sharedData.js
export stuff ...
It's not specific to vitepress. Create a normal foo.js
file and add the import there and run it with node foo.js
.
Thank you for pointing that out! I hadn't tried that specific configuration before.
While your solution works perfectly for JavaScript files, I noticed that it doesn't seem to work for TypeScript files. Do you have any suggestions on how to make this work with TypeScript as well?
Edit: "#*": "./*"
also works for JavaScript files.
Do you have any suggestions on how to make this work with TypeScript as well?
Ah, currently no. You'll need to use tsx, or switch completely to bun. Newer node versions have added an experimental strip types flag that might also work in certain cases. But even then updates in that subpath imported file won't be tracked and config won't auto-reload. You'll need to manually restart your server every time you change that file.
Describe the bug
Subpath imports configured in
package.json
are not resolved in.vitepress/config.ts
and*.data.ts
.Reproduction
StackBlitz
Configure subpath imports in
package.json
Create a module exporting data in the project root
Import the module
in
.vitepress/config.ts
in a data loader
Expected behavior
import sharedData from '#sharedData'
should work identically to
import sharedData from '../../sharedData'
and set the site title to
Shared site title
.System Info
Additional context
Running VitePress with Bun (
bun --bun run docs:dev
) or tsxtsx node_modules/vitepress/bin/vitepress.js
works around the issue.Validations