New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(vite-node): options via CLI (fixes #1208) #1215
Changes from 1 commit
f1e8323
444c806
2a96ef8
45e9846
89d874c
1ce975b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ import { createServer } from 'vite' | |
import { version } from '../package.json' | ||
import { ViteNodeServer } from './server' | ||
import { ViteNodeRunner } from './client' | ||
import type { ViteNodeServerOptions } from './types' | ||
|
||
const cli = cac('vite-node') | ||
|
||
|
@@ -12,6 +13,9 @@ cli | |
.option('-r, --root <path>', 'Use specified root directory') | ||
.option('-c, --config <path>', 'Use specified config file') | ||
.option('-w, --watch', 'Restart on file changes, similar to "nodemon"') | ||
// TODO: How could we document this? Ideally, we should link to the | ||
// TODO: ViteNodeServerOptions type since it's the source of truth. | ||
.option('--server-options <options>', 'Use specified Vite server options') | ||
.help() | ||
|
||
cli | ||
|
@@ -24,6 +28,7 @@ export interface CliOptions { | |
root?: string | ||
config?: string | ||
watch?: boolean | ||
serverOptions?: ViteNodeServerOptions | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since the option is provided by the consumer, it might not match However, we could rely on a TS guard to assert that the given options match the TS interface, but it would mean hardcoding the checks, and thus duplicating the source of truth between Do you see another way to achieve this without duplicating the source of truth? 🤔 |
||
'--'?: string[] | ||
} | ||
|
||
|
@@ -37,14 +42,17 @@ async function run(files: string[], options: CliOptions = {}) { | |
// forward argv | ||
process.argv = [...process.argv.slice(0, 2), ...(options['--'] || [])] | ||
|
||
if (options.serverOptions) | ||
parseServerOptions(options.serverOptions) | ||
|
||
const server = await createServer({ | ||
logLevel: 'error', | ||
configFile: options.config, | ||
root: options.root, | ||
}) | ||
await server.pluginContainer.buildStart({}) | ||
|
||
const node = new ViteNodeServer(server) | ||
const node = new ViteNodeServer(server, options.serverOptions) | ||
|
||
const runner = new ViteNodeRunner({ | ||
root: server.config.root, | ||
|
@@ -81,3 +89,23 @@ async function run(files: string[], options: CliOptions = {}) { | |
await runner.executeFile(file) | ||
}) | ||
} | ||
|
||
function parseServerOptions(serverOptions: ViteNodeServerOptions) { | ||
if (serverOptions.deps && serverOptions.deps.inline) { | ||
serverOptions.deps.inline = serverOptions.deps.inline.map((dep) => { | ||
return typeof dep === 'string' && dep.startsWith('/') && dep.endsWith('/') | ||
? new RegExp(dep) | ||
: dep | ||
}) | ||
} | ||
|
||
if (serverOptions.deps && serverOptions.deps.external) { | ||
serverOptions.deps.external = serverOptions.deps.external.map((dep) => { | ||
return typeof dep === 'string' && dep.startsWith('/') && dep.endsWith('/') | ||
? new RegExp(dep) | ||
: dep | ||
}) | ||
} | ||
|
||
// TODO: Handle serverOptions.transformMode. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've noticed that
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. doesn't have a reason 👀 |
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I could not find any proper example of such documentation. Should I add a few lines to the README?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I think that would do