tapio / live-server

A simple development http server with live reload capability.
http://tapiov.net/live-server/
4.43k stars 484 forks source link

Working dir resolution for middleware #330

Open rasmniel opened 5 years ago

rasmniel commented 5 years ago

Prepend process.cwd() to to supplied middleware path in order to resolve the working directory from cli execution.

I'm unsure what the point of the bare require(mw) statement was, as I found it hard pressed to match anything useful other than absolute paths. If you believe this is required to maintain backwards compat, let me know and I will rethink the PR.

1j01 commented 5 years ago

path.resolve(process.cwd(), mw) would support absolute paths easily enough.

This would still be a breaking change tho, for the relative case. To maintain backwards compatibility we could check for both locations with a try-catch, and rethrow in the catch for errors that aren't not found errors.

var m;
try {
    m = require(modulePath);
} catch (e) {
    if (e.code !== 'MODULE_NOT_FOUND') {
        throw e;
    }
    m = require(modulePathLegacy);
}

That might be a little weird, but it could be for a deprecation period, or whatever.

rasmniel commented 5 years ago

Great idea! I've updated the PR as per your suggestion. It should now be possible to resolve middleware both relatively to working directory and relatively to the live-server installation folder. Absolute resolution is also still an option.

I checked that both clauses work. Feel free to verify that and let me know if you find errors. As for deprecation; I'll let someone else figure out how and when to remove the backwards compatible path resolution — if it should be removed at all 🤷‍♂