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

exportsPresence does not work for unused imports #18273

Closed
menelaos opened this issue Apr 2, 2024 · 3 comments
Closed

exportsPresence does not work for unused imports #18273

menelaos opened this issue Apr 2, 2024 · 3 comments

Comments

@menelaos
Copy link

menelaos commented Apr 2, 2024

Bug report

What is the current behavior?

Setting exportsPresence to 'error' does not emit an error if the import is unused.

If the current behavior is a bug, please provide the steps to reproduce.

  1. git clone https://github.com/rwieruch/minimal-webpack-setup
  2. cd minimal-webpack-setup
  3. npx npm-check-updates -u
  4. npm install
  5. Create src/foo.js with the following content:
    export const foo = 42;
  6. Modify webpack.config.js to include
    module: { parser: { javascript: { exportsPresence: 'error' } } },
  7. Modify src/index.js to look like:
    import { bar, foo } from './foo';
    
    console.log(foo);
    
    // Uncommenting the following line will result in an error
    // console.log(bar);
  8. npx webpack

What is the expected behavior?

Webpack should emit an error that bar is an invalid import regardless of whether console.log(bar) is commented out or not.

Other relevant information:
webpack version: 5.91.0
Node.js version: 21.7.1

This seems to be a regression since the same issue was reported a few years ago and allegedly resolved (#4897).

@alexander-akait
Copy link
Member

alexander-akait commented Apr 3, 2024

hm, intresting, no sure we can't fix it, because we don't handle at all unused imports for perf reasons, maybe linter is the best solution here, but I look at this deeply

@alexander-akait
Copy link
Member

@vankop What do you think? Honestly, I'm not sure that we should process this, personally I think it's more work for a linter, because bar import is not unsed and we just drop it, so we don't generate broken code

@vankop
Copy link
Member

vankop commented Apr 11, 2024

probably original issue was closed as a stale one..

So basically idea of the warning/error here is to notify user about highly likely issues at runtime ( if exports are reachable code ), with unused imports it is not a problem. If you didn't disable optimisations in config, webpack will remove unused exports for you.

If you want to improve your source code, in this case better to use linters (e.g. eslint )

@vankop vankop closed this as completed Apr 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Backlog
Development

No branches or pull requests

3 participants