developit / microbundle

📦 Zero-configuration bundler for tiny modules.
https://npm.im/microbundle
MIT License
8.04k stars 362 forks source link

Native addons are not marked as external by default #885

Closed arthuro555 closed 3 years ago

arthuro555 commented 3 years ago

Hello 👋

I am making a native addon that requires some wrapper code in javascript. I thought I'd use microbundle to bundle this code, but I went into an issue. You see, to use the native module, one needs to import it. A common way to do so is using node imports, like this:

import MyNativeAddonClass from "../build/Release/binding.node";

The problem is that microbundle does not catch onto the fact that it is a native module (.node file extension) and tries to parse it as normal JavaScript: image

Would it be possible to add such files as "external" automatically, so that they go through Node's require system instead of being bundled in?

EDIT: If you are looking at this issue wondering if you can still use native modules, to be clear, you can still use something like bindings or process.dlopen to import it not using the module system. I opened this issue because it would be nice to be able to use that other subjectively cleaner way.

rschristian commented 3 years ago

As far as I understand you cannot use import with Node addons in the first place, so that's not valid.

You need to use require(), either with CJS or module.createRequire(). Microbundle works just fine if you use require().

arthuro555 commented 3 years ago

Ah, indeed, my mistake sorry for that. I thought import used the same mechanic as require for node modules. I'll close this as it is invalid to use imports that way indeed.