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.io/license
|
---|
8 | */
|
---|
9 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
10 | exports.updateApplicationTsConfigs = void 0;
|
---|
11 | const core_1 = require("@angular-devkit/core");
|
---|
12 | const path_1 = require("path");
|
---|
13 | const json_file_1 = require("../../utility/json-file");
|
---|
14 | const workspace_1 = require("../../utility/workspace");
|
---|
15 | const workspace_models_1 = require("../../utility/workspace-models");
|
---|
16 | const utils_1 = require("./utils");
|
---|
17 | /**
|
---|
18 | * Update the tsconfig files for applications
|
---|
19 | * - Removes enableIvy: true
|
---|
20 | * - Sets stricter file inclusions
|
---|
21 | * - Sets module compiler option to esnext or commonjs
|
---|
22 | */
|
---|
23 | function updateApplicationTsConfigs() {
|
---|
24 | return async (tree, { logger }) => {
|
---|
25 | const workspace = await workspace_1.getWorkspace(tree);
|
---|
26 | // Add `module` option in the workspace tsconfig
|
---|
27 | updateModuleCompilerOption(tree, '/tsconfig.json');
|
---|
28 | for (const [targetName, target, , project] of workspace_1.allWorkspaceTargets(workspace)) {
|
---|
29 | switch (targetName) {
|
---|
30 | case 'build':
|
---|
31 | if (target.builder !== workspace_models_1.Builders.Browser) {
|
---|
32 | continue;
|
---|
33 | }
|
---|
34 | break;
|
---|
35 | case 'server':
|
---|
36 | if (target.builder !== workspace_models_1.Builders.Server) {
|
---|
37 | continue;
|
---|
38 | }
|
---|
39 | break;
|
---|
40 | case 'test':
|
---|
41 | if (target.builder !== workspace_models_1.Builders.Karma) {
|
---|
42 | continue;
|
---|
43 | }
|
---|
44 | break;
|
---|
45 | default:
|
---|
46 | continue;
|
---|
47 | }
|
---|
48 | updateTsConfig(tree, target, project.sourceRoot, logger);
|
---|
49 | }
|
---|
50 | };
|
---|
51 | }
|
---|
52 | exports.updateApplicationTsConfigs = updateApplicationTsConfigs;
|
---|
53 | function updateTsConfig(tree, builderConfig, projectSourceRoot, logger) {
|
---|
54 | for (const [, options] of workspace_1.allTargetOptions(builderConfig)) {
|
---|
55 | const tsConfigPath = options.tsConfig;
|
---|
56 | if (!tsConfigPath || typeof tsConfigPath !== 'string') {
|
---|
57 | continue;
|
---|
58 | }
|
---|
59 | // Update 'module' compilerOption
|
---|
60 | updateModuleCompilerOption(tree, tsConfigPath, builderConfig.builder);
|
---|
61 | let tsConfigJson;
|
---|
62 | try {
|
---|
63 | tsConfigJson = new json_file_1.JSONFile(tree, tsConfigPath);
|
---|
64 | }
|
---|
65 | catch {
|
---|
66 | logger.warn(`Cannot find file: ${tsConfigPath}`);
|
---|
67 | continue;
|
---|
68 | }
|
---|
69 | // Remove 'enableIvy: true' since this is the default in version 9.
|
---|
70 | if (tsConfigJson.get(['angularCompilerOptions', 'enableIvy']) === true) {
|
---|
71 | const angularCompilerOptions = tsConfigJson.get(['angularCompilerOptions']);
|
---|
72 | const keys = Object.keys(angularCompilerOptions);
|
---|
73 | if (keys.length === 1) {
|
---|
74 | // remove entire 'angularCompilerOptions'
|
---|
75 | tsConfigJson.remove(['angularCompilerOptions']);
|
---|
76 | }
|
---|
77 | else {
|
---|
78 | // leave other options
|
---|
79 | tsConfigJson.remove(['angularCompilerOptions', 'enableIvy']);
|
---|
80 | }
|
---|
81 | }
|
---|
82 | // Add stricter file inclusions to avoid unused file warning during compilation
|
---|
83 | if (builderConfig.builder !== workspace_models_1.Builders.Karma) {
|
---|
84 | const include = tsConfigJson.get(['include']);
|
---|
85 | if (include && Array.isArray(include)) {
|
---|
86 | const tsInclude = include.findIndex((value) => typeof value === 'string' && value.endsWith('**/*.ts'));
|
---|
87 | if (tsInclude !== -1) {
|
---|
88 | // Replace ts includes with d.ts
|
---|
89 | tsConfigJson.modify(['include', tsInclude], include[tsInclude].replace('.ts', '.d.ts'));
|
---|
90 | }
|
---|
91 | }
|
---|
92 | else {
|
---|
93 | // Includes are not present, add includes to dts files
|
---|
94 | // By default when 'include' nor 'files' fields are used TypeScript
|
---|
95 | // will include all ts files.
|
---|
96 | const include = projectSourceRoot !== undefined
|
---|
97 | ? core_1.join(core_1.normalize(projectSourceRoot), '**/*.d.ts')
|
---|
98 | : '**/*.d.ts';
|
---|
99 | tsConfigJson.modify(['include'], [include]);
|
---|
100 | }
|
---|
101 | const files = tsConfigJson.get(['files']);
|
---|
102 | if (files === undefined) {
|
---|
103 | const newFiles = [];
|
---|
104 | const tsConfigDir = path_1.dirname(utils_1.forwardSlashPath(tsConfigPath));
|
---|
105 | const mainOption = options.main;
|
---|
106 | if (mainOption && typeof mainOption === 'string') {
|
---|
107 | newFiles.push(utils_1.forwardSlashPath(path_1.relative(tsConfigDir, utils_1.forwardSlashPath(mainOption))));
|
---|
108 | }
|
---|
109 | const polyfillsOption = options.polyfills;
|
---|
110 | if (polyfillsOption && typeof polyfillsOption === 'string') {
|
---|
111 | newFiles.push(utils_1.forwardSlashPath(path_1.relative(tsConfigDir, utils_1.forwardSlashPath(polyfillsOption))));
|
---|
112 | }
|
---|
113 | if (newFiles.length) {
|
---|
114 | tsConfigJson.modify(['files'], newFiles);
|
---|
115 | }
|
---|
116 | tsConfigJson.remove(['exclude']);
|
---|
117 | }
|
---|
118 | }
|
---|
119 | }
|
---|
120 | }
|
---|
121 | function updateModuleCompilerOption(tree, tsConfigPath, builderName) {
|
---|
122 | let tsConfigJson;
|
---|
123 | try {
|
---|
124 | tsConfigJson = new json_file_1.JSONFile(tree, tsConfigPath);
|
---|
125 | }
|
---|
126 | catch {
|
---|
127 | return;
|
---|
128 | }
|
---|
129 | const compilerOptions = tsConfigJson.get(['compilerOptions']);
|
---|
130 | if (!compilerOptions || typeof compilerOptions !== 'object') {
|
---|
131 | return;
|
---|
132 | }
|
---|
133 | const configExtends = tsConfigJson.get(['extends']);
|
---|
134 | const isExtended = configExtends && typeof configExtends === 'string';
|
---|
135 | // Server tsconfig should have a module of commonjs
|
---|
136 | const moduleType = builderName === workspace_models_1.Builders.Server ? 'commonjs' : 'esnext';
|
---|
137 | if (isExtended && builderName !== workspace_models_1.Builders.Server) {
|
---|
138 | tsConfigJson.remove(['compilerOptions', 'module']);
|
---|
139 | }
|
---|
140 | else {
|
---|
141 | tsConfigJson.modify(['compilerOptions', 'module'], moduleType);
|
---|
142 | }
|
---|
143 | }
|
---|