Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Browserify #77

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ test_project/db.sqlite3
/docs/_build/
/docs/_static/
/docs/_templates/
/.eggs/
3 changes: 3 additions & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,9 @@ Babel
``plugins``
Babel `plugins <http://babeljs.io/docs/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 = (
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def read(fname):
author_email="[email protected]",
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',
Expand Down
1 change: 1 addition & 0 deletions static_precompiler/compilers/__init__.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
6 changes: 5 additions & 1 deletion static_precompiler/compilers/babel.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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):
Expand Down
65 changes: 65 additions & 0 deletions static_precompiler/compilers/browserify.py
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions static_precompiler/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down