feat: add origin config option (#582)

Co-authored-by: Marco Pasqualetti <marco.pasqualetti@live.com>
This commit is contained in:
pveierland
2025-02-28 01:25:03 +08:00
committed by GitHub
parent 5f6bad637c
commit 1ba94238ed
10 changed files with 114 additions and 19 deletions
@@ -1,4 +1,4 @@
import fs from 'fs/promises'
import fs from 'node:fs/promises'
import { beforeEach, describe, expect, it, vitest } from 'vitest'
import react from '@vitejs/plugin-react-swc'
@@ -12,9 +12,9 @@ describe('createJsonConfig', () => {
writeFileSpy.mockClear()
})
const sampleConfig = { server: { host: 'h', port: 1 } }
it('should process config with only server property', async () => {
const sampleConfig = { server: { host: 'h', origin: null, port: 1 } }
await createJsonConfig(sampleConfig)
expect(writeFileSpy).toHaveBeenCalledWith(
@@ -25,6 +25,8 @@ describe('createJsonConfig', () => {
})
it('should process config with plugins', async () => {
const sampleConfig = { server: { host: 'h', origin: null, port: 1 } }
await createJsonConfig({ ...sampleConfig, vite: { plugins: [react()] } })
expect(writeFileSpy).toHaveBeenCalledWith(
@@ -33,4 +35,16 @@ describe('createJsonConfig', () => {
expect.any(String),
)
})
it('should process config with only server property including origin', async () => {
const sampleConfig = { server: { host: 'h', origin: 'o', port: 1 } }
await createJsonConfig(sampleConfig)
expect(writeFileSpy).toHaveBeenCalledWith(
expect.any(String),
expect.stringContaining(JSON.stringify(sampleConfig)),
expect.any(String),
)
})
})
@@ -15,7 +15,11 @@ describe('normalizeConfig', () => {
const config: TuonoConfig = {}
expect(normalizeConfig(config)).toStrictEqual({
server: { host: 'localhost', port: 3000 },
server: {
host: 'localhost',
origin: null,
port: 3000,
},
vite: {
alias: undefined,
css: undefined,
@@ -28,7 +32,11 @@ describe('normalizeConfig', () => {
it('should return an empty config if invalid values are provided', () => {
// @ts-expect-error testing invalid config
expect(normalizeConfig({ invalid: true })).toStrictEqual({
server: { host: 'localhost', port: 3000 },
server: {
host: 'localhost',
origin: null,
port: 3000,
},
vite: {
alias: undefined,
css: undefined,
@@ -55,6 +63,28 @@ describe('normalizeConfig', () => {
})
})
describe('server - origin', () => {
it('should assign the origin defined by the user', () => {
const config: TuonoConfig = {
server: {
host: '0.0.0.0',
origin: 'https://tuono.localhost',
port: 8080,
},
}
expect(normalizeConfig(config)).toStrictEqual(
expect.objectContaining({
server: expect.objectContaining({
host: '0.0.0.0',
origin: 'https://tuono.localhost',
port: 8080,
}) as unknown,
}),
)
})
})
describe('vite - alias', () => {
it('should not modify alias pointing to packages', () => {
const libraryName = '@tabler/icons-react'
@@ -71,6 +71,7 @@ export const normalizeConfig = (config: TuonoConfig): InternalTuonoConfig => {
return {
server: {
host: config.server?.host ?? 'localhost',
origin: config.server?.origin ?? null,
port: config.server?.port ?? 3000,
},
vite: {
+1
View File
@@ -11,6 +11,7 @@ import type {
export interface TuonoConfig {
server?: {
host?: string
origin?: string | null
port?: number
}
vite?: {
+6 -3
View File
@@ -3,13 +3,16 @@ import type { JSX } from 'react'
import { useTuonoContextServerPayload } from './TuonoContext'
const VITE_PROXY_PATH = '/vite-server'
const DEFAULT_SERVER_CONFIG = { host: 'localhost', port: 3000 }
const DEFAULT_SERVER_CONFIG = { host: 'localhost', origin: null, port: 3000 }
export const DevResources = (): JSX.Element => {
const { devServerConfig } = useTuonoContextServerPayload()
const { host, port } = devServerConfig ?? DEFAULT_SERVER_CONFIG
const { host, origin, port } = devServerConfig ?? DEFAULT_SERVER_CONFIG
const viteBaseUrl = `http://${host}:${port}${VITE_PROXY_PATH}`
const viteBaseUrl =
origin != null
? `${origin}${VITE_PROXY_PATH}`
: `http://${host}:${port}${VITE_PROXY_PATH}`
return (
<>
+1
View File
@@ -25,6 +25,7 @@ export interface ServerPayload<TData = unknown> {
/** Available only on 'Dev' mode */
devServerConfig?: {
port: number
origin: string | null
host: string
}
}