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.

Unused code is not removed from dynamic imports

See original GitHub issue

Feature request

What is the expected behavior?

Dynamic imports should have unused code removed the way regular imports do. Currently if you export two functions from a dynamically imported file and only ever use 1 of them the unused function will not be removed. Consider the following example:

// dynamic.js
export function usedFunction() { console.log( 'USED' ); };

export function unusedFunction() { console.log( 'UNUSED' ); };
// index.js
import( './dynamic.js' ).then( ( { usedFunction } ) => {

    usedFunction();

} );

After bundling the above files “unusedFunction” will still be included in the built dynamic.js chunk. However “unusedFunction” is removed when using import { usedFunction } from './dynamic.js';.

(tested with the latest webpack, webpack-cli)

What is motivation or use case for adding/changing the behavior?

I would like to remove as much code as possible even from the dynamically imported files to reduce the download and parse time overhead.

How should this be implemented in your opinion?

Dynamic imports should have unused code removed the way regular imports do by inspecting the fields used after running the import.

Are you willing to work on this yourself?

Unfortunately I don’t think I’ll be able to.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
alexander-akaitcommented, Jul 2, 2021

bump

1reaction
sokracommented, Mar 31, 2021

This is too complex to statically analyse, so webpack doesn’t know which exports are used.

You can tell it manually like that:

// index.js
import(/* webpackExports: "usedFunction" */ './dynamic.js' ).then( ( { usedFunction } ) => {

    usedFunction();

} );
Read more comments on GitHub >

github_iconTop Results From Across the Web

how can I remove unused code when I build my bundle?
At webpack configuration, optimization/usedExports: true will remove unused code. webpack.config.js
Read more >
Dead imported code - Tooling.Report
Exports of a module that are not imported or used by any other module in an application can also be considered dead code...
Read more >
Remove unused code - web.dev
The unused code can introduce unnecessary delays in resource load time, memory usage, and main thread activity that contribute to poor page ...
Read more >
Tree shaking and code splitting in webpack - LogRocket Blog
Tree shaking, also known as dead code elimination, is the practice of removing unused code in your production build.
Read more >
how to safely remove dead code (unused exports) in JS ...
There is no visible harm in having code in the code base, that is never used. Previously this at least would affect bundle...
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