Angular:如何为每个配置添加唯一的 .htaccess 文件?

Angular: how to add a unique .htaccess file per configuration?

我的 Angular 应用程序包含 2 个构建配置:暂存和生产。


目前我有一个适用于所有构建的标准 .htaccess 文件,将其包含在我的资产中:

"assets": [
  "src/.htaccess",
  "src/assets",
  "src/favicon.png"
],

我想用 .htpasswd 文件密码保护我的暂存构建目录,所以我更新了我的资产以包含它:

"assets": [
  "src/.htaccess",
  "src/.htpasswd"
  "src/assets",
  "src/favicon.png"
],

htpasswd 文件可以包含在每个构建中,因为在 .htaccess 文件中没有提及,它什么都不做。


然后,我创建了第二个 .htaccess 文件,名为 .htaccess.dev,添加了以下行:

AuthName "Protected"
AuthUserFile /home/admin/domains/dev.***.com/public_html/.htpasswd
AuthGroupFile /dev/null
AuthType Basic
require valid-user

这个带有额外行的文件应该替换暂存构建中的 .htaccess 文件,所以我将以下配置添加到我的 angular.json 文件中:

"build": {
  ...
  "configurations": {
    "staging": {
      ...
      "fileReplacements": [
        {
          "replace": "src/.htaccess",
          "with": "src/.htaccess.dev"
        },
        {
          "replace": "src/environments/environment.ts",
          "with": "src/environments/environment.staging.ts"
        }
      ]
    }
  }
}

fileReplacements 适用于我的 .env,但 它不会替换我的 htaccess 文件。为什么?

啊,看来 fileReplacements 只适用于 .ts 个文件。


angular/devkit#885:

Currently, the file replacement functionality works only for TS files. More specifically - it works only for files that are part of the webpack compiler host.

这将(显然)在 Angular 6.1.

中得到修复

See this GitHub thread for more info


更新2018-08-28

Angular6.1

现在支持