Closed JounQin closed 2 years ago
Can you tell what error(s) were you getting? I don't think types need to be in exports
.
Can you tell what error(s) were you getting? I don't think types need to be in
exports
.
// docs/.vitepress/theme/index.ts
import type { Theme } from 'vitepress'
import DefaultTheme from 'vitepress/theme'
import * as components from '../components'
import './index.less'
const theme: Theme = {
...DefaultTheme,
enhanceApp({ app }) {
for (const [name, Component] of Object.entries(components))
app.component(name, Component)
},
}
export default theme
We do have "types": "./types/index.d.ts"
in exports
fields.
When using strict node16
or nodenext
TypeScript
moduleResolution
, vitepress/theme
must be defined in exports
.
See https://devblogs.microsoft.com/typescript/announcing-typescript-4-7/#package-json-exports-imports-and-self-referencing for more details.
Although I'm not quite sure to understand why import DefaultTheme from 'vitepress/theme'
would work (it's a .d.ts
right?):
import DefaultTheme from 'vitepress/theme'
console.log('DefaultTheme:', DefaultTheme)
It is documented at https://vitepress.vuejs.org/guide/theme-introduction.html#extending-the-default-theme
OK, I found vitepress/theme
is transformed into the following on client:
import DefaultTheme from "/@fs/Users/JounQin/Workspaces/Alauda/vision/node_modules/vitepress/dist/client/theme-default/index.js";
console.log("DefaultTheme:", DefaultTheme);
So vitepress/theme
actually points to vitepress/dist/client/theme-default/index.js
OK, finally, this is a TypeScript usage related issue. @brc-dd
Yeah but your fix is still throwing this error in my case:
Module 'vitepress/theme' cannot be imported using this construct. The specifier only resolves to an ES module, which cannot be imported synchronously. Use dynamic import instead.
Without that fix, I was getting:
Cannot find module 'vitepress/theme' or its corresponding type declarations.
Yeah but your fix is still throwing this error in my case:
Module 'vitepress/theme' cannot be imported using this construct. The specifier only resolves to an ES module, which cannot be imported synchronously. Use dynamic import instead.
Without that fix, I was getting:
Cannot find module 'vitepress/theme' or its corresponding type declarations.
What command can I run to reproduce on this project?
And did you try my new fix?
https://github.com/brc-dd/vp-issue
Yeah I have those changes:
OK, I'll check it ASAP.
@brc-dd That's because there is no type: "module"
field in package.json
, so .ts
will be compiled as cjs
, and we have no cjs
entry for vitepress/theme
, you can update package.json
with type: 'module'
, or add a new private package.json
under .vitepress
with:
{
"private": true,
"type": "module"
}
Then, the .vitepress/theme/index.ts
will be considered as ESM correctly.
Both "Go to Definition" and "Go to Source Definition" will work as expected at the same time.
@brc-dd See https://github.com/brc-dd/vp-issue/pull/1 for demo.
Besides, export const VPHomeHero = ComponentOptions
in theme.d.ts
seems absolutely wrong, actually, it should be export const VPHomeHero: ComponentOptions
, should I fix this in the same PR or a separated one?
Besides,
export const VPHomeHero = ComponentOptions
intheme.d.ts
seems absolutely wrong, actually, it should beexport const VPHomeHero: ComponentOptions
, should I fix this in the same PR or a separated one?
This is fixed on vite 3 branch.
This is fixed on vite 3 branch.
Glad to hear.
@brc-dd
Besides, do you think is there any chance how can I apply to be a member or collaborator or vite/vitepress groups? I'd like to spend more time working on vite/vitepress in the next few days.
Hi! π
Firstly, thanks for your work on this project! π
Today I used patch-package to patch
vitepress@1.0.0-alpha.4
for the project I'm working on.Here is the diff that solved my problem:
This issue body was partially generated by patch-package.