Skip to content

Commit 7620c7b

Browse files
committed
fix(vitepress-twoslash): separate CSS deps, close #86
1 parent ec605e0 commit 7620c7b

File tree

8 files changed

+47
-8
lines changed

8 files changed

+47
-8
lines changed

docs/.vitepress/theme/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// https://vitepress.dev/guide/custom-theme
22
import Theme from 'vitepress/theme'
33
import TwoSlashFloatingVue from 'vitepress-plugin-twoslash/client'
4+
import 'vitepress-plugin-twoslash/style.css'
45
import 'uno.css'
56
import './style.css'
67
import { createPinia } from 'pinia'

docs/packages/rehype.md

-1
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,3 @@ console.log('3') // highlighted
9393
console.log('4') // highlighted
9494
```
9595
````
96-

docs/packages/vitepress.md

+17-2
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ export default defineConfig({
3030
})
3131
```
3232

33-
And then in your [`.vitepress/theme/index.ts`](https://vitepress.dev/guide/custom-theme):
33+
And then in your [`.vitepress/theme/index.ts`](https://vitepress.dev/guide/custom-theme), install the Vue plugin and import the css with `vitepress-plugin-twoslash/styles.css`.
3434

3535
```ts twoslash
3636
// @noErrors: true
3737
// .vitepress/theme/index.ts
3838
import Theme from 'vitepress/theme'
3939
import TwoSlashFloatingVue from 'vitepress-plugin-twoslash/client' // [!code hl]
40-
import './custom-style.css'
40+
import 'vitepress-plugin-twoslash/style.css' // [!code hl]
4141
import type { EnhanceAppContext } from 'vitepress'
4242

4343
export default {
@@ -48,6 +48,21 @@ export default {
4848
}
4949
```
5050

51+
::: details About style.css
52+
53+
For easier setup, `vitepress-plugin-twoslash/styles.css` bundles the styles from `floating-vue` and `shikiji-twoslash/style-rich.css` so you only need a single entry. If you are using a custom `floating-vue` style or want to have more control of the styles, you can expand them as:
54+
55+
```ts
56+
import 'vitepress-plugin-twoslash/style.css'
57+
58+
// Equivalent to:
59+
import 'shikiji-twoslash/style-rich.css'
60+
import 'floating-vue/dist/style.css'
61+
import 'vitepress-plugin-twoslash/style-core.css'
62+
```
63+
64+
:::
65+
5166
That's it, you can now use `ts twoslash` in your markdown files to enable the beautiful type hover.
5267

5368
````md
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/style.css
2+
/style-core.css
3+

packages/vitepress-plugin-twoslash/build.config.ts

+24
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import fs from 'node:fs/promises'
12
import { defineBuildConfig } from 'unbuild'
23

34
export default defineBuildConfig({
@@ -14,4 +15,27 @@ export default defineBuildConfig({
1415
'vitepress-plugin-twoslash',
1516
'vitepress-plugin-twoslash/style.css',
1617
],
18+
hooks: {
19+
'rollup:done': async () => {
20+
// eslint-disable-next-line no-console
21+
console.log('Building style.css')
22+
const floatingVue = await fs.readFile(new URL('./node_modules/floating-vue/dist/style.css', import.meta.url), 'utf-8')
23+
const twoslash = await fs.readFile(new URL('./../shikiji-twoslash/style-rich.css', import.meta.url), 'utf-8')
24+
const local = await fs.readFile(new URL('./src/style.css', import.meta.url), 'utf-8')
25+
26+
const all = [
27+
'/* BUNDLED FROM floating-vue/dist/style.css */',
28+
floatingVue,
29+
'',
30+
'/* BUNDLED FROM shikiji-twoslash/style-rich.css */',
31+
twoslash,
32+
'',
33+
'/* BUNDLED FROM vitepress-plugin-twoslash/style-core.css */',
34+
local,
35+
].join('\n')
36+
37+
await fs.writeFile(new URL('./style.css', import.meta.url), all)
38+
await fs.writeFile(new URL('./style-core.css', import.meta.url), local)
39+
},
40+
},
1741
})

packages/vitepress-plugin-twoslash/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
"types": "./dist/client.d.mts",
2828
"default": "./dist/client.mjs"
2929
},
30-
"./style.css": "./style.css"
30+
"./style.css": "./style.css",
31+
"./style-core.css": "./style-core.css"
3132
},
3233
"main": "./dist/index.mjs",
3334
"module": "./dist/index.mjs",

packages/vitepress-plugin-twoslash/src/client.ts

-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
import 'shikiji-twoslash/style-rich.css'
2-
import 'floating-vue/dist/style.css'
3-
import 'vitepress-plugin-twoslash/style.css'
4-
51
import type { Plugin } from 'vue'
62
import FloatingVue from 'floating-vue'
73

0 commit comments

Comments
 (0)