Skip to content
Draft
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
22 changes: 13 additions & 9 deletions lib/dev-middleware.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
let nollup = require('./index');
let chokidar = require('chokidar');
let expressws = require('express-ws');
let WebSocket = require('ws');
let fs = require('fs');
let url = require('url');
let hmr = require('./plugin-hmr');
Expand All @@ -9,7 +9,6 @@ let path = require('path');
let { createFilter } = require('@rollup/pluginutils');

module.exports = function (app, config, options, server) {
expressws(app, server);
let bundles = [];
let isBundling = true;
let files = {};
Expand Down Expand Up @@ -45,15 +44,20 @@ module.exports = function (app, config, options, server) {

sockets[i] = [];

app.ws('/__hmr' + (i || ''), (ws, req) => {
sockets[i].push(ws);
server.on('upgrade', (request, socket, head) => {
if (request.url === '/__hmr' + (i || '')) {
const wsServer = new WebSocket.Server({ noServer: true });
wsServer.handleUpgrade(request, socket, head, (ws) => {
sockets[i].push(ws);

// greeting -- see: https://github.com/PepsRyuu/nollup/issues/35
ws.send(JSON.stringify({ greeting: true }))
// greeting -- see: https://github.com/PepsRyuu/nollup/issues/35
ws.send(JSON.stringify({ greeting: true }));

ws.on('close', () => {
sockets[i].splice(sockets[i].indexOf(ws), 1);
});
ws.on('close', () => {
sockets[i].splice(sockets[i].indexOf(ws), 1);
});
});
}
});
});
}
Expand Down
16 changes: 8 additions & 8 deletions lib/dev-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@ async function devServer(options) {
options = await loadRc(options, '.nolluprc.js');
}

if (options.before) {
options.before(app);
}

let server
if (options.https) {
if (!(options.key && options.cert)) {
Expand All @@ -50,6 +46,10 @@ async function devServer(options) {
server = http.createServer(app)
}

if (options.before) {
options.before(app, server);
}

if (options.headers) {
app.all('*', (req, res, next) => {
for (let prop in options.headers) {
Expand Down Expand Up @@ -96,10 +96,6 @@ async function devServer(options) {
index: options.historyApiFallback? false : 'index.html'
}));

if (options.after) {
options.after(app);
}

if (options.historyApiFallback) {
let entryPoint = typeof options.historyApiFallback === 'string'? options.historyApiFallback : 'index.html';

Expand All @@ -120,6 +116,10 @@ async function devServer(options) {
app.use(fallback(entryPoint, { root: options.contentBase }));
}

if (options.after) {
options.after(app, server);

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.

Is there any particular reason for this to be moved below the fallback? This would be a breaking change, and it would probably cause the hook to not trigger at all if using historyApiFallback.

}

server.listen(options.port, options.host || 'localhost');

console.log(`[Nollup] Listening on ${options.https ? 'https' : 'http'}://${options.host || 'localhost'}:${options.port}`);
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@
"express": "^4.16.3",
"express-history-api-fallback": "^2.2.1",
"express-http-proxy": "^1.5.1",
"express-ws": "^4.0.0",
"magic-string": "^0.25.7",
"mime-types": "^2.1.29",
"source-map": "^0.5.6",
"source-map-fast": "npm:source-map@0.7.3"
"source-map-fast": "npm:source-map@0.7.3",
"ws": "^7.4.6"
},
"devDependencies": {
"chai": "^4.3.4",
Expand Down