- Install plugin
npm install replace-in-file --save-dev
Add to prod environment src/environments/environment.prod.ts new
property
export const environment = {
production: true,
version: '{BUILD_VERSION}'
}
Add build file replace.build.js
to root of your folder
var replace = require('replace-in-file');
var buildVersion = process.argv[2];
const options = {
files: 'src/environments/environment.prod.ts',
from: /{BUILD_VERSION}/g,
to: buildVersion,
allowEmptyPaths: false,
};
try {
let changedFiles = replace.sync(options);
console.log('Build version set: ' + buildVersion);
}
catch (error) {
console.error('Error occurred:', error);
}
add scripts to package.json
"updateBuild": "node ./replace.build.js"
Use environment.version
in your app
Before build call npm run updateBuild -- 1.0.1
PS. You must always remember that {BUILD_VERSION} is never committed.
PS. I wrote a bit better solution in my blog
PS.3 as @julien-100000 mentioned you should not commit environment.prod.ts with updated version. Version update must happen only in build process. And should never be committed.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…