From 48f871e45e8c9ff8fb8d946687d9e5e300c44644 Mon Sep 17 00:00:00 2001 From: lwj <674416404@qq.com> Date: Wed, 26 Nov 2025 21:36:08 +0800 Subject: [PATCH] chore(preview-site): fix fetch changelog.json 404 Not Found --- .../site/plugins/changelog-to-json/index.ts | 21 ++++++++----------- .../site/plugins/changelog-to-json/index.ts | 20 ++++++++---------- 2 files changed, 18 insertions(+), 23 deletions(-) diff --git a/packages/tdesign-miniprogram-chat/site/plugins/changelog-to-json/index.ts b/packages/tdesign-miniprogram-chat/site/plugins/changelog-to-json/index.ts index 4d257d08d..adac5f5dd 100644 --- a/packages/tdesign-miniprogram-chat/site/plugins/changelog-to-json/index.ts +++ b/packages/tdesign-miniprogram-chat/site/plugins/changelog-to-json/index.ts @@ -2,7 +2,7 @@ import { promises } from 'fs'; import path, { dirname } from 'path'; import { fileURLToPath } from 'url'; -import type { ViteDevServer } from 'vite'; +import type { ResolvedConfig, ViteDevServer } from 'vite'; import generateChangelogJson from '../../../../common/docs/plugins/changelog-to-json'; const __dirname = dirname(fileURLToPath(import.meta.url)); @@ -11,8 +11,12 @@ const outputPath = path.resolve(__dirname, '../../../site/dist/changelog.json'); const changelogPath = path.resolve(__dirname, '../../../../tdesign-miniprogram/CHANGELOG.md'); export default function changelog2Json() { + let config: ResolvedConfig; return { name: 'changelog-to-json', + configResolved(resolvedConfig: ResolvedConfig) { + config = resolvedConfig; + }, configureServer(server: ViteDevServer) { // 开发模式时拦截请求 server.middlewares.use('/changelog.json', async (_, res) => { @@ -21,18 +25,11 @@ export default function changelog2Json() { res.end(JSON.stringify(json)); }); }, - async closeBundle() { + async closeBundle(error?: Error) { + if (error) return; // 生产构建时写入物理文件 - if (process.env.NODE_ENV === 'production') { - const json = await generateChangelogJson(changelogPath, 'chat'); - const dir = path.dirname(outputPath); - try { - await promises.access(dir); - } catch (error) { - if (error.code === 'ENOENT') { - await promises.mkdir(dir, { recursive: true }); - } - } + if (config.env.PROD || config.env.MODE === 'preview') { + const json = await generateChangelogJson(changelogPath, 'mobile'); await promises.writeFile(outputPath, JSON.stringify(json)); } }, diff --git a/packages/tdesign-miniprogram/site/plugins/changelog-to-json/index.ts b/packages/tdesign-miniprogram/site/plugins/changelog-to-json/index.ts index 1e8518691..79c3596df 100644 --- a/packages/tdesign-miniprogram/site/plugins/changelog-to-json/index.ts +++ b/packages/tdesign-miniprogram/site/plugins/changelog-to-json/index.ts @@ -2,7 +2,8 @@ import { promises } from 'fs'; import path, { dirname } from 'path'; import { fileURLToPath } from 'url'; -import type { ViteDevServer } from 'vite'; +import type { ResolvedConfig, ViteDevServer } from 'vite'; + import generateChangelogJson from '../../../../common/docs/plugins/changelog-to-json'; const __dirname = dirname(fileURLToPath(import.meta.url)); @@ -11,8 +12,12 @@ const outputPath = path.resolve(__dirname, '../../../site/dist/changelog.json'); const changelogPath = path.resolve(__dirname, '../../../CHANGELOG.md'); export default function changelog2Json() { + let config: ResolvedConfig; return { name: 'changelog-to-json', + configResolved(resolvedConfig: ResolvedConfig) { + config = resolvedConfig; + }, configureServer(server: ViteDevServer) { // 开发模式时拦截请求 server.middlewares.use('/changelog.json', async (_, res) => { @@ -21,18 +26,11 @@ export default function changelog2Json() { res.end(JSON.stringify(json)); }); }, - async closeBundle() { + async closeBundle(error?: Error) { + if (error) return; // 生产构建时写入物理文件 - if (process.env.NODE_ENV === 'production') { + if (config.env.PROD || config.env.MODE === 'preview') { const json = await generateChangelogJson(changelogPath, 'mobile'); - const dir = path.dirname(outputPath); - try { - await promises.access(dir); - } catch (error) { - if (error.code === 'ENOENT') { - await promises.mkdir(dir, { recursive: true }); - } - } await promises.writeFile(outputPath, JSON.stringify(json)); } },