Add webpack build process.

This commit is contained in:
Allie Jones 2015-10-26 12:11:44 -04:00 committed by Tim Abbott
parent e20a1bc73c
commit 85809e6140
11 changed files with 81 additions and 2 deletions

1
.gitignore vendored
View File

@ -29,6 +29,7 @@ manage.log
event_queues.json event_queues.json
.vagrant .vagrant
/zproject/dev-secrets.conf /zproject/dev-secrets.conf
static/js/bundle.js
static/third/gemoji/ static/third/gemoji/
static/third/zxcvbn/ static/third/zxcvbn/
tools/emoji_dump/bitmaps/ tools/emoji_dump/bitmaps/

View File

@ -12,7 +12,9 @@
"xmlhttprequest": "1.5.0", "xmlhttprequest": "1.5.0",
"nwmatcher": "1.3.6", "nwmatcher": "1.3.6",
"htmlparser2": "3.8.3", "htmlparser2": "3.8.3",
"cssstyle": "0.2.29" "cssstyle": "0.2.29",
"webpack": "1.12.2",
"webpack-dev-server": "1.12.1"
}, },
"scripts": {}, "scripts": {},
"repository": { "repository": {

1
static/js/src/main.js Normal file
View File

@ -0,0 +1 @@
// commonjs code goes here

View File

@ -39,6 +39,10 @@ var page_params = {{ page_params }};
{% endif %} {% endif %}
{% minified_js 'app' %} {% minified_js 'app' %}
{% if not pipeline %}
<script type="text/javascript" src="/webpack/bundle.js"></script>
{% endif %}
{% if debug %} {% if debug %}
{% minified_js 'app_debug' %} {% minified_js 'app_debug' %}
{% endif %} {% endif %}

View File

@ -28,6 +28,9 @@ STATIC_PATH = 'static/'
# Compile Handlebars templates # Compile Handlebars templates
subprocess.check_call(['tools/compile-handlebars-templates']) subprocess.check_call(['tools/compile-handlebars-templates'])
# Create webpack bundle
subprocess.check_call(['tools/webpack'])
def get_changed_source_files(other_checkout): def get_changed_source_files(other_checkout):
""" Get list of changed static files since other_checkout. """ Get list of changed static files since other_checkout.
If git fails to return a reasonable looking list, this returns None, If git fails to return a reasonable looking list, this returns None,
@ -69,9 +72,10 @@ if prev_deploy:
if changed_files is None: if changed_files is None:
prev_deploy = None prev_deploy = None
# Always use the newly compiled handlebars templates. # Always use the newly compiled handlebars templates and webpack bundle.
if prev_deploy: if prev_deploy:
changed_files.add(os.path.join(STATIC_PATH, 'templates/compiled.js')) changed_files.add(os.path.join(STATIC_PATH, 'templates/compiled.js'))
changed_files.add(os.path.join(STATIC_PATH, 'js/bundle.js'))
JS_SPECS = settings.JS_SPECS JS_SPECS = settings.JS_SPECS
CLOSURE_BINARY = '/usr/bin/closure-compiler' CLOSURE_BINARY = '/usr/bin/closure-compiler'

View File

@ -62,6 +62,7 @@ sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
proxy_port = base_port proxy_port = base_port
django_port = base_port+1 django_port = base_port+1
tornado_port = base_port+2 tornado_port = base_port+2
webpack_port = base_port+3
os.chdir(os.path.join(os.path.dirname(__file__), '..')) os.chdir(os.path.join(os.path.dirname(__file__), '..'))
@ -75,6 +76,7 @@ os.setpgrp()
# Pass --nostatic because we configure static serving ourselves in # Pass --nostatic because we configure static serving ourselves in
# zulip/urls.py. # zulip/urls.py.
cmds = [['./tools/compile-handlebars-templates', 'forever'], cmds = [['./tools/compile-handlebars-templates', 'forever'],
['./tools/webpack', 'watch'],
['python', 'manage.py', 'runserver', '--nostatic'] + ['python', 'manage.py', 'runserver', '--nostatic'] +
manage_args + ['localhost:%d' % (django_port,)], manage_args + ['localhost:%d' % (django_port,)],
['python', 'manage.py', 'runtornado'] + ['python', 'manage.py', 'runtornado'] +
@ -98,6 +100,10 @@ class Resource(resource.Resource):
request.uri.startswith('/sockjs')): request.uri.startswith('/sockjs')):
return proxy.ReverseProxyResource('localhost', tornado_port, '/'+name) return proxy.ReverseProxyResource('localhost', tornado_port, '/'+name)
elif (request.uri.startswith('/webpack') or
request.uri.startswith('/socket.io')):
return proxy.ReverseProxyResource('localhost', webpack_port, '/'+name)
return proxy.ReverseProxyResource('localhost', django_port, '/'+name) return proxy.ReverseProxyResource('localhost', django_port, '/'+name)
try: try:

30
tools/webpack Executable file
View File

@ -0,0 +1,30 @@
#!/usr/bin/env python
from __future__ import absolute_import
import os
import subprocess
import sys
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))
os.environ['DJANGO_SETTINGS_MODULE'] = 'zproject.settings'
from django.conf import settings
os.chdir(settings.DEPLOY_ROOT)
STATIC_PATH = 'static/'
def run():
# write the file to disk
subprocess.check_call(['tools/node', 'node_modules/.bin/webpack'] +
['--config', 'webpack.production.config.js'])
def run_watch():
# watch and rebuild on changes, serve file from memory via webpack-dev-server
subprocess.Popen(['tools/node', 'node_modules/.bin/webpack-dev-server'] +
['--config', 'webpack.config.js', '--watch-poll'])
if __name__ == '__main__':
if len(sys.argv) == 2 and sys.argv[1] == 'watch':
run_watch()
else:
run()

