From 754caf38fae58e77f3966cd259c5c1a3946551ac Mon Sep 17 00:00:00 2001 From: Rasmus Nielsen Date: Wed, 23 Oct 2019 14:48:13 +0200 Subject: [PATCH 1/2] Working dir resolution for middleware Prepend process.cwd() to to supplied middleware path in order to resolve the working directory from cli execution. --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 77a2a7e..a626999 100644 --- a/index.js +++ b/index.js @@ -186,7 +186,7 @@ LiveServer.start = function(options) { if (ext !== ".js") { mw = require(path.join(__dirname, "middleware", mw + ".js")); } else { - mw = require(mw); + mw = require(process.cwd() + '/' + mw); } } app.use(mw); From 2dbcf12cc12daf166facf2072235f3e96fbd4a7b Mon Sep 17 00:00:00 2001 From: Rasmus Nielsen Date: Fri, 25 Oct 2019 11:16:46 +0200 Subject: [PATCH 2/2] Add compatibility fallback with try-catch --- index.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index a626999..15ccb8c 100644 --- a/index.js +++ b/index.js @@ -186,7 +186,16 @@ LiveServer.start = function(options) { if (ext !== ".js") { mw = require(path.join(__dirname, "middleware", mw + ".js")); } else { - mw = require(process.cwd() + '/' + mw); + try { + // Resolve middleware relative to current working directory + mw = require(path.resolve(process.cwd(), mw)); + } catch (e) { + if (e.code !== 'MODULE_NOT_FOUND') { + throw e; + } + // Utilize legacy resolution; relative to live-server directory + mw = require(mw); + } } } app.use(mw);