From 93c9e9cb07709a36f57204598bd72ab5e03cb7ca Mon Sep 17 00:00:00 2001 From: Alessandro Molina Date: Fri, 6 Nov 2015 15:59:59 +0100 Subject: [PATCH] Add WebAssets support --- README.rst | 10 ++++++++++ webassets/__init__.py | 0 webassets/babelfilter.py | 20 ++++++++++++++++++++ 3 files changed, 30 insertions(+) create mode 100644 webassets/__init__.py create mode 100644 webassets/babelfilter.py diff --git a/README.rst b/README.rst index 4cd5f60..fa1fe27 100644 --- a/README.rst +++ b/README.rst @@ -41,6 +41,16 @@ To compile ES6 code to ES5 for everyday usage you can use ``dukpy.babel_compile` ... ''') '"use strict";\n\nvar _prototypeProperties = function (child, staticProps, instanceProps) { if (staticProps) Object.defineProperties(child, staticProps); if (instanceProps) Object.defineProperties(child.prototype, instanceProps); };\n\nvar _classCallCheck = function (instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } };\n\nvar Point = (function () {\n function Point(x, y) {\n _classCallCheck(this, Point);\n\n this.x = x;\n this.y = y;\n }\n\n _prototypeProperties(Point, null, {\n toString: {\n value: function toString() {\n return "(" + this.x + ", " + this.y + ")";\n },\n writable: true,\n configurable: true\n }\n });\n\n return Point;\n})();\n' +The DukPY based BabelJS compiler also provides a WebAssets ( http://webassets.readthedocs.org/en/latest/ ) filter to automatically compile ES6 code in your assets pipeline. +You register this filter as ``babeljs`` within WebAssets using:: + + from webassets.filter import register_filter + from dukpy.webassets.babelfilter import BabelJS + + register_filter(BabelJS) + +Which makes the filter available with the ``babeljs`` name. + **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 dependency. Using the JavaScript Interpreter diff --git a/webassets/__init__.py b/webassets/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/webassets/babelfilter.py b/webassets/babelfilter.py new file mode 100644 index 0000000..179da29 --- /dev/null +++ b/webassets/babelfilter.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +from __future__ import absolute_import, print_function +from webassets.filter import Filter + +import dukpy + + +__all__ = ('BabelJS', ) + + +class BabelJS(Filter): + name = 'babeljs' + max_debug_level = None + + def setup(self): + super(BabelJS, self).setup() + + def input(self, _in, out, **kw): + src = dukpy.babel_compile(_in.read()) + out.write(src)