docs: clarify dev environment index loading behavior
Update file-index.ts documentation and implementation to specify that webpackContext loading is restricted to development mode. This ensures the code block is tree-shaken during production builds.
This commit is contained in:
parent
e0c3b8f4a0
commit
9a5ee695c2
|
|
@ -2,7 +2,7 @@
|
||||||
* 文件索引管理器
|
* 文件索引管理器
|
||||||
* 支持多种文件索引加载方式:
|
* 支持多种文件索引加载方式:
|
||||||
* 1. CLI 环境:从 /__CONTENT_INDEX.json 加载
|
* 1. CLI 环境:从 /__CONTENT_INDEX.json 加载
|
||||||
* 2. Dev 环境:使用 webpackContext 仅加载 .md 文件
|
* 2. Dev 环境:使用 webpackContext 仅加载 .md 文件(仅在 dev 构建中生效)
|
||||||
* 3. 浏览器环境:用户选择本地文件夹,扫描文件索引
|
* 3. 浏览器环境:用户选择本地文件夹,扫描文件索引
|
||||||
* - 支持 IndexedDB 持久化目录句柄,刷新页面无需重新选择
|
* - 支持 IndexedDB 持久化目录句柄,刷新页面无需重新选择
|
||||||
*/
|
*/
|
||||||
|
|
@ -25,7 +25,7 @@ let activeDirHandle: FileSystemDirectoryHandle | null = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 加载文件索引(只加载一次)
|
* 加载文件索引(只加载一次)
|
||||||
* 尝试顺序:CLI JSON → webpack context → 已持久化的目录句柄
|
* 尝试顺序:CLI JSON → webpack context (dev only) → 已持久化的目录句柄
|
||||||
*/
|
*/
|
||||||
function ensureIndexLoaded(): Promise<void> {
|
function ensureIndexLoaded(): Promise<void> {
|
||||||
if (indexLoadPromise) return indexLoadPromise;
|
if (indexLoadPromise) return indexLoadPromise;
|
||||||
|
|
@ -44,25 +44,28 @@ function ensureIndexLoaded(): Promise<void> {
|
||||||
// CLI 索引不可用时尝试下一个策略
|
// CLI 索引不可用时尝试下一个策略
|
||||||
}
|
}
|
||||||
|
|
||||||
// 策略 2: Dev 环境 — webpackContext + raw loader
|
// 策略 2: Dev 环境 — webpackContext + raw loader(仅 dev 构建时生效)
|
||||||
try {
|
// 生产构建时 process.env.NODE_ENV 被替换为 'production',整个分支会被 tree-shake 掉
|
||||||
const context = import.meta.webpackContext("../../content", {
|
if (process.env.NODE_ENV === "development") {
|
||||||
recursive: true,
|
try {
|
||||||
regExp: /\.md|\.yarn|\.csv$/i,
|
const context = import.meta.webpackContext("../../content", {
|
||||||
});
|
recursive: true,
|
||||||
const keys = context.keys();
|
regExp: /\.md|\.yarn|\.csv$/i,
|
||||||
const index: FileIndex = {};
|
});
|
||||||
for (const key of keys) {
|
const keys = context.keys();
|
||||||
const module = context(key) as { default?: string } | string;
|
const index: FileIndex = {};
|
||||||
const content =
|
for (const key of keys) {
|
||||||
typeof module === "string" ? module : (module.default ?? "");
|
const module = context(key) as { default?: string } | string;
|
||||||
const normalizedPath = "/content" + key.slice(1);
|
const content =
|
||||||
index[normalizedPath] = content;
|
typeof module === "string" ? module : (module.default ?? "");
|
||||||
|
const normalizedPath = "/content" + key.slice(1);
|
||||||
|
index[normalizedPath] = content;
|
||||||
|
}
|
||||||
|
fileIndex = { ...fileIndex, ...index };
|
||||||
|
activeSource = "webpack";
|
||||||
|
} catch (e) {
|
||||||
|
// webpackContext 不可用时忽略
|
||||||
}
|
}
|
||||||
fileIndex = { ...fileIndex, ...index };
|
|
||||||
activeSource = "webpack";
|
|
||||||
} catch (e) {
|
|
||||||
// webpackContext 不可用时忽略
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 策略 3: 浏览器 — 尝试从 IndexedDB 恢复保存的目录句柄
|
// 策略 3: 浏览器 — 尝试从 IndexedDB 恢复保存的目录句柄
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue