mirror of
https://github.com/WeBankFinTech/fes.js.git
synced 2025-12-11 13:40:17 +08:00
feat(builder): 添加版本信息生成功能
在构建过程中自动生成包含项目名称、版本号、构建时间等信息的 version.json 和 version.txt 文件
This commit is contained in:
parent
6868e85a9b
commit
45ecfa5591
41
packages/builder-vite/src/features/versionEmit.ts
Normal file
41
packages/builder-vite/src/features/versionEmit.ts
Normal file
@ -0,0 +1,41 @@
|
||||
import type { IPluginAPI } from '@fesjs/shared';
|
||||
import { existsSync, readFileSync } from 'node:fs';
|
||||
import { join } from 'node:path';
|
||||
import process from 'node:process';
|
||||
|
||||
export default (api: IPluginAPI) => {
|
||||
api.modifyBundleConfig((memo: any) => {
|
||||
const versionPlugin = {
|
||||
name: 'fes-version-emit',
|
||||
generateBundle() {
|
||||
const pkgPath = join(process.cwd(), 'package.json');
|
||||
let name = '';
|
||||
let version = '';
|
||||
if (existsSync(pkgPath)) {
|
||||
try {
|
||||
const pkg = JSON.parse(readFileSync(pkgPath, 'utf-8')) || {};
|
||||
name = pkg.name || '';
|
||||
version = pkg.version || '';
|
||||
}
|
||||
catch {}
|
||||
}
|
||||
|
||||
const info = {
|
||||
name,
|
||||
version,
|
||||
buildTime: new Date().toISOString(),
|
||||
builder: 'vite',
|
||||
nodeEnv: process.env.NODE_ENV,
|
||||
};
|
||||
|
||||
(this as any).emitFile({ type: 'asset', fileName: 'version.json', source: `${JSON.stringify(info, null, 2)}\n` });
|
||||
const txt = `name: ${info.name}\nversion: ${info.version}\nbuildTime: ${info.buildTime}\nbuilder: ${info.builder}\nnodeEnv: ${info.nodeEnv ?? ''}\n`;
|
||||
(this as any).emitFile({ type: 'asset', fileName: 'version.txt', source: txt });
|
||||
},
|
||||
};
|
||||
|
||||
memo.plugins = memo.plugins || [];
|
||||
memo.plugins.push(versionPlugin);
|
||||
return memo;
|
||||
});
|
||||
};
|
||||
@ -19,6 +19,7 @@ export default function (): BuilderPlugin {
|
||||
join(OWNER_DIR, 'dist/features/viteOption.mjs'),
|
||||
join(OWNER_DIR, 'dist/features/viteVueJsx.mjs'),
|
||||
join(OWNER_DIR, 'dist/features/viteVuePlugin.mjs'),
|
||||
join(OWNER_DIR, 'dist/features/versionEmit.mjs'),
|
||||
join(OWNER_DIR, 'dist/features/viteAnalyze.mjs'),
|
||||
join(OWNER_DIR, 'dist/features/viteLegacy.mjs'),
|
||||
|
||||
|
||||
@ -10,6 +10,7 @@ export default defineConfig({
|
||||
'src/features/viteOption.ts',
|
||||
'src/features/viteVueJsx.ts',
|
||||
'src/features/viteVuePlugin.ts',
|
||||
'src/features/versionEmit.ts',
|
||||
'src/features/viteAnalyze.ts',
|
||||
'src/features/viteLegacy.ts',
|
||||
'src/commands/build/index.ts',
|
||||
|
||||
@ -31,6 +31,7 @@ export default function () {
|
||||
join(__dirname, './plugins/features/extraBabelPresets.mjs'),
|
||||
join(__dirname, './plugins/features/extraPostCSSPlugins.mjs'),
|
||||
join(__dirname, './plugins/features/html.mjs'),
|
||||
join(__dirname, './plugins/features/versionEmit.mjs'),
|
||||
join(__dirname, './plugins/features/lessLoader.mjs'),
|
||||
join(__dirname, './plugins/features/postcssLoader.mjs'),
|
||||
join(__dirname, './plugins/features/nodeModulesTransform.mjs'),
|
||||
|
||||
47
packages/builder-webpack/src/plugins/features/versionEmit.ts
Normal file
47
packages/builder-webpack/src/plugins/features/versionEmit.ts
Normal file
@ -0,0 +1,47 @@
|
||||
import type { IPluginAPI } from '@fesjs/shared';
|
||||
import { existsSync, readFileSync } from 'node:fs';
|
||||
import { join } from 'node:path';
|
||||
import process from 'node:process';
|
||||
import webpack from 'webpack';
|
||||
|
||||
class VersionEmitPlugin {
|
||||
apply(compiler: webpack.Compiler) {
|
||||
compiler.hooks.thisCompilation.tap('VersionEmitPlugin', (compilation) => {
|
||||
compilation.hooks.processAssets.tap({ name: 'VersionEmitPlugin', stage: webpack.Compilation.PROCESS_ASSETS_STAGE_ADDITIONAL }, () => {
|
||||
const pkgPath = join(process.cwd(), 'package.json');
|
||||
let name = '';
|
||||
let version = '';
|
||||
if (existsSync(pkgPath)) {
|
||||
try {
|
||||
const pkg = JSON.parse(readFileSync(pkgPath, 'utf-8')) || {};
|
||||
name = pkg.name || '';
|
||||
version = pkg.version || '';
|
||||
}
|
||||
catch {}
|
||||
}
|
||||
|
||||
const info = {
|
||||
name,
|
||||
version,
|
||||
buildTime: new Date().toISOString(),
|
||||
builder: 'webpack',
|
||||
nodeEnv: process.env.NODE_ENV,
|
||||
};
|
||||
|
||||
const jsonSource = new webpack.sources.RawSource(`${JSON.stringify(info, null, 2)}\n`);
|
||||
const txt = `name: ${info.name}\nversion: ${info.version}\nbuildTime: ${info.buildTime}\nbuilder: ${info.builder}\nnodeEnv: ${info.nodeEnv ?? ''}\n`;
|
||||
const txtSource = new webpack.sources.RawSource(txt);
|
||||
compilation.emitAsset('version.json', jsonSource);
|
||||
compilation.emitAsset('version.txt', txtSource);
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export default (api: IPluginAPI) => {
|
||||
api.modifyBundleConfig((memo: any) => {
|
||||
memo.plugins = memo.plugins || [];
|
||||
memo.plugins.push(new VersionEmitPlugin());
|
||||
return memo;
|
||||
});
|
||||
};
|
||||
@ -20,6 +20,7 @@ export default defineConfig({
|
||||
'src/plugins/features/extraBabelPresets.ts',
|
||||
'src/plugins/features/extraPostCSSPlugins.ts',
|
||||
'src/plugins/features/html.ts',
|
||||
'src/plugins/features/versionEmit.ts',
|
||||
'src/plugins/features/lessLoader.ts',
|
||||
'src/plugins/features/postcssLoader.ts',
|
||||
'src/plugins/features/nodeModulesTransform.ts',
|
||||
@ -35,7 +36,7 @@ export default defineConfig({
|
||||
dts: true,
|
||||
shims: true,
|
||||
format: ['esm'],
|
||||
onSuccess() {
|
||||
onSuccess: async () => {
|
||||
copySync('src/plugins/commands/index-default.html', 'dist/plugins/commands/index-default.html');
|
||||
},
|
||||
});
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user