Skip to content

Commit 6a7ea34

Browse files
Adjust the download page
1 parent ead8d95 commit 6a7ea34

File tree

2 files changed

+35
-56
lines changed

2 files changed

+35
-56
lines changed

assets/download.js

Lines changed: 28 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,11 @@
44

55
import { getFileContents, toArray } from "./util.js";
66

7-
let components = await (await fetch("https://dev.prismjs.com/components.json")).json();
8-
9-
let treeURL = "https://api.github.com/repos/PrismJS/prism/git/trees/master?recursive=1";
10-
let tree = (await (await fetch(treeURL)).json()).tree;
7+
let components = await (await fetch("/components.json")).json();
8+
let fileSizes = await (await fetch("/file-sizes.json")).json();
119

1210
let cache = {};
1311
let form = document.querySelector("form");
14-
let minified = true;
1512

1613
let dependencies = {};
1714
let timerId = 0;
@@ -94,7 +91,13 @@ for (let category in components) {
9491
input.checked = checked;
9592
input.disabled = disabled;
9693

97-
let filepath = all.meta.path.replace(/\{id\}/g, id);
94+
let filepath = all.meta.path;
95+
if (category === "plugins") {
96+
// When built, the plugins live in one directory called `plugins/`, not every plugin in its own directory
97+
filepath = filepath.replace("{id}/", "");
98+
}
99+
100+
filepath = filepath.replace(/\{id\}/g, id);
98101

99102
let info = all[id] = {
100103
noCSS: all[id].noCSS || all.meta.noCSS,
@@ -104,33 +107,21 @@ for (let category in components) {
104107
after: toArray(all[id].after),
105108
modify: toArray(all[id].modify),
106109
files: {
107-
minified: {
108-
paths: [],
109-
size: 0
110-
},
111-
dev: {
112-
paths: [],
113-
size: 0
114-
}
115-
}
110+
paths: [],
111+
size: 0,
112+
},
116113
};
117114

118115
info.require.forEach(v => {
119116
dependencies[v] = (dependencies[v] || []).concat(id);
120117
});
121118

122119
if (!all[id].noJS && !/\.css$/.test(filepath)) {
123-
info.files.minified.paths.push(filepath.replace(/(\.js)?$/, ".min.js"));
124-
info.files.dev.paths.push(filepath.replace(/(\.js)?$/, ".js"));
120+
info.files.paths.push(filepath.replace(/(\.js)?$/, ".js"));
125121
}
126122

127-
128123
if ((!all[id].noCSS && !/\.js$/.test(filepath)) || /\.css$/.test(filepath)) {
129-
let cssFile = filepath.replace(/(\.css)?$/, ".css");
130-
let minCSSFile = cssFile.replace(/(?:\.css)$/, ".min.css");
131-
132-
info.files.minified.paths.push(minCSSFile);
133-
info.files.dev.paths.push(cssFile);
124+
info.files.paths.push(filepath.replace(/(\.css)?$/, ".css"));
134125
}
135126

136127
input.addEventListener("change", ({ target }) => {
@@ -170,20 +161,16 @@ for (let category in components) {
170161
}
171162
}
172163

173-
form.elements.compression[0].onclick =
174-
form.elements.compression[1].onclick = function () {
175-
minified = !!+this.value;
176-
177-
getFilesSizes();
178-
};
179-
180164
getFilesSizes();
181165

182-
function getFileSize(filepath) {
183-
for (let i = 0, l = tree.length; i < l; i++) {
184-
if (tree[i].path === filepath) {
185-
return tree[i].size;
186-
}
166+
function getFileSize (category, id, filepath) {
167+
let type = filepath.match(/\.(css|js)$/)[1];
168+
169+
if (category === "core") {
170+
return fileSizes.core.js;
171+
}
172+
else {
173+
return fileSizes[category][id]?.[type] ?? 0;
187174
}
188175
}
189176

@@ -196,14 +183,14 @@ function getFilesSizes() {
196183
continue;
197184
}
198185

199-
let distro = all[id].files[minified ? "minified" : "dev"];
186+
let distro = all[id].files;
200187
let files = distro.paths;
201188

202189
files.forEach(filepath => {
203190
let file = cache[filepath] = cache[filepath] || {};
204191

205192
if (!file.size) {
206-
let size = getFileSize(filepath);
193+
let size = getFileSize(category, id, filepath);
207194
if (size) {
208195
file.size = size;
209196
distro.size += file.size;
@@ -235,7 +222,7 @@ function update(updatedCategory, updatedId) {
235222
let info = all[id];
236223

237224
if (info.enabled || id === updatedId) {
238-
let distro = info.files[minified ? "minified" : "dev"];
225+
let distro = info.files;
239226

240227
distro.paths.forEach(path => {
241228
if (cache[path]) {
@@ -247,7 +234,8 @@ function update(updatedCategory, updatedId) {
247234
if (info.enabled) {
248235

249236
if (!file.contentsPromise) {
250-
file.contentsPromise = getFileContents("https://dev.prismjs.com/" + path);
237+
// FIXME: Remove “v2” when Prism v2 is released
238+
file.contentsPromise = getFileContents("https://v2.dev.prismjs.com/dist/" + path);
251239
}
252240

253241
total[type] += size;
@@ -331,7 +319,7 @@ async function generateCode() {
331319
redownload[category] = redownload[category] || [];
332320
redownload[category].push(id);
333321
}
334-
info.files[minified ? "minified" : "dev"].paths.forEach(path => {
322+
info.files.paths.forEach(path => {
335323
if (cache[path]) {
336324
let type = path.match(/\.(\w+)$/)[1];
337325

download.njk

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,19 @@ resources:
2626
{%- set name = name + " + " + title -%}
2727
{%- endfor %}
2828
{%- endif %}
29-
{%- set files_path = meta.path | replace("{id}", id) | replace(".css", "") | replace(".js", "") -%}
3029
{%- set option = component.option or meta.option -%}
3130

3231
<label data-id="{{ id }}">
33-
<input type="{{ "radio" if meta.exclusive else "checkbox" }}" name="download-{{ category }}" value="{{ id }}" {{ "checked" if option == "mandatory" }} {{ "disabled" if option == "mandatory" }} />
32+
{%- set js_size = (file_sizes[category][id].js if id != "core" else file_sizes.core.js) or 0 -%}
33+
{%- set css_size = file_sizes[category][id].css or 0 -%}
34+
{%- set total_size = js_size + css_size -%}
35+
36+
<input type="{{ "radio" if meta.exclusive else "checkbox" }}" name="download-{{ category }}" value="{{ id }}" {{ "checked" if option == "mandatory" }} {{ "disabled" if option == "mandatory" or total_size == 0 }} />
3437

3538
{%- if meta.link -%}
36-
{%- set link = meta.link | replace("{id}", id) -%}
37-
{%- set base = meta.domain or "https://prismjs.com/" -%}
38-
{%- set url = base + link -%}
39+
{%- set url = meta.link | replace("{id}", id) -%}
3940

40-
<a href="{{ url }}" class="name">{{ name }}</a>
41+
<a href="/{{ url }}" class="name">{{ name }}</a>
4142
{%- else -%}
4243
<span class="name">{{ name }}</span>
4344
{%- endif -%}
@@ -46,11 +47,6 @@ resources:
4647
<a href="https://github.com/{{ component.owner }}" class="owner" target="_blank">{{ component.owner }}</a>
4748
{% endif %}
4849

49-
{%- set js_file_path = files_path + ".min.js" -%}
50-
{%- set css_file_path = files_path + ".min.css" -%}
51-
{%- set js_size = files_sizes[js_file_path] or 0 -%}
52-
{%- set css_size = files_sizes[css_file_path] or 0 -%}
53-
{%- set total_size = js_size + css_size -%}
5450
<strong class="filesize">{{ total_size | pretty_size }}</strong>
5551
</label>
5652
{% endif %}
@@ -60,11 +56,6 @@ resources:
6056

6157
<section>
6258
<form>
63-
<p>
64-
Compression level:
65-
<label><input type="radio" name="compression" value="0" /> Development version</label>
66-
<label><input type="radio" name="compression" value="1" checked /> Minified version</label>
67-
</p>
6859
<section id="components">
6960
{%- for category, components in components -%}
7061
{{ list(category, components) }}

0 commit comments

Comments
 (0)