gay*_*nbc 3 angular-cli angular
我要在运行带有--prod选项而没有--prod选项的ng build命令时更改脚本标记的URL 。以下是一个示例。
ng build --prod
<!DOCTYPE html>
<html>
<head>
<title>Home Page</title>
</head>
<body>
<script src="https://my-site.net/prod.js"></script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
ng构建
<!DOCTYPE html>
<html>
<head>
<title>Home Page</title>
</head>
<body>
<script src="https://my-site.net/dev.js"></script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
我该如何实现?
在其中angular.json
您可以配置要使用的索引文件(我正在使用Angular 8):
"projects": {
"projectName": {
"architect": {
"build": {
"options": {
"index": "src/index.html", // this is the default location (also for ng serve)
"configurations": {
"production": {
"index": "index-prod.html" // this is the prod version (you can also link it to /prod/index.html or whatever: it will be placed in the dist root folder with the provided name)
},
"development": {
"index": "index-whatever.html" // this is the version for ng build --configuration=development`
}
}
}
}/*, UPDATED: THIS DOESN'T WORK
"serve": {
"index": "dev/index.html" // file to be used in ng serve
}*/
}
}
}
Run Code Online (Sandbox Code Playgroud)
AFAIK "index"
除了第一个以外,都不是必需的
旁注:这就是我刚刚尝试过的一些尝试,构建和过程ng serve
。我没有在任何文档中找到它。
检查出来,让我知道。
===========
更新:我注意到上面报告的配置仍src/index.html
用于serve
on "@angular/cli": "^8.3.6", "@angular/compiler-cli": "^8.2.8"
。看起来自己没有覆盖serve
。我的问题是:应该serve
始终继承基本index
配置吗?
尝试进一步挖掘,我发现了这一点:https : //github.com/angular/angular-cli/issues/14599
有一个示例说明了它应如何与新的CLI一起使用,但尝试替换新的CLI。
"development": {
index": "index-whatever.html"
}
Run Code Online (Sandbox Code Playgroud)
与一个
"development": {
index": {
"input": "index-whatever.html"
}
}
Run Code Online (Sandbox Code Playgroud)
和建筑,它给了我一个
Schema validation failed with the following errors: Data path ".index" should be string.
对于从v6.1.0-beta.2开始的最新Angular版本,您可以使用文件配置中的fileReplacements
值,angular.json
如该答案中所详述:@ massic80 /sf/answers/4009203341/。
更多信息在这里:https : //github.com/angular/angular-cli/issues/4451#issuecomment-395651237
对于v6.1.0-beta.2之前的旧版本,Angular CLI不提供任何此类功能。但是您可以使用环境变量在运行时动态注入适当的脚本。
例如:
main.ts
import { env } from './environments/environment';
if (env.production) {
const script = document.createElement('script');
script.src = https://my-site.net/prod.js
document.head.appendChild(script);
} else {
const script = document.createElement('script');
script.src = https://my-site.net/dev.js
document.head.appendChild(script);
}
Run Code Online (Sandbox Code Playgroud)
在此处阅读更多信息:https : //github.com/angular/angular-cli/issues/4451#issuecomment-285329985
小智 6
如果您想在本地服务时坚持标准src/index.html
,希望index.html
每个生产/测试配置都有专门的文件(就像我的情况一样,其中应用了不同的 Google Analytics 代码块),只需执行以下操作:
index.html
. 例如。:src/prod/index.html
src/test/index.html
angular.json
它们:"configurations": {
"production": {
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.prod.ts"
}
],
"index": "src/index/prod/index.html",
// other stuff
},
"test": {
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.test.ts"
}
],
"index": "src/index/test/index.html",
// other stuff
}
// other stuff
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4599 次 |
最近记录: |