Permalink
Browse files

javascript minification

  • Loading branch information...
1 parent 8deaa9f commit d2a5caaa50967ec7df1949120efb05566325eb17 Justine Tunney committed Aug 9, 2011
View
@@ -3,6 +3,7 @@ django_cache
occupywallst/settings_local.py
chat/settings_local.json
chat/node_modules
+occupywallst.min.js
core
*.pyc
View
@@ -0,0 +1,28 @@
+#!/usr/bin/env node
+
+var fs = require('fs');
+var uglify = require('uglify-js');
+
+var outfile = process.argv[2];
+var infiles = process.argv.slice(3);
+
+function min(code) {
+ var ast = uglify.parser.parse(code);
+ ast = uglify.uglify.ast_mangle(ast);
+ ast = uglify.uglify.ast_squeeze(ast);
+ return uglify.uglify.gen_code(ast);
+}
+
+var size = 0;
+var outcode = '';
+for (var n in infiles) {
+ var infile = infiles[n];
+ var incode = fs.readFileSync(infile, 'utf8');
+ size += incode.length;
+ outcode += min(incode) + '\n';
+}
+
+fs.writeFileSync(outfile, outcode);
+console.error("occupywallst javascript minified %d% to %dkB",
+ 100 - Math.round(outcode.length / size * 100),
+ Math.round(outcode.length / 1024));
View
@@ -7,6 +7,7 @@
"express": "2.4.3",
"jade": ">= 0.0.1",
"pg": ">= 0.5.3",
- "memcached": ">= 0.0.2"
+ "memcached": ">= 0.0.2",
+ "uglify-js": ">= 1.0.3"
}
}
@@ -18,6 +18,8 @@ def goodies(request):
else:
base = 'occupywallst/base.html'
return {'OWS_CANONICAL_URL': settings.OWS_CANONICAL_URL,
+ 'OWS_SCRIPTS_MINIFIED': settings.OWS_SCRIPTS_MINIFIED,
+ 'OWS_SCRIPTS': settings.OWS_SCRIPTS,
'DEBUG': settings.DEBUG,
'base': base}
View
@@ -7,9 +7,15 @@
"""
-from os.path import abspath, dirname, join
+import os
+import sys
+from os.path import abspath, dirname, join, exists
project_root = dirname(abspath(__file__))
+MEDIA_ROOT = join(project_root, 'media')
+GEOIP_PATH = join(project_root, 'data')
+SHP_PATH = join(project_root, 'data')
+
DEBUG = False
PAYPAL_DEBUG = DEBUG
AUTHNET_DEBUG = DEBUG
@@ -20,9 +26,9 @@
OWS_CANONICAL_URL = 'https://occupywallst.org' # no path or trailing slash
OWS_NOTIFY_PUB_ADDR = ('127.0.0.1', 9010)
-MEDIA_ROOT = join(project_root, 'media')
-GEOIP_PATH = join(project_root, 'data')
-SHP_PATH = join(project_root, 'data')
+OWS_SCRIPTS = ['js/occupywallst/' + fname
+ for fname in os.listdir(join(MEDIA_ROOT, 'js/occupywallst'))]
+OWS_SCRIPTS_MINIFIED = 'js/occupywallst.min.js'
ADMINS = (
('', 'errors@occupywallst.org'),
@@ -163,3 +169,17 @@
from occupywallst.settings_local import *
except ImportError:
pass
+
+try:
+ import subprocess
+ minifier = join(project_root, "../chat/minify.js")
+ outfile = join(MEDIA_ROOT, OWS_SCRIPTS_MINIFIED)
+ if exists(outfile):
+ os.unlink(outfile)
+ infiles = [abspath(join(MEDIA_ROOT, f)) for f in OWS_SCRIPTS]
+ proc = subprocess.Popen([minifier, outfile] + infiles)
+ assert proc.wait() == 0, "minifier exited non-zero"
+ assert exists(outfile), "minifier didn't produce output"
+except Exception, exc:
+ OWS_SCRIPTS_MINIFIED = ""
+ print >>sys.stderr, "javascript minifier failed:", exc
@@ -20,10 +20,6 @@
</style>
{% endblock stylesheets %}
-{% block scripts %}
- <script src="/media/js/article.js?v=7" type="text/javascript"></script>
-{% endblock scripts %}
-
{% block js_init %}
{{ block.super }}
article_init();
@@ -11,7 +11,6 @@
{% block scripts %}
<script src="//maps.google.com/maps/api/js?sensor=false&region=us" type="text/javascript"></script>
<script src="//maps.google.com/maps/api/js?libraries=places&sensor=false&region=us" type="text/javascript"></script>
- <script src="/media/js/attendees.js?v=7" type="text/javascript"></script>
{% endblock scripts %}
{% block js_init %}
@@ -17,16 +17,8 @@
<link rel="stylesheet" href="/media/css/article.css?v=7" type="text/css" media="all" />
{% block extra_head %}
{% endblock extra_head %}
- <script type="text/javascript">
- var _gaq = _gaq || [];
- _gaq.push(['_setAccount', 'UA-24711946-1']);
- _gaq.push(['_trackPageview']);
- (function() {
- var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
- ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
- var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
- })();
- </script>
+ {% block stylesheets %}
+ {% endblock stylesheets %}
</head>
<body>
{% block body %}
@@ -68,8 +60,6 @@
<div style="clear:both"></div>
{% endblock nav %}
<div id="content">
- {% block stylesheets %}
- {% endblock stylesheets %}
{% block content %}
<p>
No Content Available.
@@ -93,18 +83,31 @@
</div>
</div>
{% endblock body %}
- <script src="/media/js/jquery-1.6.min.js" type="text/javascript"></script>
+ <script src="/media/js/jquery-1.6.2{% if not DEBUG %}.min{% endif %}.js" type="text/javascript"></script>
+ {% if not DEBUG and OWS_SCRIPTS_MINIFIED %}
+ <script src="{{ MEDIA_URL }}{{ OWS_SCRIPTS_MINIFIED }}" type="text/javascript"></script>
+ {% else %}
+ {% for script in OWS_SCRIPTS %}
+ <script src="{{ MEDIA_URL }}{{ script }}" type="text/javascript"></script>
+ {% endfor %}
+ {% endif %}
<script src="//chat.{{ request.get_host }}/socket.io/socket.io.js" type="text/javascript"></script>
- <script src="/media/js/ows.js?v=7" type="text/javascript"></script>
{% block scripts %}
{% endblock scripts %}
<script type="text/javascript">
+ var _gaq = _gaq || [];
+ _gaq.push(['_setAccount', 'UA-24711946-1']);
+ _gaq.push(['_trackPageview']);
+ (function() {
+ var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
+ ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
+ var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
+ })();
function api(path, data, callback) {
data.csrfmiddlewaretoken = "{{ csrf_token }}";
return $.post(path, data, callback, "json");
}
$(function() {
- var OWS_DEBUG = {% if DEBUG %}true{% else %}false{% endif %};
{% block js_init %}
ows_init({
notifications: {{ notifications|jsonify|safe }}
@@ -14,10 +14,6 @@
</style>
{% endblock stylesheets %}
-{% block scripts %}
- <script src="/media/js/chat.js?v=7" type="text/javascript"></script>
-{% endblock scripts %}
-
{% block js_init %}
{{ block.super }}
chat_init({
@@ -9,10 +9,6 @@
</style>
{% endblock stylesheets %}
-{% block scripts %}
- <script src="/media/js/conference.js?v=7" type="text/javascript"></script>
-{% endblock scripts %}
-
{% block js_init %}
{{ block.super }}
conference_init({
@@ -9,10 +9,6 @@
</style>
{% endblock stylesheets %}
-{% block scripts %}
- <script src="/media/js/forum.js?v=7" type="text/javascript"></script>
-{% endblock scripts %}
-
{% block js_init %}
{{ block.super }}
forum_init();
@@ -11,7 +11,6 @@
{% block scripts %}
<script src="//maps.google.com/maps/api/js?sensor=false&region=us" type="text/javascript"></script>
<script src="//maps.google.com/maps/api/js?libraries=places&sensor=false&region=us" type="text/javascript"></script>
- <script src="/media/js/rides.js?v=7" type="text/javascript"></script>
{% endblock scripts %}
{% block js_init %}
@@ -5,7 +5,6 @@
{% block scripts %}
<script src="//maps.google.com/maps/api/js?sensor=false&region=us" type="text/javascript"></script>
<script src="//maps.google.com/maps/api/js?libraries=places&sensor=false&region=us" type="text/javascript"></script>
- <script src="/media/js/markfield.js?v=7" type="text/javascript"></script>
{% endblock scripts %}
{% block js_init %}
@@ -5,7 +5,6 @@
{% block scripts %}
<script src="//maps.google.com/maps/api/js?sensor=false&region=us" type="text/javascript"></script>
<script src="//maps.google.com/maps/api/js?libraries=places&sensor=false&region=us" type="text/javascript"></script>
- <script src="/media/js/userpage.js?v=7" type="text/javascript"></script>
{% endblock scripts %}
{% block js_init %}

0 comments on commit d2a5caa

Please sign in to comment.