Giter Site home page Giter Site logo

node-modules-prod's Introduction

node-modules-prod

prepare node_modules for production, convert symlinks, remove clutter

install

npm i node-modules-prod --save-dev

why

node_modules packages contain a lot of clutter, test folders, examples, documentation etc. Production site will never use them.
Small files containing just a few lines like .gitignore or .npmignore are taking a whloe allocation unit on a disk, that's why a 10Mb node_modules folder by size, may take 18Mb of disk space.
Local packages are stored as symbolic links in node_modules - great for development, but npm install on PROD will leave them as symbolic links, which will be broken once pushed to server.

This utility will:

  • convert symlink folders to actual folders
  • strip unneeded files and folders to bare minimum (Raspberry Pi friendly)
  • ignore packages listed as devDependencies

gulp use example

const copyNodeModules = require('node-modules-prod');

var dest = 'BUILD';

gulp.task('copy-node-modules', function(cb){

    copyNodeModules(
        './',        // sourceRoot relative to DEV root
        dest,        // destinationRoot relative to DEV root
        {},          // use default options
        cb           // callback for gulp task
    );
});

default options

quiet: true             // do not show progress in console, (progress shows only folder names)
logIgnored: false       // do not show ignored items in console (in magenta color)
noDevDependencies: true,// do not copy devDependencies (as per package.json)
useNpmignore: true,     // use npmignore file in each folder to exclude files
noIgnoreList: false,    // use ignore.js list
packageDir:'',          // dir name for package.json when not in dev root
logToFile: false        // for debugging, logfiles are created in LOG/ folder

NOTE:

The node-modules-prod only copies existing folders form DEV node_modules to BUILD folder.

  1. Run npm install in DEV, to make sure that all your dependencies in node_modules are in working order for target operating system!
  2. Make sure not to change default options to exclude unneeded folders and files from copying:
noDevDependencies: true, // do not copy devDependencies (form package.json)
useNpmignore: true,      // use npmignore file in each folder to exclude files
  1. If symlinks are in package.json, do NOT run npm install in BUILD folder!
  2. The node-modules-prod aggresively excludes unneeded files saving a lot of space. Excluded files can be listed in console (color cyan- excluded by npmignore, magenta - other than npmignore) with <- mark, by using logIgnored:true option. Exclusion list is in ignore.js file. Readme and License files are excluded too. Check licensing requirements for each package.

when node_modules and package.json is in a subfolder of dev root folder

  • do NOT append that subfolderName to dest
  • add options.packageDir=subfolderName to tell where to look for package.json otherwise devDependencies exclusion will not work.
const copyNodeModules = require('node-modules-prod');
var dest = 'BUILD';

gulp.task('copy-npm-modules', function(cb){

    copyNodeModules(
        './subfolderName',              // sourceRoot relative to DEV root
        dest,                           // destinationRoot relative to DEV root
        'node_modules',                 // dirName
        {                               // options:
            packageDir: 'subfolderName';// tell that package.json is in this subfolder
        }, 
        cb                              // callback for gulp task
    );
});

console

copying progress is shown in console with options {quiet:false} only folders are shown. (there are too many files to show) Additionally option {logIgnored: true} will show ignored files.

source -> destionation      // default color 
ignored <-                  // magenta {logIgnored:true}
symbolicLink => folder      // blue

debugging your build

use option {logToFile: true} to create file logs for copying operation. Folder LOG/ must exist in DEV root prior to running the task. Separate log files for excluded items will be created, for:

  • excluded-byCondition excluded because item is listed in devDependencies or in ignore.js list
  • excluded-byNpmignore excluded because items match .npmignore patterns.
    File .npmignore in some module may contain patterns that are not correctly interpreted by micromatch

aboul symbolic links

To check if you have symbolic links in node_modules, look for little link arrows on the folder icon in file explorer:

related: bower_components

to remove unneeded files and folders from bower_components try bower-purge.
Polymer 2 components are great! Don't bother with Polymer 3 (for C!# or C--).

requirements

NodeJS v8 and gulp v3 (tested on Win7)

node-modules-prod's People

Contributors

alfalabs avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.