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()],
},
},
bundlerChain: (chain) => {
// 为 .md 文件配置 raw-loader支持 webpackContext 加载
chain.module
.rule('md-raw')
.test(/\.md$/)
.type('asset/source');
},
},
html: {
template: './src/index.html',

View File

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