ngsw-config.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const json_utils_1 = require("../../utility/json-utils");
  4. const workspace_models_1 = require("../../utility/workspace-models");
  5. const utils_1 = require("./utils");
  6. /**
  7. * Update ngsw-config.json to fix issue https://github.com/angular/angular-cli/pull/15277
  8. */
  9. function updateNGSWConfig() {
  10. return (tree, context) => {
  11. const workspace = utils_1.getWorkspace(tree);
  12. const logger = context.logger;
  13. for (const { target } of utils_1.getTargets(workspace, 'build', workspace_models_1.Builders.Browser)) {
  14. for (const options of utils_1.getAllOptions(target)) {
  15. const ngswConfigPath = json_utils_1.findPropertyInAstObject(options, 'ngswConfigPath');
  16. if (!ngswConfigPath || ngswConfigPath.kind !== 'string') {
  17. continue;
  18. }
  19. const path = ngswConfigPath.value;
  20. const ngswConfigAst = utils_1.readJsonFileAsAstObject(tree, path);
  21. if (!ngswConfigAst || ngswConfigAst.kind !== 'object') {
  22. logger.warn(`Cannot find file: ${ngswConfigPath}`);
  23. continue;
  24. }
  25. const assetGroups = json_utils_1.findPropertyInAstObject(ngswConfigAst, 'assetGroups');
  26. if (!assetGroups || assetGroups.kind !== 'array') {
  27. continue;
  28. }
  29. const prefetchElement = assetGroups.elements.find(element => {
  30. const installMode = element.kind === 'object' && json_utils_1.findPropertyInAstObject(element, 'installMode');
  31. return installMode && installMode.value === 'prefetch';
  32. });
  33. if (!prefetchElement || prefetchElement.kind !== 'object') {
  34. continue;
  35. }
  36. const resources = json_utils_1.findPropertyInAstObject(prefetchElement, 'resources');
  37. if (!resources || resources.kind !== 'object') {
  38. continue;
  39. }
  40. const files = json_utils_1.findPropertyInAstObject(resources, 'files');
  41. if (!files || files.kind !== 'array') {
  42. continue;
  43. }
  44. const hasManifest = files.elements
  45. .some(({ value }) => typeof value === 'string' && value.endsWith('manifest.webmanifest'));
  46. if (hasManifest) {
  47. continue;
  48. }
  49. const recorder = tree.beginUpdate(path);
  50. json_utils_1.appendValueInAstArray(recorder, files, '/manifest.webmanifest', 10);
  51. tree.commitUpdate(recorder);
  52. }
  53. }
  54. return tree;
  55. };
  56. }
  57. exports.updateNGSWConfig = updateNGSWConfig;