Skip to content

Commit 27f535e

Browse files
committed
fix(@schematics/angular): do not set esModuleInterop and moduleResolution when module is preserve
1 parent 61a027d commit 27f535e

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

packages/schematics/angular/migrations/use-application-builder/migration.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -362,10 +362,16 @@ export default function (): Rule {
362362
),
363363
// Update main tsconfig
364364
updateJsonFile('tsconfig.json', (rootJson) => {
365-
rootJson.modify(['compilerOptions', 'esModuleInterop'], true);
365+
const module = rootJson.get(['compilerOptions', 'module']);
366+
const hasPreserveModule = typeof module === 'string' && module.toLowerCase() === 'preserve';
367+
368+
if (!hasPreserveModule) {
369+
rootJson.modify(['compilerOptions', 'esModuleInterop'], true);
370+
rootJson.modify(['compilerOptions', 'moduleResolution'], 'bundler');
371+
}
372+
366373
rootJson.modify(['compilerOptions', 'downlevelIteration'], undefined);
367374
rootJson.modify(['compilerOptions', 'allowSyntheticDefaultImports'], undefined);
368-
rootJson.modify(['compilerOptions', 'moduleResolution'], 'bundler');
369375
}),
370376
]);
371377
}

packages/schematics/angular/migrations/use-application-builder/migration_spec.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* found in the LICENSE file at https://angular.dev/license
77
*/
88

9+
import { isJsonObject } from '@angular-devkit/core';
910
import { EmptyTree } from '@angular-devkit/schematics';
1011
import { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/testing';
1112
import { Builders, ProjectType, WorkspaceSchema } from '../../utility/workspace-models';
@@ -36,7 +37,10 @@ function createWorkSpaceConfig(tree: UnitTestTree) {
3637
};
3738

3839
tree.create('/angular.json', JSON.stringify(angularConfig, undefined, 2));
39-
tree.create('/tsconfig.json', JSON.stringify({}, undefined, 2));
40+
tree.create(
41+
'/tsconfig.json',
42+
JSON.stringify({ compilerOptions: { module: 'preserve' } }, undefined, 2),
43+
);
4044
tree.create('/package.json', JSON.stringify({}, undefined, 2));
4145
}
4246

@@ -49,6 +53,23 @@ function addWorkspaceTarget(tree: UnitTestTree, targetName: string, targetEntry:
4953
tree.overwrite('/angular.json', JSON.stringify(workspaceContent));
5054
}
5155

56+
function getCompilerOptionsValue(tree: UnitTestTree, filePath: string): Record<string, unknown> {
57+
const json = tree.readJson(filePath);
58+
if (isJsonObject(json) && isJsonObject(json.compilerOptions)) {
59+
return json.compilerOptions;
60+
}
61+
62+
throw new Error(`Cannot retrieve 'compilerOptions'.`);
63+
}
64+
65+
function createJsonFile(tree: UnitTestTree, filePath: string, content: {}): void {
66+
const stringifiedContent = JSON.stringify(content, undefined, 2);
67+
if (tree.exists(filePath)) {
68+
tree.overwrite(filePath, stringifiedContent);
69+
} else {
70+
tree.create(filePath, stringifiedContent);
71+
}
72+
}
5273
describe(`Migration to use the application builder`, () => {
5374
const schematicName = 'use-application-builder';
5475
const schematicRunner = new SchematicTestRunner(
@@ -448,4 +469,14 @@ describe(`Migration to use the application builder`, () => {
448469

449470
expect(devDependencies['postcss']).toBeUndefined();
450471
});
472+
473+
it('it should not add esModuleInterop and moduleResolution when module is preserve', async () => {
474+
createJsonFile(tree, 'tsconfig.json', {
475+
compilerOptions: { module: 'preserve' },
476+
});
477+
const newTree = await schematicRunner.runSchematic(schematicName, {}, tree);
478+
479+
const compilerOptions = getCompilerOptionsValue(newTree, 'tsconfig.json');
480+
expect(compilerOptions).toEqual({ module: 'preserve' });
481+
});
451482
});

0 commit comments

Comments
 (0)