-
Notifications
You must be signed in to change notification settings - Fork 361
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
Docs: Clarify Dependency Bundling #454
Comments
I had to come digging around in issues to find this, so something doc related would be greatly appreciated |
What about a bundle that should work directly via <script> tag? |
It seems like microbundle's focus is bundling libraries to be used in other JS apps. I expected something like a micro version of webpack which generates self-contained js files ready to be used in
There may have been some caching issue or re-running However the output still includes |
Hey @IJMacD I've come up to a solution where I simply generate one extra bundle file for browsers purpose. See this: https://github.com/adriandmitroca/mimeeq-auth-html-client/blob/master/package.json#L10 This generates extra index.standalone.umd.js file that can be used within the browser. |
Maybe I am wrong here because the docs aren't clear but seems the reason is
src: #87 (comment) And the docs say:
Which is confusing since Also, seems |
Which options are you referring to? I only see a singular difference, which is |
Didn't take note of all the ones I found, a sourcemap alias comes to mind and those you mention might be the rest. Sure it's just an alias but can be confusing at first. |
Another thing that confused me, in readme.md line 119: ...
"main": "./dist/foo.umd.js", // legacy UMD output (for Node & CDN use)
... Shouldn't that be |
Not necessarily, no. What format you provide through |
Not sure I understood, does that mean this would produce the same output of umd bundles? ...
"main": "./dist/foo.umd.js",,
"umd:main": "./dist/foo.unpkg.js",
... What about this?
That's line 159 of readme.md I'm not understanding then. And in line 164 of readme.md: ...
"main": "dist/foo.js", // CommonJS output bundle
... Filename suffix is not specified and the comment indicates it will produce a CommonJS bundle? is it because that's the default behavior? |
Couple things: What outputs are generated depends on the Assuming you're using default outputs, no,
Yes, the CJS output gets mapped to |
It's worth mentioning that there's quite a big gap between what Microbundle outputs and what is valid and can be used by a package. Microbundle's setup aims to provide users with a number of commonly used formats. However, if you choose to limit these formats, say by only outputting ESM as So the In the case of the UMD/ |
Yes, I was thinking of it in terms of using |
Microbundle's CJS output is tied to If this sounds a bit complicated and like a bit of mess, in truth, it is. Build tools (including Node) can more or less look for arbitrary keys in your library's |
Bundling of dependencies has a lot of value in certain situations. It's a technique that can allow libraries to import modules while mutating their behavior (through constant inlining or transforms), or to inline specific known working versions of modules that would otherwise be too difficult to offload to the module consumer.
However, it's possible Microbundle's behavior here is unclear. Here's what we currently do for web (
--target node
doesn't have these semantics and doesn't generally inline):When you import a module in your library, Microbundle checks to see how that module was added to your
package.json
. If the module is listed inpeerDependencies
ordependencies
, it will be considered "external" and won't be inlined into your bundled code - instead arequire()
orimport
will be left in place matching your source. If the module you imported is only referenced in yourpackage.json
'sdevDependencies
, it will get inlined into the bundle.This behaviour makes sense: when a package consumer installs your package, anything in
dependencies
gets downloaded with it, so it's assumed your package will use those co-installed versions. Similarly forpeerDependencies
, which aren't automatically downloaded bynpm
, but produce warnings when the target dependency is not installed or does not meet the package's version criteria.So, how do you use Microbundle to bundle dependencies? Here's a quick reference:
1. I want to bundle a dependency
In your
package.json
, install that dependency as adevDependency
- it's only going to be used at build time, since it'll be inlined when the user installs instead of dynamically downloaded and referenced.2. I want to bundle specific dependencies
Sometimes projects have more than one build configuration, or run
microbundle
multiple times. It could be that you have a modern bundle and a legacy compatibility bundle, and the modern bundle inlines helpers. Or perhaps you are producing "development" and "production" builds, where each is different based on differing build-time constants. Regardless, in these situations it may be necessary to explicitly tell Microbundle which dependencies should be inlined, and which should be left as external.Notice how, in the above example's
development
build, onlypretty-format
is left external. Even thoughdebug
is listed independendencies
, it will be inlined because it is not listed in the value passed to--external
. When--external
is specified, it overrides all defaults.3. I want to explicitly bundle all dependencies
For cases similar to the above, sometimes you want to produce a bundle where all dependencies are inlined - even if they're specified as
dependencies
orpeerDependencies
in the package.json. One real-world example of this is preact-redux, since it needs to inline various dependencies in order to apply preact-specific transformations and optimizations to them at build time. For this, Microbundle has an option to force all dependencies to be inlined called--external none
:The text was updated successfully, but these errors were encountered: