From cec2bfcb9bbfebb59d8ba89fc0aa5294de8df71e Mon Sep 17 00:00:00 2001 From: David Avs Date: Wed, 6 Jan 2016 00:05:28 -0500 Subject: [PATCH 1/5] Add babel presets --- docs/index.rst | 3 +++ static_precompiler/compilers/babel.py | 6 +++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/docs/index.rst b/docs/index.rst index 0fe63ed..09d551d 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -177,6 +177,9 @@ Babel ``plugins`` Babel `plugins `_ command line option. Default: ``None`` (uses Babel's default option). +``presets`` + Babel ``presets`` command line option (sets of plugins). Default: ``None`` (uses Babel's default option). + Example:: STATIC_PRECOMPILER_COMPILERS = ( diff --git a/static_precompiler/compilers/babel.py b/static_precompiler/compilers/babel.py index 00825e0..8e6453b 100644 --- a/static_precompiler/compilers/babel.py +++ b/static_precompiler/compilers/babel.py @@ -16,13 +16,14 @@ class Babel(base.BaseCompiler): input_extension = "es6" output_extension = "js" - def __init__(self, executable="babel", sourcemap_enabled=False, modules=None, plugins=None): + def __init__(self, executable="babel", sourcemap_enabled=False, modules=None, plugins=None, presets=None): self.executable = executable self.is_sourcemap_enabled = sourcemap_enabled if modules: warnings.warn("'modules' option is removed in Babel 6.0. Use `plugins` instead.", DeprecationWarning) self.modules = modules self.plugins = plugins + self.presets = presets super(Babel, self).__init__() def get_extra_args(self): @@ -34,6 +35,9 @@ def get_extra_args(self): if self.plugins is not None: args += ["--plugins", self.plugins] + if self.presets is not None: + args += ["--presets", self.presets] + return args def compile_file(self, source_path): From 12c0fcc406bc6dd4a174e1175b7947d71beedde1 Mon Sep 17 00:00:00 2001 From: David Avs Date: Wed, 6 Jan 2016 00:42:30 -0500 Subject: [PATCH 2/5] Add browserify --- static_precompiler/compilers/browserify.py | 65 ++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 static_precompiler/compilers/browserify.py diff --git a/static_precompiler/compilers/browserify.py b/static_precompiler/compilers/browserify.py new file mode 100644 index 0000000..5b5201d --- /dev/null +++ b/static_precompiler/compilers/browserify.py @@ -0,0 +1,65 @@ +import os +import warnings + +from static_precompiler import exceptions, utils + +from . import base + +__all__ = ( + "Browserify", +) + + +class Browserify(base.BaseCompiler): + + name = "browserify" + input_extension = "jsx" + output_extension = "js" + + def __init__(self, executable="browserify", transform=None): + self.executable = executable + self.transform = transform + super(Browserify, self).__init__() + + def get_extra_args(self): + args = [] + + if self.transform: + args += ["-t", self.transform] + + return args + + def compile_file(self, source_path): + args = [ + self.executable, + ] + self.get_extra_args() + + full_output_path = self.get_full_output_path(source_path) + + full_output_dirname = os.path.dirname(full_output_path) + if not os.path.exists(full_output_dirname): + os.makedirs(full_output_dirname) + + args.append(self.get_full_source_path(source_path)) + args.extend(["-o", full_output_path]) + + out, errors = utils.run_command(args) + if errors: + raise exceptions.StaticCompilationError(errors) + + if self.is_sourcemap_enabled: + utils.fix_sourcemap(full_output_path + ".map", source_path, full_output_path) + + return self.get_output_path(source_path) + + def compile_source(self, source): + args = [ + "-", + self.executable, + ] + self.get_extra_args() + + out, errors = utils.run_command(args, source) + if errors: + raise exceptions.StaticCompilationError(errors) + + return out From 77ae9623e7c439d7052f05c3e12fa87c7301d205 Mon Sep 17 00:00:00 2001 From: David Avs Date: Wed, 6 Jan 2016 00:46:47 -0500 Subject: [PATCH 3/5] Import browserify --- .gitignore | 1 + setup.py | 2 +- static_precompiler/compilers/__init__.py | 1 + static_precompiler/settings.py | 1 + 4 files changed, 4 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index b515def..fa25bbf 100644 --- a/.gitignore +++ b/.gitignore @@ -19,3 +19,4 @@ test_project/db.sqlite3 /docs/_build/ /docs/_static/ /docs/_templates/ +/.eggs/ diff --git a/setup.py b/setup.py index e70c9a3..4b5de7e 100644 --- a/setup.py +++ b/setup.py @@ -46,7 +46,7 @@ def read(fname): author_email="andrey.fedoseev@gmail.com", url="https://github.com/andreyfedoseev/django-static-precompiler", description="Django template tags to compile all kinds of static files " - "(SASS, LESS, Stylus, CoffeeScript, Babel, LiveScript, Handlebars).", + "(SASS, LESS, Stylus, CoffeeScript, Babel, Browserify, LiveScript, Handlebars).", long_description="\n\n".join([README, CHANGES]), classifiers=[ 'Development Status :: 4 - Beta', diff --git a/static_precompiler/compilers/__init__.py b/static_precompiler/compilers/__init__.py index 30a8c92..fa569db 100644 --- a/static_precompiler/compilers/__init__.py +++ b/static_precompiler/compilers/__init__.py @@ -1,6 +1,7 @@ from .base import * # noqa from .coffeescript import * # noqa from .babel import * # noqa +from .browserify import * # noqa from .scss import * # noqa from .less import * # noqa from .stylus import * # noqa diff --git a/static_precompiler/settings.py b/static_precompiler/settings.py index 4bfd3f9..26cfd6a 100644 --- a/static_precompiler/settings.py +++ b/static_precompiler/settings.py @@ -14,6 +14,7 @@ COMPILERS = getattr(settings, "STATIC_PRECOMPILER_COMPILERS", ( "static_precompiler.compilers.CoffeeScript", "static_precompiler.compilers.Babel", + "static_precompiler.compilers.Browserify", "static_precompiler.compilers.Handlebars", "static_precompiler.compilers.SASS", "static_precompiler.compilers.SCSS", From 1b1ea3df1344baff185a133540e9f224cc83d9bc Mon Sep 17 00:00:00 2001 From: David Avs Date: Wed, 6 Jan 2016 00:48:52 -0500 Subject: [PATCH 4/5] Fix command line args --- static_precompiler/compilers/browserify.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/static_precompiler/compilers/browserify.py b/static_precompiler/compilers/browserify.py index 5b5201d..80af95c 100644 --- a/static_precompiler/compilers/browserify.py +++ b/static_precompiler/compilers/browserify.py @@ -54,8 +54,8 @@ def compile_file(self, source_path): def compile_source(self, source): args = [ - "-", self.executable, + "-", ] + self.get_extra_args() out, errors = utils.run_command(args, source) From fec6cb43386a35d317ba7b8b97fe020179e5df1c Mon Sep 17 00:00:00 2001 From: David Avs Date: Wed, 6 Jan 2016 00:53:07 -0500 Subject: [PATCH 5/5] Try to fix args for browserify --- static_precompiler/compilers/browserify.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/static_precompiler/compilers/browserify.py b/static_precompiler/compilers/browserify.py index 80af95c..8ff4dc0 100644 --- a/static_precompiler/compilers/browserify.py +++ b/static_precompiler/compilers/browserify.py @@ -25,7 +25,7 @@ def get_extra_args(self): args = [] if self.transform: - args += ["-t", self.transform] + args += ["-t"] + self.transform.split(' ') return args