Created
May 20, 2015 17:43
-
-
Save jieter/9cc855148959429abbda to your computer and use it in GitHub Desktop.
Experiment using browserify with django-compressor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import logging | |
import subprocess | |
from compressor.exceptions import FilterError | |
from compressor.filters import CompilerFilter | |
from django.conf import settings | |
logger = logging.getLogger("coconut.helpers.compressor") | |
def npm_install(): | |
'''run npm install in cwd''' | |
logger.info('running `npm install`') | |
command = '/usr/bin/npm install' | |
if settings.PRODUCTION: | |
command = 'sudo ' + command | |
try: | |
proc = subprocess.Popen( | |
command, | |
shell=True, cwd=settings.BROWSERIFY_DIR, stdout=subprocess.PIPE, | |
stdin=subprocess.PIPE, stderr=subprocess.PIPE) | |
filtered, err = proc.communicate() | |
except (IOError, OSError) as e: | |
raise FilterError('Running `npm install` failed: %s' % str(e)) | |
else: | |
if proc.wait() != 0: | |
raise FilterError(err) | |
# run on import | |
npm_install() | |
class BrowserifyFilter(CompilerFilter): | |
command = 'node_modules/.bin/browserify {infile} -o {outfile}' | |
def __init__(self, *args, **kwargs): | |
super(BrowserifyFilter, self).__init__(*args, **kwargs) | |
self.cwd = settings.BROWSERIFY_DIR | |
def input(self, **kwargs): | |
# if compress_offline=True, run here too. Seems to be necessary. | |
if settings.COMPRESS_OFFLINE: | |
npm_install() | |
return super(BrowserifyFilter, self).input(**kwargs) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment