Website: speed up load time, enable service worker (#2172)
* feat(website): speed up website load 💥
* chore(package): fix bad merge
This commit is contained in:
1 parent
4106a69b6d
commit
eccb17495a
12 files changed
+261
-67
No files matched your search
Binary file not shown.
|
After Width: | Height: | Size: 4.5 KiB |
+71
-24
@@ -2,21 +2,39 @@
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
|
||||
<title>Prettier</title>
|
||||
|
||||
<!-- CodeMirror -->
|
||||
<link rel="stylesheet" href="https://codemirror.net/lib/codemirror.css">
|
||||
<link rel="stylesheet" href="https://codemirror.net/theme/neat.css">
|
||||
<script src="https://codemirror.net/lib/codemirror.js"></script>
|
||||
<script src="https://codemirror.net/mode/javascript/javascript.js"></script>
|
||||
<script src="https://codemirror.net/addon/display/rulers.js"></script>
|
||||
<script src="https://codemirror.net/addon/search/searchcursor.js"></script>
|
||||
<script src="https://codemirror.net/addon/edit/matchbrackets.js"></script>
|
||||
<script src="https://codemirror.net/addon/edit/closebrackets.js"></script>
|
||||
<script src="https://codemirror.net/addon/comment/comment.js"></script>
|
||||
<script src="https://codemirror.net/addon/wrap/hardwrap.js"></script>
|
||||
<script src="https://codemirror.net/keymap/sublime.js"></script>
|
||||
<link rel="manifest" href="manifest.json">
|
||||
|
||||
<meta name="mobile-web-app-capable" content="yes">
|
||||
<meta name="apple-mobile-web-app-capable" content="yes">
|
||||
<meta name="application-name" content="Prettier">
|
||||
<meta name="apple-mobile-web-app-title" content="Prettier">
|
||||
<meta name="theme-color" content="#1A2B34">
|
||||
<meta name="msapplication-navbutton-color" content="#1A2B34">
|
||||
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
|
||||
<meta name="msapplication-starturl" content="/prettier/">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
|
||||
|
||||
<link rel="icon" href="icon.png">
|
||||
<link rel="apple-touch-icon" href="icon.png">
|
||||
|
||||
<link rel="stylesheet" crossorigin href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.26.0/codemirror.css">
|
||||
<link rel="stylesheet" crossorigin href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.26.0/theme/neat.css">
|
||||
|
||||
<script src="lib/prettier-version.js"></script>
|
||||
<script src="playground.js"></script>
|
||||
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.26.0/codemirror.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.26.0/mode/javascript/javascript.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.26.0/addon/display/rulers.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.26.0/addon/search/searchcursor.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.26.0/addon/edit/matchbrackets.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.26.0/addon/edit/closebrackets.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.26.0/addon/comment/comment.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.26.0/addon/wrap/hardwrap.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.26.0/keymap/sublime.js"></script>
|
||||
|
||||
<style type="text/css">
|
||||
html,
|
||||
@@ -36,7 +54,7 @@
|
||||
}
|
||||
|
||||
.options-summary {
|
||||
font-size: 1.2em;
|
||||
font-size: 1.4em;
|
||||
margin-top: 8px;
|
||||
text-align: center;
|
||||
}
|
||||
@@ -68,7 +86,9 @@
|
||||
}
|
||||
|
||||
.logo {
|
||||
height: 100px;
|
||||
height: 45px;
|
||||
margin-left: 10px;
|
||||
margin-right: 20px;
|
||||
}
|
||||
|
||||
.tagline {
|
||||
@@ -81,6 +101,10 @@
|
||||
margin-right: 30px;
|
||||
}
|
||||
|
||||
textarea.loading {
|
||||
opacity: 0;
|
||||
}
|
||||
|
||||
.editors-container {
|
||||
display: flex;
|
||||
flex-grow: 1;
|
||||
@@ -93,13 +117,27 @@
|
||||
}
|
||||
|
||||
.editor {
|
||||
min-width: 400px;
|
||||
display: flex;
|
||||
flex: 1;
|
||||
flex-basis: 100%;
|
||||
position: relative;
|
||||
border-top: 1px solid #1A2B34;
|
||||
}
|
||||
|
||||
/* display as 2x2 grid */
|
||||
@media (min-width: 800px) {
|
||||
.editor {
|
||||
flex-basis: 50%;
|
||||
}
|
||||
}
|
||||
|
||||
/* display as four columns */
|
||||
@media (min-width: 1200px) {
|
||||
.editor {
|
||||
flex-basis: 25%;
|
||||
}
|
||||
}
|
||||
|
||||
.arrow {
|
||||
align-self: center;
|
||||
font-size: 1.2em;
|
||||
@@ -139,10 +177,16 @@
|
||||
margin: 0 10px;
|
||||
}
|
||||
|
||||
@media (max-width: 400px) {
|
||||
@media (max-width: 408px) {
|
||||
label {
|
||||
padding: 8px;
|
||||
}
|
||||
.options-container {
|
||||
flex-direction: column-reverse;
|
||||
}
|
||||
.options.last {
|
||||
flex-direction: column-reverse;
|
||||
}
|
||||
}
|
||||
|
||||
input[type="number"] {
|
||||
@@ -158,7 +202,7 @@
|
||||
<body>
|
||||
<header>
|
||||
<a href=".">
|
||||
<img class="logo" src="https://cloud.githubusercontent.com/assets/311752/24730511/e6690c38-1a18-11e7-95a9-2af91bb60440.png">
|
||||
<img class="logo" src="prettier.png">
|
||||
</a>
|
||||
<h2 class="tagline"><em>Opinionated</em> code formatter</h2>
|
||||
<span class="links">
|
||||
@@ -175,13 +219,16 @@
|
||||
<section class="editors-container">
|
||||
<div class="editors">
|
||||
<div class="editor input">
|
||||
<textarea id="input-editor"></textarea>
|
||||
<textarea class="loading" id="input-editor"></textarea>
|
||||
</div>
|
||||
<div class="editor ast">
|
||||
<textarea class="loading" id="ast-editor"></textarea>
|
||||
</div>
|
||||
<div class="editor doc">
|
||||
<textarea id="doc-editor"></textarea>
|
||||
<textarea class="loading" id="doc-editor"></textarea>
|
||||
</div>
|
||||
<div class="editor output">
|
||||
<textarea id="output-editor"></textarea>
|
||||
<textarea class="loading" id="output-editor"></textarea>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
@@ -202,10 +249,11 @@
|
||||
<label><input type="checkbox" data-inverted id="bracketSpacing"></input> --no-bracket-spacing</label>
|
||||
<label><input type="checkbox" id="jsxBracketSameLine"></input> --jsx-bracket-same-line</label>
|
||||
</div>
|
||||
<div class="options">
|
||||
<div class="options last">
|
||||
<label>--trailing-comma <select id="trailingComma"><option value="none">none</option><option value="es5">es5</option><option value="all">all</option></select></label>
|
||||
<label>--parser <select id="parser"><option value="babylon">babylon</option><option value="flow">flow</option><option value="typescript">typescript</option><option value="postcss">postcss</option></select></label>
|
||||
<span style="flex: 0.3"></span>
|
||||
<label><input type="checkbox" id="ast"></input> show AST (debug)</label>
|
||||
<label><input type="checkbox" id="doc"></input> show doc (debug)</label>
|
||||
</div>
|
||||
</div>
|
||||
@@ -222,8 +270,7 @@
|
||||
</p>
|
||||
</footer>
|
||||
|
||||
<script src="lib/prettier-version.js"></script>
|
||||
<script src="playground.js"></script>
|
||||
<script src="lib/sw-toolbox-companion.js" data-service-worker="service-worker.js"></script>
|
||||
|
||||
<script async defer src="https://buttons.github.io/buttons.js"></script>
|
||||
</body>
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
/*
|
||||
Copyright 2016 Google Inc. All Rights Reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
(function() {
|
||||
'use strict';
|
||||
var workerScript = document.currentScript && document.currentScript.dataset.serviceWorker;
|
||||
|
||||
if (workerScript && 'serviceWorker' in navigator) {
|
||||
navigator.serviceWorker.register(workerScript);
|
||||
}
|
||||
})();
|
||||
@@ -0,0 +1,16 @@
|
||||
/*
|
||||
Copyright 2016 Google Inc. All Rights Reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/!function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var t;t="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this,t.toolbox=e()}}(function(){return function e(t,n,r){function o(c,s){if(!n[c]){if(!t[c]){var a="function"==typeof require&&require;if(!s&&a)return a(c,!0);if(i)return i(c,!0);var u=new Error("Cannot find module '"+c+"'");throw u.code="MODULE_NOT_FOUND",u}var f=n[c]={exports:{}};t[c][0].call(f.exports,function(e){var n=t[c][1][e];return o(n?n:e)},f,f.exports,e,t,n,r)}return n[c].exports}for(var i="function"==typeof require&&require,c=0;c<r.length;c++)o(r[c]);return o}({1:[function(e,t,n){"use strict";function r(e,t){t=t||{};var n=t.debug||m.debug;n&&console.log("[sw-toolbox] "+e)}function o(e){var t;return e&&e.cache&&(t=e.cache.name),t=t||m.cache.name,caches.open(t)}function i(e,t){t=t||{};var n=t.successResponses||m.successResponses;return fetch(e.clone()).then(function(r){return"GET"===e.method&&n.test(r.status)&&o(t).then(function(n){n.put(e,r).then(function(){var r=t.cache||m.cache;(r.maxEntries||r.maxAgeSeconds)&&r.name&&c(e,n,r)})}),r.clone()})}function c(e,t,n){var r=s.bind(null,e,t,n);d=d?d.then(r):r()}function s(e,t,n){var o=e.url,i=n.maxAgeSeconds,c=n.maxEntries,s=n.name,a=Date.now();return r("Updating LRU order for "+o+". Max entries is "+c+", max age is "+i),g.getDb(s).then(function(e){return g.setTimestampForUrl(e,o,a)}).then(function(e){return g.expireEntries(e,c,i,a)}).then(function(e){r("Successfully updated IDB.");var n=e.map(function(e){return t.delete(e)});return Promise.all(n).then(function(){r("Done with cache cleanup.")})}).catch(function(e){r(e)})}function a(e,t,n){return r("Renaming cache: ["+e+"] to ["+t+"]",n),caches.delete(t).then(function(){return Promise.all([caches.open(e),caches.open(t)]).then(function(t){var n=t[0],r=t[1];return n.keys().then(function(e){return Promise.all(e.map(function(e){return n.match(e).then(function(t){return r.put(e,t)})}))}).then(function(){return caches.delete(e)})})})}function u(e,t){return o(t).then(function(t){return t.add(e)})}function f(e,t){return o(t).then(function(t){return t.delete(e)})}function h(e){e instanceof Promise||p(e),m.preCacheItems=m.preCacheItems.concat(e)}function p(e){var t=Array.isArray(e);if(t&&e.forEach(function(e){"string"==typeof e||e instanceof Request||(t=!1)}),!t)throw new TypeError("The precache method expects either an array of strings and/or Requests or a Promise that resolves to an array of strings and/or Requests.");return e}function l(e,t,n){if(!e)return!1;if(t){var r=e.headers.get("date");if(r){var o=new Date(r);if(o.getTime()+1e3*t<n)return!1}}return!0}var d,m=e("./options"),g=e("./idb-cache-expiration");t.exports={debug:r,fetchAndCache:i,openCache:o,renameCache:a,cache:u,uncache:f,precache:h,validatePrecacheInput:p,isResponseFresh:l}},{"./idb-cache-expiration":2,"./options":4}],2:[function(e,t,n){"use strict";function r(e){return new Promise(function(t,n){var r=indexedDB.open(u+e,f);r.onupgradeneeded=function(){var e=r.result.createObjectStore(h,{keyPath:p});e.createIndex(l,l,{unique:!1})},r.onsuccess=function(){t(r.result)},r.onerror=function(){n(r.error)}})}function o(e){return e in d||(d[e]=r(e)),d[e]}function i(e,t,n){return new Promise(function(r,o){var i=e.transaction(h,"readwrite"),c=i.objectStore(h);c.put({url:t,timestamp:n}),i.oncomplete=function(){r(e)},i.onabort=function(){o(i.error)}})}function c(e,t,n){return t?new Promise(function(r,o){var i=1e3*t,c=[],s=e.transaction(h,"readwrite"),a=s.objectStore(h),u=a.index(l);u.openCursor().onsuccess=function(e){var t=e.target.result;if(t&&n-i>t.value[l]){var r=t.value[p];c.push(r),a.delete(r),t.continue()}},s.oncomplete=function(){r(c)},s.onabort=o}):Promise.resolve([])}function s(e,t){return t?new Promise(function(n,r){var o=[],i=e.transaction(h,"readwrite"),c=i.objectStore(h),s=c.index(l),a=s.count();s.count().onsuccess=function(){var e=a.result;e>t&&(s.openCursor().onsuccess=function(n){var r=n.target.result;if(r){var i=r.value[p];o.push(i),c.delete(i),e-o.length>t&&r.continue()}})},i.oncomplete=function(){n(o)},i.onabort=r}):Promise.resolve([])}function a(e,t,n,r){return c(e,n,r).then(function(n){return s(e,t).then(function(e){return n.concat(e)})})}var u="sw-toolbox-",f=1,h="store",p="url",l="timestamp",d={};t.exports={getDb:o,setTimestampForUrl:i,expireEntries:a}},{}],3:[function(e,t,n){"use strict";function r(e){var t=a.match(e.request);t?e.respondWith(t(e.request)):a.default&&"GET"===e.request.method&&0===e.request.url.indexOf("http")&&e.respondWith(a.default(e.request))}function o(e){s.debug("activate event fired");var t=u.cache.name+"$$$inactive$$$";e.waitUntil(s.renameCache(t,u.cache.name))}function i(e){return e.reduce(function(e,t){return e.concat(t)},[])}function c(e){var t=u.cache.name+"$$$inactive$$$";s.debug("install event fired"),s.debug("creating cache ["+t+"]"),e.waitUntil(s.opeLine truncated
|
||||
//# sourceMappingURL=sw-toolbox.js.map
|
||||
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"name": "Prettier",
|
||||
"short_name": "Prettier",
|
||||
"description": "Opinionated code formatter",
|
||||
"lang": "en-US",
|
||||
"start_url": ".",
|
||||
"scope": ".",
|
||||
"display": "browser",
|
||||
"theme_color": "#1A2B34",
|
||||
"icons": [
|
||||
{
|
||||
"src": "icon.png"
|
||||
}
|
||||
]
|
||||
}
|
||||
+48
-37
@@ -2,7 +2,28 @@
|
||||
/* eslint no-var: off, strict: off, prefer-arrow-callback: off */
|
||||
/* global CodeMirror prettierVersion */
|
||||
|
||||
(function() {
|
||||
var state = (function loadState(hash) {
|
||||
try {
|
||||
return JSON.parse(hash);
|
||||
} catch (error) {
|
||||
return {
|
||||
options: undefined,
|
||||
content:
|
||||
'hello ( "world"\n);\n\n' +
|
||||
'[ "lorem", "ipsum", \'dolor\', sit("amet"), consectetur[ \'adipiscing\' ] + "elit" ].reduce(\n (first, second) => first + second,\n "")\n\n' +
|
||||
"const Foo = ({ bar, baz, things }) => {\n" +
|
||||
' return <div style={{\ncolor: "papayawhip"}}>\n' +
|
||||
" <br/>{things.map(thing => reallyLongPleaseDontPutOnOneLine(thing) ? <p>{ok}</p> : <Quax bar={bar} baz={ baz } {...thing}></Quax>)\n" +
|
||||
" }</div>}"
|
||||
};
|
||||
}
|
||||
})(decodeURIComponent(location.hash.slice(1)));
|
||||
|
||||
var worker = new Worker("./worker.js");
|
||||
// Warm up the worker (load the current parser while CodeMirror loads)
|
||||
worker.postMessage({ text: "", options: state.options });
|
||||
|
||||
window.onload = function() {
|
||||
var OPTIONS = [
|
||||
"printWidth",
|
||||
"tabWidth",
|
||||
@@ -13,8 +34,11 @@
|
||||
"parser",
|
||||
"semi",
|
||||
"useTabs",
|
||||
"doc"
|
||||
"doc",
|
||||
"ast"
|
||||
];
|
||||
state.options && setOptions(state.options);
|
||||
|
||||
function setOptions(options) {
|
||||
OPTIONS.forEach(function(option) {
|
||||
var elem = document.getElementById(option);
|
||||
@@ -45,12 +69,6 @@
|
||||
return options;
|
||||
}
|
||||
|
||||
function omitNonFormatterOptions(options) {
|
||||
var optionsClone = Object.assign({}, options);
|
||||
delete optionsClone.doc;
|
||||
return optionsClone;
|
||||
}
|
||||
|
||||
function replaceHash(hash) {
|
||||
if (
|
||||
typeof URL === "function" &&
|
||||
@@ -68,12 +86,13 @@
|
||||
function formatAsync() {
|
||||
var options = getOptions();
|
||||
|
||||
[docEditor, outputEditor].forEach(function(editor) {
|
||||
editor.setOption("rulers", [
|
||||
{ column: options.printWidth, color: "#444444" }
|
||||
]);
|
||||
});
|
||||
document.getElementsByClassName("doc")[0].style.display = options.doc
|
||||
outputEditor.setOption("rulers", [
|
||||
{ column: options.printWidth, color: "#444444" }
|
||||
]);
|
||||
document.querySelector(".ast").style.display = options.ast
|
||||
? "flex"
|
||||
: "none";
|
||||
document.querySelector(".doc").style.display = options.doc
|
||||
? "flex"
|
||||
: "none";
|
||||
|
||||
@@ -83,16 +102,14 @@
|
||||
)
|
||||
);
|
||||
replaceHash(value);
|
||||
var formatterOptions = omitNonFormatterOptions(options);
|
||||
worker.postMessage({
|
||||
text: inputEditor.getValue(),
|
||||
options: formatterOptions,
|
||||
options: options,
|
||||
ast: options.ast,
|
||||
doc: options.doc
|
||||
});
|
||||
}
|
||||
|
||||
document.querySelector(".options-container").onchange = formatAsync;
|
||||
|
||||
var editorOptions = {
|
||||
lineNumbers: true,
|
||||
keyMap: "sublime",
|
||||
@@ -112,32 +129,26 @@
|
||||
document.getElementById("doc-editor"),
|
||||
{ readOnly: true, lineNumbers: false, theme: "neat" }
|
||||
);
|
||||
|
||||
var astEditor = CodeMirror.fromTextArea(
|
||||
document.getElementById("ast-editor"),
|
||||
{ readOnly: true, lineNumbers: false, theme: "neat" }
|
||||
);
|
||||
var outputEditor = CodeMirror.fromTextArea(
|
||||
document.getElementById("output-editor"),
|
||||
{ readOnly: true, lineNumbers: true, theme: "neat" }
|
||||
);
|
||||
|
||||
var worker = new Worker("./worker.js");
|
||||
Array.from(document.querySelectorAll("textarea")).forEach(function(element) {
|
||||
element.classList.remove("loading");
|
||||
});
|
||||
|
||||
worker.onmessage = function(message) {
|
||||
outputEditor.setValue(message.data.formatted);
|
||||
docEditor.setValue(message.data.doc || "");
|
||||
astEditor.setValue(message.data.ast || "");
|
||||
};
|
||||
|
||||
document.getElementsByClassName("version")[0].innerText = prettierVersion;
|
||||
|
||||
try {
|
||||
var json = JSON.parse(decodeURIComponent(location.hash.slice(1)));
|
||||
setOptions(json.options);
|
||||
inputEditor.setValue(json.content);
|
||||
} catch (e) {
|
||||
inputEditor.setValue(
|
||||
'hello ( "world"\n);\n\n' +
|
||||
'[ "lorem", "ipsum", \'dolor\', sit("amet"), consectetur[ \'adipiscing\' ] + "elit" ].reduce(\n (first, second) => first + second,\n "")\n\n' +
|
||||
"const Foo = ({ bar, baz, things }) => {\n" +
|
||||
' return <div style={{\ncolor: "papayawhip"}}>\n' +
|
||||
" <br/>{things.map(thing => reallyLongPleaseDontPutOnOneLine(thing) ? <p>{ok}</p> : <Quax bar={bar} baz={ baz } {...thing}></Quax>)\n" +
|
||||
" }</div>}"
|
||||
);
|
||||
}
|
||||
})();
|
||||
inputEditor.setValue(state.content);
|
||||
document.querySelector(".options-container").onchange = formatAsync;
|
||||
document.querySelector(".version").innerText = prettierVersion;
|
||||
};
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 17 KiB |
@@ -0,0 +1,36 @@
|
||||
/* eslint-env serviceworker */
|
||||
/* global toolbox */
|
||||
|
||||
"use strict";
|
||||
|
||||
importScripts("lib/sw-toolbox.js");
|
||||
|
||||
toolbox.precache([
|
||||
// Scripts
|
||||
"lib/prettier-version.js",
|
||||
"lib/index.js",
|
||||
"lib/parser-babylon.js",
|
||||
"lib/parser-typescript.js",
|
||||
"lib/parser-flow.js",
|
||||
"playground.js",
|
||||
"lib/sw-toolbox.js",
|
||||
"lib/sw-toolbox-companion.js",
|
||||
|
||||
// CodeMirror
|
||||
"https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.26.0/codemirror.css",
|
||||
"https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.26.0/theme/neat.css",
|
||||
"https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.26.0/codemirror.js",
|
||||
"https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.26.0/mode/javascript/javascript.js",
|
||||
"https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.26.0/addon/display/rulers.js",
|
||||
"https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.26.0/addon/search/searchcursor.js",
|
||||
"https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.26.0/addon/edit/matchbrackets.js",
|
||||
"https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.26.0/addon/edit/closebrackets.js",
|
||||
"https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.26.0/addon/comment/comment.js",
|
||||
"https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.26.0/addon/wrap/hardwrap.js",
|
||||
"https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.26.0/keymap/sublime.js",
|
||||
|
||||
// Images
|
||||
"prettier.png"
|
||||
]);
|
||||
|
||||
toolbox.router.default = toolbox.fastest;
|
||||
+26
-6
@@ -22,27 +22,47 @@ var prettier = index; // eslint-disable-line
|
||||
var parsersLoaded = {};
|
||||
|
||||
self.onmessage = function(message) {
|
||||
lazyLoadParser(message.data.options.parser);
|
||||
const options = message.data.options || {
|
||||
parser: "babylon"
|
||||
};
|
||||
|
||||
var formatted, doc;
|
||||
delete options.ast;
|
||||
delete options.doc;
|
||||
|
||||
lazyLoadParser(options.parser);
|
||||
|
||||
var formatted, doc, ast;
|
||||
try {
|
||||
formatted = prettier.format(message.data.text, message.data.options);
|
||||
formatted = prettier.format(message.data.text, options);
|
||||
} catch (e) {
|
||||
formatted = e.toString();
|
||||
}
|
||||
|
||||
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, message.data.options),
|
||||
{}
|
||||
prettier.__debug.printToDoc(message.data.text, options),
|
||||
{ parser: "babylon" }
|
||||
);
|
||||
} catch (e) {
|
||||
doc = e.toString();
|
||||
}
|
||||
}
|
||||
|
||||
self.postMessage({ formatted: formatted, doc: doc });
|
||||
self.postMessage({ formatted: formatted, doc: doc, ast: ast });
|
||||
};
|
||||
|
||||
function lazyLoadParser(parser) {
|
||||
|
||||
Reference in new issue
Block a user