Skip to content

Commit 214e493

Browse files
committed
feat: use unpkg to load prettier, load parsers only when needed
1 parent dd03c8c commit 214e493

File tree

7 files changed

+161
-122
lines changed

7 files changed

+161
-122
lines changed

README.md

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,3 @@
11
# web-beautify
22

33
Beautify languages on the web through web workers.
4-
5-
## Supported languages
6-
7-
- JavaScript
8-
- TypeScript
9-
- CSS
10-
- SCSS
11-
- Less
12-
- JSON
13-
- JSON5
14-
- GraphQL
15-
- Markdown
16-
- HTML
17-
- Vue
18-
- YAML
19-
- PHP

examples/src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ worker.onmessage = (e) => {
88

99
worker.postMessage({
1010
source: 'const \n x \n = (((\n 5 \n + \n 10)))',
11-
language: 'javascript',
11+
language: 'babel',
1212
});

package-lock.json

Lines changed: 0 additions & 53 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
{
22
"name": "@sourcebin/web-beautify",
3-
"version": "0.0.1",
3+
"version": "0.0.2",
44
"main": "src/index.js",
55
"scripts": {
66
"lint": "eslint --ignore-path .gitignore .",
77
"test": "npm run lint"
88
},
9-
"dependencies": {
10-
"@prettier/plugin-php": "^0.13.0",
11-
"prettier": "^1.19.1"
12-
},
139
"devDependencies": {
1410
"@syntek/eslint-config-syntek": "^2.0.2",
1511
"eslint": "^6.8.0",

src/index.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
import * as prettier from 'prettier/standalone';
1+
/* globals prettier */
22

3+
import { unpkgPrettier } from './unpkg.js';
34
import { parsers } from './parsers.js';
45

5-
export async function beautify(source, language) {
6-
const parser = parsers[language];
7-
const plugins = await Promise.all(parser.import());
6+
unpkgPrettier('standalone.js');
87

8+
export function beautify(source, language) {
99
return prettier.format(source, {
10-
plugins,
11-
parser: parser.name,
10+
parser: language,
11+
plugins: [{ parsers }],
1212
singleQuote: true,
1313
});
1414
}

src/parsers.js

Lines changed: 146 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,163 @@
1-
function prettier(...names) {
2-
return () => names.map(name => import(`prettier/parser-${name}.js`));
1+
/* eslint-disable no-underscore-dangle */
2+
/* globals prettierPlugins */
3+
4+
import { unpkgPrettier } from './unpkg.js';
5+
6+
function prettier(name) {
7+
return unpkgPrettier(`parser-${name}.js`);
38
}
49

10+
// https://github.com/prettier/prettier/blob/a093bb3f7b9f59d8cbaf7e20f97f6fafceaef21b/website/static/worker.js#L13-L146
511
export const parsers = {
6-
javascript: {
7-
name: 'babel',
8-
import: prettier('babylon'),
12+
// JS - Babel
13+
get babel() {
14+
prettier('babylon');
15+
return prettierPlugins.babylon.parsers.babel;
16+
},
17+
18+
get 'babel-flow'() {
19+
prettier('babylon');
20+
return prettierPlugins.babylon.parsers['babel-flow'];
21+
},
22+
23+
// backward compatibility
24+
get babylon() {
25+
prettier('babylon');
26+
return prettierPlugins.babylon.parsers.babylon;
27+
},
28+
29+
get json() {
30+
prettier('babylon');
31+
return prettierPlugins.babylon.parsers.json;
32+
},
33+
34+
get json5() {
35+
prettier('babylon');
36+
return prettierPlugins.babylon.parsers.json5;
37+
},
38+
39+
get 'json-stringify'() {
40+
prettier('babylon');
41+
return prettierPlugins.babylon.parsers['json-stringify'];
42+
},
43+
44+
get __js_expression() {
45+
prettier('babylon');
46+
return prettierPlugins.babylon.parsers.__js_expression;
47+
},
48+
49+
get __vue_expression() {
50+
prettier('babylon');
51+
return prettierPlugins.babylon.parsers.__vue_expression;
52+
},
53+
54+
get __vue_event_binding() {
55+
prettier('babylon');
56+
return prettierPlugins.babylon.parsers.__vue_event_binding;
57+
},
58+
59+
// JS - Flow
60+
get flow() {
61+
prettier('flow');
62+
return prettierPlugins.flow.parsers.flow;
63+
},
64+
65+
// JS - TypeScript
66+
get typescript() {
67+
prettier('typescript');
68+
return prettierPlugins.typescript.parsers.typescript;
969
},
10-
typescript: {
11-
name: 'typescript',
12-
import: prettier('typescript'),
70+
71+
// JS - Angular Action
72+
get __ng_action() {
73+
prettier('angular');
74+
return prettierPlugins.angular.parsers.__ng_action;
1375
},
14-
css: {
15-
name: 'css',
16-
import: prettier('postcss'),
76+
77+
// JS - Angular Binding
78+
get __ng_binding() {
79+
prettier('angular');
80+
return prettierPlugins.angular.parsers.__ng_binding;
1781
},
18-
scss: {
19-
name: 'scss',
20-
import: prettier('postcss'),
82+
83+
// JS - Angular Interpolation
84+
get __ng_interpolation() {
85+
prettier('angular');
86+
return prettierPlugins.angular.parsers.__ng_interpolation;
2187
},
22-
less: {
23-
name: 'less',
24-
import: prettier('postcss'),
88+
89+
// JS - Angular Directive
90+
get __ng_directive() {
91+
prettier('angular');
92+
return prettierPlugins.angular.parsers.__ng_directive;
2593
},
26-
json: {
27-
name: 'json',
28-
import: prettier('babylon'),
94+
95+
// CSS
96+
get css() {
97+
prettier('postcss');
98+
return prettierPlugins.postcss.parsers.css;
2999
},
30-
json5: {
31-
name: 'json5',
32-
import: prettier('babylon'),
100+
101+
get less() {
102+
prettier('postcss');
103+
return prettierPlugins.postcss.parsers.css;
33104
},
34-
graphql: {
35-
name: 'graphql',
36-
import: prettier('graphql'),
105+
106+
get scss() {
107+
prettier('postcss');
108+
return prettierPlugins.postcss.parsers.css;
37109
},
38-
markdown: {
39-
name: 'markdown',
40-
import: prettier('markdown'),
110+
111+
// GraphQL
112+
get graphql() {
113+
prettier('graphql');
114+
return prettierPlugins.graphql.parsers.graphql;
41115
},
42-
html: {
43-
name: 'html',
44-
import: prettier('html', 'babylon', 'postcss'),
116+
117+
// Markdown
118+
get markdown() {
119+
prettier('markdown');
120+
return prettierPlugins.markdown.parsers.remark;
45121
},
46-
vue: {
47-
name: 'vue',
48-
import: prettier('html', 'babylon', 'postcss'),
122+
123+
get mdx() {
124+
prettier('markdown');
125+
return prettierPlugins.markdown.parsers.mdx;
49126
},
50-
yaml: {
51-
name: 'yaml',
52-
import: prettier('yaml'),
127+
128+
// YAML
129+
get yaml() {
130+
prettier('yaml');
131+
return prettierPlugins.yaml.parsers.yaml;
132+
},
133+
134+
// Handlebars
135+
get glimmer() {
136+
prettier('glimmer');
137+
return prettierPlugins.glimmer.parsers.glimmer;
138+
},
139+
140+
// HTML
141+
get html() {
142+
prettier('html');
143+
return prettierPlugins.html.parsers.html;
53144
},
54-
php: {
55-
name: 'php',
56-
import: () => [import('@prettier/plugin-php/standalone')],
145+
146+
// Vue
147+
get vue() {
148+
prettier('html');
149+
return prettierPlugins.html.parsers.vue;
150+
},
151+
152+
// Angular
153+
get angular() {
154+
prettier('html');
155+
return prettierPlugins.html.parsers.angular;
156+
},
157+
158+
// Lightning Web Components
159+
get lwc() {
160+
prettier('html');
161+
return prettierPlugins.html.parsers.lwc;
57162
},
58163
};

src/unpkg.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export function unpkg(pkg, version, file) {
2+
return importScripts(`https://unpkg.com/${pkg}@${version}/${file}`);
3+
}
4+
5+
export function unpkgPrettier(file) {
6+
return unpkg('prettier', '1.19.1', file);
7+
}

0 commit comments

Comments
 (0)