twitst4tz

twitter statistics web application
Log | Files | Refs | README | LICENSE

bundle.js (1795B)


      1 'use strict';
      2 
      3 var fs = require('fs')
      4   , path = require('path')
      5   , browserify = require('browserify')
      6   , uglify = require('uglify-js');
      7 
      8 var pkg = process.argv[2]
      9   , standalone = process.argv[3]
     10   , compress = process.argv[4];
     11 
     12 var packageDir = path.join(__dirname, '..');
     13 if (pkg != '.') packageDir = path.join(packageDir, 'node_modules', pkg);
     14 
     15 var json = require(path.join(packageDir, 'package.json'));
     16 
     17 var distDir = path.join(__dirname, '..', 'dist');
     18 if (!fs.existsSync(distDir)) fs.mkdirSync(distDir);
     19 
     20 var bOpts = {};
     21 if (standalone) bOpts.standalone = standalone;
     22 
     23 browserify(bOpts)
     24 .require(path.join(packageDir, json.main), {expose: json.name})
     25 .bundle(function (err, buf) {
     26   if (err) {
     27     console.error('browserify error:', err);
     28     process.exit(1);
     29   }
     30 
     31   var outputFile = path.join(distDir, json.name);
     32   var uglifyOpts = {
     33     warnings: true,
     34     compress: {},
     35     output: {
     36       preamble: '/* ' + json.name + ' ' + json.version + ': ' + json.description + ' */'
     37     }
     38   };
     39   if (compress) {
     40     var compressOpts = compress.split(',');
     41     for (var i=0, il = compressOpts.length; i<il; ++i) {
     42       var pair = compressOpts[i].split('=');
     43       uglifyOpts.compress[pair[0]] = pair.length < 1 || pair[1] != 'false';
     44     }
     45   }
     46   if (standalone) {
     47     uglifyOpts.sourceMap = {
     48       filename: json.name + '.min.js',
     49       url: json.name + '.min.js.map'
     50     };
     51   }
     52 
     53   var result = uglify.minify(buf.toString(), uglifyOpts);
     54   fs.writeFileSync(outputFile + '.min.js', result.code);
     55   if (result.map) fs.writeFileSync(outputFile + '.min.js.map', result.map);
     56   if (standalone) fs.writeFileSync(outputFile + '.bundle.js', buf);
     57   if (result.warnings) {
     58     for (var j=0, jl = result.warnings.length; j<jl; ++j)
     59       console.warn('UglifyJS warning:', result.warnings[j]);
     60   }
     61 });