question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Modules imports for side effects from barrel files are not included in bundle

See original GitHub issue

Bug report

What is the current behavior?

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. Despite specifying index.ts, abstract-control.ts and abstract-control.directive.ts in my package.json’s "sideEffects", they are all excluded from the bundle. The current workaround is to not use barrel files, meaning to import abstract-control.ts, abstract-control.directive.ts and custom-control-status.directive.ts directly in app.module.ts.

If the current behavior is a bug, please provide the steps to reproduce.

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

  1. Run npm run build, or
  2. Run npm run serve and access through http://localhost:4200

The contents of abstract-control.ts, abstract-control.directive.ts and index.ts in src/app/utils/forms/ are not included in the bundle, and you therefore the side effects will not be performed. You should get the following runtime error:

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

What is the expected behavior?

The contents of abstract-control.ts and abstract-control.directive.ts should be included in the bundle, and the side effects should be performed. No runtime errors should occur.

Other relevant information: webpack version: 5.67.0 Node.js version: 16.13.1 Angular version: 13.2.7 Operating System: macOS Monterey 12.4

My SO question https://github.com/webpack/webpack/issues/15610#issuecomment-1138473225 According to an Angular team member, it seems to be a webpack issue: https://github.com/angular/angular-cli/issues/23246#issuecomment-1141113638

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:1
  • Comments:14 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
vankopcommented, Jun 2, 2022

I created an issue

1reaction
vankopcommented, Jun 2, 2022

yeah, issue should be solved by preventing removing reexport. webpack@5 can handle reexport, so instead of

import * as i2 from "./utils/forms/custom-control-status.directive";

should be

import * as i2 from "./utils/forms"; // or another form of importing "./index"
Read more comments on GitHub >

github_iconTop Results From Across the Web

Tree shaking doesn't work with Typescript barrel files #12557
Since imported files can have side effects that would be introduced in development but not in the tree-shaken version. Since side effects are ......
Read more >
Why are these modules imported for side effects not included ...
Adding the following rule in Webpack config with "sideEffects" set to true : const path = require('path'); module. exports = { module: {...
Read more >
Tree Shaking - webpack
components/Button.css : No export is used, but flagged with sideEffects -> include it. In this case only 4 modules are included into the...
Read more >
Angular Performance Checklist - Taskade
Once the application has been bundled, it does not export the unused code so it ... Angular applications because of the heavy usage...
Read more >
Why Webpack 2's Tree Shaking is not as effective as you think
The root of the problem is side effects. In many use-cases importing a library does not necessarily result in a bundled piece of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found