How to handle packaged modules with webpack #175
Labels
exp/novice
Someone with a little familiarity can pick up
help wanted
Seeking public contribution on this issue
kind/enhancement
A net-new feature or improvement to an existing feature
I'm opening this in here as this seems to be how a lot of ipfs modules are being packaged with this tool (I personally had this experience with the
js-libp2p
module). When using it on the client side with webpack there's basically three possibilities:require('js-lib2p')
. But this assumes that the source code has to be transpiled by webpack, even for this module. Asexclude: /node_modules/
is a fairly popular (and sensible) setting in a lot of webpack configs, this would have to be explicitly included. That assumes a certain configuration (basically the same as the config used to build this module). But why would I want to transpile it again, when there seems to be adist
already available? Which brings us to optionrequire('js-lib2p/dist')
, which sadly doesn't work as the dist doesn't export the generated var but just puts it into the global context (which doesn't work when compiled with webpack). That just leaves us with optionwindow
. This might be great for experimenting or debugging but isn't really great in big production apps.So my suggestion would be to export the dist module wrapped in a UMD wrapper (like this one: https://github.com/umdjs/umd/blob/master/templates/commonjsStrict.js). This would not break anything (as it'll still be on the window, when imported in a script tag) but would work with commonjs and AMD module compilers. I'd happily provide a PR to integrate that.
With webpack it's just a matter of setting the
libraryTarget
to the desired value here.The text was updated successfully, but these errors were encountered: