You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

uglifycss 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #! /usr/bin/env node
  2. /**
  3. * UglifyCSS
  4. * Port of YUI CSS Compressor to NodeJS
  5. * Author: Franck Marcia - https://github.com/fmarcia
  6. * MIT licenced
  7. */
  8. /**
  9. * cssmin.js
  10. * Author: Stoyan Stefanov - http://phpied.com/
  11. * This is a JavaScript port of the CSS minification tool
  12. * distributed with YUICompressor, itself a port
  13. * of the cssmin utility by Isaac Schlueter - http://foohack.com/
  14. * Permission is hereby granted to use the JavaScript version under the same
  15. * conditions as the YUICompressor (original YUICompressor note below).
  16. */
  17. /**
  18. * YUI Compressor
  19. * http://developer.yahoo.com/yui/compressor/
  20. * Author: Julien Lecomte - http://www.julienlecomte.net/
  21. * Copyright (c) 2011 Yahoo! Inc. All rights reserved.
  22. * The copyrights embodied in the content of this file are licensed
  23. * by Yahoo! Inc. under the BSD (revised) open source license.
  24. */
  25. var uglifycss = require("./");
  26. // Print usage
  27. function usage() {
  28. console.log(
  29. "Usage: uglifycss [options] file1.css [file2.css [...]] > output\n" +
  30. "options:\n" +
  31. "--max-line-len x add a newline every x characters\n" +
  32. "--expand-vars expand variables\n" +
  33. "--ugly-comments remove newlines within preserved comments\n" +
  34. "--cute-comments preserve newlines within and around preserved comments\n" +
  35. "--convert-urls d convert relative urls with the d directory as css location\n" +
  36. "--debug print full error stack on error"
  37. );
  38. }
  39. // Transform strings like "the-parameter" to "theParameter"
  40. function par2var(param) {
  41. return param.replace(/-(.)/g, function (ignore, character) {
  42. return character === "-" ? "" : character.toUpperCase();
  43. });
  44. }
  45. // Parse parameters from command line
  46. function parseArguments(argv) {
  47. var params = {
  48. options: uglifycss.defaultOptions,
  49. files: []
  50. };
  51. var len = argv.length;
  52. for (var i = 2; i < len; i += 1) {
  53. // get "propertyName" from "--argument-name"
  54. var v = par2var(argv[i]);
  55. // boolean parameters
  56. if (typeof params.options[v] === "boolean") {
  57. params.options[v] = true;
  58. // string parameter
  59. } else if (typeof params.options[v] === "string") {
  60. params.options[v] = argv[i + 1] || "";
  61. i += 1;
  62. // integer parameter
  63. } else if (typeof params.options[v] !== "undefined") {
  64. params.options[v] = parseInt(argv[i + 1], 10);
  65. i += 1;
  66. // file
  67. } else {
  68. params.files.push(argv[i]);
  69. }
  70. }
  71. return params;
  72. }
  73. // Entry
  74. (function main() {
  75. var params = parseArguments(process.argv);
  76. var nFiles = params.files.length;
  77. var stdin = process.stdin;
  78. var content = "";
  79. // . files
  80. if (nFiles) {
  81. console.log(uglifycss.processFiles(params.files, params.options));
  82. // . usage
  83. } else if (stdin.isTTY) {
  84. usage();
  85. // . stdin
  86. } else {
  87. stdin.on("data", function (part) {
  88. content += part;
  89. });
  90. stdin.on("end", function () {
  91. console.log(uglifycss.processString(content, params.options));
  92. });
  93. }
  94. }());