Skip to content

Webpack sometimes skips modules loaded through expose-loader when tree shaking is enabled #89

@flut1

Description

@flut1

I wasn't sure if this issue should be reported in expose-loader or webpack itself. Please let me know if I should report it elsewhere.

  • Operating System: Windows
  • Node Version: 11.15.0
  • NPM Version: 6.9.0
  • webpack Version: 4.35.0
  • expose-loader Version: 0.7.5

This issue is for a:

  • bug
  • feature request
  • modification request

Code

I ran into this issue while trying to expose the styled-components library as a global variable:

import 'expose-loader?styled!styled-components';
webpack.config.js

https://raw.githubusercontent.com/flut1/reproduction-expose-loader-optimization/master/with-expose-loader/webpack.config.js

Expected Behavior

styled-component is bundled and exposed as a global styled variable.

Actual Behavior

styled-component is not included in the output bundle, neither is the styled variable declaration. They are stripped out because of the { sideEffects: false } tree shaking setting in Webpack (default under { mode: 'production' }).

See the build output here.

How Do We Reproduce?

I created a minimal reproduction repository here:
https://github.com/flut1/reproduction-expose-loader-optimization

The bug reproduction can be found in the with-expose-loader directory, but I also added a variant without expose-loader and one without sideEffects: false, both of which do produce the expected output. I committed the build output for each so you don't have to clone the repository to inspect it. Additional information can be found in the repository readme.

PS: I'm not 100% sure if this is technically speaking a bug. I'm assuming that exposing a module as a global should count as "using the export" and should therefore not be stripped from output.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions