Files
test-repo/website/static/worker.js
T
Frank Li 80f38ce75b Website (#2241)
* initial docusaurus set-up

* Delete why-prettier.md

* Update README.md

* Update Docusaurus structure

* Update package.json

* Update siteConfig.js

* Remove ~ files

* chore(website): update build script

* Add redirect script

* Get eslint working on website

* Fix to allow for baseUrl of '/'

* Update docusaurus version

* Update README.md

* Change redirect script to use baseUrl of '/'

* Fix index.js formatting

* Load redirect script

* General cleanup

* Add projects using prettier
* Update service worker to just scope to playground
* Reduce link repeatition
* Link "Try it out" button to /playground/
* Make header of playground approximately same as main site
* Remove .html from URLs (e.g. users.html -> users/)
* Link Gitter Chat

* Add circleci.yml

* Only run builds on master

* Do deployment using Travis
2017-07-02 10:14:24 +10:00

83 lines
1.9 KiB
JavaScript

/* eslint-env worker */
/* eslint no-var: off, strict: off */
// "Polyfills" in order for all the code to run
self.global = self;
self.Buffer = {
isBuffer: function() {
return false;
}
};
// eslint-disable-next-line
fs = module$1 = module = path = os = crypto = {};
self.process = { argv: [], env: { PRETTIER_DEBUG: true } };
self.assert = { ok: function() {}, strictEqual: function() {} };
self.require = function require(path) {
return self[path.replace(/.+-/, "")];
};
importScripts("lib/index.js");
var prettier = index; // eslint-disable-line
var parsersLoaded = {};
self.onmessage = function(message) {
const options = message.data.options || {
parser: "babylon"
};
delete options.ast;
delete options.doc;
var formatted = formatCode(message.data.text, options);
var doc, ast;
if (message.data.ast) {
try {
ast = JSON.stringify(
prettier.__debug.parse(message.data.text, options),
null,
2
);
} catch (e) {
ast = e.toString();
}
}
if (message.data.doc) {
lazyLoadParser("babylon");
try {
doc = prettier.__debug.formatDoc(
prettier.__debug.printToDoc(message.data.text, options),
{ parser: "babylon" }
);
} catch (e) {
doc = e.toString();
}
}
self.postMessage({ formatted: formatted, doc: doc, ast: ast });
};
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 && !parsersLoaded[e.parser]) {
lazyLoadParser(e.parser);
return formatCode(text, options);
}
return e.toString();
}
}
function lazyLoadParser(parser) {
if (!parsersLoaded[parser]) {
importScripts("lib/parser-" + parser + ".js");
parsersLoaded[parser] = true;
}
}