Skip to content
This repository was archived by the owner on Nov 1, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ title: 项目配置

启用文件系统路由并配置,默认为 `undefined`。具体格式参考 [简易模式 & 文件系统路由](./file-route)。

## static: ServeOptions | boolean

构建产物的静态托管配置,默认为 `true`,表示使用内置的 `@midwayjs/serve` 组件托管静态文件。设置为 `false` 时,需要自行托管构建产物。
如果像使用内置的组件托管,并配置更多的托管参数,可以传入 `ServeOptions` 对象, 其值可以参考 [midway 文档](https://midwayjs.org/docs/extensions/static_file#%E5%8F%AF%E7%94%A8%E9%85%8D%E7%BD%AE)。

## dev.include: string[]

配置全栈应用下,如果请求的 URL 中包含 `dev.include` 的关键词,则请求将由后端 Server 处理。
Expand Down
13 changes: 10 additions & 3 deletions packages/hooks-kit/src/command/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { getProjectRoot } from '@midwayjs/hooks-internal'
import { KitConfig, resolveConfig } from '../config'
import consola from 'consola'
import { register } from '@midwayjs/esrun'
import { ServeOptions } from '@midwayjs/serve'

type BuildOptions = {
outDir: string
Expand Down Expand Up @@ -58,7 +59,8 @@ export function setupBuildCommand(cli: CAC) {
consola.info(
'Static files and html will be automatically served from server'
)
createRender(join(root, outDir))
const options = typeof userConfig.static === 'object' ? userConfig.static : undefined
createRender(join(root, outDir), options)
} else {
consola.info(
'Serve static disabled, you should serve static files manually'
Expand Down Expand Up @@ -111,8 +113,13 @@ async function executePromise(promise: Promise<any> | any) {
}
}

function createRender(dist: string) {
const code = `exports.default = require('@midwayjs/serve').Serve('/*', { dir: '_client', isKit: true });`
function createRender(dist: string, options?: ServeOptions) {
options = {
dir: '_client',
isKit: true,
...options
}
const code = `exports.default = require('@midwayjs/serve').Serve('/*', ${JSON.stringify(options)});`
const file = join(dist, '_serve/index.js')
fs.ensureFileSync(file)
fs.writeFileSync(file, code, 'utf-8')
Expand Down
3 changes: 2 additions & 1 deletion packages/hooks-kit/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import type { UserConfig } from '@midwayjs/hooks-internal'
import { getConfig } from '@midwayjs/hooks-internal'
import { ServeOptions } from '@midwayjs/serve'
import type { UserConfig as ViteConfig } from 'vite'

export interface KitConfig extends Omit<UserConfig, 'source'> {
[key: string]: any
vite?: ViteConfig
static?: boolean
static?: boolean | ServeOptions
}

export function defineConfig(config: KitConfig): KitConfig {
Expand Down