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 dist
.
Currently I generate both lib
and 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 lib
.
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 css
?
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)?
lib is short for library which is often used for common files, utility classes, imported dependencies, or 'back in the days' also for dlls for (desktop) applications. It's in general a 'library' of supporting code for the core application.
dist stands for distributable, not distribution. It is the directory that once everything ha been compiled, gulped, transpiled, assembled and produced from all the other sources and files and trinkets etc.. this is what you want to distribute or indicate to others that it is the distributable!
The dist folder, short for distribution folder, is dynamically generated when using the nuxt generate commands and includes the generated production ready HTML files and assets that are necessary to deploy and run your statically generated Nuxt application.
Directories. lib/ is intended for code that can run as-is. src/ is intended for code that needs to be manipulated before it can be used. build/ is for any scripts or tooling needed to build your project. dist/ is for compiled modules that can be used with other systems.
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!
Usually the dist
folder is for shipping a UMD that a user can use if they aren't using package management. The lib
folder is what package.json
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 node_modules
.
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With