In a Nx workspace, the custom webpack configuration file can be named according to your preference, as you will reference it explicitly in the angular.json
or workspace.json
file. A common convention for naming this file is webpack.config.js
or custom-webpack.config.js
.
See "Configure webpack on your Nx workspace"
In your webpack configuration file, you can import the package.json
:
const packageJson = require('./package.json');
const version = packageJson.version;
Use the DefinePlugin
in your webpack configuration to define the variable based on version
.
See "Using environment variables in Angular applications".
In your custom webpack configuration, it might look like this:
const webpack = require('webpack');
module.exports = (config) => {
config.plugins.push(
new webpack.DefinePlugin({
'process.env.NX_PACKAGE_VERSION': JSON.stringify(version)
})
);
return config;
};
Update your application's build options to use the custom webpack configuration. For example, if you are using Angular, you can update the angular.json
file:
"architect": {
"build": {
"builder": "@angular-builders/custom-webpack:browser",
"options": {
"customWebpackConfig": {
"path": "./custom-webpack.config.js"
},
}
}
}
You should now be able to access the version in your code like this:
console.log(process.env.NX_PACKAGE_VERSION);