Skip to content

Commit 45ecfa5

Browse files
committed
feat(builder): 添加版本信息生成功能
在构建过程中自动生成包含项目名称、版本号、构建时间等信息的 version.json 和 version.txt 文件
1 parent 6868e85 commit 45ecfa5

File tree

6 files changed

+93
-1
lines changed

6 files changed

+93
-1
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import type { IPluginAPI } from '@fesjs/shared';
2+
import { existsSync, readFileSync } from 'node:fs';
3+
import { join } from 'node:path';
4+
import process from 'node:process';
5+
6+
export default (api: IPluginAPI) => {
7+
api.modifyBundleConfig((memo: any) => {
8+
const versionPlugin = {
9+
name: 'fes-version-emit',
10+
generateBundle() {
11+
const pkgPath = join(process.cwd(), 'package.json');
12+
let name = '';
13+
let version = '';
14+
if (existsSync(pkgPath)) {
15+
try {
16+
const pkg = JSON.parse(readFileSync(pkgPath, 'utf-8')) || {};
17+
name = pkg.name || '';
18+
version = pkg.version || '';
19+
}
20+
catch {}
21+
}
22+
23+
const info = {
24+
name,
25+
version,
26+
buildTime: new Date().toISOString(),
27+
builder: 'vite',
28+
nodeEnv: process.env.NODE_ENV,
29+
};
30+
31+
(this as any).emitFile({ type: 'asset', fileName: 'version.json', source: `${JSON.stringify(info, null, 2)}\n` });
32+
const txt = `name: ${info.name}\nversion: ${info.version}\nbuildTime: ${info.buildTime}\nbuilder: ${info.builder}\nnodeEnv: ${info.nodeEnv ?? ''}\n`;
33+
(this as any).emitFile({ type: 'asset', fileName: 'version.txt', source: txt });
34+
},
35+
};
36+
37+
memo.plugins = memo.plugins || [];
38+
memo.plugins.push(versionPlugin);
39+
return memo;
40+
});
41+
};

packages/builder-vite/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export default function (): BuilderPlugin {
1919
join(OWNER_DIR, 'dist/features/viteOption.mjs'),
2020
join(OWNER_DIR, 'dist/features/viteVueJsx.mjs'),
2121
join(OWNER_DIR, 'dist/features/viteVuePlugin.mjs'),
22+
join(OWNER_DIR, 'dist/features/versionEmit.mjs'),
2223
join(OWNER_DIR, 'dist/features/viteAnalyze.mjs'),
2324
join(OWNER_DIR, 'dist/features/viteLegacy.mjs'),
2425

packages/builder-vite/tsup.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export default defineConfig({
1010
'src/features/viteOption.ts',
1111
'src/features/viteVueJsx.ts',
1212
'src/features/viteVuePlugin.ts',
13+
'src/features/versionEmit.ts',
1314
'src/features/viteAnalyze.ts',
1415
'src/features/viteLegacy.ts',
1516
'src/commands/build/index.ts',

packages/builder-webpack/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export default function () {
3131
join(__dirname, './plugins/features/extraBabelPresets.mjs'),
3232
join(__dirname, './plugins/features/extraPostCSSPlugins.mjs'),
3333
join(__dirname, './plugins/features/html.mjs'),
34+
join(__dirname, './plugins/features/versionEmit.mjs'),
3435
join(__dirname, './plugins/features/lessLoader.mjs'),
3536
join(__dirname, './plugins/features/postcssLoader.mjs'),
3637
join(__dirname, './plugins/features/nodeModulesTransform.mjs'),
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import type { IPluginAPI } from '@fesjs/shared';
2+
import { existsSync, readFileSync } from 'node:fs';
3+
import { join } from 'node:path';
4+
import process from 'node:process';
5+
import webpack from 'webpack';
6+
7+
class VersionEmitPlugin {
8+
apply(compiler: webpack.Compiler) {
9+
compiler.hooks.thisCompilation.tap('VersionEmitPlugin', (compilation) => {
10+
compilation.hooks.processAssets.tap({ name: 'VersionEmitPlugin', stage: webpack.Compilation.PROCESS_ASSETS_STAGE_ADDITIONAL }, () => {
11+
const pkgPath = join(process.cwd(), 'package.json');
12+
let name = '';
13+
let version = '';
14+
if (existsSync(pkgPath)) {
15+
try {
16+
const pkg = JSON.parse(readFileSync(pkgPath, 'utf-8')) || {};
17+
name = pkg.name || '';
18+
version = pkg.version || '';
19+
}
20+
catch {}
21+
}
22+
23+
const info = {
24+
name,
25+
version,
26+
buildTime: new Date().toISOString(),
27+
builder: 'webpack',
28+
nodeEnv: process.env.NODE_ENV,
29+
};
30+
31+
const jsonSource = new webpack.sources.RawSource(`${JSON.stringify(info, null, 2)}\n`);
32+
const txt = `name: ${info.name}\nversion: ${info.version}\nbuildTime: ${info.buildTime}\nbuilder: ${info.builder}\nnodeEnv: ${info.nodeEnv ?? ''}\n`;
33+
const txtSource = new webpack.sources.RawSource(txt);
34+
compilation.emitAsset('version.json', jsonSource);
35+
compilation.emitAsset('version.txt', txtSource);
36+
});
37+
});
38+
}
39+
}
40+
41+
export default (api: IPluginAPI) => {
42+
api.modifyBundleConfig((memo: any) => {
43+
memo.plugins = memo.plugins || [];
44+
memo.plugins.push(new VersionEmitPlugin());
45+
return memo;
46+
});
47+
};

packages/builder-webpack/tsup.config.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export default defineConfig({
2020
'src/plugins/features/extraBabelPresets.ts',
2121
'src/plugins/features/extraPostCSSPlugins.ts',
2222
'src/plugins/features/html.ts',
23+
'src/plugins/features/versionEmit.ts',
2324
'src/plugins/features/lessLoader.ts',
2425
'src/plugins/features/postcssLoader.ts',
2526
'src/plugins/features/nodeModulesTransform.ts',
@@ -35,7 +36,7 @@ export default defineConfig({
3536
dts: true,
3637
shims: true,
3738
format: ['esm'],
38-
onSuccess() {
39+
onSuccess: async () => {
3940
copySync('src/plugins/commands/index-default.html', 'dist/plugins/commands/index-default.html');
4041
},
4142
});

0 commit comments

Comments
 (0)