Jeff T. Jeff T. - 1 year ago 116
Node.js Question

Use NodeJS to run YUI compressor for output file

I tried to use YUI Compressor to compress my js file through NodeJS according to the document.


the module, successfully output the compressed string. Then I tried to output any file, but failed. The document says the option
is used for output file.

Is it possible to use
in NodeJS as well ? How ?

P.S., I know how to output file in java command (YUI Compressor is based on java).

Answer Source

Nope you can't pass -o in the options sent from your node program (these are the valid options if you use the compressor in a node program) however you can write the file on your own

var compressor = require('yuicompressor');
var fs = require('fs');

compressor.compress('/path/to/file or String of JS', {
    //Compressor Options:
    charset: 'utf8',
    type: 'js',
    nomunge: true,
    'line-break': 80
}, function(err, data, extra) {
    //err   If compressor encounters an error, it's stderr will be here
    //data  The compressed string, you write it out where you want it
    //extra The stderr (warnings are printed here in case you want to echo them
    if (err) throw err
    fs.writeFileSync('./output.js', data)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download