【问题标题】:npm run build on multiple environments without mention specified environmentnpm run build 在多个环境中没有提及指定环境
【发布时间】:2019-03-05 11:21:22
【问题描述】:

我正在使用具有多个环境的 Angular 项目。每当为单一环境进行构建时,需要很长时间。我怎样才能减少它。我已经在这里添加了我的项目配置。

package.json:

{
  "name": "test",
  "version": "0.0.0",
  "license": "MIT",
  "scripts": {
    "ng": "ng",
    "start": "node --max_old_space_size=10240 ./node_modules/@angular/cli/bin/ng serve --aot",
    "build": "rimraf dist && node --max_old_space_size=10240 ./node_modules/@angular/cli/bin/ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e",
    "build:universal": "npm run build:client-and-server-bundles && npm run webpack:server",
    "serve:universal": "node dist/server.js",
    "build:client-and-server-bundles": "ng build --prod && ng build --prod --app 1 --output-hashing=false",
    "webpack:server": "webpack --config webpack.server.config.js --progress --colors",
    "postbuild": "node zipbuild"
  },
  "private": true,
  "dependencies": {
    "@angular-devkit/core": "7.3.3",
    "@angular/animations": "7.2.6",
    "@angular/cdk": "^7.3.3",
    "@angular/cli": "7.3.3",
    "@angular/common": "7.2.6",
    "@angular/compiler": "7.2.6",
    "@angular/core": "7.2.6",
    "@angular/flex-layout": "7.0.0-beta.23",
    "@angular/forms": "7.2.6",
    "@angular/http": "7.2.6",
    "@angular/material": "^7.3.3",
    "@angular/platform-browser": "7.2.6",
    "@angular/platform-browser-dynamic": "7.2.6",
    "@angular/platform-server": "7.2.6",
    "@angular/router": "7.2.6",
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "~0.13.3",
    "@angular/compiler-cli": "7.2.6",
    "@angular/language-service": "7.2.6",
    "@types/jasmine": "~3.3.9",
    "@types/jasminewd2": "~2.0.6",
    "@types/node": "^11.9.5",
    "codelyzer": "^4.5.0",
    "css-loader": "2.1.0",
    "jasmine-core": "^3.3.0",
    "jasmine-spec-reporter": "^4.2.1",
    "karma": "~4.0.0",
    "karma-chrome-launcher": "~2.2.0",
    "karma-coverage-istanbul-reporter": "^2.0.5",
    "karma-jasmine": "^2.0.1",
    "karma-jasmine-html-reporter": "^1.4.0",
    "protractor": "^5.4.2",
    "ts-node": "^8.0.2",
    "tslint": "^5.13.0",
    "typescript": "3.2.4"
  }
}

