@@ -4,11 +4,12 @@ const EventEmitter = require('events');
44const watchify = require ( 'watchify' ) ;
55const devnull = require ( 'dev-null' ) ;
66const express = require ( 'express' ) ;
7- const Writer = require ( 'asset-pipe-js-writer' ) ;
7+ const JsWriter = require ( 'asset-pipe-js-writer' ) ;
8+ const CssWriter = require ( 'asset-pipe-css-writer' ) ;
89const emits = require ( 'emits' ) ;
910
1011module . exports = class Middleware extends EventEmitter {
11- constructor ( files = [ ] , options = { } ) {
12+ constructor ( jsFiles = [ ] , cssFiles = [ ] , options = { } ) {
1213 super ( ) ;
1314
1415 this . options = Object . assign (
@@ -17,17 +18,22 @@ module.exports = class Middleware extends EventEmitter {
1718 packageCache : { } ,
1819 debug : true ,
1920 jsPath : '/js' ,
21+ cssPath : '/css' ,
2022 } ,
2123 options
2224 ) ;
2325
2426 this . emits = emits ;
25- this . writer = new Writer ( files , this . options , true ) ;
26- this . writer . plugin ( watchify ) ;
27+ this . writers = {
28+ js : new JsWriter ( jsFiles , this . options , true ) ,
29+ css : new CssWriter ( cssFiles , this . options ) ,
30+ } ;
31+
32+ this . writers . js . plugin ( watchify ) ;
2733
2834 // On every filechange, drain the stream to keep the cache up to date
29- this . writer . on ( 'update' , ( ) => {
30- const bundler = this . writer . bundle ( ) ;
35+ this . writers . js . on ( 'update' , ( ) => {
36+ const bundler = this . writers . js . bundle ( ) ;
3137
3238 bundler . on ( 'error' , e => {
3339 this . emit ( 'error' , e ) ;
@@ -36,31 +42,50 @@ module.exports = class Middleware extends EventEmitter {
3642 bundler . pipe ( devnull ( ) ) ;
3743 } ) ;
3844
39- // Proxy underlaying events
40- this . writer . on ( 'error' , this . emits ( 'error' ) ) ;
41- this . writer . on ( 'update' , this . emits ( 'update' ) ) ;
42- this . writer . on ( 'bytes ' , this . emits ( 'bytes' ) ) ;
43- this . writer . on ( 'time ', this . emits ( 'time' ) ) ;
44- this . writer . on ( 'log' , this . emits ( 'log' ) ) ;
45+ this . writers . css . on ( 'update' , ( ) => {
46+ const bundler = this . writers . css . bundle ( ) ;
47+
48+ bundler . on ( 'error ' , e => {
49+ this . emit ( 'error ', e ) ;
50+ } ) ;
4551
46- this . app = express . Router ( { // eslint-disable-line
52+ bundler . pipe ( devnull ( ) ) ;
53+ } ) ;
54+
55+ // Proxy underlaying events
56+ this . writers . js . on ( 'error' , this . emits ( 'error' ) ) ;
57+ this . writers . js . on ( 'update' , this . emits ( 'update' ) ) ;
58+ this . writers . js . on ( 'bytes' , this . emits ( 'bytes' ) ) ;
59+ this . writers . js . on ( 'time' , this . emits ( 'time' ) ) ;
60+ this . writers . js . on ( 'log' , this . emits ( 'log' ) ) ;
61+
62+ this . writers . css . on ( 'error' , this . emits ( 'error' ) ) ;
63+ this . writers . css . on ( 'update' , this . emits ( 'update' ) ) ;
64+ this . writers . css . on ( 'bytes' , this . emits ( 'bytes' ) ) ;
65+ this . writers . css . on ( 'time' , this . emits ( 'time' ) ) ;
66+ this . writers . css . on ( 'log' , this . emits ( 'log' ) ) ;
67+
68+ // eslint-disable-next-line new-cap
69+ this . app = express . Router ( {
4770 mergeParams : true ,
4871 } ) ;
4972
5073 this . app . get ( this . options . jsPath , this . js ( ) ) ;
74+ this . app . get ( this . options . cssPath , this . css ( ) ) ;
5175 }
5276
5377 transform ( transform , options ) {
54- this . writer . transform ( transform , options ) ;
78+ this . writers . js . transform ( transform , options ) ;
5579 }
5680
5781 plugin ( plugin , options ) {
58- this . writer . plugin ( plugin , options ) ;
82+ this . writers . js . plugin ( plugin , options ) ;
5983 }
6084
61- middelware ( jsProp = 'js' ) {
85+ middleware ( jsProp = 'js' , cssProp = 'css ') {
6286 return ( req , res , next ) => {
6387 res . locals [ jsProp ] = this . options . jsPath ;
88+ res . locals [ cssProp ] = this . options . cssPath ;
6489 next ( ) ;
6590 } ;
6691 }
@@ -72,7 +97,26 @@ module.exports = class Middleware extends EventEmitter {
7297 js ( ) {
7398 return ( req , res , next ) => {
7499 res . writeHead ( 200 , { 'Content-Type' : 'application/javascript' } ) ;
75- const bundler = this . writer . bundle ( ) ;
100+ const bundler = this . writers . js . bundle ( ) ;
101+
102+ bundler . on ( 'error' , cleanup ) ;
103+
104+ const writeStream = bundler . pipe ( res ) . on ( 'error' , cleanup ) ;
105+
106+ function cleanup ( e ) {
107+ res . write ( `console.error(${ JSON . stringify ( e . stack ) } )` ) ;
108+ bundler . pause ( ) ;
109+ bundler . unpipe ( writeStream ) ;
110+ writeStream . end ( ) ;
111+ next ( e ) ;
112+ }
113+ } ;
114+ }
115+
116+ css ( ) {
117+ return ( req , res , next ) => {
118+ res . writeHead ( 200 , { 'Content-Type' : 'text/css' } ) ;
119+ const bundler = this . writers . css . bundle ( ) ;
76120
77121 bundler . on ( 'error' , cleanup ) ;
78122
0 commit comments