Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(485)

Side by Side Diff: Source/web/scripts/make-file-arrays.py

Issue 252633006: Compact JS resource better by using rjsmin. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 # Copyright (C) 2012 Google Inc. All rights reserved. 2 # Copyright (C) 2012 Google Inc. All rights reserved.
3 # 3 #
4 # Redistribution and use in source and binary forms, with or without 4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions are 5 # modification, are permitted provided that the following conditions are
6 # met: 6 # met:
7 # 7 #
8 # * Redistributions of source code must retain the above copyright 8 # * Redistributions of source code must retain the above copyright
9 # notice, this list of conditions and the following disclaimer. 9 # notice, this list of conditions and the following disclaimer.
10 # * Redistributions in binary form must reproduce the above 10 # * Redistributions in binary form must reproduce the above
(...skipping 16 matching lines...) Expand all
27 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 29
30 # Usage: make-file-arrays.py [--condition=condition-string] --out-h=<header-file -name> --out-cpp=<cpp-file-name> <input-file>... 30 # Usage: make-file-arrays.py [--condition=condition-string] --out-h=<header-file -name> --out-cpp=<cpp-file-name> <input-file>...
31 31
32 import os.path 32 import os.path
33 import re 33 import re
34 import sys 34 import sys
35 from optparse import OptionParser 35 from optparse import OptionParser
36 36
37 rjsmin_path = os.path.abspath(os.path.join(
38 os.path.dirname(__file__),
39 "..",
40 "..",
41 "build",
42 "scripts"))
43 sys.path.append(rjsmin_path)
44 import rjsmin
45
37 46
38 def make_variable_name_and_read(file_name): 47 def make_variable_name_and_read(file_name):
39 result = re.match(r'([\w\d_]+)\.([\w\d_]+)', os.path.basename(file_name)) 48 result = re.match(r'([\w\d_]+)\.([\w\d_]+)', os.path.basename(file_name))
40 if not result: 49 if not result:
41 print 'Invalid input file name:', os.path.basename(file_name) 50 print 'Invalid input file name:', os.path.basename(file_name)
42 sys.exit(1) 51 sys.exit(1)
43 variable_name = result.group(1)[0].lower() + result.group(1)[1:] + result.gr oup(2).capitalize() 52 variable_name = result.group(1)[0].lower() + result.group(1)[1:] + result.gr oup(2).capitalize()
44 with open(file_name, 'rb') as f: 53 with open(file_name, 'rb') as f:
45 content = f.read() 54 content = f.read()
46 return variable_name, content 55 return variable_name, content
47 56
48 57
49 def strip_whitespace_and_comments(file_name, content): 58 def strip_whitespace_and_comments(file_name, content):
50 result = re.match(r'.*\.([^.]+)', file_name) 59 result = re.match(r'.*\.([^.]+)', file_name)
51 if not result: 60 if not result:
52 print 'The file name has no extension:', file_name 61 print 'The file name has no extension:', file_name
53 sys.exit(1) 62 sys.exit(1)
54 extension = result.group(1).lower() 63 extension = result.group(1).lower()
55 multi_line_comment = re.compile(r'/\*.*?\*/', re.MULTILINE | re.DOTALL) 64 multi_line_comment = re.compile(r'/\*.*?\*/', re.MULTILINE | re.DOTALL)
56 single_line_comment = re.compile(r'^//.*$', re.MULTILINE)
57 # Don't accidentally match URLs (http://...) 65 # Don't accidentally match URLs (http://...)
58 trailing_comment = re.compile(r'([^:])//.*$', re.MULTILINE)
59 repeating_space = re.compile(r'[ \t]+', re.MULTILINE) 66 repeating_space = re.compile(r'[ \t]+', re.MULTILINE)
60 leading_space = re.compile(r'^[ \t]+', re.MULTILINE) 67 leading_space = re.compile(r'^[ \t]+', re.MULTILINE)
61 trailing_space = re.compile(r'[ \t]+$', re.MULTILINE) 68 trailing_space = re.compile(r'[ \t]+$', re.MULTILINE)
62 empty_line = re.compile(r'\n+') 69 empty_line = re.compile(r'\n+')
63 if extension == 'js': 70 if extension == 'js':
64 content = multi_line_comment.sub('', content) 71 content = rjsmin.jsmin(content)
65 content = single_line_comment.sub('', content)
66 content = trailing_comment.sub(r'\1', content)
67 content = repeating_space.sub(' ', content)
68 content = leading_space.sub('', content)
69 content = trailing_space.sub('', content)
70 content = empty_line.sub('\n', content)
abarth-chromium 2014/04/26 17:08:36 It replaces this one, but may this one is too simp
71 elif extension == 'css': 72 elif extension == 'css':
72 content = multi_line_comment.sub('', content) 73 content = multi_line_comment.sub('', content)
73 content = repeating_space.sub(' ', content) 74 content = repeating_space.sub(' ', content)
74 content = leading_space.sub('', content) 75 content = leading_space.sub('', content)
75 content = trailing_space.sub('', content) 76 content = trailing_space.sub('', content)
76 content = empty_line.sub('\n', content) 77 content = empty_line.sub('\n', content)
77 return content 78 return content
78 79
79 80
80 def process_file(file_name): 81 def process_file(file_name):
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
147 148
148 names_and_contents = [process_file(file_name) for file_name in args] 149 names_and_contents = [process_file(file_name) for file_name in args]
149 150
150 if options.out_header: 151 if options.out_header:
151 write_header_file(options.out_header, options.flag, names_and_contents) 152 write_header_file(options.out_header, options.flag, names_and_contents)
152 write_cpp_file(options.out_cpp, options.flag, names_and_contents, options.ou t_header) 153 write_cpp_file(options.out_cpp, options.flag, names_and_contents, options.ou t_header)
153 154
154 155
155 if __name__ == '__main__': 156 if __name__ == '__main__':
156 sys.exit(main()) 157 sys.exit(main())
OLDNEW
« Source/build/scripts/rjsmin.py ('K') | « Source/devtools/scripts/rjsmin.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698