【问题标题】:Angular Build-Config: add conditional file (robots.txt)Angular Build-Config:添加条件文件(robots.txt)
【发布时间】:2020-12-01 10:02:42
【问题描述】:

我有两个构建配置“生产”和“开发”。通过选择以下两个命令之一,我可以决定要为哪个配置构建应用程序:

  1. ng build --configuration=development
  2. ng build --configuration=production

angular.json 中,我为每个配置指定了构建选项:

        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
           .
           .
           .
          },
          "configurations": {
            "production": {
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.prod.ts"
                }
              ],
              "index": {
                "input": "src/index.prod.html",
                "output": "index.html"
              },
              .
              .
              .
            },
            "development": {
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.dev.ts"
                }
              ],
              .
              .
              .
            }
          }
        },

基本上我会根据选择的构建配置更改environment.tsindex.html

有没有办法在项目文件夹中有两个 robots.txt 文件,例如 robots.dev.txtrobots.prod.txt 并在构建期间有条件地将其中一个作为 robots.txt 添加到 dist 文件夹?几乎我如何使用索引文件来做到这一点。 目的是防止所有搜索引擎机器人索引我的开发版本,但允许它们索引生产版本。

【问题讨论】:

    标签: angular


    【解决方案1】:

    我不认为这可以从angular.json 处理。

    作为替代方案,robots.txt 可以作为构建后步骤进行复制。去做这个, 安装copy npm 包有一个复制CLI命令,它是跨平台的:

    npm install copy --save-dev
    

    使用以下命令将用于生产构建的脚本添加到 package.json

    "build:prod": "ng build --prod && copy robots.txt dist"
    

    运行 npm run build:prod 以生成包含 robots.txt 的生产版本。

    以上示例假设robots.txtpackage.json 位于同一目录中,并假设构建的应用程序位于dist 文件夹的根目录中。

    更新

    如果复制的文件需要重命名,可以使用copy-file-from-to 包。如果有更多来自不同来源的文件并且它们也需要重命名,则特别方便。要在问题的上下文中使用它,请执行以下步骤:

    安装thirdpaty作为开发依赖:

    npm install copy-files-from-to --save-dev
    

    在项目根目录下为copy-files-from-to创建一个名为copyFiles.json的配置文件,内容如下:

    {
        "copyFiles": [
            {
                "from": "robots.dev.txt",
                "to": "dist/robots.txt"
            }
        ]
    }
    

    创建一个 npm 脚本,使用 copy-files-from-to 和上述配置:

    "build:prod": "ng build --prod && copy-files-from-to --config copyFiles.json"
    

    运行npm run build:prodrobots.dev.txt的内容将在dist/robots.txt中构建后复制。

    【讨论】:

    • 谢谢,我会调查的。是否还有在复制过程中重命名文件的选项?例如copy robots.dev.txt dist robots.txt?
    • 我检查了copy 第三方并没有找到任何重命名选项。所以我用另一个解决方案更新了答案,这使得文件的重命名成为可能。
    【解决方案2】:

    这是我的做法。

    在我的angular.json 文件中,我定义了要包含在architect->build->options 中的资产:

            "assets": [
              "src/favicon.ico",
              "src/assets",
              "src/robots.txt",
              "src/sitemap.txt"
            ],
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-10
      • 2021-12-22
      • 1970-01-01
      • 2017-10-24
      相关资源
      最近更新 更多