unplugin icon indicating copy to clipboard operation
unplugin copied to clipboard

Runtime externals don't work in webpack

Open ef4 opened this issue 3 years ago • 2 comments

Environment

unplugin 1.0.1

Reproduction

https://github.com/ef4/unplugin-bug-repro (edited to add)

Describe the bug

This code:

https://github.com/unjs/unplugin/blob/c29ef3e6d39bf65da63fd43269f030c64587ed4b/src/webpack/index.ts#L126-L128

tries to locate external modules on the filesystem at build time, and if they're not found it introduces empty virtual modules for them.

But this means we cannot actually use externals the way that both webpack and rollup typically understand externals: they're supposed to get resolved at runtime. Whether they existed during build time is not supposed to matter.

Expected behavior:

  • If my resolveId hook returns false, the original import statement should be left alone in the output, so that it will be resolved at runtime.
  • If my resolveId hook returns { external: true, id: 'xyz' }, the original import statement gets replaced with an import statement for 'xyz', and that redirected import statement will get resolved at runtime.

Actual behavior:

  • In both the above cases, the original import is replaced with an empty virtual module, and no runtime resolution ever happens.

Additional context

No response

Logs

No response

ef4 avatar Dec 19 '22 03:12 ef4

Could you provide a duplicate? I can't debugger

JerryWu1234 avatar Mar 20 '23 10:03 JerryWu1234

Here is a reproduction: https://github.com/ef4/unplugin-bug-repro

It contains both rollup and webpack configs, so you can see that the rollup build works correctly and the webpack build fails at runtime.

ef4 avatar Mar 20 '23 14:03 ef4