fix(builder): 统一使用api.paths.cwd获取工作目录路径

修改vite和webpack构建器的版本信息插件,不再直接使用process.cwd(),而是通过api.paths.cwd获取工作目录路径,确保路径获取的一致性
This commit is contained in:
harrywan 2025-12-08 17:21:27 +08:00
parent 45ecfa5591
commit c8ce8eba53
2 changed files with 4 additions and 3 deletions

View File

@ -8,7 +8,7 @@ export default (api: IPluginAPI) => {
const versionPlugin = { const versionPlugin = {
name: 'fes-version-emit', name: 'fes-version-emit',
generateBundle() { generateBundle() {
const pkgPath = join(process.cwd(), 'package.json'); const pkgPath = join(api.paths.cwd, 'package.json');
let name = ''; let name = '';
let version = ''; let version = '';
if (existsSync(pkgPath)) { if (existsSync(pkgPath)) {

View File

@ -5,10 +5,11 @@ import process from 'node:process';
import webpack from 'webpack'; import webpack from 'webpack';
class VersionEmitPlugin { class VersionEmitPlugin {
constructor(private cwd: string) {}
apply(compiler: webpack.Compiler) { apply(compiler: webpack.Compiler) {
compiler.hooks.thisCompilation.tap('VersionEmitPlugin', (compilation) => { compiler.hooks.thisCompilation.tap('VersionEmitPlugin', (compilation) => {
compilation.hooks.processAssets.tap({ name: 'VersionEmitPlugin', stage: webpack.Compilation.PROCESS_ASSETS_STAGE_ADDITIONAL }, () => { compilation.hooks.processAssets.tap({ name: 'VersionEmitPlugin', stage: webpack.Compilation.PROCESS_ASSETS_STAGE_ADDITIONAL }, () => {
const pkgPath = join(process.cwd(), 'package.json'); const pkgPath = join(this.cwd, 'package.json');
let name = ''; let name = '';
let version = ''; let version = '';
if (existsSync(pkgPath)) { if (existsSync(pkgPath)) {
@ -41,7 +42,7 @@ class VersionEmitPlugin {
export default (api: IPluginAPI) => { export default (api: IPluginAPI) => {
api.modifyBundleConfig((memo: any) => { api.modifyBundleConfig((memo: any) => {
memo.plugins = memo.plugins || []; memo.plugins = memo.plugins || [];
memo.plugins.push(new VersionEmitPlugin()); memo.plugins.push(new VersionEmitPlugin(api.paths.cwd));
return memo; return memo;
}); });
}; };