OLD | NEW |
(Empty) | |
| 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 |
| 3 # found in the LICENSE file. |
| 4 |
| 5 """Converts gwt-compiled javascript to standalone javascript |
| 6 |
| 7 gwt-compiled javascript is in the form of an js file that is expected to be |
| 8 loaded into its own script tag. This reads such a compiled file and converts it |
| 9 to standalone javascript that can be loaded as Chrome does. |
| 10 """ |
| 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 |
| 16 import glob |
| 17 import optparse |
| 18 import os |
| 19 import re |
| 20 import shutil |
| 21 import sys |
| 22 |
| 23 def ExtractJavascript(content, module): |
| 24 """ Extracts javascript from within <script> tags in content. """ |
| 25 lines = content.split('\n'); |
| 26 # The generated javascript looks something like: |
| 27 # |
| 28 # function domdistiller() { |
| 29 # ... |
| 30 # } |
| 31 # domdistiller();<useful code here> |
| 32 # <more useful code> |
| 33 # <last useful code>;if (domdistiller) domdistiller.onScriptLoad(gwtOnLoad); |
| 34 # |
| 35 # And so we extract the useful parts and append the correct gwtOnLoad call. |
| 36 marker = module + '();' |
| 37 for i, l in enumerate(lines): |
| 38 if l.startswith(marker): |
| 39 return '\n'.join( |
| 40 [l[len(marker):]] + |
| 41 lines[i + 1:-1] + |
| 42 [lines[-1].replace( |
| 43 'if ({0}) {0}.onScriptLoad(gwtOnLoad);'.format(module), |
| 44 'gwtOnLoad(undefined, "{0}", "", 0);'.format(module)) |
| 45 ]) |
| 46 raise Exception('Failed to find marker line') |
| 47 |
| 48 def main(argv): |
| 49 parser = optparse.OptionParser() |
| 50 parser.add_option('-i', '--infile') |
| 51 parser.add_option('-o', '--outfile') |
| 52 parser.add_option('--module', help='Name of generated javascript module.') |
| 53 parser.add_option('--auto', action='store_true', |
| 54 help='Calculate input/output paths based on module name.') |
| 55 parser.add_option('--sourcemaps', action='store_true', |
| 56 help='Also copy sourcemaps.') |
| 57 options, _ = parser.parse_args(argv) |
| 58 |
| 59 inpath = options.infile |
| 60 outpath = options.outfile |
| 61 if options.auto: |
| 62 inpath = 'war/{0}/{0}.nocache.js'.format(options.module) |
| 63 outpath = 'out/{0}.js'.format(options.module) |
| 64 |
| 65 if inpath: |
| 66 infile = open(inpath, 'r') |
| 67 else: |
| 68 print 'Reading input from stdin' |
| 69 infile = sys.stdin |
| 70 |
| 71 if outpath: |
| 72 outfile = open(outpath, 'w') |
| 73 else: |
| 74 outfile = sys.stdout |
| 75 |
| 76 compiledJs = infile.read() |
| 77 # The compiled js expects to be running in its own iframe. This won't be the |
| 78 # case for the standalone js. |
| 79 compiledJs = compiledJs.replace('var $wnd = parent', 'var $wnd = window') |
| 80 outfile.write(ExtractJavascript(compiledJs, options.module)) |
| 81 if options.sourcemaps: |
| 82 sourcemap = 'debug/{0}/src/{0}.sourcemap'.format(options.module) |
| 83 outfile.write('\n') |
| 84 outfile.write('//@ sourceMappingURL=%s' % sourcemap) |
| 85 |
| 86 insourcemap = glob.glob( |
| 87 'war/WEB-INF/deploy/%s/symbolMaps/*sourceMap*' % options.module)[0] |
| 88 outsourcemap = 'out/%s' % sourcemap |
| 89 shutil.copy(insourcemap, outsourcemap) |
| 90 |
| 91 return 0 |
| 92 |
| 93 |
| 94 if __name__ == '__main__': |
| 95 sys.exit(main(sys.argv)) |
OLD | NEW |