Skip to content

Commit ad1034d

Browse files
authored
feat(vscode): allow to customize dev command (#2029)
1 parent 3192aeb commit ad1034d

File tree

4 files changed

+32
-2
lines changed

4 files changed

+32
-2
lines changed

docs/features/vscode-extension.md

+15
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,18 @@ You can add glob patterns to the `slidev.include` configuration to include files
6363
"slidev.include": ["**/presentation.md"]
6464
}
6565
```
66+
67+
#### Dev Command {#dev-command}
68+
69+
You can customize the command to start dev server by setting the `slidev.dev-command` configuration. The default value is `npm exec -c 'slidev ${args}'`.
70+
71+
The configured command can contain placeholders:
72+
73+
- `${args}`: All CLI arguments. e.g. `slides.md --port 3000 --remote`
74+
- `${port}`: The port number. e.g. `3000`
75+
76+
Examples:
77+
78+
- Global installation: `slidev ${args}`
79+
- For PNPM users, you can set it to `pnpm slidev ${args}`.
80+
- For [code-server](https://coder.com/docs/code-server/) users, you can set it to `pnpm slidev ${args} --base /proxy/${port}/`. This will make the dev server accessible at `http://localhost:8080/proxy/3000/`.

packages/vscode/package.json

+6
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,12 @@
397397
"scope": "window",
398398
"description": "A glob pattern to exclude slides entries",
399399
"default": "**/node_modules/**"
400+
},
401+
"slidev.dev-command": {
402+
"type": "string",
403+
"scope": "window",
404+
"description": "The command to start Slidev dev server. See https://sli.dev/features/vscode-extension#dev-command",
405+
"default": "npm exec -c 'slidev ${args}'"
400406
}
401407
}
402408
},

packages/vscode/src/composables/useDevServer.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import type { Ref } from 'reactive-vscode'
2-
import type { Terminal } from 'vscode'
32
import type { SlidevProject } from '../projects'
43
import { basename } from 'node:path'
54
import { getPort as getPortPlease } from 'get-port-please'
65
import { toRef } from 'reactive-vscode'
6+
import { env, type Terminal } from 'vscode'
7+
import { devCommand } from '../configs'
78
import { useServerTerminal } from '../views/serverTerminal'
89
import { useServerDetector } from './useServerDetector'
910

@@ -29,7 +30,13 @@ export function useDevServer(project: SlidevProject) {
2930
if (getIsActive())
3031
return
3132
port.value ??= await getPort()
32-
sendText(`npm exec -c 'slidev ${JSON.stringify(basename(project.entry))} --port ${port.value}'`)
33+
const args = [
34+
JSON.stringify(basename(project.entry)),
35+
`--port ${port.value}`,
36+
env.remoteName != null ? '--remote' : '',
37+
].filter(Boolean).join(' ')
38+
// eslint-disable-next-line no-template-curly-in-string
39+
sendText(devCommand.value.replaceAll('${args}', args))
3340
}
3441

3542
function stop() {

packages/vscode/src/configs.ts

+2
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@ export const {
88
'preview-sync': previewSyncInitial,
99
include,
1010
exclude,
11+
'dev-command': devCommand,
1112
} = defineConfigs('slidev', {
1213
'force-enabled': Boolean,
1314
'port': Number,
1415
'annotations': Boolean,
1516
'preview-sync': Boolean,
1617
'include': Object as ConfigType<string[]>,
1718
'exclude': String,
19+
'dev-command': String,
1820
})
1921

2022
export const configuredPort = ref(configuredPortInitial)

0 commit comments

Comments
 (0)