无法将 fileReplacements 设置为用 Angular 11 替换文件夹内容
Cannot set fileReplacements to replace folder content with Angular 11
我在 angular.json 文件中使用 angular cli 配置 fileReplacements
来替换文件夹内容,如下所示:
{
"projects": {
"myProject": {
"architect": {
"build": {
"configurations": {
"prod": {
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.prod.ts"
},
{
"replace": "node_modules/moment/locale/",
"with": "src/moment-js-locale/"
}
]
}
}
}
}
}
}
}
请注意,在 replace
值中,我使用的是文件夹路径而不是文件路径。
这是 Angular 10.
的工作代码
我升级到 Angular 11,现在我在尝试使用此命令构建产品版本时遇到错误:
ng build --prod
我得到的错误是:
Schema validation failed with the following errors:
Data path ".fileReplacements[1]" should NOT have additional properties(replace).
Data path ".fileReplacements[1].replace" should match pattern "\.(([cm]?j|t)sx?|json)$".
Data path ".fileReplacements[1]" should match exactly one schema in oneOf.
ESLint 向我显示此警告:
String does not match the pattern of "\.(([cm]?j|t)sx?|json)$".
我能用它做什么?这是 Angular 11 错误吗?我应该使用不同的方法吗?
我们将不胜感激。
原来这个特征实际上是一个错误
在 Angular 10 之前可以进行文件夹替换这一事实从一开始就不应该起作用。
我在 angular-cli repo 中问了这个问题,这是我得到的答案:
I am sorry that you are experiencing this issue after updating. However, replacing directories was never a supported use-case of the file replacements feature.
我想这需要开发人员来处理。
在我的例子中,我只是添加了一个脚本来使用 shell
命令来执行此操作,并且我将此脚本 运行 作为使用 npm run script
.
的预构建脚本
我成功地为 web.config 和专用于生产的图像进行了以下配置(我将生产文件放入 src/deployment/prod 并将它们复制到 dist 目录到“输出”变量的目录中):
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.prod.ts"
}
],
"assets": [
"src/assets",
"src/favicon.ico",
{
"input": "src/deployment/prod",
"output": "/",
"glob": "web.config"
},
{
"input": "src/deployment/prod",
"output": "assets/img",
"glob": "*.png"
}
]
所以在你的情况下你可能会使用:
{
"input": "node_modules/moment/locale/", -- source folder in ClientApp/
"output": "moment-js-locale", -- destination folder in dist/
"glob": "*.js" -- all js files will be copied
}
我在 angular.json 文件中使用 angular cli 配置 fileReplacements
来替换文件夹内容,如下所示:
{
"projects": {
"myProject": {
"architect": {
"build": {
"configurations": {
"prod": {
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.prod.ts"
},
{
"replace": "node_modules/moment/locale/",
"with": "src/moment-js-locale/"
}
]
}
}
}
}
}
}
}
请注意,在 replace
值中,我使用的是文件夹路径而不是文件路径。
这是 Angular 10.
我升级到 Angular 11,现在我在尝试使用此命令构建产品版本时遇到错误:
ng build --prod
我得到的错误是:
Schema validation failed with the following errors:
Data path ".fileReplacements[1]" should NOT have additional properties(replace).
Data path ".fileReplacements[1].replace" should match pattern "\.(([cm]?j|t)sx?|json)$".
Data path ".fileReplacements[1]" should match exactly one schema in oneOf.
ESLint 向我显示此警告:
String does not match the pattern of "\.(([cm]?j|t)sx?|json)$".
我能用它做什么?这是 Angular 11 错误吗?我应该使用不同的方法吗?
我们将不胜感激。
原来这个特征实际上是一个错误 在 Angular 10 之前可以进行文件夹替换这一事实从一开始就不应该起作用。
我在 angular-cli repo 中问了这个问题,这是我得到的答案:
I am sorry that you are experiencing this issue after updating. However, replacing directories was never a supported use-case of the file replacements feature.
我想这需要开发人员来处理。
在我的例子中,我只是添加了一个脚本来使用 shell
命令来执行此操作,并且我将此脚本 运行 作为使用 npm run script
.
我成功地为 web.config 和专用于生产的图像进行了以下配置(我将生产文件放入 src/deployment/prod 并将它们复制到 dist 目录到“输出”变量的目录中):
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.prod.ts"
}
],
"assets": [
"src/assets",
"src/favicon.ico",
{
"input": "src/deployment/prod",
"output": "/",
"glob": "web.config"
},
{
"input": "src/deployment/prod",
"output": "assets/img",
"glob": "*.png"
}
]
所以在你的情况下你可能会使用:
{
"input": "node_modules/moment/locale/", -- source folder in ClientApp/
"output": "moment-js-locale", -- destination folder in dist/
"glob": "*.js" -- all js files will be copied
}