Question

Is there any method or solution for javascript compilation and combination like with lessc? I need a cli command for merging javascripts into one file. Maybe is there any nodejs app for it? Earlier I used CodeKit on mac, and that could compress and merge every file into one with annotated filenames like this:

// @codekit-prepend "blahblah.js"

I found Uglify2 for it, but I don't know surely it's the best solution for my problem.

I need something like this:

jscompilecommand filename.js > combined.min.js

or

jscompilecommand filename1.js, filename2.js, filename3.js > combined.min.js
Was it helpful?

Solution

Concatenation

There is a Grunt plugin https://github.com/gruntjs/grunt-contrib-concat . This provides the ability to concatenate any files, including JavaScript.

// Project configuration.
grunt.initConfig({
  concat: {
    dist: {
      src: ['src/intro.js', 'src/project.js', 'src/outro.js'],
      dest: 'dist/built.js',
    },
  },
});

It could be used to add banners (license notes, versions and etc.) in the sources too.

Merging

To merge JavaScript files in specific positions you can use the http://bem.info/articles/borschik/#js . It provides the ability to use various annotations in different situations. For example to include file in another use comments like:

var prj = {};

/* borschik:include:components/cookie.js */

function foo() {}

It is also available as a Grunt plugin.

Module systems

In many cases, it is better to use modular systems instead of plain files. There are provides many advantages as compared to, including tools to build optimized bundles from many source files. For example you can use http://requirejs.org/docs/optimization.html#onejs for AMD style modules or http://browserify.org/ for CommonJS modules.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top