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

Side by Side Diff: tools/js2c.py

Issue 19013: Further minify non-visible JavaScript. (Closed)
Patch Set: Add to LICENSE Created 11 years, 11 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
« no previous file with comments | « src/mirror-delay.js ('k') | tools/jsmin.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 # 2 #
3 # Copyright 2006-2008 the V8 project authors. All rights reserved. 3 # Copyright 2006-2008 the V8 project authors. All rights reserved.
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 14 matching lines...) Expand all
25 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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 # This is a utility for converting JavaScript source code into C-style 30 # This is a utility for converting JavaScript source code into C-style
31 # char arrays. It is used for embedded JavaScript code in the V8 31 # char arrays. It is used for embedded JavaScript code in the V8
32 # library. 32 # library.
33 33
34 import os, re, sys, string 34 import os, re, sys, string
35 import jsmin
35 36
36 37
37 def ToCArray(lines): 38 def ToCArray(lines):
38 result = [] 39 result = []
39 for chr in lines: 40 for chr in lines:
40 value = ord(chr) 41 value = ord(chr)
41 assert value < 128 42 assert value < 128
42 result.append(str(value)) 43 result.append(str(value))
43 result.append("0") 44 result.append("0")
44 return ", ".join(result) 45 return ", ".join(result)
45 46
46 47
47 def CompressScript(lines): 48 def CompressScript(lines, do_jsmin):
49 # If we're not expecting this code to be user visible, we can run it through
50 # a more aggressive minifier.
51 if do_jsmin:
52 return jsmin.jsmin(lines)
53
48 # Remove stuff from the source that we don't want to appear when 54 # Remove stuff from the source that we don't want to appear when
49 # people print the source code using Function.prototype.toString(). 55 # people print the source code using Function.prototype.toString().
50 # Note that we could easily compress the scripts mode but don't 56 # Note that we could easily compress the scripts mode but don't
51 # since we want it to remain readable. 57 # since we want it to remain readable.
52 lines = re.sub('//.*\n', '\n', lines) # end-of-line comments 58 lines = re.sub('//.*\n', '\n', lines) # end-of-line comments
53 lines = re.sub(re.compile(r'/\*.*?\*/', re.DOTALL), '', lines) # comments. 59 lines = re.sub(re.compile(r'/\*.*?\*/', re.DOTALL), '', lines) # comments.
54 lines = re.sub('\s+\n+', '\n', lines) # trailing whitespace 60 lines = re.sub('\s+\n+', '\n', lines) # trailing whitespace
55 return lines 61 return lines
56 62
57 63
(...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after
265 (consts, macros) = ReadMacros(ReadLines(str(s))) 271 (consts, macros) = ReadMacros(ReadLines(str(s)))
266 else: 272 else:
267 modules.append(s) 273 modules.append(s)
268 274
269 # Build source code lines 275 # Build source code lines
270 source_lines = [ ] 276 source_lines = [ ]
271 source_lines_empty = [] 277 source_lines_empty = []
272 for s in modules: 278 for s in modules:
273 delay = str(s).endswith('-delay.js') 279 delay = str(s).endswith('-delay.js')
274 lines = ReadFile(str(s)) 280 lines = ReadFile(str(s))
281 do_jsmin = lines.find('// jsminify this file, js2c: jsmin') != -1
275 lines = ExpandConstants(lines, consts) 282 lines = ExpandConstants(lines, consts)
276 lines = ExpandMacros(lines, macros) 283 lines = ExpandMacros(lines, macros)
277 lines = CompressScript(lines) 284 lines = CompressScript(lines, do_jsmin)
278 data = ToCArray(lines) 285 data = ToCArray(lines)
279 id = (os.path.split(str(s))[1])[:-3] 286 id = (os.path.split(str(s))[1])[:-3]
280 if delay: id = id[:-6] 287 if delay: id = id[:-6]
281 if delay: 288 if delay:
282 delay_ids.append((id, len(lines))) 289 delay_ids.append((id, len(lines)))
283 else: 290 else:
284 ids.append((id, len(lines))) 291 ids.append((id, len(lines)))
285 source_lines.append(SOURCE_DECLARATION % { 'id': id, 'data': data }) 292 source_lines.append(SOURCE_DECLARATION % { 'id': id, 'data': data })
286 source_lines_empty.append(SOURCE_DECLARATION % { 'id': id, 'data': 0 }) 293 source_lines_empty.append(SOURCE_DECLARATION % { 'id': id, 'data': 0 })
287 294
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
349 356
350 def main(): 357 def main():
351 natives = sys.argv[1] 358 natives = sys.argv[1]
352 natives_empty = sys.argv[2] 359 natives_empty = sys.argv[2]
353 type = sys.argv[3] 360 type = sys.argv[3]
354 source_files = sys.argv[4:] 361 source_files = sys.argv[4:]
355 JS2C(source_files, [natives, natives_empty], { 'TYPE': type }) 362 JS2C(source_files, [natives, natives_empty], { 'TYPE': type })
356 363
357 if __name__ == "__main__": 364 if __name__ == "__main__":
358 main() 365 main()
OLDNEW
« no previous file with comments | « src/mirror-delay.js ('k') | tools/jsmin.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698