Skip to content

Commit c6018b6

Browse files
authored
feat(site): add a changelog for each component page (#793)
* feat(site): add a changelog for each component page * chore: update NODE_ENV * chore: fix cjs config * chore: fix build * chore: fix mobile preview * chore: fix mobile preview * chore: adjust code * chore: code style
1 parent dcced6f commit c6018b6

File tree

8 files changed

+45
-7
lines changed

8 files changed

+45
-7
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"site:build": "cd site && vite build",
3333
"site:serve": "cd site && vite preview",
3434
"site:intranet": "cd site && vite build --mode intranet",
35-
"site:preview": "cd site && cross-env NODE_ENV=development vite build --mode preview",
35+
"site:preview": "cd site && vite build --mode preview",
3636
"postsite:preview": "cp _site/index.html _site/404.html",
3737
"lint": "eslint --ext .ts,.tsx ./ --max-warnings 0",
3838
"lint:fix": "eslint --ext .ts,.tsx ./ --max-warnings 0 --fix",

site/.env.preview

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
NODE_ENV=development
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ import { defineConfig } from 'vite';
44
import replace from '@rollup/plugin-replace';
55
import react from '@vitejs/plugin-react';
66
import { VitePWA } from 'vite-plugin-pwa';
7-
import tdocPlugin from './plugin-tdoc';
87
import pwaConfig from './pwaConfig';
98

9+
import changelog2Json from './web/plugins/changelog-to-json';
10+
import tdocPlugin from './web/plugins/plugin-tdoc';
11+
1012
const publicPathMap = {
1113
preview: '/',
1214
intranet: '/mobile-react/',
@@ -63,6 +65,7 @@ export default ({ mode }) =>
6365
plugins: [
6466
react(),
6567
tdocPlugin(),
68+
changelog2Json(),
6669
VitePWA(pwaConfig),
6770
disableTreeShakingPlugin(['style/']),
6871
replace({ __DATE__: new Date().toISOString() }),
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { promises } from 'fs';
2+
import path from 'path';
3+
4+
import generateChangelogJson from '../../../../src/_common/docs/plugins/changelog-to-json';
5+
6+
const outputPath = path.resolve(__dirname, '../../../../_site/changelog.json');
7+
const changelogPath = path.resolve(__dirname, '../../../../CHANGELOG.md');
8+
9+
export default function changelog2Json() {
10+
let config;
11+
return {
12+
name: 'changelog-to-json',
13+
configResolved(resolvedConfig) {
14+
config = resolvedConfig;
15+
},
16+
configureServer(server) {
17+
// 开发模式时拦截请求
18+
server.middlewares.use('/changelog.json', async (_, res) => {
19+
const json = await generateChangelogJson(changelogPath, 'mobile');
20+
res.setHeader('Content-Type', 'application/json');
21+
res.end(JSON.stringify(json));
22+
});
23+
},
24+
async closeBundle() {
25+
// 生产构建时写入物理文件
26+
if (config.env.PROD || config.env.MODE === 'preview') {
27+
const json = await generateChangelogJson(changelogPath, 'mobile');
28+
await promises.writeFile(outputPath, JSON.stringify(json));
29+
}
30+
},
31+
};
32+
}

site/plugin-tdoc/md-to-react.js renamed to site/web/plugins/plugin-tdoc/md-to-react.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ import path from 'path';
44
import matter from 'gray-matter';
55
import camelCase from 'camelcase';
66

7-
import testCoverage from '../test-coverage';
7+
import testCoverage from '../../../test-coverage';
88

99
import { transformSync } from '@babel/core';
10+
import babelPresetReact from '@babel/preset-react';
1011

1112
export default function mdToReact(options) {
1213
const mdSegment = customRender(options);
@@ -152,7 +153,7 @@ export default function mdToReact(options) {
152153
generatorOpts: {
153154
decoratorsBeforeExport: true,
154155
},
155-
presets: [require('@babel/preset-react')],
156+
presets: [babelPresetReact],
156157
});
157158

158159
return { code: result.code, map: result.map };
@@ -231,12 +232,13 @@ function customRender({ source, file, md }) {
231232
}
232233

233234
// 移动端路由地址
235+
// console.log('process.env', process.env);
234236
const prefix = process.env.NODE_ENV === 'development' ? `/mobile.html` : `/mobile-react/mobile.html`;
235237
mdSegment.mobileUrl = `${prefix}#/${componentName}`;
236238

237239
// 设计指南内容 不展示 design Tab 则不解析
238240
if (pageData.isComponent && pageData.tdDocTabs.some((item) => item.tab === 'design')) {
239-
const designDocPath = path.resolve(__dirname, `../../src/_common/docs/mobile/design/${componentName}.md`);
241+
const designDocPath = path.resolve(__dirname, `../../../../src/_common/docs/mobile/design/${componentName}.md`);
240242

241243
if (fs.existsSync(designDocPath)) {
242244
const designMd = fs.readFileSync(designDocPath, 'utf-8');

site/plugin-tdoc/transforms.js renamed to site/web/plugins/plugin-tdoc/transforms.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ export default {
1717

1818
// 统一换成 common 文档内容
1919
if (fileName && source.includes(':: BASE_DOC ::')) {
20-
const localeDocPath = path.resolve(__dirname, `../../src/_common/docs/mobile/api/${fileName}`);
21-
const defaultDocPath = path.resolve(__dirname, `../../src/_common/docs/mobile/api/${componentName}.md`);
20+
const localeDocPath = path.resolve(__dirname, `../../../../src/_common/docs/mobile/api/${fileName}`);
21+
const defaultDocPath = path.resolve(__dirname, `../../../../src/_common/docs/mobile/api/${componentName}.md`);
2222

2323
let baseDoc = '';
2424
if (fs.existsSync(localeDocPath)) {

0 commit comments

Comments
 (0)