Ive just published my first package (a react component) to npm but im having some trouble understanding the difference between what the
lib directory is compared to the
Currently I generate both
dist however my package "main" points to the
dist unminified js file which has been built using webpack and output as UMD. The lib folder is built using
babel taking the
src and outputting to
The dist folder contains both [unminified/minified].js files as well as [unminified/minified].css files.
My main confusion is with the lib folder since imports from there currently wouldn't work seeing as I just transform
src -> lib meaning the scss references are still there and the scss files aren't transformed either.
I use CSS Modules (css-loader, styles-loader, postcss-loader etc) to generate my CSS files and this is where the confusion is since, wouldn't I also need to use webpack to generate my
lib folder seeing as the
scss files/import references need to be transformed to
Are you meant to have both lib and dist or is the UMD build in dist fulling the same purpose as that of having a lib folder?
If you are supposed to have both how would I achieve this, since I couldnt find any info regarding generating the lib folder when using CSS modules within your js files and still maintaing the same folder structure of that of src (while still generating dist)?
dist folder is for shipping a UMD that a user can use if they aren't using package management. The
lib folder is what
main points to, and users that install your package using
npm will consume that directly. The only use of the
lib as opposed to
src is to transform your source using babel and webpack to be more generally compatible, since most build processes don't run babel transforms on packages in
As far as handling the style imports, it's probably a good idea to not import scss or css files in your source js that you export. This is because node can't import styles like that by default. If you have an example that demos your component, it makes sense to import the styles there. The common pattern is to publish minified and unminified css in the
dist folder, and in your documentation tell the consumer to explicitly import the css file using whatever technique they prefer. I took this approach with redux bug reporter if you need an example. Hope that helps!
Ok think I found out how to do this. There is a babel plugin that allows you to use webpack loaders when running babel (babel-plugin-webpack-loaders). Thus my CSS mapping is inlined within the js file and the mapping hashes used are also the same as that used when building dist. Yay!
lib refers to libraries that are included in a package,