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 | const core_1 = require("@angular-devkit/core");
|
---|
11 | const json_file_1 = require("../../utility/json-file");
|
---|
12 | const workspace_1 = require("../../utility/workspace");
|
---|
13 | const workspace_models_1 = require("../../utility/workspace-models");
|
---|
14 | function default_1() {
|
---|
15 | return async (host, { logger }) => {
|
---|
16 | var _a;
|
---|
17 | // Workspace level tsconfig
|
---|
18 | try {
|
---|
19 | updateModuleAndTarget(host, 'tsconfig.json', {
|
---|
20 | oldModule: 'esnext',
|
---|
21 | newModule: 'es2020',
|
---|
22 | });
|
---|
23 | }
|
---|
24 | catch (error) {
|
---|
25 | logger.warn(`Unable to update 'tsconfig.json' module option from 'esnext' to 'es2020': ${error.message || error}`);
|
---|
26 | }
|
---|
27 | const workspace = await workspace_1.getWorkspace(host);
|
---|
28 | // Find all tsconfig which are refereces used by builders
|
---|
29 | for (const [, project] of workspace.projects) {
|
---|
30 | for (const [, target] of project.targets) {
|
---|
31 | // E2E builder doesn't reference a tsconfig but it uses one found in the root folder.
|
---|
32 | if (target.builder === workspace_models_1.Builders.Protractor &&
|
---|
33 | typeof ((_a = target.options) === null || _a === void 0 ? void 0 : _a.protractorConfig) === 'string') {
|
---|
34 | const tsConfigPath = core_1.join(core_1.dirname(core_1.normalize(target.options.protractorConfig)), 'tsconfig.json');
|
---|
35 | try {
|
---|
36 | updateModuleAndTarget(host, tsConfigPath, {
|
---|
37 | oldTarget: 'es5',
|
---|
38 | newTarget: 'es2018',
|
---|
39 | });
|
---|
40 | }
|
---|
41 | catch (error) {
|
---|
42 | logger.warn(`Unable to update '${tsConfigPath}' target option from 'es5' to 'es2018': ${error.message || error}`);
|
---|
43 | }
|
---|
44 | continue;
|
---|
45 | }
|
---|
46 | // Update all other known CLI builders that use a tsconfig
|
---|
47 | const tsConfigs = [target.options || {}, ...Object.values(target.configurations || {})]
|
---|
48 | .filter((opt) => typeof (opt === null || opt === void 0 ? void 0 : opt.tsConfig) === 'string')
|
---|
49 | .map((opt) => opt.tsConfig);
|
---|
50 | const uniqueTsConfigs = [...new Set(tsConfigs)];
|
---|
51 | if (uniqueTsConfigs.length < 1) {
|
---|
52 | continue;
|
---|
53 | }
|
---|
54 | switch (target.builder) {
|
---|
55 | case workspace_models_1.Builders.Server:
|
---|
56 | uniqueTsConfigs.forEach((p) => {
|
---|
57 | try {
|
---|
58 | updateModuleAndTarget(host, p, {
|
---|
59 | oldModule: 'commonjs',
|
---|
60 | // False will remove the module
|
---|
61 | // NB: For server we no longer use commonjs because it is bundled using webpack which has it's own module system.
|
---|
62 | // This ensures that lazy-loaded works on the server.
|
---|
63 | newModule: false,
|
---|
64 | });
|
---|
65 | }
|
---|
66 | catch (error) {
|
---|
67 | logger.warn(`Unable to remove '${p}' module option (was 'commonjs'): ${error.message || error}`);
|
---|
68 | }
|
---|
69 | try {
|
---|
70 | updateModuleAndTarget(host, p, {
|
---|
71 | newTarget: 'es2016',
|
---|
72 | });
|
---|
73 | }
|
---|
74 | catch (error) {
|
---|
75 | logger.warn(`Unable to update '${p}' target option to 'es2016': ${error.message || error}`);
|
---|
76 | }
|
---|
77 | });
|
---|
78 | break;
|
---|
79 | case workspace_models_1.Builders.Karma:
|
---|
80 | case workspace_models_1.Builders.Browser:
|
---|
81 | case workspace_models_1.Builders.DeprecatedNgPackagr:
|
---|
82 | uniqueTsConfigs.forEach((p) => {
|
---|
83 | try {
|
---|
84 | updateModuleAndTarget(host, p, {
|
---|
85 | oldModule: 'esnext',
|
---|
86 | newModule: 'es2020',
|
---|
87 | });
|
---|
88 | }
|
---|
89 | catch (error) {
|
---|
90 | logger.warn(`Unable to update '${p}' module option from 'esnext' to 'es2020': ${error.message || error}`);
|
---|
91 | }
|
---|
92 | });
|
---|
93 | break;
|
---|
94 | }
|
---|
95 | }
|
---|
96 | }
|
---|
97 | };
|
---|
98 | }
|
---|
99 | exports.default = default_1;
|
---|
100 | function updateModuleAndTarget(host, tsConfigPath, replacements) {
|
---|
101 | const json = new json_file_1.JSONFile(host, tsConfigPath);
|
---|
102 | const { oldTarget, newTarget, newModule, oldModule } = replacements;
|
---|
103 | if (newTarget) {
|
---|
104 | const target = json.get(['compilerOptions', 'target']);
|
---|
105 | if ((typeof target === 'string' && (!oldTarget || oldTarget === target.toLowerCase())) ||
|
---|
106 | !target) {
|
---|
107 | json.modify(['compilerOptions', 'target'], newTarget);
|
---|
108 | }
|
---|
109 | }
|
---|
110 | if (newModule === false) {
|
---|
111 | json.remove(['compilerOptions', 'module']);
|
---|
112 | }
|
---|
113 | else if (newModule) {
|
---|
114 | const module = json.get(['compilerOptions', 'module']);
|
---|
115 | if (typeof module === 'string' && oldModule === module.toLowerCase()) {
|
---|
116 | json.modify(['compilerOptions', 'module'], newModule);
|
---|
117 | }
|
---|
118 | }
|
---|
119 | }
|
---|