Generate a buildinfo.json file to public directory.
# Webpack (CRA)
yarn add -D gen-buildinfo-webpack-plugin
# Next.js
yarn add gen-buildinfo-webpack-plugin
// webpack.config.js
const GenBuildinfoWebpackPlugin = require('gen-buildinfo-webpack-plugin');
new GenBuildinfoWebpackPlugin({
package: require('./package.json'),
//
// [Options]
// fileDir: PROJECT_PUBLIC_DIR,
// fileName: 'buildinfo.json',
// commitHashLength: 4,
})
// next.config.js
// use `process.env.NEXT_PUBLIC_BUILD_INFO`
const { getBuildInfo } = require('gen-buildinfo-webpack-plugin');
module.exports = {
env: {
NEXT_PUBLIC_BUILD_INFO: JSON.stringify(
getBuildInfo({ package: require('./package.json') }),
),
},
webpack(webpackConfig) {
return webpackConfig;
},
// ....
}
const { getBuildInfo } = require('gen-buildinfo-webpack-plugin');
// {
// "PKG_NAME": "demo",
// "AUTHOR": "Jason Feng <solidzoro[#]live.com>",
// "VERSION": "2.66.1",
// "BUILD_NUMBER": 2066001,
// "BUILD_TIME": "20230406-142929",
// "COMMIT_HASH": "c6ed"
// };
const buildInfo = getBuildInfo({ package: require('../../package.json') });
MIT © Jason Feng