Skip to content

Commit d3d6b1f

Browse files
jgouxdammy001
andauthoredFeb 11, 2023
feat: add --script option to vite-node (#2793)
* feat: add script-mode option to vite-node * make it more resilient * edit README * remove tsx mention * Update packages/vite-node/README.md Co-authored-by: Anjorin Damilare <damilareanjorin1@gmail.com> * Update packages/vite-node/README.md Co-authored-by: Anjorin Damilare <damilareanjorin1@gmail.com> --------- Co-authored-by: Anjorin Damilare <damilareanjorin1@gmail.com>
1 parent d534184 commit d3d6b1f

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed
 

‎packages/vite-node/README.md

+26
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,32 @@ npx vite-node --options.deps.inline="module-name" --options.deps.external="/modu
4848

4949
Note that for options supporting RegExps, strings passed to the CLI must start _and_ end with a `/`;
5050

51+
### Hashbang
52+
53+
If you prefer to write scripts that don't need to be passed into Vite Node, you can declare it in the [hashbang](https://bash.cyberciti.biz/guide/Shebang).
54+
55+
Simply add `#!/usr/bin/env vite-node --script` at the top of your file:
56+
57+
_file.ts_
58+
```ts
59+
#!/usr/bin/env vite-node --script
60+
61+
console.log('argv:', process.argv.slice(2))
62+
```
63+
64+
And make the file executable:
65+
```sh
66+
chmod +x ./file.ts
67+
```
68+
69+
Now, you can run the file without passing it into Vite Node:
70+
```sh
71+
$ ./file.ts hello
72+
argv: [ 'hello' ]
73+
```
74+
75+
Note that when using the `--script` option, Vite Node forwards every argument and option to the script to execute, even the one supported by Vite Node itself.
76+
5177
## Programmatic Usage
5278

5379
In Vite Node, the server and runner (client) are separated, so you can integrate them in different contexts (workers, cross-process, or remote) if needed. The demo below shows a simple example of having both (server and runner) running in the same context

‎packages/vite-node/src/cli.ts

+12-3
Original file line numberDiff line numberDiff line change
@@ -16,33 +16,42 @@ cli
1616
.option('-r, --root <path>', 'Use specified root directory')
1717
.option('-c, --config <path>', 'Use specified config file')
1818
.option('-w, --watch', 'Restart on file changes, similar to "nodemon"')
19+
.option('--script', 'Use vite-node as a script runner')
1920
.option('--options <options>', 'Use specified Vite server options')
2021
.help()
2122

2223
cli
2324
.command('[...files]')
25+
.allowUnknownOptions()
2426
.action(run)
2527

2628
cli.parse()
2729

2830
export interface CliOptions {
2931
root?: string
32+
script?: boolean
3033
config?: string
3134
watch?: boolean
3235
options?: ViteNodeServerOptionsCLI
3336
'--'?: string[]
3437
}
3538

3639
async function run(files: string[], options: CliOptions = {}) {
40+
if (options.script) {
41+
files = [files[0]]
42+
options = {}
43+
process.argv = [process.argv[0], files[0], ...process.argv.slice(2).filter(arg => arg !== '--script' && arg !== files[0])]
44+
}
45+
else {
46+
process.argv = [...process.argv.slice(0, 2), ...(options['--'] || [])]
47+
}
48+
3749
if (!files.length) {
3850
console.error(c.red('No files specified.'))
3951
cli.outputHelp()
4052
process.exit(1)
4153
}
4254

43-
// forward argv
44-
process.argv = [...process.argv.slice(0, 2), ...(options['--'] || [])]
45-
4655
const serverOptions = options.options
4756
? parseServerOptions(options.options)
4857
: {}

0 commit comments

Comments
 (0)
Please sign in to comment.