Skip to content

Commit 95a3cda

Browse files
authored
feat: add input to server.fs.allow (#23035)
1 parent 41c4658 commit 95a3cda

3 files changed

Lines changed: 45 additions & 3 deletions

File tree

‎packages/vite/src/node/__tests__/config.spec.ts‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1235,6 +1235,27 @@ describe('resolveConfig', () => {
12351235
})
12361236
})
12371237

1238+
test('adds input to server.fs.allow by default', async () => {
1239+
const inputs = {
1240+
main: resolveInputFromRoot('src/a.ts'),
1241+
admin: resolveInputFromRoot('src/b.ts'),
1242+
}
1243+
const config = await resolveConfig(
1244+
{ input: { main: 'src/a.ts', admin: 'src/b.ts' } },
1245+
'serve',
1246+
)
1247+
1248+
expect(config.server.fs.allow).toStrictEqual(
1249+
expect.arrayContaining(Object.values(inputs)),
1250+
)
1251+
})
1252+
1253+
test('adds the default index.html input to server.fs.allow', async () => {
1254+
const config = await resolveConfig({}, 'serve')
1255+
1256+
expect(config.server.fs.allow).toContain(resolveInputFromRoot('index.html'))
1257+
})
1258+
12381259
test('reserves glob characters in input', async () => {
12391260
const cases: { name: string; input: UserConfig['input'] }[] = [
12401261
{ name: 'wildcard', input: 'src/*.ts' },
@@ -2026,6 +2047,7 @@ describe('resolveServerOptions', () => {
20262047
const resolved = await resolveServerOptions(
20272048
'/root',
20282049
{ allowedHosts: [] },
2050+
undefined,
20292051
logger,
20302052
)
20312053
expect(resolved.allowedHosts).toEqual(['example.com'])
@@ -2037,6 +2059,7 @@ describe('resolveServerOptions', () => {
20372059
const resolved = await resolveServerOptions(
20382060
'/root',
20392061
{ allowedHosts: [] },
2062+
undefined,
20402063
logger,
20412064
)
20422065
expect(resolved.allowedHosts).toEqual([
@@ -2052,6 +2075,7 @@ describe('resolveServerOptions', () => {
20522075
const resolved = await resolveServerOptions(
20532076
'/root',
20542077
{ allowedHosts: [] },
2078+
undefined,
20552079
logger,
20562080
)
20572081
expect(resolved.allowedHosts).toEqual([
@@ -2067,6 +2091,7 @@ describe('resolveServerOptions', () => {
20672091
const resolved = await resolveServerOptions(
20682092
'/root',
20692093
{ allowedHosts: [] },
2094+
undefined,
20702095
logger,
20712096
)
20722097
expect(resolved.allowedHosts).toEqual(['example.com', 'test.com'])
@@ -2077,6 +2102,7 @@ describe('resolveServerOptions', () => {
20772102
const resolved = await resolveServerOptions(
20782103
'/root',
20792104
{ allowedHosts: ['existing.com'] },
2105+
undefined,
20802106
logger,
20812107
)
20822108
expect(resolved.allowedHosts).toEqual([
@@ -2091,6 +2117,7 @@ describe('resolveServerOptions', () => {
20912117
const resolved = await resolveServerOptions(
20922118
'/root',
20932119
{ allowedHosts: true },
2120+
undefined,
20942121
logger,
20952122
)
20962123
expect(resolved.allowedHosts).toBe(true)
@@ -2103,6 +2130,7 @@ describe('resolveServerOptions', () => {
21032130
const resolved = await resolveServerOptions(
21042131
'/root',
21052132
{ allowedHosts: [] },
2133+
undefined,
21062134
logger,
21072135
)
21082136
expect(resolved.allowedHosts).toEqual([])

‎packages/vite/src/node/config.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1865,7 +1865,13 @@ export async function resolveConfig(
18651865
)
18661866
: ''
18671867

1868-
const server = await resolveServerOptions(resolvedRoot, config.server, logger)
1868+
const input = resolveInput(config.input, resolvedRoot)
1869+
const server = await resolveServerOptions(
1870+
resolvedRoot,
1871+
config.server,
1872+
input,
1873+
logger,
1874+
)
18691875

18701876
const builder = resolveBuilderOptions(config.builder)
18711877

@@ -2109,7 +2115,7 @@ export async function resolveConfig(
21092115

21102116
ssr,
21112117

2112-
input: resolveInput(config.input, resolvedRoot),
2118+
input,
21132119
optimizeDeps: backwardCompatibleOptimizeDeps,
21142120
resolve: resolvedDefaultResolve,
21152121
dev: resolvedDevEnvironmentOptions,

‎packages/vite/src/node/server/index.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import chokidar from 'chokidar'
1414
import launchEditorMiddleware from 'launch-editor-middleware'
1515
import { determineAgent } from '@vercel/detect-agent'
1616
import { disableCache } from '@voidzero-dev/vite-task-client'
17-
import type { SourceMap } from 'rolldown'
17+
import type { InputOption, SourceMap } from 'rolldown'
1818
import type { ModuleRunner } from 'vite/module-runner'
1919
import type { FSWatcher, WatchOptions } from '#dep-types/chokidar'
2020
import type { Connect } from '#dep-types/connect'
@@ -1214,6 +1214,7 @@ const RESERVED_ALLOWED_HOSTS_CHARACTERS_RE = /[\\"']/
12141214
export async function resolveServerOptions(
12151215
root: string,
12161216
raw: ServerOptions | undefined,
1217+
input: InputOption | undefined,
12171218
logger: Logger,
12181219
): Promise<ResolvedServerOptions> {
12191220
const _server = mergeWithDefaults(
@@ -1243,6 +1244,7 @@ export async function resolveServerOptions(
12431244
}
12441245

12451246
let allowDirs = server.fs.allow
1247+
allowDirs.push(...getInputPaths(root, input))
12461248

12471249
const cwd = searchForPackageRoot(root)
12481250
if (process.versions.pnp) {
@@ -1343,6 +1345,12 @@ export async function resolveServerOptions(
13431345
return server
13441346
}
13451347

1348+
function getInputPaths(root: string, input: ResolvedConfig['input']): string[] {
1349+
if (input == null) return [path.resolve(root, 'index.html')]
1350+
if (typeof input === 'string') return [input]
1351+
return Array.isArray(input) ? input : Object.values(input)
1352+
}
1353+
13461354
async function restartServer(server: ViteDevServer) {
13471355
global.__vite_start_time = performance.now()
13481356

0 commit comments

Comments
 (0)