Skip to content

Commit

Permalink
use new variants branch of polyserve
Browse files Browse the repository at this point in the history
  • Loading branch information
Fred K. Schott committed Nov 22, 2016
1 parent 4342646 commit f9440db
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 47 deletions.
4 changes: 2 additions & 2 deletions data/index.html
Expand Up @@ -4,10 +4,10 @@
<meta charset="utf-8">
<script>WCT = <%= JSON.stringify(clientOptions) %>;</script>
<script>window.__generatedByWct = true;</script>
<script src="/web-component-tester/browser.js"></script>
<script src="../web-component-tester/browser.js"></script>
<% extraScripts.forEach(function(script) { %> <script src="<%- script %>"></script>
<% }); %>
<script src="/web-component-tester/data/a11ySuite.js"></script>
<script src="../web-component-tester/data/a11ySuite.js"></script>
</head>
<body>
<script>
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -79,7 +79,7 @@
"mocha": "^3.1.2",
"multer": "^1.1.0",
"nomnom": "^1.8.1",
"polyserve": "^0.14.0",
"polyserve": "github:polymerlabs/polyserve#wct-variants",
"promisify-node": "^0.4.0",
"resolve": "^1.0.0",
"send": "^0.11.1",
Expand Down
67 changes: 23 additions & 44 deletions runner/webserver.ts
Expand Up @@ -19,7 +19,7 @@ import * as fs from 'fs';
import * as http from 'http';
import * as _ from 'lodash';
import * as path from 'path';
import {makeApp} from 'polyserve';
import {ServerInfo, startServers} from 'polyserve';
import * as send from 'send';
import * as serveWaterfall from 'serve-waterfall';
import * as serverDestroy from 'server-destroy';
Expand All @@ -34,15 +34,6 @@ const INDEX_TEMPLATE = _.template(fs.readFileSync(

// We prefer serving local assets over bower assets.
const WCT_ROOT = path.resolve(__dirname, '..');
const SERVE_STATIC = {
// Keys are regexps.
'^(.*/web-component-tester|)/browser\\.js$':
path.join(WCT_ROOT, 'browser.js'),
'^(.*/web-component-tester|)/browser\\.js\\.map$':
path.join(WCT_ROOT, 'browser.js.map'),
'^(.*/web-component-tester|)/data/a11ySuite\\.js$':
path.join(WCT_ROOT, 'data', 'a11ySuite.js'),
};

const DEFAULT_HEADERS = {
'Cache-Control': 'no-cache, no-store, must-revalidate',
Expand Down Expand Up @@ -83,7 +74,7 @@ export function webserver(wct: Context): void {
webRunnerContent: undefined,
// Map of route expressions (regular expressions) to local file paths that
// should be served by the webserver.
staticContent: SERVE_STATIC,
staticContent: [],
});

if (options.verbose) {
Expand All @@ -97,59 +88,47 @@ export function webserver(wct: Context): void {

// Hacky workaround for Firefox + Windows issue where FF screws up pathing.
// Bug: https://github.com/Polymer/web-component-tester/issues/194

options.suites = options.suites.map((cv) => cv.replace(/\\/g, '/'));

options.webserver.webRunnerContent = INDEX_TEMPLATE(options);
});

wct.hook('prepare', async function() {
const wsOptions = options.webserver;

const port = await getPort();

// `port` (and `webRunnerPath`) is read down the line by `BrowserRunner`.
wsOptions.port = port;

const app = express();
const server = http.createServer(app);
// `runTests` needs a reference to this (for the socket.io endpoint).
wct._httpServer = server;

// Debugging information for each request.
app.use(function(request, response, next) {
const msg = request.url + ' (' + request.header('referer') + ')';
wct.emit('log:debug', chalk.magenta(request.method), msg);
next();
});
const additionalRoutes = new Map<string, express.RequestHandler>();

// Mapped static content (overriding files served at the root).
if (wsOptions.webRunnerContent) {
additionalRoutes.set(
wsOptions.webRunnerPath, function(request, response) {
response.set(DEFAULT_HEADERS);
response.send(wsOptions.webRunnerContent);
});
}
_.each(wsOptions.staticContent, function(file, url) {
app.get(new RegExp(url), function(request, response) {
additionalRoutes.set(url, function(request, response) {
response.set(DEFAULT_HEADERS);
send(request, file).pipe(response);
});
});

// The generated web runner, if present.
if (wsOptions.webRunnerContent) {
app.get(wsOptions.webRunnerPath, function(request, response) {
response.set(DEFAULT_HEADERS);
response.send(wsOptions.webRunnerContent);
});
}

// At this point, we allow other plugins to hook and configure the
// webserver as they please.
await wct.emitHook('prepare:webserver', app);

// Serve up project & dependencies via polyserve
const polyserve = makeApp({
const polyserveServers = await startServers({
root: options.root,
headers: DEFAULT_HEADERS,
packageName: path.basename(options.root),
additionalRoutes: additionalRoutes,
});
app.use('/components/', polyserve);
console.assert(polyserveServers.length === 1);
const polyserve: ServerInfo = polyserveServers[0];
const {app, server} = polyserve;
const port = wsOptions.port = server.address().port;

wct._httpServer = server;

// At this point, we allow other plugins to hook and configure the
// webserver as they please.
await wct.emitHook('prepare:webserver', app);

app.use('/httpbin', httpbin.httpbin);

Expand Down

0 comments on commit f9440db

Please sign in to comment.