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

时间:2018-06-28 14:39:38

标签: angular angular-schematics

当我使用原理图(ng new --collection = @ foo / schematics myproject)构建站点时,一切正常,除了一件事:

生成的angular.json文件在styles数组中仅包含一个样式文件,我需要它包含多个自定义样式文件:

在生成新原理图之后的当前angular.json:

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

所需:

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

如何告诉角度示意图例程要包含这些其他样式表?

1 个答案:

答案 0 :(得分:2)

我不确定这是否是最佳做法,但最近我也不得不弄清楚这是...我所做的...

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;
}

再次检查上面的代码-由于某种原因,我无法复制/粘贴我的工作解决方案,因此我尝试输入此内容是为了帮助您。希望您能从上面得到...的想法对我来说很好...

相关问题