From 46e2df1b66a6b5628ff4632b182c3283619b14e7 Mon Sep 17 00:00:00 2001 From: Charles Lyding <19598772+clydin@users.noreply.github.com> Date: Fri, 19 Apr 2019 14:57:39 -0400 Subject: [PATCH] fix(@schematics/angular): migrate TS module type to esnext --- .../update-8/differential-loading.ts | 23 ++++++++++++------- .../update-8/differential-loading_spec.ts | 18 +++++++++++++++ 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/packages/schematics/angular/migrations/update-8/differential-loading.ts b/packages/schematics/angular/migrations/update-8/differential-loading.ts index dcc7084a058d..da953a25f000 100644 --- a/packages/schematics/angular/migrations/update-8/differential-loading.ts +++ b/packages/schematics/angular/migrations/update-8/differential-loading.ts @@ -53,19 +53,26 @@ export function updateES5Projects(): Rule { return host; } - const scriptTarget = findPropertyInAstObject(compilerOptions, 'target'); - if (scriptTarget && scriptTarget.value === 'es2015') { - return host; - } - const recorder = host.beginUpdate(tsConfigPath); - if (scriptTarget) { + + const scriptTarget = findPropertyInAstObject(compilerOptions, 'target'); + if (!scriptTarget) { + insertPropertyInAstObjectInOrder(recorder, compilerOptions, 'target', 'es2015', 4); + } else if (scriptTarget.value !== 'es2015') { const { start, end } = scriptTarget; recorder.remove(start.offset, end.offset - start.offset); recorder.insertLeft(start.offset, '"es2015"'); - } else { - insertPropertyInAstObjectInOrder(recorder, compilerOptions, 'target', 'es2015', 4); } + + const scriptModule = findPropertyInAstObject(compilerOptions, 'module'); + if (!scriptModule) { + insertPropertyInAstObjectInOrder(recorder, compilerOptions, 'module', 'esnext', 4); + } else if (scriptModule.value !== 'esnext') { + const { start, end } = scriptModule; + recorder.remove(start.offset, end.offset - start.offset); + recorder.insertLeft(start.offset, '"esnext"'); + } + host.commitUpdate(recorder); return updateBrowserlist; diff --git a/packages/schematics/angular/migrations/update-8/differential-loading_spec.ts b/packages/schematics/angular/migrations/update-8/differential-loading_spec.ts index d326e30752b3..3242389c69d4 100644 --- a/packages/schematics/angular/migrations/update-8/differential-loading_spec.ts +++ b/packages/schematics/angular/migrations/update-8/differential-loading_spec.ts @@ -62,6 +62,24 @@ describe('Migration to version 8', () => { expect(target).toBe('es2015'); }); + it(`should update 'module' to esnext when property exists`, () => { + const tree2 = schematicRunner.runSchematic('migration-07', {}, tree.branch()); + const { module } = JSON.parse(tree2.readContent(tsConfigPath)).compilerOptions; + expect(module).toBe('esnext'); + }); + + it(`should create 'module' property when doesn't exists`, () => { + const compilerOptions = { + ...oldTsConfig.compilerOptions, + module: undefined, + }; + + tree.overwrite(tsConfigPath, JSON.stringify({ compilerOptions }, null, 2)); + const tree2 = schematicRunner.runSchematic('migration-07', {}, tree.branch()); + const { module } = JSON.parse(tree2.readContent(tsConfigPath)).compilerOptions; + expect(module).toBe('esnext'); + }); + it(`should update browserslist file to add an non evergreen browser`, () => { const tree2 = schematicRunner.runSchematic('migration-07', {}, tree.branch()); expect(tree2.readContent('/browserslist')).toContain('Chrome 41');