Webpack sometimes skips modules loaded through expose-loader when tree shaking is enabled
flut1 opened this issue · comments
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
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.
This issue is still present on the latest version of 1.0.1
. Following @flut1's findings, I'm also unsure whether webpack
or expose-loader
are the real problem here.
To re-test this issue, I have updated @flut1's reproduction repository, to now include the latest version and the latest builds.
This is the build output of the with-expose-loader version of the test: https://github.com/psimk/reproduction-expose-loader-optimization/blob/master/with-expose-loader/build/main.js.
And this is the whole reproduction repository: https://github.com/psimk/reproduction-expose-loader-optimization
Please let me know, if I should create a separate issue for this.
Yes, please create separate issue