• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2# Copyright (c) 2012 The Chromium Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5import optparse
6import calcdeps
7import sys
8import StringIO
9
10def flatten_module_contents(filenames):
11  out = StringIO.StringIO()
12  load_sequence = calcdeps.calc_load_sequence(filenames)
13
14  flattened_module_names = ["'%s'" % module.name for module in load_sequence]
15  out.write("    if (!window.FLATTENED) window.FLATTENED = {};\n")
16  for module in load_sequence:
17    out.write("    window.FLATTENED['%s'] = true;\n" % module.name);
18
19  for module in load_sequence:
20    out.write(module.contents)
21    if module.contents[-1] != '\n':
22      out.write('\n')
23  return out.getvalue()
24
25def flatten_style_sheet_contents(filenames):
26  out = StringIO.StringIO()
27  load_sequence = calcdeps.calc_load_sequence(filenames)
28
29  # Stylesheets should be sourced from topmsot in, not inner-out.
30  load_sequence.reverse()
31
32  for module in load_sequence:
33    for style_sheet in module.style_sheets:
34      out.write(style_sheet.contents)
35      if style_sheet.contents[-1] != '\n':
36        out.write('\n')
37  return out.getvalue()
38
39def main(argv):
40  parser = optparse.OptionParser(usage="flatten filename1.js [filename2.js ...]")
41  parser.add_option("--css", dest="flatten_css", action="store_true", help="Outputs a flattened stylesheet.")
42  options, args = parser.parse_args(argv)
43
44  if options.flatten_css:
45    sys.stdout.write(flatten_style_sheet_contents(args))
46  else:
47    sys.stdout.write(flatten_module_contents(args))
48  return 255
49
50if __name__ == "__main__":
51  sys.exit(main(sys.argv))
52