OLD | NEW |
---|---|
1 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2014 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 """Converts gwt-compiled javascript to standalone javascript | 5 """Converts gwt-compiled javascript to standalone javascript |
6 | 6 |
7 gwt-compiled javascript is in the form of an html file that is expected to be | 7 gwt-compiled javascript is in the form of an js file that is expected to be |
8 loaded into its own iframe (with some extra work done in the embedding page). | 8 loaded into its own script tag. This reads such a compiled file and converts it |
9 This reads such a compiled file and converts it to standalone javascript that | 9 to standalone javascript that can be loaded as Chrome does. |
10 can be loaded in the main frame of a page. | |
11 """ | 10 """ |
12 | 11 |
12 # TODO(cjhopman): The proper way to do this is to write a gwt Linker | |
13 # (gwt.core.ext.Linker) and use that for compilation. See | |
14 # http://crbug.com/437113 | |
15 | |
13 import glob | 16 import glob |
14 import optparse | 17 import optparse |
15 import os | 18 import os |
16 import re | 19 import re |
17 import sys | 20 import sys |
18 | 21 |
19 def ExtractJavascript(content): | 22 def ExtractJavascript(content): |
20 """ Extracts javascript from within <script> tags in content. """ | 23 """ Extracts javascript from within <script> tags in content. """ |
21 scriptre = re.compile('<script>(<!--)?(?P<inner>.*?)(-->)?</script>', | 24 lines = content.split('\n'); |
22 re.MULTILINE | re.DOTALL) | 25 for i, l in enumerate(lines): |
23 result = '' | 26 marker = 'domdistiller();' |
nyquist
2014/12/02 06:31:03
Any reason why |marker| is defined inside the loop
cjhopman
2014/12/02 20:10:35
Done.
| |
24 for match in scriptre.finditer(content): | 27 if l.startswith(marker): |
25 result += match.group('inner') | 28 return '\n'.join( |
26 return result | 29 [l[len(marker):]] + |
nyquist
2014/12/02 06:31:03
Maybe add a tiny comment about including everythin
cjhopman
2014/12/02 20:10:35
Done.
| |
27 | 30 lines[i + 1:-1] + |
28 def FindInputPath(indir): | 31 [lines[-1].replace( |
29 """ Finds the path to a file of the form | 32 'if (domdistiller) domdistiller.onScriptLoad(gwtOnLoad);', |
30 in/dir/DC2C3039DDCBB4AD9B63A9D3E25A0BDF.cache.html | 33 'gwtOnLoad(undefined, \'domdistiller\', \'\', 0);') |
31 | 34 ]) |
32 There should only be one such file in indir. | 35 raise Exception('Failed to find marker line') |
33 """ | |
34 files = glob.glob(os.path.join(indir, '*.cache.html')) | |
35 if len(files) != 1: | |
36 print 'Unable to find input path: ', files | |
37 return None | |
38 return files[0] | |
39 | 36 |
40 def main(argv): | 37 def main(argv): |
41 parser = optparse.OptionParser() | 38 parser = optparse.OptionParser() |
42 parser.add_option('-i', '--indir') | 39 parser.add_option('-i', '--infile') |
43 parser.add_option('-o', '--outfile') | 40 parser.add_option('-o', '--outfile') |
44 options, _ = parser.parse_args(argv) | 41 options, _ = parser.parse_args(argv) |
45 | 42 |
46 if options.indir: | 43 if options.infile: |
47 inpath = FindInputPath(options.indir) | 44 infile = open(options.infile, 'r') |
48 if not inpath: | |
49 return 1 | |
50 infile = open(inpath, 'r') | |
51 else: | 45 else: |
52 print 'Reading input from stdin' | 46 print 'Reading input from stdin' |
53 infile = sys.stdin | 47 infile = sys.stdin |
54 | 48 |
55 if options.outfile: | 49 if options.outfile: |
56 outfile = open(options.outfile, 'w') | 50 outfile = open(options.outfile, 'w') |
57 else: | 51 else: |
58 outfile = sys.stdout | 52 outfile = sys.stdout |
59 | 53 |
60 compiledJs = infile.read() | 54 compiledJs = infile.read() |
61 # The compiled js expects to be running in its own iframe. This won't be the | 55 # The compiled js expects to be running in its own iframe. This won't be the |
62 # case for the standalone js. | 56 # case for the standalone js. |
63 compiledJs = compiledJs.replace('var $wnd = parent', 'var $wnd = window') | 57 compiledJs = compiledJs.replace('var $wnd = parent', 'var $wnd = window') |
64 outfile.write(ExtractJavascript(compiledJs)) | 58 outfile.write(ExtractJavascript(compiledJs)) |
65 outfile.write('gwtOnLoad(undefined,"domdistiller","",0);\n') | |
66 | 59 |
67 return 0 | 60 return 0 |
68 | 61 |
69 | 62 |
70 if __name__ == '__main__': | 63 if __name__ == '__main__': |
71 sys.exit(main(sys.argv)) | 64 sys.exit(main(sys.argv)) |
OLD | NEW |