update-workspace-config.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const config_1 = require("../../utility/config");
  4. const json_utils_1 = require("../../utility/json-utils");
  5. const workspace_models_1 = require("../../utility/workspace-models");
  6. const utils_1 = require("./utils");
  7. exports.ANY_COMPONENT_STYLE_BUDGET = {
  8. type: 'anyComponentStyle',
  9. maximumWarning: '6kb',
  10. };
  11. function updateWorkspaceConfig() {
  12. return (tree, context) => {
  13. const workspacePath = config_1.getWorkspacePath(tree);
  14. const workspace = utils_1.getWorkspace(tree);
  15. const recorder = tree.beginUpdate(workspacePath);
  16. for (const { target } of utils_1.getTargets(workspace, 'build', workspace_models_1.Builders.Browser)) {
  17. updateStyleOrScriptOption('styles', recorder, target);
  18. updateStyleOrScriptOption('scripts', recorder, target);
  19. addAnyComponentStyleBudget(recorder, target);
  20. updateAotOption(tree, recorder, target);
  21. }
  22. for (const { target } of utils_1.getTargets(workspace, 'test', workspace_models_1.Builders.Karma)) {
  23. updateStyleOrScriptOption('styles', recorder, target);
  24. updateStyleOrScriptOption('scripts', recorder, target);
  25. }
  26. for (const { target } of utils_1.getTargets(workspace, 'server', workspace_models_1.Builders.Server)) {
  27. updateOptimizationOption(recorder, target);
  28. }
  29. tree.commitUpdate(recorder);
  30. return tree;
  31. };
  32. }
  33. exports.updateWorkspaceConfig = updateWorkspaceConfig;
  34. function updateAotOption(tree, recorder, builderConfig) {
  35. const options = json_utils_1.findPropertyInAstObject(builderConfig, 'options');
  36. if (!options || options.kind !== 'object') {
  37. return;
  38. }
  39. const tsConfig = json_utils_1.findPropertyInAstObject(options, 'tsConfig');
  40. // Do not add aot option if the users already opted out from Ivy.
  41. if (tsConfig && tsConfig.kind === 'string' && !utils_1.isIvyEnabled(tree, tsConfig.value)) {
  42. return;
  43. }
  44. // Add aot to options.
  45. const aotOption = json_utils_1.findPropertyInAstObject(options, 'aot');
  46. if (!aotOption) {
  47. json_utils_1.insertPropertyInAstObjectInOrder(recorder, options, 'aot', true, 12);
  48. return;
  49. }
  50. if (aotOption.kind !== 'true') {
  51. const { start, end } = aotOption;
  52. recorder.remove(start.offset, end.offset - start.offset);
  53. recorder.insertLeft(start.offset, 'true');
  54. }
  55. // Remove aot properties from other configurations as they are no redundant
  56. const configOptions = utils_1.getAllOptions(builderConfig, true);
  57. for (const options of configOptions) {
  58. json_utils_1.removePropertyInAstObject(recorder, options, 'aot');
  59. }
  60. }
  61. function updateStyleOrScriptOption(property, recorder, builderConfig) {
  62. const options = utils_1.getAllOptions(builderConfig);
  63. for (const option of options) {
  64. const propertyOption = json_utils_1.findPropertyInAstObject(option, property);
  65. if (!propertyOption || propertyOption.kind !== 'array') {
  66. continue;
  67. }
  68. for (const node of propertyOption.elements) {
  69. if (!node || node.kind !== 'object') {
  70. // skip non complex objects
  71. continue;
  72. }
  73. const lazy = json_utils_1.findPropertyInAstObject(node, 'lazy');
  74. json_utils_1.removePropertyInAstObject(recorder, node, 'lazy');
  75. // if lazy was not true, it is redundant hence, don't add it
  76. if (lazy && lazy.kind === 'true') {
  77. json_utils_1.insertPropertyInAstObjectInOrder(recorder, node, 'inject', false, 0);
  78. }
  79. }
  80. }
  81. }
  82. function addAnyComponentStyleBudget(recorder, builderConfig) {
  83. const options = utils_1.getAllOptions(builderConfig, true);
  84. for (const option of options) {
  85. const budgetOption = json_utils_1.findPropertyInAstObject(option, 'budgets');
  86. if (!budgetOption) {
  87. // add
  88. json_utils_1.insertPropertyInAstObjectInOrder(recorder, option, 'budgets', [exports.ANY_COMPONENT_STYLE_BUDGET], 14);
  89. continue;
  90. }
  91. if (budgetOption.kind !== 'array') {
  92. continue;
  93. }
  94. // if 'anyComponentStyle' budget already exists don't add.
  95. const hasAnyComponentStyle = budgetOption.elements.some(node => {
  96. if (!node || node.kind !== 'object') {
  97. // skip non complex objects
  98. return false;
  99. }
  100. const budget = json_utils_1.findPropertyInAstObject(node, 'type');
  101. return !!budget && budget.kind === 'string' && budget.value === 'anyComponentStyle';
  102. });
  103. if (!hasAnyComponentStyle) {
  104. json_utils_1.appendValueInAstArray(recorder, budgetOption, exports.ANY_COMPONENT_STYLE_BUDGET, 16);
  105. }
  106. }
  107. }
  108. function updateOptimizationOption(recorder, builderConfig) {
  109. const options = utils_1.getAllOptions(builderConfig, true);
  110. for (const option of options) {
  111. const optimizationOption = json_utils_1.findPropertyInAstObject(option, 'optimization');
  112. if (!optimizationOption) {
  113. // add
  114. json_utils_1.insertPropertyInAstObjectInOrder(recorder, option, 'optimization', true, 14);
  115. continue;
  116. }
  117. if (optimizationOption.kind !== 'true') {
  118. const { start, end } = optimizationOption;
  119. recorder.remove(start.offset, end.offset - start.offset);
  120. recorder.insertLeft(start.offset, 'true');
  121. }
  122. }
  123. }