refactor: replace webpack context loading with dev server proxy

Remove the webpack-based file indexing strategy in favor of a proxy
approach. The dev server now proxies content requests to a local CLI
server, simplifying the data loading logic and removing webpack-specific
types and loaders.
This commit is contained in:
hypercross 2026-07-11 09:19:41 +08:00
parent 52563fd3ef
commit c661d2a1d2
3 changed files with 22 additions and 61 deletions

View File

@ -18,15 +18,6 @@ export default defineConfig({
},
},
rspack: {
module: {
rules: [
{
test: /\.md|\.yarn|\.csv$/,
exclude: /\.schema\.csv$/,
type: "asset/source",
},
],
},
plugins: [
process.env.ANALYZE ? new BundleAnalyzerPlugin() : undefined,
].filter(Boolean),
@ -40,13 +31,18 @@ export default defineConfig({
index: "./src/main.tsx",
},
},
server: {
proxy: {
// Proxy content API requests to the CLI dev server (npm run tttk)
"/__CONTENT_INDEX.json": "http://localhost:3000",
"/__COMPLETIONS.json": "http://localhost:3000",
"/content": "http://localhost:3000",
"/journal": "http://localhost:3000",
},
},
output: {
distPath: {
root: "dist/web",
},
copy:
process.env.NODE_ENV === "development"
? [{ from: "./content", to: "content" }]
: [],
},
});

View File

@ -1,10 +1,14 @@
/**
*
*
* 1. CLI /__CONTENT_INDEX.json
* 2. Dev 使 webpackContext .md dev
* 3.
*
* 1. CLI / /__CONTENT_INDEX.json
* 2.
* - IndexedDB
*
* Dev CLI rsbuild
* > npm run tttk serve ./content
* > npm run dev
* rsbuild /__CONTENT_INDEX.json/__COMPLETIONS.json/content/ CLI
*/
import {
@ -18,20 +22,20 @@ type FileIndex = Record<string, string>;
let fileIndex: FileIndex | null = null;
let indexLoadPromise: Promise<void> | null = null;
let activeSource: "cli" | "webpack" | "folder" | null = null;
let activeSource: "cli" | "folder" | null = null;
/** Currently active directory handle (if folder source) */
let activeDirHandle: FileSystemDirectoryHandle | null = null;
/**
*
* CLI JSON webpack context (dev only)
* CLI JSON
*/
function ensureIndexLoaded(): Promise<void> {
if (indexLoadPromise) return indexLoadPromise;
indexLoadPromise = (async () => {
// 策略 1: CLI 环境 — 从 /__CONTENT_INDEX.json 加载
// 策略 1: CLI / 代理环境 — 从 /__CONTENT_INDEX.json 加载
try {
const response = await fetch("/__CONTENT_INDEX.json");
if (response.ok) {
@ -44,31 +48,7 @@ function ensureIndexLoaded(): Promise<void> {
// CLI 索引不可用时尝试下一个策略
}
// 策略 2: Dev 环境 — webpackContext + raw loader仅 dev 构建时生效)
// 生产构建时 process.env.NODE_ENV 被替换为 'production',整个分支会被 tree-shake 掉
if (process.env.NODE_ENV === "development") {
try {
const context = import.meta.webpackContext("../../content", {
recursive: true,
regExp: /\.md|\.yarn|\.csv|\.svg$/i,
});
const keys = context.keys();
const index: FileIndex = {};
for (const key of keys) {
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;
}
fileIndex = { ...fileIndex, ...index };
activeSource = "webpack";
} catch (e) {
// webpackContext 不可用时忽略
}
}
// 策略 3: 浏览器 — 尝试从 IndexedDB 恢复保存的目录句柄
// 策略 2: 浏览器 — 尝试从 IndexedDB 恢复保存的目录句柄
if (!activeSource) {
try {
const restored = await restoreSavedHandle();
@ -176,7 +156,7 @@ export async function loadFromUserFolder(): Promise<string[] | null> {
/**
*
* 退 CLI/webpack
* 退 CLI
*/
export async function switchToBuiltInSource(): Promise<void> {
await removeHandle();

15
src/global.d.ts vendored
View File

@ -35,21 +35,6 @@ interface Window {
): Promise<FileSystemDirectoryHandle>;
}
interface WebpackContext {
(path: string): { default?: string } | string;
keys(): string[];
}
interface ImportMeta {
webpackContext(
directory: string,
options: {
recursive?: boolean;
regExp?: RegExp;
},
): WebpackContext;
}
declare module "*.md" {
const content: string;
export default content;