Skip to content
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

fix(vite-node): Prevent crash when passing single module as options #1406

Merged
merged 2 commits into from Jun 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 7 additions & 6 deletions packages/vite-node/src/cli.ts
Expand Up @@ -5,6 +5,7 @@ import { version } from '../package.json'
import { ViteNodeServer } from './server'
import { ViteNodeRunner } from './client'
import type { ViteNodeServerOptions } from './types'
import { toArray } from './utils'

const cli = cac('vite-node')

Expand Down Expand Up @@ -93,12 +94,12 @@ function parseServerOptions(serverOptions: ViteNodeServerOptionsCLI): ViteNodeSe
...serverOptions,
deps: {
...serverOptions.deps,
inline: serverOptions.deps?.inline?.map((dep) => {
inline: toArray(serverOptions.deps?.inline).map((dep) => {
return dep.startsWith('/') && dep.endsWith('/')
? new RegExp(dep)
: dep
}),
external: serverOptions.deps?.external?.map((dep) => {
external: toArray(serverOptions.deps?.external).map((dep) => {
return dep.startsWith('/') && dep.endsWith('/')
? new RegExp(dep)
: dep
Expand All @@ -108,18 +109,18 @@ function parseServerOptions(serverOptions: ViteNodeServerOptionsCLI): ViteNodeSe
transformMode: {
...serverOptions.transformMode,

ssr: serverOptions.transformMode?.ssr?.map(dep => new RegExp(dep)),
web: serverOptions.transformMode?.ssr?.map(dep => new RegExp(dep)),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There was a mistake here. It should have been web instead. It's now fixed.

ssr: toArray(serverOptions.transformMode?.ssr).map(dep => new RegExp(dep)),
web: toArray(serverOptions.transformMode?.web).map(dep => new RegExp(dep)),
},
}
}

type Optional<T> = T | undefined
type ComputeViteNodeServerOptionsCLI<T extends Record<string, any>> = {
[K in keyof T]: T[K] extends Optional<RegExp[]>
? string[]
? string | string[]
: T[K] extends Optional<(string | RegExp)[]>
? string[]
? string | string[]
: T[K] extends Optional<Record<string, any>>
? ComputeViteNodeServerOptionsCLI<T[K]>
: T[K]
Expand Down
3 changes: 3 additions & 0 deletions packages/vite-node/src/types.ts
@@ -1,5 +1,8 @@
import type { ModuleCacheMap } from './client'

export type Nullable<T> = T | null | undefined
export type Arrayable<T> = T | Array<T>

export interface DepsHandlingOptions {
external?: (string | RegExp)[]
inline?: (string | RegExp)[]
Expand Down
16 changes: 16 additions & 0 deletions packages/vite-node/src/utils.ts
@@ -1,6 +1,7 @@
import { fileURLToPath, pathToFileURL } from 'url'
import { dirname, resolve } from 'pathe'
import type { TransformResult } from 'vite'
import type { Arrayable, Nullable } from './types'

export const isWindows = process.platform === 'win32'

Expand Down Expand Up @@ -74,3 +75,18 @@ export async function withInlineSourcemap(result: TransformResult) {

return result
}

/**
* Convert `Arrayable<T>` to `Array<T>`
*
* @category Array
*/
export function toArray<T>(array?: Nullable<Arrayable<T>>): Array<T> {
if (array === null || array === undefined)
array = []

if (Array.isArray(array))
return array

return [array]
}