Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unused code is not removed from dynamic imports #13028

Closed
gkjohnson opened this issue Mar 31, 2021 · 8 comments
Closed

Unused code is not removed from dynamic imports #13028

gkjohnson opened this issue Mar 31, 2021 · 8 comments

Comments

@gkjohnson
Copy link
Contributor

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.

@sokra
Copy link
Member

sokra commented 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();

} );

@gkjohnson
Copy link
Contributor Author

gkjohnson commented Apr 1, 2021

I did expect that handling every scenario would be too complicated but what about supporting a limited syntax for this? Limited syntax support works pretty well for WebWorkers. The magic comments you suggest are nearly like the immediate destructuring case and seems like the used exports could be derived in this case but I'm not an expert in this area:

const { usedFunction } = await import( './dynamic.js' );

// or

import( './dynamic.js' ).then( ( { usedFunction } ) => { /* ... */ } );

Either way I appreciate your response!

@sokra
Copy link
Member

sokra commented Apr 1, 2021

Yes we probably want to implement the await syntax eventually

@webpack-bot
Copy link
Contributor

This issue had no activity for at least three months.

It's subject to automatic issue closing if there is no activity in the next 15 days.

@alexander-akait
Copy link
Member

bump

@webpack-bot
Copy link
Contributor

Issue was closed because of inactivity.

If you think this is still a valid issue, please file a new issue with additional information.

@vankop
Copy link
Member

vankop commented Oct 16, 2021

bump

@vankop vankop reopened this Oct 16, 2021
@vankop
Copy link
Member

vankop commented Nov 23, 2021

closing in favor of #14800

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Done
Development

No branches or pull requests

5 participants