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.

[webpack2] Tree shaking and "unused harmony default export"

See original GitHub issue

I’m running webpack 2 beta 20 and for a development build, I can see a lot of unused harmony default export in my UI library (which is normal since I don’t use everything that is available in the library).

I checked the code and instead of exporting the class with the webpack require function, i see this, which seems ok.

/* unused harmony default export */ var _unused_webpack_default_export = ComponentA;

But when I build the app in a production environment, the component is still there, even if it has been marked as unused.

The component is imported and exported from another file before my app really uses it.

export { default as ComponentA } from './ComponentA';

and inside that file, it’s marked like this:

/* harmony import */ var __WEBPACK_IMPORTED_MODULE_19__ComponentA__ = __webpack_require__(622);
/* unused harmony reexport ComponentA */

I also checked the compiled code in production, and the code is there but as it’s mentioned with the unused reexport, nothing is exported… No t.default = ...

From what I see, it seems there’s an issue in the tree-shaking/code elimination. Can you confirm, or tell me what would be wrong in what I’m trying to do?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:13 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
davidmoshalcommented, Mar 16, 2017

@iamakulov thanks, enabling UglifyJS did NOT solve the problem, but it DID lead to a solution.

Recall that the problem initially surfaced when IE 11 refused to run ES6 code, by failing to run the main.js script which contained ES6 code:

/* unused harmony export */ 
let sum = (xs) =>

Enabling UglifyJS resulted in Uglify also complaining about the ES6 code !!

Fiddling with tsconfig.json did not help either. Awesome-typescript was correctly emitting ES5 code, but Webpack was adding unused code into the main.js as ES6 !!

This seems to have been for lodash which was set as a provided resource in webpack.

Adding babel-loader solved the problem, both for IE 11 and UglifyJS.

So it seems like something this is happening:

  1. awesome-typescript is emitting es5 correctly.
  2. webpack’s tree shaking is adding unused JS as es6 (!!)
  3. Babel-loader is converting that unused JS to es5 correctly.
  4. UglifyJS is removing the unused JS correctly.

This workflow works, but doubles the transpilation time, so I’m only using this workflow for build scripts.

I’d suggest that emitting unused imports as es6 is a bug, not a feature.

David

1reaction
davidmoshalcommented, Mar 16, 2017

have run into the situation where /* unused harmony export */ exports es6 code for usused functions, e.g.:

let sum = (xs) => … problem is that this causes older non-es6 browsers, such as IE 11 to fail to render the page. Seems odd to included unused functions, in es6 !!

Stack:

webpack 2 awesomes-typescript tsconfig set to export es5 Uglify not being used. David

Read more comments on GitHub >

github_iconTop Results From Across the Web

Tree Shaking - webpack
Tree shaking is a term commonly used in the JavaScript context for dead-code elimination. ... Note the unused harmony export square comment above....
Read more >
Webpack including unused exports in final bundle (not tree ...
When I import 1 of the exported libraries from that entry point, looks like Webpack is not able to tree-shake the output. running...
Read more >
Tree-shaking in real world: what could go wrong? - Medium
Official webpack guide on the topic promises that tree-shaking would work out of the box, and we'll see unused harmony export comments in ......
Read more >
Tree shaking and code splitting in webpack - LogRocket Blog
Tree shaking dynamic imports. Dynamic imports resolve the entire module — with its default and named exports — without tree shaking unused ......
Read more >
Learn about tree shaking in Webpack 5 - Saeloun Blog
Tree shaking is a technique used by webpack to optimize the builds for production and reducing the build size by shaking off the...
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