/hyde/ext/plugins/uglify.py

http://github.com/hyde/hyde · Python · 75 lines · 47 code · 12 blank · 16 comment · 4 complexity · d42e2e958344356f73ef3e2741216da4 MD5 · raw file

  1. # -*- coding: utf-8 -*-
  2. """
  3. Uglify plugin
  4. """
  5. from hyde.plugin import CLTransformer
  6. from hyde.fs import File
  7. class UglifyPlugin(CLTransformer):
  8. """
  9. The plugin class for Uglify JS
  10. """
  11. def __init__(self, site):
  12. super(UglifyPlugin, self).__init__(site)
  13. @property
  14. def executable_name(self):
  15. return "uglifyjs"
  16. @property
  17. def plugin_name(self):
  18. """
  19. The name of the plugin.
  20. """
  21. return "uglify"
  22. def text_resource_complete(self, resource, text):
  23. """
  24. If the site is in development mode, just return.
  25. Otherwise, save the file to a temporary place
  26. and run the uglify app. Read the generated file
  27. and return the text as output.
  28. """
  29. try:
  30. mode = self.site.config.mode
  31. except AttributeError:
  32. mode = "production"
  33. if not resource.source_file.kind == 'js':
  34. return
  35. if mode.startswith('dev'):
  36. self.logger.debug("Skipping uglify in development mode.")
  37. return
  38. supported = [
  39. ("beautify", "b"),
  40. ("indent", "i"),
  41. ("quote-keys", "q"),
  42. ("mangle-toplevel", "mt"),
  43. ("no-mangle", "nm"),
  44. ("no-squeeze", "ns"),
  45. "no-seqs",
  46. "no-dead-code",
  47. ("no-copyright", "nc"),
  48. "overwrite",
  49. "verbose",
  50. "unsafe",
  51. "max-line-len",
  52. "reserved-names",
  53. "ascii"
  54. ]
  55. uglify = self.app
  56. source = File.make_temp(text)
  57. target = File.make_temp('')
  58. args = [unicode(uglify)]
  59. args.extend(self.process_args(supported))
  60. args.extend(["-o", unicode(target), unicode(source)])
  61. self.call_app(args)
  62. out = target.read_all()
  63. return out