Merge c8ce8eba53e92ec714ad54b9bcdba0f73a205d0a into 6868e85a9b1f4bd184faac38468995b2c9f7af28

This commit is contained in:
听海 2025-12-08 17:22:06 +08:00 committed by GitHub
commit 39bab23730
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 94 additions and 1 deletions

View 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(api.paths.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;
});
};

View File

@ -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'),

View File

@ -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',

View File

@ -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'),

View File

@ -0,0 +1,48 @@
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 {
constructor(private cwd: string) {}
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(this.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(api.paths.cwd));
return memo;
});
};

View File

@ -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');
},
});