From e36d92fc65ba7acbf108c272d3e91daf331788e1 Mon Sep 17 00:00:00 2001 From: Alex Eagle Date: Thu, 23 Aug 2018 17:48:46 -0700 Subject: [PATCH] fix(compiler-cli): adapt for removal of tsickle --- packages/compiler-cli/src/main.ts | 44 ++++++++++++++++---------- packages/compiler-cli/test/ngc_spec.ts | 19 +++++------ 2 files changed, 38 insertions(+), 25 deletions(-) diff --git a/packages/compiler-cli/src/main.ts b/packages/compiler-cli/src/main.ts index d70bd458967406..3c6f057e13af54 100644 --- a/packages/compiler-cli/src/main.ts +++ b/packages/compiler-cli/src/main.ts @@ -63,22 +63,34 @@ function createEmitCallback(options: api.CompilerOptions): api.TsEmitCallback|un convertIndexImportShorthand: false, transformDecorators, transformTypesToClosure, }; - return ({ - program, - targetSourceFile, - writeFile, - cancellationToken, - emitOnlyDtsFiles, - customTransformers = {}, - host, - options - }) => - tsickle.emitWithTsickle( - program, {...tsickleHost, options, host}, host, options, targetSourceFile, - writeFile, cancellationToken, emitOnlyDtsFiles, { - beforeTs: customTransformers.before, - afterTs: customTransformers.after, - }); + if (options.annotateForClosureCompiler || options.annotationsAs === 'static fields') { + return ({ + program, + targetSourceFile, + writeFile, + cancellationToken, + emitOnlyDtsFiles, + customTransformers = {}, + host, + options + }) => + // tslint:disable-next-line:no-require-imports only depend on tsickle if requested + require('tsickle').emitWithTsickle( + program, {...tsickleHost, options, host}, host, options, targetSourceFile, + writeFile, cancellationToken, emitOnlyDtsFiles, { + beforeTs: customTransformers.before, + afterTs: customTransformers.after, + }); + } else { + return ({ + program, + targetSourceFile, + writeFile, + cancellationToken, + emitOnlyDtsFiles, + customTransformers = {}, + }) => program.emit(targetSourceFile, writeFile, cancellationToken, emitOnlyDtsFiles, {after: customTransformers.after, before: customTransformers.before}); + } } export interface NgcParsedConfiguration extends ParsedConfiguration { watch?: boolean; } diff --git a/packages/compiler-cli/test/ngc_spec.ts b/packages/compiler-cli/test/ngc_spec.ts index ca4d822aa0dcf2..a874c5b89c578a 100644 --- a/packages/compiler-cli/test/ngc_spec.ts +++ b/packages/compiler-cli/test/ngc_spec.ts @@ -554,10 +554,10 @@ describe('ngc transformer command-line', () => { }); describe('closure', () => { - it('should not generate closure specific code by default', () => { + it('should not run tsickle by default', () => { writeConfig(`{ "extends": "./tsconfig-base.json", - "files": ["mymodule.ts"] + "files": ["mymodule.ts"], }`); write('mymodule.ts', ` import {NgModule, Component} from '@angular/core'; @@ -575,7 +575,8 @@ describe('ngc transformer command-line', () => { const mymodulejs = path.resolve(outDir, 'mymodule.js'); const mymoduleSource = fs.readFileSync(mymodulejs, 'utf8'); expect(mymoduleSource).not.toContain('@fileoverview added by tsickle'); - expect(mymoduleSource).toContain('MyComp.decorators = ['); + expect(mymoduleSource).toContain('MyComp = __decorate'); + expect(mymoduleSource).not.toContain('MyComp.decorators = ['); }); it('should add closure annotations', () => { @@ -873,7 +874,7 @@ describe('ngc transformer command-line', () => { write('mymodule.ts', ` import {Component, NgModule} from '@angular/core'; import {RouterModule} from '@angular/router'; - + export function foo(): string { console.log('side-effect'); return 'test'; @@ -2373,7 +2374,7 @@ describe('ngc transformer command-line', () => { } }`); write('service.ts', ` - import {Injectable, Self} from '@angular/core'; + import {Injectable, Self} from '@angular/core'; @Injectable() export class ServiceA {} @@ -2388,10 +2389,10 @@ describe('ngc transformer command-line', () => { const modulePath = path.resolve(outDir, 'service.js'); const moduleSource = fs.readFileSync(modulePath, 'utf8'); - expect(moduleSource).not.toMatch(/ServiceA\.decorators =/); - expect(moduleSource).toMatch(/ServiceB\.decorators =/); - expect(moduleSource).toMatch(/type: Self/); - expect(moduleSource).not.toMatch(/type: Injectable/); + expect(moduleSource).toMatch(/ServiceA = __decorate/); + expect(moduleSource).not.toMatch(/ServiceB == __decorate/); + expect(moduleSource).not.toMatch(/Self\(\)/); + expect(moduleSource).toMatch(/Injectable\(\)/); }); it('rewrites Injector to INJECTOR in Ivy factory functions ', () => {