Commit ed4afca0 authored by Robert Knight's avatar Robert Knight

Add an express server which serves the package's contents.

Add an express server which serve's the package's contents in a manner
similar to unpkg:

 - The '/' route serves the package's entry point script

 - Other routes serve files from the package

This can be used together with the CLIENT_URL setting in the Hypothesis
service to configure the service to serve the local development version
of the client instead of the production version.
parent cffcd255
......@@ -22,6 +22,7 @@ var through = require('through2');
var createBundle = require('./scripts/gulp/create-bundle');
var manifest = require('./scripts/gulp/manifest');
var servePackage = require('./scripts/gulp/serve-package');
var vendorBundles = require('./scripts/gulp/vendor-bundles');
var IS_PRODUCTION_BUILD = process.env.NODE_ENV === 'production';
......@@ -286,11 +287,15 @@ gulp.task('watch-manifest', function () {
}));
});
gulp.task('start-live-reload-server', function () {
gulp.task('serve-live-reload', function () {
var LiveReloadServer = require('./scripts/gulp/live-reload-server');
liveReloadServer = new LiveReloadServer(3000, 'http://localhost:5000');
});
gulp.task('serve-package', function () {
servePackage(3001);
});
gulp.task('build',
['build-js',
'build-css',
......@@ -299,7 +304,8 @@ gulp.task('build',
generateManifest);
gulp.task('watch',
['start-live-reload-server',
['serve-package',
'serve-live-reload',
'watch-js',
'watch-css',
'watch-fonts',
......
This diff is collapsed.
......@@ -43,6 +43,7 @@
"eslint": "^3.0.1",
"eslint-config-hypothesis": "^1.0.0",
"exorcist": "^0.4.0",
"express": "^4.14.1",
"extend": "^2.0.0",
"gulp": "^3.9.1",
"gulp-batch": "^1.0.5",
......
'use strict';
var express = require('express');
var { log } = require('gulp-util');
var { readFileSync } = require('fs');
/**
* An express server which serves the contents of the package.
*
* The server behaves similarly to npm CDNs, such as unpkg.com. The '/' route
* returns the contents of the package's entry point. Other routes return the
* contents of the specified file within the package.
*
* When developing the client, the Hypothesis service should be configured to
* use the URL of this service as the client URL, so that the boot script is
* returned by the service's '/embed.js' route and included in the '/app.html'
* app.
*/
function servePackage(port) {
var app = express();
app.get('/', function (req, res) {
var entryPath = require.resolve('../..');
var entryScript = readFileSync(entryPath).toString('utf-8');
res.send(entryScript);
});
app.use(express.static('.'));
app.listen(port, function () {
log(`Package served at http://localhost:${port}`);
});
}
module.exports = servePackage;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment