From 38ba9f91877cdbc3b687de16803c41d6fffafb74 Mon Sep 17 00:00:00 2001 From: Martin von Gagern Date: Tue, 10 Jan 2017 14:28:23 +0100 Subject: [PATCH] Serve files with and without babelify step MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit As babelify is slow, it may be desriable to not run it during development. This is OK if the browser is recent enough to understand ES6 natively. (This does not include current Firefox due to it having problems with for(const … in …), https://bugzilla.mozilla.org/show_bug.cgi?id=1094995.) For older browsers, or to check issues possibly introduced by babelify, adding /babel as the first component of the path will switch to a version which has been processed by babelify. This is also used for screenshots. --- dockers/Screenshotter/screenshotter.js | 4 +- server.js | 57 +++++++++++++++----------- test/screenshotter/test.html | 4 +- 3 files changed, 37 insertions(+), 28 deletions(-) diff --git a/dockers/Screenshotter/screenshotter.js b/dockers/Screenshotter/screenshotter.js index 968a717..045813e 100644 --- a/dockers/Screenshotter/screenshotter.js +++ b/dockers/Screenshotter/screenshotter.js @@ -291,7 +291,7 @@ function findHostIP() { } if (katexIP !== "*any*" || katexURL) { if (!katexURL) { - katexURL = "http://" + katexIP + ":" + katexPort + "/"; + katexURL = "http://" + katexIP + ":" + katexPort + "/babel/"; console.log("KaTeX URL is " + katexURL); } process.nextTick(takeScreenshots); @@ -303,7 +303,7 @@ function findHostIP() { app.get("/ss-connect.js", function(req, res, next) { if (!katexURL) { katexIP = req.query.ip; - katexURL = "http://" + katexIP + ":" + katexPort + "/"; + katexURL = "http://" + katexIP + ":" + katexPort + "/babel/"; console.log("KaTeX URL is " + katexURL); process.nextTick(takeScreenshots); } diff --git a/server.js b/server.js index 9a2b47a..c7a1ae3 100644 --- a/server.js +++ b/server.js @@ -15,7 +15,7 @@ if (require.main === module) { ":date[iso] :method :url HTTP/:http-version - :status")); } -var serveBrowserified = function(file, standaloneName) { +function serveBrowserified(file, standaloneName, doBabelify) { return function(req, res, next) { let files; if (Array.isArray(file)) { @@ -26,9 +26,10 @@ var serveBrowserified = function(file, standaloneName) { files = [path.join(__dirname, file)]; } - const options = { - transform: [babelify] - }; + const options = {}; + if (doBabelify) { + options.transform = [babelify]; + } if (standaloneName) { options.standalone = standaloneName; } @@ -43,22 +44,32 @@ var serveBrowserified = function(file, standaloneName) { res.send(body); }); }; -}; +} -app.get("/katex.js", serveBrowserified("katex", "katex")); -app.use("/test/jasmine", - express["static"]( - path.dirname( - require.resolve("jasmine-core/lib/jasmine-core/jasmine.js") - ) - ) -); -app.get("/test/katex-spec.js", serveBrowserified("test/*[Ss]pec.js")); -app.get("/contrib/auto-render/auto-render.js", - serveBrowserified("contrib/auto-render/auto-render", - "renderMathInElement")); +function twoBrowserified(url, file, standaloneName) { + app.get(url, serveBrowserified(file, standaloneName, false)); + app.get("/babel" + url, serveBrowserified(file, standaloneName, true)); +} -app.get("/katex.css", function(req, res, next) { +function twoUse(url, handler) { + app.use(url, handler); + app.use("/babel" + url, handler); +} + +function twoStatic(url, file) { + twoUse(url, express.static(path.join(__dirname, file))); +} + +twoBrowserified("/katex.js", "katex", "katex"); +twoUse("/test/jasmine", express.static(path.dirname( + require.resolve("jasmine-core/lib/jasmine-core/jasmine.js")))); +twoBrowserified("/test/katex-spec.js", "test/*[Ss]pec.js"); +twoBrowserified( + "/contrib/auto-render/auto-render.js", + "contrib/auto-render/auto-render", + "renderMathInElement"); + +twoUse("/katex.css", function(req, res, next) { const lessfile = path.join(__dirname, "static", "katex.less"); fs.readFile(lessfile, {encoding: "utf8"}, function(err, data) { if (err) { @@ -82,12 +93,10 @@ app.get("/katex.css", function(req, res, next) { }); }); -app.use(express["static"](path.join(__dirname, "static"))); -app.use(express["static"](path.join(__dirname, "build"))); -app.use("/test", express["static"](path.join(__dirname, "test"))); -app.use("/contrib", express["static"](path.join(__dirname, "contrib"))); -// app.use("/unicode-fonts", -// express["static"](path.join(__dirname, "static", "unicode-fonts"))); +twoStatic("", "static"); +twoStatic("", "build"); +twoStatic("/test", "test"); +twoStatic("/contrib", "contrib"); app.use(function(err, req, res, next) { console.error(err.stack); diff --git a/test/screenshotter/test.html b/test/screenshotter/test.html index 3694ba9..4d9745f 100644 --- a/test/screenshotter/test.html +++ b/test/screenshotter/test.html @@ -2,8 +2,8 @@ Screenshotter test - - + +