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.

Webpack Cannot Treeshake an Unused Export Created by a Function

See original GitHub issue

webpack version: 4.29.0 Node.js version: v10.14.2 Operating System: Ubuntu 16.04.5 Additional tools: eslint 5.12.1

Issue:

Summary()
{
	// Webpack cannot treeshake an unused export function if that function 
	// was created by another function inside of the same module.
}

For example, lets say I have the following file named test.mjs :

export const mirror = function mirror(str)
{
    return str;
};

let createCounterFunction = function createCounterFunction()
{
    let i = 0;
    let counter = function counter()
    {
        i += 1;
        return i;
    };
    return counter;
};
export const funcCount = createCounterFunction();

Now, let’s say I try to only import “mirror” into my index.js file:

import { mirror } from './test.mjs';

Result:

Result()
{
	// Weppack 4 is unable to treeshake any of the code above, despite the 
	// fact that we have no intention of importing any of the "counter-related" code.
}

I’ve concluded that anytime you call a function inside a module itself, that function is considered “used” because you used it before even exporting it. However, it seems possible that a smart enough bundler could treeshake this still.

My workaround, is to put functions like this into their own code file, so that they only get imported when explicitly specified.

One thing you might find interesting, is that Chrome’s Coverage Tool also considers these unintended imports “used”, because after slipping by the tree-shaker, createCounterFunction() gets called in the client.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
sokracommented, Feb 1, 2019

This seem to be a terser issue:

This code can be removed

let createCounterFunction = function createCounterFunction()
{
    var i = 0; // <=== var
    let counter = function counter()
    {
        i += 1;
        return i;
    };
    return counter;
};
createCounterFunction();

But this leave code behind:

let createCounterFunction = function createCounterFunction()
{
    let i = 0; // <==== let
    let counter = function counter()
    {
        i += 1;
        return i;
    };
    return counter;
};
createCounterFunction();

Note the only difference is var vs let

1reaction
sokracommented, Feb 1, 2019

Seems like increasing the compress.passes option fixes the issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Webpack5 does not seem to tree-shake unused exports
I figured it out myself, auto-answer for the record : The tsconfig.json was wrong, it wasn't preserving the ES6 module syntaxe so webpack...
Read more >
Tree Shaking - webpack
That function is what's known as "dead code", meaning an unused export that should be dropped. Now let's run our npm script, npm...
Read more >
Tree shaking and code splitting in webpack - LogRocket Blog
What is tree shaking? Tree shaking, also known as dead code elimination, is the practice of removing unused code in your production build....
Read more >
How to Do Proper Tree-Shaking in Webpack 2 - Emarsys
The reason behind this is that Webpack only marks code unused and doesn't export it inside the module. It pulls in all of...
Read more >
Tree-shaking in real world: what could go wrong? - Medium
In order for webpack to analyze the bundle for unused exports (and ... If you want to create a tree-shakable library, you cannot...
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