Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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);
});

});