Skip to content

Commit

Permalink
fix(ivy): ngtsc program emit ignoring custom transformers (angular#27837
Browse files Browse the repository at this point in the history
)

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

PR Close angular#27837
  • Loading branch information
crisbeto authored and devversion committed Jan 8, 2019
1 parent a59cc5c commit 6c6e05d
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 9 deletions.
10 changes: 7 additions & 3 deletions packages/compiler-cli/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {performWatchCompilation, createPerformWatchHost} from './perform_watch'

export function main(
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} =
config || readNgcCommandLineAndConfiguration(args);
if (configErrors.length) {
Expand All @@ -32,8 +32,12 @@ export function main(
const result = watchMode(project, options, consoleError);
return reportErrorsAndExit(result.firstCompileResult, options, consoleError);
}
const {diagnostics: compileDiags} = performCompilation(
{rootNames, options, emitFlags, emitCallback: createEmitCallback(options)});
const {diagnostics: compileDiags} = performCompilation({
rootNames,
options,
emitFlags,
emitCallback: createEmitCallback(options), customTransformers
});
return reportErrorsAndExit(compileDiags, options, consoleError);
}

Expand Down
16 changes: 12 additions & 4 deletions packages/compiler-cli/src/ngtsc/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,22 +208,30 @@ export class NgtscProgram implements api.Program {
this.host.writeFile(fileName, data, writeByteOrderMark, onError, sourceFiles);
};

const transforms =
const customTransforms = opts && opts.customTransformers;
const beforeTransforms =
[ivyTransformFactory(this.compilation !, this.reflector, this.coreImportsFrom)];

if (this.factoryToSourceInfo !== null) {
transforms.push(generatedFactoryTransform(this.factoryToSourceInfo, this.coreImportsFrom));
beforeTransforms.push(
generatedFactoryTransform(this.factoryToSourceInfo, this.coreImportsFrom));
}
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.
const emitResult = emitCallback({
program: this.tsProgram,
host: this.host,
options: this.options,
emitOnlyDtsFiles: false, writeFile,
customTransformers: {
before: transforms,
before: beforeTransforms,
after: customTransforms && customTransforms.afterTs,
},
});
return emitResult;
Expand Down
5 changes: 3 additions & 2 deletions packages/compiler-cli/test/ngtsc/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/

import {CustomTransformers} from '@angular/compiler-cli';
import * as fs from 'fs';
import * as path from 'path';
import * as ts from 'typescript';
Expand Down Expand Up @@ -111,9 +112,9 @@ export class NgtscTestEnvironment {
/**
* 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 exitCode = main(['-p', this.basePath], errorSpy);
const exitCode = main(['-p', this.basePath], errorSpy, undefined, customTransformers);
expect(errorSpy).not.toHaveBeenCalled();
expect(exitCode).toBe(0);
}
Expand Down
28 changes: 28 additions & 0 deletions packages/compiler-cli/test/ngtsc/ngtsc_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1227,4 +1227,32 @@ describe('ngtsc behavioral tests', () => {
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);
});

});

0 comments on commit 6c6e05d

Please sign in to comment.