Fix multiparser on playground (#3407)

* Fix multiparser in playground

* Remove unnecessary function call
This commit is contained in:
Lucas Duailibe authored and Lucas Azzola committed 2017-12-06 09:14:02 +11:00
1 parent 7b299fb94e
commit 36a11b86bc
1 file changed
+11 -26
+11 -26
View File
@@ -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;
}