@@ -19,7 +19,7 @@ function getDevServer(compilation) {
19
19
const { userWorkspace, scratchDir } = context ;
20
20
const app = new Koa ( ) ;
21
21
22
- // TODO export this an async function
22
+ // TODO use url.endsWith!!
23
23
app . use ( async ctx => {
24
24
// console.debug('URL', ctx.request.url);
25
25
@@ -32,7 +32,6 @@ function getDevServer(compilation) {
32
32
33
33
// make sure this only happens for "pages", nor partials or fixtures, templates, et)
34
34
if ( ctx . request . url . indexOf ( '.html' ) >= 0 ) {
35
- // TODO get this stuff from compilation
36
35
let title = config . title ;
37
36
const metaOutletContent = config . meta . map ( item => {
38
37
let metaHtml = '' ;
@@ -250,6 +249,7 @@ function getDevServer(compilation) {
250
249
}
251
250
}
252
251
252
+ // TODO break up into distinct font / icons / svg handlers, not related to assets/
253
253
if ( ctx . request . url . indexOf ( 'assets/' ) >= 0 && ctx . request . url . indexOf ( '.css' ) < 0 ) {
254
254
const assetPath = path . join ( userWorkspace , ctx . request . url ) ;
255
255
const ext = path . extname ( assetPath ) ;
@@ -285,6 +285,75 @@ function getDevServer(compilation) {
285
285
return app ;
286
286
}
287
287
288
+ function getProdServer ( compilation ) {
289
+ const app = new Koa ( ) ;
290
+
291
+ app . use ( async ctx => {
292
+ // console.debug('URL', ctx.request.url);
293
+ const { outputDir } = compilation . context ;
294
+
295
+ if ( ctx . request . url . endsWith ( '/' ) ) {
296
+ ctx . redirect ( `http://localhost:8080${ ctx . request . url } index.html` ) ;
297
+ }
298
+
299
+ if ( ctx . request . url . endsWith ( '.html' ) ) {
300
+ const contents = await fsp . readFile ( path . join ( outputDir , ctx . request . url ) , 'utf-8' ) ;
301
+
302
+ ctx . set ( 'Content-Type' , 'text/html' ) ;
303
+ ctx . body = contents ;
304
+ }
305
+
306
+ if ( ctx . request . url . endsWith ( '.js' ) ) {
307
+ const contents = await fsp . readFile ( path . join ( outputDir , ctx . request . url ) , 'utf-8' ) ;
308
+
309
+ ctx . set ( 'Content-Type' , 'text/javascript' ) ;
310
+ ctx . body = contents ;
311
+ }
312
+
313
+ if ( ctx . request . url . endsWith ( '.css' ) ) {
314
+ const contents = await fsp . readFile ( path . join ( outputDir , ctx . request . url ) , 'utf-8' ) ;
315
+
316
+ ctx . set ( 'Content-Type' , 'text/css' ) ;
317
+ ctx . body = contents ;
318
+ }
319
+
320
+ // TODO break up into distinct font / icons / svg handlers, decouple from to assets/
321
+ if ( ctx . request . url . indexOf ( 'assets/' ) ) {
322
+ const assetPath = path . join ( outputDir , ctx . request . url ) ;
323
+ const ext = path . extname ( assetPath ) ;
324
+ const type = ext === '.svg'
325
+ ? `${ ext . replace ( '.' , '' ) } +xml`
326
+ : ext . replace ( '.' , '' ) ;
327
+
328
+ if ( [ '.jpg' , '.png' , '.gif' , '.svg' ] . includes ( ext ) ) {
329
+ ctx . set ( 'Content-Type' , `image/${ type } ` ) ;
330
+
331
+ if ( ext === '.svg' ) {
332
+ ctx . body = await fsp . readFile ( assetPath , 'utf-8' ) ;
333
+ } else {
334
+ ctx . body = await fsp . readFile ( assetPath ) ;
335
+ }
336
+ } else if ( [ '.woff2' , '.woff' , '.ttf' ] . includes ( ext ) ) {
337
+ ctx . set ( 'Content-Type' , `font/${ type } ` ) ;
338
+ ctx . body = await fsp . readFile ( assetPath ) ;
339
+ } else if ( [ '.ico' ] . includes ( ext ) ) {
340
+ ctx . set ( 'Content-Type' , `image/x-icon` ) ;
341
+ ctx . body = await fsp . readFile ( assetPath ) ;
342
+ }
343
+ }
344
+
345
+ if ( ctx . request . url . endsWith ( '.json' ) ) {
346
+ const contents = await fsp . readFile ( path . join ( outputDir , 'graph.json' ) , 'utf-8' ) ;
347
+
348
+ ctx . set ( 'Content-Type' , 'application/json' ) ;
349
+ ctx . body = JSON . parse ( contents ) ;
350
+ }
351
+ } ) ;
352
+
353
+ return app ;
354
+ }
355
+
288
356
module . exports = {
289
- devServer : getDevServer
357
+ devServer : getDevServer ,
358
+ prodServer : getProdServer
290
359
} ;
0 commit comments