developit / microbundle

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

Question: How to use dynamic ES imports (creating multiple exports) #987

Closed bestow-jd closed 1 year ago

bestow-jd commented 1 year ago

I'm attempting to use dynamic ES imports

if (someCondition) {
  import("./dynamicImport.js");
}

However, since this creates multiple chunks when bundling, it gives this error.

$ microbundle-crl --no-compress --format modern,cjs
Error: You must set "output.dir" instead of "output.file" when generating multiple chunks.

    at error (/Users/jdfrancis/Desktop/Bestow/design-system/node_modules/microbundle-crl/node_modules/rollup/dist/shared/node-entry.js:5400:30)
    at normalizeOutputOptions (/Users/jdfrancis/Desktop/Bestow/design-system/node_modules/microbundle-crl/node_modules/rollup/dist/shared/node-entry.js:14128:20)
    at getOutputOptionsAndPluginDriver (/Users/jdfrancis/Desktop/Bestow/design-system/node_modules/microbundle-crl/node_modules/rollup/dist/shared/node-entry.js:13910:32)
    at Object.write (/Users/jdfrancis/Desktop/Bestow/design-system/node_modules/microbundle-crl/node_modules/rollup/dist/shared/node-entry.js:13983:63)
    at /Users/jdfrancis/Desktop/Bestow/design-system/node_modules/microbundle-crl/dist/microbundle.js:431:49

How can I change this setting under the hood?

rschristian commented 1 year ago

Seems like you're having an issue with microbundle-crl. micrrobundle-crl is a fork, and not this repository. You can find it here.

Your example works just fine in microbundle proper.

bestow-jd commented 1 year ago

Oh sorry! My fault

rschristian commented 1 year ago

No worries! That package does (unfortunately) point to this repo (rather than its own), so it's a really common mix up (we even have a label for it).

That fork is quite unmaintained; depending on the features you use, migrating to microbundle could be beneficial. The main difference (as far as I understand, anyhow) is that we do less asset handling and focus more on the JS.