prettier/website/static/worker.js

114 lines
2.4 KiB
JavaScript
Raw Normal View History

/* eslint-env worker */
/* eslint no-var: off, strict: off */
var parsersLoaded = {};
// "Polyfills" in order for all the code to run
self.global = self;
self.util = {};
self.path = {};
self.path.resolve = self.path.join = self.path.dirname = function() {
return "";
};
self.path.parse = function() {
return { root: "" };
};
self.Buffer = {
isBuffer: function() {
return false;
}
};
2017-12-06 03:13:38 +03:00
self.constants = {};
module$1 = module = os = crypto = {};
self.fs = { readFile: function() {} };
2017-09-16 16:35:32 +03:00
os.homedir = function() {
return "/home/prettier";
};
2018-04-12 00:22:03 +03:00
os.EOL = "\n";
self.process = {
argv: [],
env: { PRETTIER_DEBUG: true },
version: "v8.5.0",
binding: function() {
return {};
},
cwd: function() {
return "";
}
};
self.assert = { ok: function() {}, strictEqual: function() {} };
self.require = function require(path) {
if (path === "stream") {
return { PassThrough() {} };
}
if (path === "./third-party") {
return {};
}
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];
};
var prettier;
importScripts("lib/index.js");
if (typeof prettier === "undefined") {
prettier = module.exports; // eslint-disable-line
}
if (typeof prettier === "undefined") {
prettier = index; // eslint-disable-line
}
2018-04-12 00:22:03 +03:00
self.onmessage = function(event) {
var uid = event.data.uid;
var message = event.data.message;
switch (message.type) {
case "meta":
self.postMessage({
uid: uid,
message: {
type: "meta",
supportInfo: JSON.parse(JSON.stringify(prettier.getSupportInfo())),
version: prettier.version
}
});
break;
2018-04-12 00:22:03 +03:00
case "format":
var options = message.options || {};
2018-04-12 00:22:03 +03:00
delete options.ast;
delete options.doc;
delete options.output2;
2018-04-12 00:22:03 +03:00
self.postMessage({
uid: uid,
message: {
formatted: formatCode(message.code, options)
}
});
break;
}
};
function formatCode(text, options) {
try {
return prettier.format(text, options);
} catch (e) {
if (e.constructor && e.constructor.name === "SyntaxError") {
// Likely something wrong with the user's code
return String(e);
}
// Likely a bug in Prettier
// Provide the whole stack for debugging
return e.stack || String(e);
}
}