@@ -2,7 +2,7 @@ import { promises } from 'fs';
22import path , { dirname } from 'path' ;
33import { fileURLToPath } from 'url' ;
44
5- import type { ViteDevServer } from 'vite' ;
5+ import type { ResolvedConfig , ViteDevServer } from 'vite' ;
66import generateChangelogJson from '../../../../common/docs/plugins/changelog-to-json' ;
77
88const __dirname = dirname ( fileURLToPath ( import . meta. url ) ) ;
@@ -11,8 +11,12 @@ const outputPath = path.resolve(__dirname, '../../../site/dist/changelog.json');
1111const changelogPath = path . resolve ( __dirname , '../../../../tdesign-miniprogram/CHANGELOG.md' ) ;
1212
1313export default function changelog2Json ( ) {
14+ let config : ResolvedConfig ;
1415 return {
1516 name : 'changelog-to-json' ,
17+ configResolved ( resolvedConfig : ResolvedConfig ) {
18+ config = resolvedConfig ;
19+ } ,
1620 configureServer ( server : ViteDevServer ) {
1721 // 开发模式时拦截请求
1822 server . middlewares . use ( '/changelog.json' , async ( _ , res ) => {
@@ -21,18 +25,11 @@ export default function changelog2Json() {
2125 res . end ( JSON . stringify ( json ) ) ;
2226 } ) ;
2327 } ,
24- async closeBundle ( ) {
28+ async closeBundle ( error ?: Error ) {
29+ if ( error ) return ;
2530 // 生产构建时写入物理文件
26- if ( process . env . NODE_ENV === 'production' ) {
27- const json = await generateChangelogJson ( changelogPath , 'chat' ) ;
28- const dir = path . dirname ( outputPath ) ;
29- try {
30- await promises . access ( dir ) ;
31- } catch ( error ) {
32- if ( error . code === 'ENOENT' ) {
33- await promises . mkdir ( dir , { recursive : true } ) ;
34- }
35- }
31+ if ( config . env . PROD || config . env . MODE === 'preview' ) {
32+ const json = await generateChangelogJson ( changelogPath , 'mobile' ) ;
3633 await promises . writeFile ( outputPath , JSON . stringify ( json ) ) ;
3734 }
3835 } ,
0 commit comments