developit / microbundle

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

inline of dependency leads to failed imports in source #1058

Closed artnaseef closed 1 year ago

artnaseef commented 1 year ago

I am trying to inline a dependency and when I do, I get failed imports.

Module (1) (called contract-client) is an auto-generated contract library (using openapi generator). Module (2) (called node-client) is the library I'm developing and releasing.

When I build node-client using the following (populated by create-react-library), the library builds and packages, but the output includes a require('contract-client') call - which fails downstream:

"scripts": {
  "build": "microbundle-crl --no-compress --format modern,cjs",

So, I found inline. And tried it. The result was the same as putting the dependency in "devDependencies" - I get an error like this when building node-client:

npm ERR! Error: 'NotTheRealName' is not exported by ../contract-client/dist/index.js, imported by AnotherBogusName.ts

Here is how I forced inlining:

"scripts": {
  "build": "microbundle-crl --no-compress --format modern,cjs --external none",

Please help!

rschristian commented 1 year ago

microbundle-crl is an unmaintained fork that can be found here.


Have you tried using microbundle itself?

artnaseef commented 1 year ago

microbundle-crl is an unmaintained fork that can be found here.

Have you tried using microbundle itself?

OK, I'll try. FYI - I saw that CRL was separate, looked at it's NPM page for the github link, which brought me back here. In addition, I saw talk of merging it back into this project. And create-react-library still seems to use the CRL one (unless I failed to update it properly), so .... I'm confused.

artnaseef commented 1 year ago

Just switching microbundle-crl to microbundle:

"build": "microbundle --no-compress --format modern,cjs",
"start": "microbundle watch --no-compress --format modern,cjs",
"microbundle": "^0.15.1",

I'm getting the same error when using --external=none - the import fails.

rschristian commented 1 year ago

Do you have a reproduction?

artnaseef commented 1 year ago

Are there any examples around where the inlined dependency with an import works?

Can you share any ideas on how to track down the problem?

rschristian commented 1 year ago

I'm not quite sure what "inlined dependency with an import works" means, to be quite honest.

If you can provide a reproduction, I can take a look.

artnaseef commented 1 year ago

In node-client, my source has an import of contract-client, like this:

import { NotTheRealName } from 'contract-client';
rschristian commented 1 year ago

We need a repo we can clone, trying to guess your project's structure and configuration isn't a good use of anyone's time.

artnaseef commented 1 year ago

Here is an POC that reproduces the problem:

https://github.com/artnaseef/microbundler-node-example

artnaseef commented 1 year ago

Can we remove the CRL label on this issue since CRL is no longer involved?

rschristian commented 1 year ago
npm ERR! Error: 'NotTheRealName' is not exported by ../contract-client/dist/index.js, imported by AnotherBogusName.ts

This is the correct behavior. You're referencing a built CJS package as an input and trying to import from it -- microbundle does not support this behavior.

Provide an ESM output for aaa-contract-client. You can have microbundle build aaa-contract-client, run tsc twice with different configs, etc. Just ensure there's ESM output for it to process.


In the future, please provide a minimal reproduction for issues (on any repo, not just this one). When you have a whole bunch of entirely unrelated configs and dependencies, it slows down the maintainer trying to help you.

artnaseef commented 1 year ago

In the future, please provide a minimal reproduction for issues (on any repo, not just this one). When you have a whole bunch of entirely unrelated configs and dependencies, it slows down the maintainer trying to help you.

Thank you for the feedback - I actually figured this out on my own (using strace to track back to rollupjs).

While we are on the topic of "please provide a minimal..." - I get it. I work on opensource as well (feel free to google it). But really - you couldn't come up with "check your module packaging" without it?

In the future Ryan - please do me a favor. Don't help.

artnaseef commented 1 year ago

BTW, who actually solved this one?

rschristian commented 1 year ago

If you had already figured this out yourself, a heads up/closing of the issue would be nice.

But really - you couldn't come up with "check your module packaging" without it?

Are you asking if I had to clone your repo and build both packages to observe what was wrong? Er, yes, I did. It would've been helpful if I didn't need to clean up the package.json dependencies and extraneous config files first is all.

In the future Ryan - please do me a favor. Don't help.

Yeah this isn't a way to thank someone volunteering time to help you.

BTW, who actually solved this one?

Not quite sure what you're asking?