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:
- Just do
require('js-lib2p'). But this assumes that the source code has to be transpiled by webpack, even for this module. As exclude: /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 a dist already available? Which brings us to option
require('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 option
- Include the file as a script tag and put it into the
window. 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.
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-libp2pmodule). 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 adistalready 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
libraryTargetto the desired value here.