angular.json:

    {
      "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
      "version": 1,
      "newProjectRoot": "projects",
      "projects": {
        "test": {
          "root": "",
          "sourceRoot": "src",
          "projectType": "application",
          "architect": {
            "build": {
              "builder": "@angular-devkit/build-angular:browser",
              "options": {
                "outputPath": "dist",
                "index": "src/index.html",
                "main": "src/main.ts",
                "tsConfig": "src/tsconfig.app.json",
                "polyfills": "src/polyfills.ts",
                "assets": [
                  "src/assets",
                  "src/favicon.ico",
                  "src/web.config"
                ],
                "styles": [
                  "src/styles.scss"
                ],
                "scripts": [
                  "src/assets/SigWebTablet.js"
                ]
              },
              "configurations": {
                "demo": {
                  "optimization": true,
                  "outputHashing": "all",
                  "sourceMap": false,
                  "extractCss": true,
                  "namedChunks": false,
                  "aot": true,
                  "extractLicenses": true,
                  "vendorChunk": false,
                  "buildOptimizer": true,
                  "outputPath": "dist/demo",
                  "fileReplacements": [
                    {
                      "replace": "src/environments/environment.ts",
                      "with": "src/environments/environment.demo.ts"
                    }
                  ]
                },
"demo1": {
                  "optimization": true,
                  "outputHashing": "all",
                  "sourceMap": false,
                  "extractCss": true,
                  "namedChunks": false,
                  "aot": true,
                  "extractLicenses": true,
                  "vendorChunk": false,
                  "buildOptimizer": true,
                  "outputPath": "dist/demo",
                  "fileReplacements": [
                    {
                      "replace": "src/environments/environment.ts",
                      "with": "src/environments/environment.demo.ts"
                    }
                  ]
                }
              }
            },
            "serve": {
              "builder": "@angular-devkit/build-angular:dev-server",
              "options": {
                "browserTarget": "test:build"
              },
              "configurations": {
                "production": {
                  "browserTarget": "test:build:production"
                },
                "sacramento": {
                  "browserTarget": "test:build:sacramento"
                },
                "denver": {
                  "browserTarget": "test:build:denver"
                },
                "orange": {
                  "browserTarget": "test:build:orange"
                },
                "slo": {
                  "browserTarget": "test:build:slo"
                }
              }
            },
            "extract-i18n": {
              "builder": "@angular-devkit/build-angular:extract-i18n",
              "options": {
                "browserTarget": "atims-prebook:build"
              }
            },
            "test": {
              "builder": "@angular-devkit/build-angular:karma",
              "options": {
                "main": "src/test.ts",
                "karmaConfig": "./karma.conf.js",
                "polyfills": "src/polyfills.ts",
                "tsConfig": "src/tsconfig.spec.json",
                "scripts": [],
                "styles": [
                  "src/styles.scss"
                ],
                "assets": [
                  "src/assets",
                  "src/favicon.ico"
                ]
              }
            },
            "lint": {
              "builder": "@angular-devkit/build-angular:tslint",
              "options": {
                "tsConfig": [
                  "src/tsconfig.app.json",
                  "src/tsconfig.spec.json"
                ],
                "exclude": [
                  "**/node_modules/**"
                ]
              }
            },
            "server": {
              "builder": "@angular-devkit/build-angular:server",
              "options": {
                "outputPath": "dist/server",
                "main": "main.server.ts",
                "tsConfig": "tsconfig.server.json"
              }
            }
          }
        },
        "test-e2e": {
          "root": "",
          "sourceRoot": "e2e",
          "projectType": "application",
          "architect": {
            "e2e": {
              "builder": "@angular-devkit/build-angular:protractor",
              "options": {
                "protractorConfig": "./protractor.conf.js",
                "devServerTarget": "test:serve"
              }
            },
            "lint": {
              "builder": "@angular-devkit/build-angular:tslint",
              "options": {
                "tsConfig": [
                  "e2e/tsconfig.e2e.json"
                ],
                "exclude": [
                  "**/node_modules/**"
                ]
              }
            }
          }
        }
      },
      "defaultProject": "test",
      "schematics": {
        "@schematics/angular:class": {
          "spec": false
        },
        "@schematics/angular:component": {
          "spec": false,
          "prefix": "pbpc",
          "styleext": "scss"
        },
        "@schematics/angular:directive": {
          "spec": false,
          "prefix": "pbpc"
        },
        "@schematics/angular:guard": {
          "spec": false
        },
        "@schematics/angular:module": {
          "spec": false
        },
        "@schematics/angular:pipe": {
          "spec": false
        },
        "@schematics/angular:service": {
          "spec": false
        }
      }
    }

我使用的构建命令是 "npm run build -- --c demo" & "npm run build -- --c demo1" 来构建单独的命令提示符。每次构建大约需要 20 到 30 分钟。我们可以选择构建单个查询吗?

【问题讨论】:

    标签: angular npm


    【解决方案1】:

    您可以一起执行命令,如下所示:

    npm run build -- --c demo && npm run build -- --c demo1
    

    或者您可以指定自己的命令,使用包 json 脚本:

    {
      "name": "myApp",
      "version": "0.0.0",
      "engines": {
        "node": "10.13.0",
        "npm": "6.4.1"
      },
      "scripts": {
        "test": "jest src/app",
        "test:watch": "jest src/app --watch",
        "lint": "tslint -c tslint.json 'src/app/**/*.ts' --project src/tsconfig.json",
        "appStart": "npm run build -- --c demo && npm run build -- --c demo1" // This line
      },
    }
    

    然后简单地运行

    npm run appStart
    

    注意:

    还有更多选项可以使用,一起执行多个命令,这里有一个备忘单

    A; B    # Run A and then B, regardless of success of A
    A && B  # Run B if and only if A succeeded
    A || B  # Run B if and only if A failed
    A &     # Run A in background.
    

    【讨论】:

    • 谢谢。它就像魅力一样。我想知道,是否可以只构建一个环境,我们是否可以更改构建文件中剩余环境的配置设置。因为,对于单个构建本身需要 30 分钟。有什么解决办法吗?我期待像 ASP.NET 发布程序。
    • 很高兴我能帮上忙(:不确定我是否遵循您的问题。可能只构建一个 env = 是的。更改构建文件中的配置设置 = 不确定我理解。构建所需的时间 =似乎是您必须提出的一个新问题,并提供更多信息。构建通常不会花费这么长时间。
    • 感谢您的信息。我会问一个新问题。
    • 当然,抱歉,我在这里帮不上忙,似乎没有足够的信息来解决为什么构建需要很长时间,可能有几个原因。如果此答案解决了您最初的问题,请接受它
    • @venkateswaran R 我需要一个相同的单一构建,它应该基于部署环境的动态值工作。我不想为每个环境单独构建。你有没有得到任何有关这方面的信息??
    【解决方案2】:

    您可以使用 child_process 和 Promise 编写一个 Node.js 程序来并行运行多个命令。

    【讨论】:

    • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
    猜你喜欢
    • 1970-01-01
    • 2021-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-29
    • 2019-02-23
    • 1970-01-01
    • 2019-08-29
    相关资源
    最近更新 更多