I have a problem with replacing file contents of environments.ts
file with prod or dev contents.
First of all, I have a script that reads .env files and adds values to environments.dev
or prod
file. So I expect when I run ng build --prod to replace environments.ts
with environments.prod
Angular version: 11.0.9
angular.json
"architect": {
"configurations": {
"production": {
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.prod.ts"
}
],
"optimization": true,
"outputHashing": "all",
"sourceMap": false,
"namedChunks": false,
"extractLicenses": true,
"vendorChunk": false,
"buildOptimizer": true,
"budgets": [
{
"type": "initial",
"maximumWarning": "500kb",
"maximumError": "1mb"
},
{
"type": "anyComponentStyle",
"maximumWarning": "2kb",
"maximumError": "4kb"
}
]
}
},
}
package.json
"start": "npm run config -- --environment=dev && ng serve",
"build": "npm run config -- --environment=prod && ng build --prod",
setenv.ts
const { writeFile } = require('fs');
const { argv } = require('yargs');
const ENV = 'local';
require('dotenv').config({ path: `.env.${ENV}` });
const environment = argv.environment;
const isProd = environment === 'prod';
const targetPath = `./src/environments/environment.${environment}.ts`;
const envConfigFile = `
export const environment = {
production: ${isProd},
host: '${process.env.HOST}',
};
`
writeFile(targetPath, envConfigFile, (err: any) => {
if (err) {
console.error(err);
}
});
question from:
https://stackoverflow.com/questions/66067228/angular-filereplacements-doesnt-replace-environments-file 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…