19
webpack.config.js Normal file
View File

@ -0,0 +1,19 @@
module.exports = {
entry: [
'webpack-dev-server/client?http://localhost:9991/socket.io',
'./static/js/src/main.js'
],
devtool: 'eval',
output: {
publicPath: 'http://localhost:9991/webpack/',
path: './static/js',
filename: 'bundle.js'
},
devServer: {
port: 9994,
watchOptions: {
aggregateTimeout: 300,
poll: 1000
}
}
};

View File

@ -0,0 +1,7 @@
module.exports = {
entry: './static/js/src/main.js',
output: {
path: './static/js',
filename: 'bundle.js'
}
};

View File

@ -962,6 +962,7 @@ def home(request):
'avatar_url': avatar_url(user_profile), 'avatar_url': avatar_url(user_profile),
'show_debug': 'show_debug':
settings.DEBUG and ('show_debug' in request.GET), settings.DEBUG and ('show_debug' in request.GET),
'pipeline': settings.PIPELINE,
'show_invites': show_invites, 'show_invites': show_invites,
'is_admin': user_profile.is_admin(), 'is_admin': user_profile.is_admin(),
'show_webathena': user_profile.realm.domain == "mit.edu", 'show_webathena': user_profile.realm.domain == "mit.edu",

View File

@ -704,6 +704,7 @@ JS_SPECS = {
'js/referral.js', 'js/referral.js',
'js/custom_markdown.js', 'js/custom_markdown.js',
'js/bot_data.js', 'js/bot_data.js',
# JS bundled by webpack is also included here if PIPELINE setting is true
], ],
'output_filename': 'min/app.js' 'output_filename': 'min/app.js'
}, },
@ -720,6 +721,9 @@ JS_SPECS = {
}, },
} }
if PIPELINE:
JS_SPECS['app']['source_filenames'].append('js/bundle.js')
app_srcs = JS_SPECS['app']['source_filenames'] app_srcs = JS_SPECS['app']['source_filenames']
PIPELINE_JS = {} # Now handled in tools/minify-js PIPELINE_JS = {} # Now handled in tools/minify-js