var http = require("http");
var fs = require("fs");
var endsWith = require('ends-with');
var stringContains = require("string-contains");
var processJavaScriptCode = require("./process-javascript-code")

http.createServer(handleRequest).listen(8888)

function handleRequest(request, response){
    var path = "." + request.url.split("?")[0];
    console.log("Request for path ", path)
    if (endsWith(path, ".js.map")){
        path = path.substr(0, path.length - ".map".length)
    }

    if (fs.existsSync(path)){
        var fileContents = fs.readFileSync(path).toString()

        if ((endsWith(request.url, ".js") || endsWith(request.url, ".js.map")) &&
            !stringContains(request.url, "/dontprocess") &&
            !stringContains(request.url, "/vis") &&
            !stringContains(request.url, "?dontprocess=yes")
        ) {
            if (endsWith(request.url, ".js")) {
                var res = processJavaScriptCode(fileContents)
                fileContents = res.code
                fileContents += "\n//# sourceMappingURL=" + path.split("/")[path.split("/").length - 1] + ".map"

            }
            if (endsWith(request.url, ".js.map")){
                fileContents = JSON.stringify(processJavaScriptCode(fileContents).map)
            }

        }
        response.write(fileContents)
    } else {
        response.writeHead(404, {"Content-Type": "text/plain"});
        response.write("not found")
    }
    response.end();
}

@@ -834,24 +834,97 @@
         ) %7B%0A
+            var jsFileName = path.split(%22/%22)%5Bpath.split(%22/%22).length - 1%5D%0A
             
@@ -1125,51 +1125,18 @@
 %22 + 
-path.split(%22/%22)%5Bpath.split(%22/%22).length - 1%5D
+jsFileName
  + %22
@@ -1291,13 +1291,46 @@
 ents
-).map
+, %7Bfilename: jsFileName%7D).map, null, 4
 )%0A  

Fix source map file names not working.