diff --git a/cli/start.js b/cli/start.js index a7baa15e..0b3c8560 100644 --- a/cli/start.js +++ b/cli/start.js @@ -57,7 +57,10 @@ function getHtmlHandler (htmlSettings, entryFile, id) { _appendHtml: [ '') + }) + + return hyperstream({ + body: { _appendHtml: script$ } + }) +} diff --git a/handler-js.js b/handler-js.js index f67764e3..953aaa70 100644 --- a/handler-js.js +++ b/handler-js.js @@ -1,12 +1,13 @@ -const sheetify = require('sheetify/transform') +const assert = require('assert') +const bl = require('bl') const cssExtract = require('css-extract') -const stream = require('readable-stream') +const Emitter = require('events') const errorify = require('errorify') +const sheetify = require('sheetify/transform') +const stream = require('readable-stream') +const sse = require('sse-stream') const watchify = require('watchify') -const Emitter = require('events') -const assert = require('assert') const xtend = require('xtend') -const bl = require('bl') module.exports = js @@ -22,14 +23,20 @@ function js (state) { // signal to CSS that browserify is registered state.jsRegistered = true + state.jsOpts = { + src: src, + opts: opts + } const baseBrowserifyOpts = { cache: {}, + require: [], packageCache: {}, entries: [ require.resolve(src) ], fullPaths: true } - var b = browserify(xtend(baseBrowserifyOpts, opts)) + const browserifyOpts = xtend(baseBrowserifyOpts, opts) + var b = browserify(browserifyOpts) // enable css if registered if (state.cssOpts) { @@ -63,6 +70,19 @@ function js (state) { b.close() }) } + if (state.env === 'development' && !b.sse) { + b.sse = sse('/' + state.htmlOpts.entry) + b.sse.install(req.connection.server) + + const eventStream = new stream.PassThrough() + b.on('update', function (ids) { + eventStream.push(JSON.stringify({update: ids})) + }) + + b.sse.on('connection', function (client) { + eventStream.pipe(client) + }) + } handler(req, res, function (err, js) { if (err) return ts.emit('error', err) state.cssBuf.end() diff --git a/index.js b/index.js index a59624fd..7fc44991 100644 --- a/index.js +++ b/index.js @@ -6,9 +6,12 @@ const state = new Emitter() state.env = process.env.NODE_ENV === 'production' ? 'production' : 'development' state.cssStream = new stream.PassThrough() state.jsRegistered = false +state.htmlOpts = null +state.jsOpts = null state.cssReady = false state.cssOpts = null state.cssBuf = null +state.cssSse = null exports.html = require('./handler-html')(state) exports.css = require('./handler-css')(state) diff --git a/package.json b/package.json index ed4ee0ac..7e7e2d20 100644 --- a/package.json +++ b/package.json @@ -29,19 +29,26 @@ "bl": "^1.1.2", "css-extract": "^1.1.1", "errorify": "^0.3.1", + "es6-promise": "^3.2.1", "hyperstream": "^1.2.2", "inject-lr-script-stream": "^1.1.1", "meow": "^3.7.0", + "nanoajax": "^0.4.3", "opn": "^4.0.2", "project-name-generator": "^2.1.2", "readable-stream": "^2.1.4", + "request": "^2.74.0", "resolve": "^1.1.7", "server-router": "^2.1.0", "sheetify": "^5.0.5", "simple-html-index": "^1.3.0", + "sse-stream": "0.0.4", "string-to-stream": "^1.1.0", "watchify": "^3.7.0", - "xtend": "^4.0.1" + "whatwg-fetch": "^1.0.0", + "xtend": "^4.0.1", + "yo": "^1.8.4", + "yo-yo": "^1.2.2" }, "devDependencies": { "browserify": "^13.1.0", diff --git a/test/index.js b/test/index.js index 9ccf5387..f861cae0 100644 --- a/test/index.js +++ b/test/index.js @@ -82,7 +82,7 @@ test('js', function (t) { // Needed because watchify does not reliably clean up all handles behind itself // * https://github.com/substack/watchify/issues/22#issuecomment-88115610 // * https://github.com/substack/watchify/blob/master/test/zzz.js -test('zzz', function (t) { +test('zzz', function (t) { t.on('end', function () { setTimeout(function () { process.exit()