jEdit宏,用于使用YUI compressor压缩.js

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了jEdit宏,用于使用YUI compressor压缩.js相关的知识,希望对你有一定的参考价值。

*language: beanshell*

be sure to adapt your YUICompressor path
  1. /*
  2.  * compress.bsh - a BeanShell macro script for the
  3.  * jEdit text editor - compresses js files and saves as <name>-compressed.js
  4.  *
  5.  * Copyright (C) 2010 Patrik Plihal
  6.  * patrik.plihal gmail com
  7.  *
  8.  * This program is free software; you can redistribute it and/or
  9.  * modify it under the terms of the GNU General Public License
  10.  * as published by the Free Software Foundation; either version 2
  11.  * of the License, or any later version.
  12.  *
  13.  * This program is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16.  * GNU General Public License for more details.
  17.  *
  18.  */
  19.  
  20. /* Be sure to adapt 'myPathToYUICompressor' */
  21.  
  22. void compress(){
  23. if(buffer.isNewFile())
  24. buffer.saveAs(view, true);
  25. else
  26. buffer.save(view, buffer.getPath());
  27.  
  28. mode = buffer.getMode().getName();
  29. path = buffer.getPath();
  30. os = System.getProperty("os.name");
  31.  
  32. if(os.indexOf("Windows") != -1)
  33. path = """ + path + """;
  34.  
  35. if(mode.equals("javascript")) {
  36. ls = System.getProperty("line.separator");
  37.  
  38. newpath = buffer.getPath();
  39. newpath = newpath.replace(".js", "-compressed.js");
  40. myPathToYUICompressor = ""E:\lib\yuicompressor-2.4.2.jar"";
  41.  
  42. runInSystemShell(view,"java -jar " + myPathToYUICompressor + " " + path + " -o " + newpath);
  43.  
  44. /* I want my .js head comments also in my -compress.js */
  45. compressed_comment = "";
  46. for(int i=0;i<8;i++){ //ajust this for amount of comment lines
  47. compressed_comment = compressed_comment + buffer.getLineText(i) + ls;
  48. }
  49.  
  50. //wait for YUI Compressor
  51. waitForConsole(view);
  52.  
  53. StringBuilder contents = new StringBuilder();
  54. try {
  55. //read
  56. BufferedReader input = new BufferedReader(new FileReader(newpath));
  57. try {
  58. String line = null; //not declared within while loop
  59. while (( line = input.readLine()) != null){
  60. contents.append(line);
  61. }
  62. }
  63. finally {
  64. input.close();
  65. }
  66.  
  67. //modify (prepend)
  68. compressed = compressed_comment + ls + contents.toString();
  69.  
  70. //write
  71. BufferedWriter out = new BufferedWriter(new FileWriter(newpath));
  72. try {
  73. out.write(compressed);
  74. }
  75. finally {
  76. out.close();
  77. }
  78. }
  79. catch (IOException ex){
  80. ex.printStackTrace();
  81. }
  82. }
  83. else {
  84. Macros.error(view,
  85. "The current file does not appear to be a javascript.");
  86. }
  87. }
  88.  
  89. compress();

以上是关于jEdit宏,用于使用YUI compressor压缩.js的主要内容,如果未能解决你的问题,请参考以下文章

JS代码压缩使用YUI Compressor对js文件进行压缩处理

YUI Compressor 和“使用严格”提示

Maven压缩插件YUI Compressor使用介绍

PHPStorm自动压缩YUI Compressor配置

使用 maven、spring、freemarker 等帮助设置 YUI Compressor 以适应不同的环境(测试/开发/生产)

YUI Compressor完成代码混淆示例