Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ if (config.webpack) { // boot up webpack (if configured)
Zen.webpack = new WebpackAdapter()
}

// worker and head are the only supported pageTypes right now
Zen.indexHtml = function indexHtml (pageType, forS3) {
let deps = ['lib/client/latte.js']
if (pageType == 'head') {
Expand Down
71 changes: 49 additions & 22 deletions lib/local-server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,46 +16,73 @@ module.exports = class Server {
this.results = [] // results of all tests run
this.passedFocus = [] // all tests that passed after running

const {app, server} = this.createWebApp(Zen.config)
this.startWebpackServer(Zen.webpack, app)

Zen.s3Sync.on('status', this.sendStatus.bind(this))
new WebSocket.Server({server}).on('connection', this.onWebsocket.bind(this))

this.chrome = new ChromeWrapper() // headless chrome instance
this.chrome.launchLocal({port: 9222})

this.startWorkers(Zen.config, app)
}

createWebApp(config) {
// start up the local webserver for `head` to connect to
let app = connect()
let server = http.createServer(app).listen(Zen.config.port)
const app = connect()
const server = http.createServer(app).listen(config.port)
app.use('/lib', Util.serveWith404(path.join(__dirname, '..'))) // serve up stuff out of lib
app.use('/node_modules', Util.serveWith404(path.resolve(Zen.config.appRoot, './node_modules'))) // TODO: this doesn't work when yarn link'd
app.use('/base', Util.serveWith404(Zen.config.appRoot)) // base serves things out of the application's root
app.use('/node_modules', Util.serveWith404(path.resolve(config.appRoot, './node_modules'))) // TODO: this doesn't work when yarn link'd
app.use('/base', Util.serveWith404(config.appRoot)) // base serves things out of the application's root
app.use('/svelte', Util.serveSvelte)
app.use('/icons', Util.serveIcons)

if (Zen.webpack) {
Zen.webpack.startDevServer(app)
Zen.webpack.on('status', stats => {
if (Zen.webpack.status == 'done') {
this.workers.forEach(w => w.tab && w.tab.setCodeHash(Zen.webpack.compile.hash))
// host worker and head
app.use(async (req, resp, next) => {
let pageType
if (req.url.match(/^\/worker/)) {
pageType = 'worker'
} else {
const pathname = req._parsedUrl.pathname
if (pathname.match(/^\/(index(\.htm(l)?)?)?$/)) {
pageType = 'head'
}
}

if (pageType) {
resp.end(Zen.indexHtml(pageType))
} else {
next()
}
})
Comment on lines +41 to +58
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems much bigger than the 1 line it was previously.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I can tell the previous 1 line was sending head.js to any request that failed to match a route. This new code should send a 404 if an unknown asset is requested.


return {app, server}
}

startWebpackServer(webpack, app) {
if (webpack) {
webpack.startDevServer(app)
webpack.on('status', stats => {
if (webpack.status == 'done') {
this.workers.forEach(w => w.tab && w.tab.setCodeHash(webpack.compile.hash))
}
this.sendStatus() // notify head of the compile status
})
}
}

Zen.s3Sync.on('status', this.sendStatus.bind(this))

this.chrome = new ChromeWrapper() // headless chrome instance
this.chrome.launchLocal({port: 9222})
new WebSocket.Server({server}).on('connection', this.onWebsocket.bind(this))

startWorkers(config, app) {
// create a server for each worker. This gives us different origins and isolates things like localStorage
this.workers = Array.construct(8, id => {
id = id + 1
let port = Zen.config.port + id
let port = config.port + id
http.createServer(app).listen(port)
let worker = {id, port}
this.chrome.openTab(`http://localhost:${port}/worker?id=${id}`, `w${id}`, Zen.config)
this.chrome.openTab(`http://localhost:${port}/worker?id=${id}`, `w${id}`, config)
.then(t => worker.tab = t)
return worker
})

// host worker and head. NB this should go last after all other `app.use()` calls
app.use(async (req, resp) => {
resp.end(Zen.indexHtml(req.url.match(/^\/worker/) ? 'worker' : 'head'))
})
}

filterTests (msg) {
Expand Down