From 36a11b86bc1861b4d610e1966b61439b57675ff9 Mon Sep 17 00:00:00 2001 From: Lucas Duailibe Date: Tue, 5 Dec 2017 20:14:02 -0200 Subject: [PATCH] Fix multiparser on playground (#3407) * Fix multiparser in playground * Remove unnecessary function call --- website/static/worker.js | 37 +++++++++++-------------------------- 1 file changed, 11 insertions(+), 26 deletions(-) diff --git a/website/static/worker.js b/website/static/worker.js index f4554c61b..ce1e1158b 100644 --- a/website/static/worker.js +++ b/website/static/worker.js @@ -26,7 +26,17 @@ self.require = function require(path) { if (path === "./third-party") { return {}; } - return self[path.replace(/.+-/, "")]; + + if (~path.indexOf("parser-")) { + var parser = path.replace(/.+-/, ""); + if (!parsersLoaded[parser]) { + importScripts("lib/parser-" + parser + ".js"); + parsersLoaded[parser] = true; + } + return self[parser]; + } + + return self[path]; }; importScripts("lib/index.js"); @@ -67,7 +77,6 @@ self.onmessage = function(message) { } if (message.data.doc) { - lazyLoadParser("babylon"); try { doc = prettier.__debug.formatDoc( prettier.__debug.printToDoc(message.data.text, options), @@ -92,33 +101,9 @@ self.onmessage = function(message) { }; function formatCode(text, options) { - lazyLoadParser(options.parser); try { return prettier.format(text, options); } catch (e) { - // Multiparser may throw if we haven't loaded the right parser - // Load it lazily and retry! - if (e.parser && !lazyLoadParser(e.parser)) { - return formatCode(text, options); - } return String(e); } } - -function lazyLoadParser(parser) { - var actualParser = - parser === "json" - ? "babylon" - : parser === "css" || parser === "less" || parser === "scss" - ? "postcss" - : parser; - var script = "parser-" + actualParser + ".js"; - - if (parsersLoaded[actualParser]) { - return true; - } - - importScripts("lib/" + script); - parsersLoaded[actualParser] = true; - return false; -}