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

first attempt for minified versions #44

Open
wants to merge 1 commit into
base: master
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
16 changes: 15 additions & 1 deletion bowerstatic/core.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import re
import json
from .publisher import Publisher
from .injector import Injector
Expand All @@ -11,11 +12,12 @@
class Bower(object):
"""Contains a bunch of bower_components directories.
"""
def __init__(self, publisher_signature='bowerstatic', autoversion=None):
def __init__(self, publisher_signature='bowerstatic', autoversion=None, use_minified=False):
self.publisher_signature = publisher_signature
self._component_collections = {}
self._renderer = Renderer()
self.autoversion = autoversion or filesystem_second_autoversion
self.use_minified = use_minified

def components(self, name, path):
if name in self._component_collections:
Expand Down Expand Up @@ -45,6 +47,9 @@ def renderer(self, ext, render_func):
self._renderer.register(ext, render_func)

def html(self, resource):
if self.use_minified and resource.minified_resource:
resource = resource.minified_resource

return self._renderer.html(resource)

def filter_by_known_ext(self, paths):
Expand Down Expand Up @@ -274,6 +279,15 @@ def __init__(self, component, file_path, dependencies):
self.dependencies = dependencies
dummy, self.ext = os.path.splitext(file_path)

self.minified_resource = None

if self.component.bower.use_minified:
file_path = re.sub(r"(?<!\.min)\.js$", ".min.js", file_path)
file_path = re.sub(r"(?<!\.min)\.css$", ".min.css", file_path)
full_path = os.path.join(component.path, file_path)
if file_path != self.file_path and os.path.exists(full_path):
self.minified_resource = Resource(component, file_path, [])

def url(self):
return self.component.url() + self.file_path

Expand Down
35 changes: 35 additions & 0 deletions bowerstatic/tests/test_injector.py
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,41 @@ def wsgi(environ, start_response):
b'</head><body>Hello!</body></html>')


def test_injector_minified():
bower = bowerstatic.Bower(use_minified=True)

components = bower.components('components', os.path.join(
os.path.dirname(__file__), 'bower_components'))

def wsgi(environ, start_response):
start_response('200 OK', [('Content-Type', 'text/html;charset=UTF-8')])
include = components.includer(environ)
# going to pull in jquery-ui and jquery twice
include('jquery-ui')
include('jquery-ui-bootstrap')
return ['<html><head></head><body>Hello!</body></html>']

injector = bower.injector(wsgi)

c = Client(injector)

response = c.get('/')

assert response.body == (
b'<html><head>'
b'<script type="text/javascript" '
b'src="/bowerstatic/components/jquery/2.1.1/dist/jquery.min.js">'
b'</script>\n'
b'<script type="text/javascript" '
b'src="/bowerstatic/components/jquery-ui/1.10.4/ui/jquery-ui.js">'
b'</script>\n'
b'<link rel="stylesheet" type="text/css" '
b'href="/bowerstatic/components/jquery-ui-bootstrap/0.2.5/'
b'jquery.ui.theme.css" />'
b'</head><body>Hello!</body></html>')



def test_injector_no_head_to_inject():
bower = bowerstatic.Bower()

Expand Down