Concat and minify JS files in Node

2019-01-12 22:01发布

Is there any module in NodeJS to concatenate and minify JavaScript files?

9条回答
迷人小祖宗
2楼-- · 2019-01-12 22:34

If you like the Rails 3.1 asset pipeline approach, you should try my connect-assets library.

查看更多
倾城 Initia
3楼-- · 2019-01-12 22:35

If you're using Connect, then I've had good luck with Connect-Assetmanager

查看更多
4楼-- · 2019-01-12 22:39

UglifyJS is a Node module that is all about minifying javascript. I don't think it also joins files, but there might be an option I missed.

Edit: With UglifyJS 2, it has built in concatenation as well.

If you want to do this inline in your node app it's really easy. This allows you to dynamically generate your minified/concatenated js script at runtime without using the grunt or yeoman way.

npm install uglify-js

and in your module:

var fs = require('fs');
var uglify = require("uglify-js");

var uglified = uglify.minify(['file1.js', 'file2.js', 'file3.js']);

fs.writeFile('concat.min.js', uglified.code, function (err){
  if(err) {
    console.log(err);
  } else {
    console.log("Script generated and saved:", 'concat.min.js');
  }      
});
查看更多
登录 后发表回答