配置

Methanol 将按顺序尝试加载以下配置文件:

methanol.config.{js,mjs,cjs,ts,jsx,tsx,mts,cts}

若未发现配置文件,Methanol 将基于当前目录推断并应用默认设置。

配置结构

配置文件需导出一个函数,该函数接收构建上下文并返回配置对象。

export default ({ mode, root, HTMLRenderer }) => ({
	// config values
})

上下文参数说明:

核心配置属性

root

项目根目录,默认为当前工作目录(CWD)。

site

站点元信息(供模板使用):

注意:

pagesDir

MDX/MD 源文件目录(路由根源)。默认为 pages;若 pages/ 不存在,将自动回退查找 docs

componentsDir

JSX/TSX 组件目录。默认为 components

若显式指定了 pagesDircomponentsDir(包括 CLI 参数指定),当目录不存在时将抛出错误。

publicDir

静态资源目录(映射至站点根路径,如 public/foo.png/foo.png)。默认为 public

Methanol 亦支持主题资源(theme.publicDir)。构建时将执行合并策略(主题资源垫底,用户资源覆盖),同名文件以用户资源为准。

执行合并操作时,临时目录将生成于 node_modules/.methanol/assets(若无 node_modules/,则生成于 {pagesDir}/.methanol/assets)。

设置 publicDir: false 可彻底禁用 Public 资源功能(包括主题资源)。

distDir

生产构建输出目录。默认为 dist

buildDir

中间构建目录(用于 emitIntermediate)。默认为 build

intermediateDir

中间态 HTML 输出目录(仅 build 时生效)。

emitIntermediate

布尔值。为 true 时在构建期间写入中间 HTML 到 build/

theme

主题深度配置。详见 主题(进阶)

关键字段:

mdx

自定义 MDX 配置函数。接收 { mode, root } 并返回配置对象。

vite

自定义 Vite 配置函数。接收 { command, mode, root, isPreview }

preBuild / preBundle / postBundle / postBuild

构建流程扩展钩子(Hooks)。

Hook 上下文:

preBundle / postBundle / postBuild 额外参数:

示例:

export default () => ({
	preBuild: ({ data, site }) => {
		data.startedAt = Date.now()
		console.log('Building', site.name)
	},
	preBundle: ({ pages }) => {
		console.log('Rendered pages:', pages.length)
	},
	postBuild: ({ data, pages }) => {
		console.log('Duration (ms):', Date.now() - data.startedAt)
		console.log('Pages:', pages.length)
	}
})

pagefind

启用或配置 Pagefind 搜索。默认值:false

pagefind: false
// or
pagefind: { enabled: true }
// or
pagefind: {
	enabled: true,
	excerptLength: 30,
	build: {
		outputSubdir: 'pagefind',
		verbose: true
	}
}

说明:

starryNight

代码高亮配置(基于 rehype-starry-night)。默认值:false

starryNight: false
// or
starryNight: true
// or
starryNight: {
	// options forwarded to rehype-starry-night
}

gfm

GitHub Flavored Markdown (GFM) 开关。默认值:true

开启后,Methanol 将支持标准 GFM 语法特性(表格、任务列表、删除线、脚注、自动链接等)。

gfm: true
// or
gfm: false

CLI 覆盖

命令行参数优先级高于配置文件:

示例

import tailwindcss from '@tailwindcss/vite'

export default ({ mode }) => ({
	site: { name: 'My Docs' },
	theme: {
		root: '.',
		template: ({ PageContent, ExtraHead, ctx, withBase, HTMLRenderer, components }) => (
			<>
				{HTMLRenderer.rawHTML`<!doctype html>`}
				<html lang="en">
					<head>
						<meta charset="UTF-8" />
						<meta name="viewport" content="width=device-width" />
						<ExtraHead />
						<title>{ctx.page.title || ctx.site.name}</title>
						<link rel="icon" href="/favicon.png" />
					</head>
					<body>
						{components.Callout ? <components.Callout>Hi</components.Callout> : null}
						<PageContent />
					</body>
				</html>
			</>
		),
		components: {
			Callout: ({ children }) => <div class="callout">{children}</div>
		}
	},
	mdx: () => ({
		development: mode !== 'production'
	}),
	vite: () => ({
		server: { port: 5173 },
		plugins: [tailwindcss()]
	})
})