Skip to content

Commit

Permalink
Wip: Basic implemenetation
Browse files Browse the repository at this point in the history
  • Loading branch information
antoinerey committed May 10, 2022
1 parent f85d020 commit f1e8323
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion packages/vite-node/src/cli.ts
Expand Up @@ -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')

Expand All @@ -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
Expand All @@ -24,6 +28,7 @@ export interface CliOptions {
root?: string
config?: string
watch?: boolean
serverOptions?: ViteNodeServerOptions
'--'?: string[]
}

Expand All @@ -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,
Expand Down Expand Up @@ -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.
}

0 comments on commit f1e8323

Please sign in to comment.