generate /js/bundle.js via browserify.

This commit is contained in:
Atul Varma 2015-01-22 19:56:12 -05:00
Родитель 685fc4da21
Коммит 01d4c3a705
4 изменённых файлов: 34 добавлений и 4 удалений

Просмотреть файл

@ -29,6 +29,7 @@ npm install
npm test
export BLITLINE_APPLICATION_ID='your blitline application id'
export S3_BUCKET='your s3 bucket'
export DEBUG=
node app.js
```
@ -75,6 +76,10 @@ you can define `REDIS_URL` to point at it. Use the form
`REDISTOGO_URL` is a synonym for `REDIS_URL`, to make deployment on
Heroku easier.
If 'DEBUG' is defined in the environment (even as an empty string),
then some assets will be always be regenerated on-the-fly, which
is useful for debugging.
## Running Tests
To run the test suite, run `npm test`.

19
app.js
Просмотреть файл

@ -1,5 +1,6 @@
var request = require('request');
var express = require('express');
var browserify = require('browserify');
var bodyParser = require('body-parser');
var RedisCache = require('./redis-cache');
@ -7,6 +8,7 @@ var keys = require('./keys');
var makes = require('./makes');
var blitline = require('./blitline');
var DEBUG = 'DEBUG' in process.env;
var DEFAULT_WAIT = 0;
var EXTENDED_WAIT = 10000;
var PORT = process.env.PORT || 3000;
@ -25,6 +27,7 @@ if (!BLITLINE_APPLICATION_ID)
if (!S3_BUCKET)
throw new Error('S3_BUCKET must be defined');
var bundlejs;
var redisCache = new RedisCache(process.env.REDIS_URL ||
process.env.REDISTOGO_URL);
var app = express();
@ -68,6 +71,22 @@ redisCache.client.on('error', function(err) {
app.use(bodyParser.json());
app.get('/js/bundle.js', function(req, res, next) {
if (!bundlejs || DEBUG) {
var b = browserify();
b.ignore('request');
b.require('./keys');
b.require('./makes');
b.bundle(function(err, buf) {
if (err) return next(err);
bundlejs = buf;
next();
});
} else next();
}, function(req, res) {
res.type('text/javascript').send(bundlejs);
});
app.post('/', function(req, res, next) {
var url = makes.validateAndNormalizeUrl(req.body.url);
var wait = req.body.wait ? EXTENDED_WAIT : DEFAULT_WAIT;

Просмотреть файл

@ -5,6 +5,7 @@
"dependencies": {
"body-parser": "~1.2.0",
"redis": "0.12.1",
"browserify": "8.1.1",
"express": "~4.2.0",
"request": "2.34.0"
},

Просмотреть файл

@ -53,10 +53,13 @@
</div>
</div>
</body>
<script src="/js/bundle.js"></script>
<script>
var NUM_SIMULTANEOUS_REQUESTS = 3;
var MIN_LOAD_TIME = 1500;
var keys = require('./keys');
var makes = require('./makes');
var displayerForm = document.getElementById('screenshot-displayer');
var screenshots = document.getElementById('screenshots');
@ -89,10 +92,12 @@ function createScreenshot(key, parent) {
displayerForm.addEventListener('submit', function(e) {
e.preventDefault();
var url = document.getElementById('url').value;
var match = url.match(/^https:\/\/(.+)$/);
if (!match)
return alert("URL must be a https url.");
var key = match[1];
url = makes.validateAndNormalizeUrl(url);
if (!url)
return alert("URL does not appear to be a Webmaker make.");
var key = keys.fromMakeUrl(url);
screenshots.innerHTML = '';