From f1e83239751278b433c7ac2bde4d971b8ea7338d Mon Sep 17 00:00:00 2001 From: Antoine Rey Date: Tue, 10 May 2022 09:14:45 +0200 Subject: [PATCH] Wip: Basic implemenetation --- packages/vite-node/src/cli.ts | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/packages/vite-node/src/cli.ts b/packages/vite-node/src/cli.ts index 3867136a6714..28d9abd52f4c 100644 --- a/packages/vite-node/src/cli.ts +++ b/packages/vite-node/src/cli.ts @@ -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 ', 'Use specified root directory') .option('-c, --config ', '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 ', 'Use specified Vite server options') .help() cli @@ -24,6 +28,7 @@ export interface CliOptions { root?: string config?: string watch?: boolean + serverOptions?: ViteNodeServerOptions '--'?: string[] } @@ -37,6 +42,9 @@ 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, @@ -44,7 +52,7 @@ async function run(files: string[], options: CliOptions = {}) { }) 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. +}