简体   繁体   English

角示意图。 自定义angular.json文件

[英]Angular Schematics. Customizing the angular.json file

When I build my site with schematics (ng new --collection=@foo/schematics myproject), everything works fine, except one thing: 当我使用原理图(ng new --collection = @ foo / schematics myproject)构建站点时,一切正常,除了以下几点:

The resulting angular.json file only includes a single style file in the styles array, I need it to include multiple custom style files: 生成的angular.json文件在styles数组中仅包含一个样式文件,我需要它包含多个自定义样式文件:

Current angular.json after ng new schematics build: 在构建新原理图之后的当前angular.json:

"build": {
  "options": {
    "styles: [
      "src/styles.scss"
    ]
  }
}

Desired: 期望:

"build": {
  "options": {
    "styles: [
      "src/styles.scss",
      "src/custom1.scss",
      "src/custom2.scss"
    ]
  }
}

How can I tell the angular schematics routine that I want to include these additional stylesheets? 如何告诉角度示意图例程要包含这些其他样式表?

I'm not sure if this is the best practice or not, but I recently had to figure this out also... Here's what I did... 我不确定这是否是最佳做法,但最近我也不得不弄清楚这是...我所做的...

 function updateAngularJson(options: any): Rule { return (host: Tree, context: SchematicContext) => { updateAngularJsonOptions(host, options); return host; } } function updateAngularJsonOptions(host, options) { var projectDir = (options.directory || options.name) + '/'; var configPath = projectDir + 'angular.json'; var workspace = getWorkspace(host, projectDirectory); if (host.exists(configPath)) { var currentAngularJson = host.read(configPath)!.toString('utf-8'); var json = JSON.parse(currentAngularJson); var optionsJson = json['projects'][options.name]['architect']['build']['options']; optionsJson['assets'].push("src/custom1.scss"); optionsJson['assets'].push("src/custom2.scss"); json['projects'][options.name]['architect']['build']['options'] = optionsJson; host.overwrite(configPath, JSON.stringify(json, null, 2)); } else { throw new SchematicsException('angular.json not found at ' + configPath); } return host; } 

Double check the code above - I can't copy/paste my working solution for reasons, so I've typed this out for the purpose of trying to assist.. Hopefully you get the idea from the above... Works pretty well for me... 请仔细检查上面的代码-由于某种原因,我无法复制/粘贴我的工作解决方案,因此我为寻求帮助而键入了此代码。希望您能从上面得到启发...对于我...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM