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; -}