fix(ivy): ngtsc program emit ignoring custom transformers (#27837)

Fixes the `customTransformers` that are passed to the `NgtscProgram.emit` not being passed along.

PR Close #27837
This commit is contained in:
Kristiyan Kostadinov 2019-01-03 12:23:00 +02:00 committed by Kara Erickson
parent 48555f95c6
commit 13d23f315b
4 changed files with 50 additions and 9 deletions

View File

@ -22,7 +22,7 @@ import {performWatchCompilation, createPerformWatchHost} from './perform_watch'
export function main( export function main(
args: string[], consoleError: (s: string) => void = console.error, args: string[], consoleError: (s: string) => void = console.error,
config?: NgcParsedConfiguration): number { config?: NgcParsedConfiguration, customTransformers?: api.CustomTransformers): number {
let {project, rootNames, options, errors: configErrors, watch, emitFlags} = let {project, rootNames, options, errors: configErrors, watch, emitFlags} =
config || readNgcCommandLineAndConfiguration(args); config || readNgcCommandLineAndConfiguration(args);
if (configErrors.length) { if (configErrors.length) {
@ -32,8 +32,12 @@ export function main(
const result = watchMode(project, options, consoleError); const result = watchMode(project, options, consoleError);
return reportErrorsAndExit(result.firstCompileResult, options, consoleError); return reportErrorsAndExit(result.firstCompileResult, options, consoleError);
} }
const {diagnostics: compileDiags} = performCompilation( const {diagnostics: compileDiags} = performCompilation({
{rootNames, options, emitFlags, emitCallback: createEmitCallback(options)}); rootNames,
options,
emitFlags,
emitCallback: createEmitCallback(options), customTransformers
});
return reportErrorsAndExit(compileDiags, options, consoleError); return reportErrorsAndExit(compileDiags, options, consoleError);
} }

View File

@ -208,14 +208,21 @@ export class NgtscProgram implements api.Program {
this.host.writeFile(fileName, data, writeByteOrderMark, onError, sourceFiles); this.host.writeFile(fileName, data, writeByteOrderMark, onError, sourceFiles);
}; };
const transforms = const customTransforms = opts && opts.customTransformers;
const beforeTransforms =
[ivyTransformFactory(this.compilation !, this.reflector, this.coreImportsFrom)]; [ivyTransformFactory(this.compilation !, this.reflector, this.coreImportsFrom)];
if (this.factoryToSourceInfo !== null) { if (this.factoryToSourceInfo !== null) {
transforms.push(generatedFactoryTransform(this.factoryToSourceInfo, this.coreImportsFrom)); beforeTransforms.push(
generatedFactoryTransform(this.factoryToSourceInfo, this.coreImportsFrom));
} }
if (this.isCore) { if (this.isCore) {
transforms.push(ivySwitchTransform); beforeTransforms.push(ivySwitchTransform);
} }
if (customTransforms && customTransforms.beforeTs) {
beforeTransforms.push(...customTransforms.beforeTs);
}
// Run the emit, including a custom transformer that will downlevel the Ivy decorators in code. // Run the emit, including a custom transformer that will downlevel the Ivy decorators in code.
const emitResult = emitCallback({ const emitResult = emitCallback({
program: this.tsProgram, program: this.tsProgram,
@ -223,7 +230,8 @@ export class NgtscProgram implements api.Program {
options: this.options, options: this.options,
emitOnlyDtsFiles: false, writeFile, emitOnlyDtsFiles: false, writeFile,
customTransformers: { customTransformers: {
before: transforms, before: beforeTransforms,
after: customTransforms && customTransforms.afterTs,
}, },
}); });
return emitResult; return emitResult;

View File

@ -6,6 +6,7 @@
* found in the LICENSE file at https://angular.io/license * found in the LICENSE file at https://angular.io/license
*/ */
import {CustomTransformers} from '@angular/compiler-cli';
import * as fs from 'fs'; import * as fs from 'fs';
import * as path from 'path'; import * as path from 'path';
import * as ts from 'typescript'; import * as ts from 'typescript';
@ -111,9 +112,9 @@ export class NgtscTestEnvironment {
/** /**
* Run the compiler to completion, and assert that no errors occurred. * Run the compiler to completion, and assert that no errors occurred.
*/ */
driveMain(): void { driveMain(customTransformers?: CustomTransformers): void {
const errorSpy = jasmine.createSpy('consoleError').and.callFake(console.error); const errorSpy = jasmine.createSpy('consoleError').and.callFake(console.error);
const exitCode = main(['-p', this.basePath], errorSpy); const exitCode = main(['-p', this.basePath], errorSpy, undefined, customTransformers);
expect(errorSpy).not.toHaveBeenCalled(); expect(errorSpy).not.toHaveBeenCalled();
expect(exitCode).toBe(0); expect(exitCode).toBe(0);
} }

View File

@ -1227,4 +1227,32 @@ describe('ngtsc behavioral tests', () => {
expect(dtsContents).toContain('/// <amd-module name="@mymodule" />'); expect(dtsContents).toContain('/// <amd-module name="@mymodule" />');
}); });
}); });
it('should execute custom transformers', () => {
let beforeCount = 0;
let afterCount = 0;
env.tsconfig();
env.write('test.ts', `
import {NgModule} from '@angular/core';
@NgModule({})
class Module {}
`);
env.driveMain({
beforeTs: [() => sourceFile => {
beforeCount++;
return sourceFile;
}],
afterTs: [() => sourceFile => {
afterCount++;
return sourceFile;
}],
});
expect(beforeCount).toBe(1);
expect(afterCount).toBe(1);
});
}); });