Skip to content

Prod build removing code that's being used #12912

@zeratulmdq

Description

@zeratulmdq

Bug Report or Feature Request (mark with an x)

- [x] bug report -> please search issues before submitting
- [ ] feature request

Command (mark with an x)

- [ ] new
- [x] build
- [x] serve
- [ ] test
- [ ] e2e
- [ ] generate
- [ ] add
- [ ] update
- [ ] lint
- [ ] xi18n
- [ ] run
- [ ] config
- [ ] help
- [ ] version
- [ ] doc

Versions

npm --version
6.4.1

node --version
v8.12.0

ng --version
@angular-devkit/architect         0.10.3
@angular-devkit/build-angular     0.10.3
@angular-devkit/build-optimizer   0.10.3
@angular-devkit/build-webpack     0.10.3
@angular-devkit/core              7.0.3
@angular-devkit/schematics        7.0.5
@angular/cli                      7.0.5
@ngtools/webpack                  7.0.3
@schematics/angular               7.0.5
@schematics/update                0.10.5
rxjs                              6.3.3
typescript                        3.1.6
webpack                           4.19.1

Repro steps

I had a NG5 app which I migrated to NG7. Everything working perfectly except one minor detail. I'm consuming a third party library which has this bit of code:

# index.ts
.....
import "./robot-ladder";
import "./robot-plug";

export const ERROR_PAGE_DIRECTIVES: Type<any>[] = [
    CspErrorPageComponent,
];
......

Both robot-ladder and robot-plug are being imported just for their side effects (no export on those files):

# robot-x.ts
import { ClarityIcons } from '@clr/icons';

ClarityIcons.add({
    "robot-plug": `
    ......
})

but the code from both files is excluded from the final bundle (CspErrorPageComponent is a component I'm using BTW).

This happens when building or running dev mode with prod set to true:

ng build
ng serve --prod=true

which makes me think it's UglifyJS related. The thing is, I'm following the same pattern on my own app (importing a file for it's side effects, also calling the ClarityIcons.add function in it) and the code is not removed from the bundle.

Mention any other details that might be useful

I have access to the third party repo so I can definitely submit a patch to export a function from those files and import it and call it inside index.ts. I guess my question would be: is this intended behavior? should library authors be aware of this in order to avoid the pattern?

EDIT: it works fine with NG6

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