Search code examples
javascriptnode.jsnpmconcatenation

Concatenate files with npm as build tool


I recently discovered that I can use npm as a task runner instead of gulp or grunt, everything is fantastic so far (lint, stylus, jade, uglify, watch .. etc) but the concatenation part, I cannot seem to achieve that. With gulp it was something like:

gulp.task('scripts', function() {
  return gulp.src('www/js/**/*.js')
    .pipe(concat('all.js'))
    .pipe(gulp.dest('www/dist'))
    .pipe(rename('all.min.js'))
    .pipe(uglify())
    .pipe(gulp.dest('www/dist'));
});

Is there a way I can do that with npm?

To be more clear, my goal is to do something like this:

// package.json

{
  "name": "f_todo",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "author": "",
  "license": "MIT",
  "devDependencies": {
    "concat": "^1.0.0",
    "rerun-script": "^0.6.0",
    "stylus": "^0.53.0"
  },
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "stylus": "stylus ss --compress --out lib/stylesheets",
    "concat": "concat dependency code would be here",
    "dev": "rerun-script"
  },
  "watches": {
    "stylus": "ss/**"
  }
}

Solution

  • try this

    var concat = require('concat')    
    concat(['a.css', 'b.css', 'c.css'], 'all.css')
    

    https://www.npmjs.com/package/concat

    and don't forget about npm install concat

    By command use concat-glob-cli

    "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1",
        "concat": "concat-glob-cli -f path/to/**/*.js -o bundle.js",
        ...
     },
    

    https://www.npmjs.com/package/concat-glob-cli