Skip to content

[ngtools] exported services/pipes are removed from AOT build #8845

@QuentinFchx

Description

@QuentinFchx

Versions

@angular/core: ^5.1.0
@ngtools/webpack: ^1.9.0

Repro steps

Code sample npm run build:aot

  • Step 1
    Write a Pipe/Service and declare it in a module
  • Step 2
    Export the Pipe/Service from the module
  • Step 3
    Use the exported service anywhere else (ie: angularJS app through downgradeInjectable(HelloService))

Observed behavior

It's impossible to export a service and use it in an angularJS application through downgradeInjectable

// webpack output
Object.defineProperty(exports, "__esModule", { value: true });
var AppModule = /** @class */ (function () {
    function AppModule() {
    }
    return AppModule;
}());
exports.AppModule = AppModule;

Desired behavior

The service should be exported.

// webpack output
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = __webpack_require__(0);
var core_1 = __webpack_require__(2);
var platform_browser_1 = __webpack_require__(18);
var AppPage_1 = __webpack_require__(50);
var HelloPipe_1 = __webpack_require__(51);
exports.HelloPipe = HelloPipe_1.HelloPipe;
var HelloService_1 = __webpack_require__(19);
exports.HelloService = HelloService_1.HelloService;
var AppModule = /** @class */ (function () {
    function AppModule() {
    }
    AppModule = tslib_1.__decorate([
        core_1.NgModule({
            imports: [platform_browser_1.BrowserModule],
            providers: [HelloService_1.HelloService],
            declarations: [AppPage_1.AppPage, HelloPipe_1.HelloPipe],
            exports: [HelloPipe_1.HelloPipe],
            bootstrap: [AppPage_1.AppPage]
        })
    ], AppModule);
    return AppModule;
}());
exports.AppModule = AppModule;

Mention any other details that might be useful (optional)

About pipes: I could not find any "best practice" on how to use pipes in code (community answer1, community answer2). This is not related to angular-cli but someone might have the answer here? To work around this issue (the pipe removal), for now I just use an intermediate function that I call in the pipe and that I import in my code. (But I cannot apply this hack to Injectables)

import { Pipe, PipeTransform } from '@angular/core';

export function hello(){ return 'Hello' }

@Pipe({ name: 'hello' })
export class HelloPipe implements PipeTransform {
    transform(val: string): string { return hello(); }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions