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/docs/index.rst b/docs/index.rst index e8c9ce2..eb180a6 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -180,6 +180,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/setup.py b/setup.py index 79fabbf..8ec10ec 100644 --- a/setup.py +++ b/setup.py @@ -57,7 +57,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/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): diff --git a/static_precompiler/compilers/browserify.py b/static_precompiler/compilers/browserify.py new file mode 100644 index 0000000..8ff4dc0 --- /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.split(' ') + + 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 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",