fix: loading

This commit is contained in:
hypercross 2026-02-26 14:40:02 +08:00
parent e262a83aac
commit 5746249a47
2 changed files with 24 additions and 16 deletions

View File

@ -16,6 +16,13 @@ export default defineConfig({
plugins: [tailwindcss()], plugins: [tailwindcss()],
}, },
}, },
bundlerChain: (chain) => {
// 为 .md 文件配置 raw-loader支持 webpackContext 加载
chain.module
.rule('md-raw')
.test(/\.md$/)
.type('asset/source');
},
}, },
html: { html: {
template: './src/index.html', template: './src/index.html',

View File

@ -42,23 +42,24 @@ export function ensureIndexLoaded(): Promise<void> {
// CLI 索引不可用时尝试 dev 环境 // CLI 索引不可用时尝试 dev 环境
} }
// Dev 环境:使用 import.meta.glob 加载 // Dev 环境:使用 import.meta.webpackContext + raw loader 加载
if (typeof import.meta !== "undefined" && import.meta.glob) {
try { try {
// @ts-ignore - 只加载 .md 文件 const context = import.meta.webpackContext("../../content", {
const modules = import.meta.glob("../../content/**/*.md", { recursive: true,
as: "raw", regExp: /\.md$/,
eager: true,
}); });
const keys = context.keys();
const index: Record<string, string> = {}; const index: Record<string, string> = {};
for (const [path, content] of Object.entries(modules)) { for (const key of keys) {
const normalizedPath = path.replace("/src/", "/"); // context 返回的是模块,需要访问其 default 导出raw-loader 处理后的内容)
index[normalizedPath] = content as string; const module = context(key) as { default?: string } | string;
const content = typeof module === "string" ? module : module.default ?? "";
const normalizedPath = "/content" + key.slice(1);
index[normalizedPath] = content;
} }
dataIndex = { ...dataIndex, ...index }; dataIndex = { ...dataIndex, ...index };
} catch (e) { } catch (e) {
// glob 不可用时忽略 // webpackContext 不可用时忽略
}
} }
})(); })();