4

There is a module that is used as a namespace (for testability reasons or else):

export function bar() {}

export function baz() {
    this.bar();
}

export function qux() {}

This can be considered a bad practice because baz depends on the context and cannot be imported alone as named export:

import * as Foo from './foo';
Foo.baz();

Is it safe to assume that this.bar doesn't affect tree shaking mechanism, and bar and qux will be tree-shaken?

Is it possible to make a bundler to be aware of a relation between bar and baz, so if Foo.baz is in use, only qux is tree-shaken?

The question primarily addresses Webpack tree shaking, but an explanation for other bundlers (Rollup) is also welcome.

Estus Flask
  • 206,104
  • 70
  • 425
  • 565
  • Yes, it looks like a bad practice. I don't know how it affects tree shaking, but to fix it (and make the bundler aware of the dependency) just refer to `bar()`. – Bergi May 22 '18 at 10:20

0 Answers0