fix: loading
This commit is contained in:
parent
e262a83aac
commit
5746249a47
|
|
@ -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',
|
||||||
|
|
|
||||||
|
|
@ -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 {
|
const context = import.meta.webpackContext("../../content", {
|
||||||
// @ts-ignore - 只加载 .md 文件
|
recursive: true,
|
||||||
const modules = import.meta.glob("../../content/**/*.md", {
|
regExp: /\.md$/,
|
||||||
as: "raw",
|
});
|
||||||
eager: true,
|
const keys = context.keys();
|
||||||
});
|
const index: Record<string, string> = {};
|
||||||
const index: Record<string, string> = {};
|
for (const key of keys) {
|
||||||
for (const [path, content] of Object.entries(modules)) {
|
// context 返回的是模块,需要访问其 default 导出(raw-loader 处理后的内容)
|
||||||
const normalizedPath = path.replace("/src/", "/");
|
const module = context(key) as { default?: string } | string;
|
||||||
index[normalizedPath] = content as string;
|
const content = typeof module === "string" ? module : module.default ?? "";
|
||||||
}
|
const normalizedPath = "/content" + key.slice(1);
|
||||||
dataIndex = { ...dataIndex, ...index };
|
index[normalizedPath] = content;
|
||||||
} catch (e) {
|
|
||||||
// glob 不可用时忽略
|
|
||||||
}
|
}
|
||||||
|
dataIndex = { ...dataIndex, ...index };
|
||||||
|
} catch (e) {
|
||||||
|
// webpackContext 不可用时忽略
|
||||||
}
|
}
|
||||||
})();
|
})();
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue