Skip to content

Instantly share code, notes, and snippets.

@amol-
Last active October 29, 2015 09:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amol-/25bd86dfc630bf43aab2 to your computer and use it in GitHub Desktop.
Save amol-/25bd86dfc630bf43aab2 to your computer and use it in GitHub Desktop.
WebAssets Filter to compile ES6 to ES5 using DukPY and BabelJS
# -*- coding: utf-8 -*-
from __future__ import absolute_import, print_function
from webassets.filter import Filter
__all__ = ('BabelJS',)
# NOTE: When using the BabelJS compiler for code
# that needs to run in the browser make sure to add
# https://cdnjs.cloudflare.com/ajax/libs/babel-core/4.6.6/browser-polyfill.js
class BabelJS(Filter):
"""WebAssets filter to compile ES6 to ES5 using DukPY.
can be registered in WebAssets using::
import webassets.filter
webassets.filter.register_filter(BabelJS)
it will be available as ``babeljs``
"""
name = 'babeljs'
max_debug_level = None
def setup(self):
try:
import dukpy
except ImportError:
raise EnvironmentError('The "dukpy" package is not installed.')
else:
self.dukpy = dukpy
super(BabelJS, self).setup()
def input(self, _in, out, **kw):
src = self.dukpy.babel_compile(_in.read())
out.write(src)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment