Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions packages/builder-vite/src/features/versionEmit.ts
Original file line number Diff line number Diff line change
@@ -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;
});
};
1 change: 1 addition & 0 deletions packages/builder-vite/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'),

Expand Down
1 change: 1 addition & 0 deletions packages/builder-vite/tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
1 change: 1 addition & 0 deletions packages/builder-webpack/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down
48 changes: 48 additions & 0 deletions packages/builder-webpack/src/plugins/features/versionEmit.ts
Original file line number Diff line number Diff line change
@@ -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;
});
};
3 changes: 2 additions & 1 deletion packages/builder-webpack/tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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');
},
});