Search code examples
node-modulespackage.jsonpackage-managersyarnpkgcss-frameworks

How to install node modules but commit only relevant styles


So, I am setting up a new site and my project's folder structure looks like this now.

foo.com/
       index.php
       assets/
          css/
          img/
          js/
          vendor/

I have added vendor/ for js/css libraries that I must install to keep them separate, since I want anyone who installs my project to install those in vendor from package.json - most libraries contain too many files 99% which I don't want to push to production.

Now once the project is finished, I would like to push the code to production with only the necessary js/css files.

This is where the problem comes. For example, if I install bulma css using:

yarn add bulma --modules-folder ./assets/vendor

It will dump all bulma-related files which are almost 70 into /vendor/bulma/ but I will only be needing one or two css files afterwards, since I will compiles the sass file to css as:

sass  vendors/bulmna/style.scss assets/css/style.css 

So my questions is: I am assuming this is how every developer does it and there are no documentations I can find that suggest how to do it. Is it safe to ignore the /vendor directory? What if I install vue, font-awesome, bootstrap .. how can I only fetch the files I need but not everything in /vendors folder?


Solution

  • Your question is actually quite broad - however, I'll try to list as much as possible.


    Lets say you're building a project from scratch and needed to include vuejs, jquery, fontawesome but only need to include a couple of files.

    The issue you're hitting here is module dependency with respect to npm modules. (and there are many different tools that you can use to manage versions on your library dependencies as well as ensuring they are included into your project, i.e. package managers).

    Ok - now from here, you may say to yourself

    but I only need say, one icon from fontawesome in your final build (dist) and I don't want to commit all my modules into source control

    Again, this is where you omit node_modules and other dependent libraries from source control (i.e. include node_modules your .gitignore)

    To reiterate

    1. You can install the required library,
    2. add node_modules to .gitignore ,
    3. bundle those libraries into a vendor single file to be consumed by your users (can be via browserify/webpack/rollup/gulp/grunt/yarn etc).
    4. generate bundle within npm script

    Now you may ask further -

    Why would I use any of those tools? - they're distracting me from simply copy/pasting vendor libaries into my source control.

    Build tools were created to

    1. streamline the developer pipeline so that you DONT have to copy/paste vendor libaries into a vendor folder.
    2. ensures that all files are bundled to the client automatically
    3. allows you to keep track/restrict library version updates/ when required via package.json
    4. allows you to add other build steps (such as minification, md5hash versioning, compression, code splitting, asset management to name a few).

    Now lets break down the original question here:

    1. How to ensure other developers get everything they need when cloning the repository
    2. how do I ensure I can provide only the necessary files to the end user (if I only use specific parts of vendor libaries?)

    1. How to ensure developers get what they need

    Again, to reiterate above, adding devDependancies and .gitignoring allows you to only add the necessary files to your project.

    2. How can I ensure clients get what they need without bloating request files?

    This is where build tools such as webpack, browserify, gulp, grunt, rollup, attempt to achieve. This is because with some libraries that exceed in file size of 200kb minified, you may need to separate these files into different client requests (and as such, not demand the user to request one large file, which was symtomatic of browserify projects).

    The second technique you will need to be aware of, is with specific libraries, you can use import mdn link where you can require one function/class from a dependant library (which further reduces file size).

    Another technique is using less import statements (which can extract less functions/styles similar to above, but this isn't currently supported in SCSS). for SCSS, you're basically left with copy/pasting the necessary styles into your base scss and that'll save you space as well.

    EDIT How to create a bundle from npm install libaries

    From the comments you've mentioned above (about not wanting to include a tool into your workflow, there's no point outlining any one particular strategy - you can find answers/tutorials online about how to setup gulp/browserify/webpack for your particular needs).

    However, As you are currently using yarn - I'll go into details about that.

    Firstly, yarn is a package manager (like npm). All it does with the --modules-folder is install the package into the specified folder, that's all. So we don't really care about that (since it's doing the same thing as npm). (i.e. your vendor folder is the same as node_modules in many respects).

    We could use

    1. webpack
    2. gulp
    3. grunt
    4. rollup
    5. browserify
    6. brunch

    (All build tools that essentially allow you to bundle all those packages into a single entry point to be packaged to the client).

    I won't go into how, because that is a process you can find online, and from the above comments, I can tell you don't particularly care either.

    What you are looking for is a zero-config javascript build tool. (Extremely out of the scope of your original question, and i'll only answer that in a separate Q&A).