44
55import { 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
1210let cache = { } ;
1311let form = document . querySelector ( "form" ) ;
14- let minified = true ;
1512
1613let dependencies = { } ;
1714let 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 ( / \{ i d \} / 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 ( / \{ i d \} / 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 && ! / \. c s s $ / . test ( filepath ) ) {
123- info . files . minified . paths . push ( filepath . replace ( / ( \. j s ) ? $ / , ".min.js" ) ) ;
124- info . files . dev . paths . push ( filepath . replace ( / ( \. j s ) ? $ / , ".js" ) ) ;
120+ info . files . paths . push ( filepath . replace ( / ( \. j s ) ? $ / , ".js" ) ) ;
125121 }
126122
127-
128123 if ( ( ! all [ id ] . noCSS && ! / \. j s $ / . test ( filepath ) ) || / \. c s s $ / . test ( filepath ) ) {
129- let cssFile = filepath . replace ( / ( \. c s s ) ? $ / , ".css" ) ;
130- let minCSSFile = cssFile . replace ( / (?: \. c s s ) $ / , ".min.css" ) ;
131-
132- info . files . minified . paths . push ( minCSSFile ) ;
133- info . files . dev . paths . push ( cssFile ) ;
124+ info . files . paths . push ( filepath . replace ( / ( \. c s s ) ? $ / , ".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-
180164getFilesSizes ( ) ;
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 ( / \. ( c s s | j s ) $ / ) [ 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
0 commit comments