migration.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. "use strict";
  2. /**
  3. * @license
  4. * Copyright Google LLC All Rights Reserved.
  5. *
  6. * Use of this source code is governed by an MIT-style license that can be
  7. * found in the LICENSE file at https://angular.dev/license
  8. */
  9. Object.defineProperty(exports, "__esModule", { value: true });
  10. exports.default = default_1;
  11. const json_file_1 = require("../../utility/json-file");
  12. const workspace_1 = require("../../utility/workspace");
  13. function default_1() {
  14. return async (host) => {
  15. const uniqueTsConfigs = new Set();
  16. if (host.exists('tsconfig.json')) {
  17. // Workspace level tsconfig
  18. uniqueTsConfigs.add('tsconfig.json');
  19. }
  20. const workspace = await (0, workspace_1.getWorkspace)(host);
  21. for (const [, target] of (0, workspace_1.allWorkspaceTargets)(workspace)) {
  22. for (const [, opt] of (0, workspace_1.allTargetOptions)(target)) {
  23. if (typeof opt?.tsConfig === 'string') {
  24. uniqueTsConfigs.add(opt.tsConfig);
  25. }
  26. }
  27. }
  28. for (const tsConfig of uniqueTsConfigs) {
  29. if (host.exists(tsConfig)) {
  30. updateModuleResolution(host, tsConfig);
  31. }
  32. }
  33. };
  34. }
  35. function updateModuleResolution(host, tsConfigPath) {
  36. const json = new json_file_1.JSONFile(host, tsConfigPath);
  37. const jsonPath = ['compilerOptions'];
  38. const compilerOptions = json.get(jsonPath);
  39. if (compilerOptions && typeof compilerOptions === 'object') {
  40. const { moduleResolution, module } = compilerOptions;
  41. if (typeof moduleResolution !== 'string' || moduleResolution.toLowerCase() === 'bundler') {
  42. return;
  43. }
  44. if (typeof module === 'string' && module.toLowerCase() === 'preserve') {
  45. return;
  46. }
  47. json.modify(jsonPath, {
  48. ...compilerOptions,
  49. 'moduleResolution': 'bundler',
  50. });
  51. }
  52. }