To-Slalom

minifier js css sublime text plugin

Feb 19th, 2020 (edited)
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.89 KB | None | 0 0
  1. import sublime, sublime_plugin
  2. import re , sys
  3.  
  4. # importante nome da Class tem de ser igual "command": "css_tominifier",
  5. class CssTominifierCommand ( sublime_plugin.TextCommand ) :
  6.  
  7.     # activar ou desactivar botao caso seja um dos files que pretendo
  8.     def is_enabled(self):
  9.         return run()
  10.  
  11.     def run ( self , edit ) :
  12.         selections              = self.view.sel()
  13.         file_types_to_minify    = ['js', 'css']
  14.         not_minify              = ['min']
  15.         file_name_parts         = self.view.file_name().split('.')
  16.         current_file_and_path   = self.view.file_name()
  17.         SplitWhat               = '\\' if sys.platform == 'win32' else '/'
  18.         parts_of_file_and_path  = self.view.file_name().split( SplitWhat )
  19.         current_file            = parts_of_file_and_path[-1]
  20.         current_file_name       = current_file.split('.')[0]
  21.         current_file_size       = self.view.size()
  22.         current_file_ext        = file_name_parts[-1]
  23.         current_path            = current_file_and_path.replace ( current_file , "" )
  24.         full_file               = current_path + current_file_name + '.min.' + current_file_ext
  25.  
  26.         # Files to exame css and js only
  27.         if current_file_ext in file_types_to_minify :
  28.             with open( current_file_and_path ) as file_in:
  29.                 lines = []
  30.                 for line in file_in:
  31.                     line = re.sub ( r'\s*\+\s*'                   , '+' , line )
  32.                     line = re.sub ( r'\s*>\s*'                    , '>' , line )
  33.                     line = re.sub ( r'\s*{\s*'                    , '{' , line )
  34.                     line = re.sub ( r'\s*}\s*'                    , '}' , line )
  35.                     line = re.sub ( r'\s*\(\s*'                   , '(' , line )
  36.                     line = re.sub ( r'\s*\)\s*'                   , ')' , line )
  37.                     line = re.sub ( r'\s*,\s*'                    , ',' , line )
  38.                     line = re.sub ( r'\s*!\s*'                    , '!' , line )
  39.                     line = re.sub ( r'\s*:\s*'                    , ':' , line )
  40.                     line = re.sub ( r'\s*;\s*'                    , ';' , line )
  41.                     line = re.sub ( r'\s*\*\s*'                   , '*' , line )
  42.                     line = re.sub ( r'\t|\n|\r|\s\s+|\/(.+)\/'    , ''  , line )
  43.                     lines.append(line)
  44.             # agora vou meter tudo numa so linha
  45.             clean = ''.join ( l for l in lines )
  46.             # Criar novo ficheiro min
  47.             if sublime.version() < '3':
  48.                 with open( full_file , 'w+' ) as min_file:
  49.                     min_file.write( clean )
  50.             else:
  51.                 with open(full_file, 'wb+' ) as min_file:
  52.                     min_file.write ( bytes ( clean , 'utf-8' ) )
  53.             # abrir o novo ficheiro numa nova tab
  54.             self.view.window().open_file( full_file )
Add Comment
Please, Sign In to add comment