Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
388 views
in Technique[技术] by (71.8m points)

Angular fileReplacements doesnt replace environments file

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Can you please try

- - configuration=production 

instead of

- - environment=prod`

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...