Python 使用jsmin压缩JavaScript代码

##JS压缩

#使用jsmin 项目主页 https://pypi.python.org/pypi/jsmin/

import os
from jsmin import jsmin


class FormatJs:
    
    fromPath = "E:/Workspaces/DGSXGL/WebRoot/assets/app"  #压缩目录

    def main(self):
        path = self.parseCode(self.fromPath)
        
    def parseCode(self, path):
        dirs = os.listdir(path)    

        for d in dirs:        
            subPath = os.path.join(path, d)
            if os.path.isfile(subPath):
                code = self.formatJs(subPath)
                fw = open(subPath, "w")
                fw.write(code)
                fw.flush()
                fw.close()
                
            else:                
                self.parseCode(subPath)
        
    def formatJs(self, path):
        with open(path) as js_file:
            return jsmin(js_file.read())

#使用
f = FormatJs()
f.main()

 

发表评论