html_to_js.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. # Copyright 2020 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. # Inlines an HTML file into a JS (or TS) file at a location specified by a
  5. # placeholder. This is useful for implementing Web Components using JS modules,
  6. # where all the HTML needs to reside in the JS file (no more HTML imports).
  7. import argparse
  8. import sys
  9. import io
  10. from os import path, getcwd, makedirs
  11. from polymer import process_v3_ready
  12. _CWD = getcwd()
  13. def main(argv):
  14. parser = argparse.ArgumentParser()
  15. parser.add_argument('--in_folder', required=True)
  16. parser.add_argument('--out_folder', required=True)
  17. parser.add_argument('--js_files', required=True, nargs="*")
  18. args = parser.parse_args(argv)
  19. in_folder = path.normpath(path.join(_CWD, args.in_folder))
  20. out_folder = path.normpath(path.join(_CWD, args.out_folder))
  21. results = []
  22. for js_file in args.js_files:
  23. # Detect file extension, since it can be either a .ts or .js file.
  24. extension = path.splitext(js_file)[1]
  25. html_file = js_file[:-len(extension)] + '.html'
  26. result = process_v3_ready(
  27. path.join(in_folder, js_file), path.join(in_folder, html_file))
  28. out_folder_for_file = path.join(out_folder, path.dirname(js_file))
  29. makedirs(out_folder_for_file, exist_ok=True)
  30. with io.open(path.join(out_folder, js_file), mode='wb') as f:
  31. for l in result[0]:
  32. f.write(l.encode('utf-8'))
  33. return
  34. if __name__ == '__main__':
  35. main(sys.argv[1:])