Skip to content

Modules imported for side effects are not included in optimized bundle #23246

@amitbeck

Description

@amitbeck

🐞 Bug report

Command (mark with an x)

  • new
  • build
  • serve
  • test
  • e2e
  • generate
  • add
  • update
  • lint
  • extract-i18n
  • run
  • config
  • help
  • version
  • doc

Description

I have a index.ts which imports 2 modules (abstract-control.ts and abstract-control.directive.ts) for side effects, and exports another module (custom-control-status.directive.ts) therefore also acting as a barrel file. custom-control-status.directive.ts is imported through the barrel file (index.ts), therefore I thought index.ts and the 2 modules imported for side effects would be included in the bundle, but they're removed in the tree shaking process.
I tried both of the following:

  • specifying "sideEffects" in package.json's:
    1. "sideEffects": ["<path>/index.ts", "<path>/abstract-control.ts" and "<path>/abstract-control.directive.ts"] in the app's package.json
    2. Add a package.json to the directory and set "sideEffects": ["./index.ts", "./abstract-control.ts" and "./abstract-control.directive.ts"] in it
    3. Add a package.json to the directory and set "sideEffects": true in it
  • specifying a rule in Webpack's module.rules that matches every TS file in the directory and set sideEffects: true

Despite that, they are all excluded from the bundle. Could this be an issue with Angular's webpack config or @angular-builders/custom-webpack?

🔬 Minimal Reproduction

Reproduction repository: https://github.com/amitbeck/ng-webpack-sideEffects-repro
Either:

  1. Run npm run build (alias for ng build --configuration production), or
  2. Run npm run serve (alias for ng serve --configuration production) and access through http://localhost:4200. You should get the following error:

    TypeError: this.formGroup.controls.text.setWarnings is not a function

abstract-control.ts, abstract-control.directive.ts and index.ts in src/app/utils/forms/ are not included in the bundle.

🌍 Your Environment


Angular CLI: 13.2.6
Node: 16.13.1
Package Manager: npm 8.1.2
OS: darwin x64

Angular: 13.2.7
... animations, common, compiler, compiler-cli, core, forms
... language-service, localize, platform-browser
... platform-browser-dynamic, router, service-worker, upgrade

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1303.1
@angular-devkit/build-angular   13.2.6
@angular-devkit/core            13.3.1
@angular-devkit/schematics      13.2.6
@angular/cdk                    13.2.6
@angular/cdk-experimental       13.2.6
@angular/cli                    13.2.6
@angular/material               13.2.6
@schematics/angular             13.2.6
rxjs                            7.5.5
typescript                      4.5.5

Anything else relevant?

Possibly related to #12912 and webpack/webpack#15610.
Also asked a question in SO and asked a Webpack team member for help in webpack/webpack#15610 (comment).

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