@@ -4,11 +4,12 @@ const EventEmitter = require('events');
4
4
const watchify = require ( 'watchify' ) ;
5
5
const devnull = require ( 'dev-null' ) ;
6
6
const 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' ) ;
8
9
const emits = require ( 'emits' ) ;
9
10
10
11
module . exports = class Middleware extends EventEmitter {
11
- constructor ( files = [ ] , options = { } ) {
12
+ constructor ( jsFiles = [ ] , cssFiles = [ ] , options = { } ) {
12
13
super ( ) ;
13
14
14
15
this . options = Object . assign (
@@ -17,17 +18,22 @@ module.exports = class Middleware extends EventEmitter {
17
18
packageCache : { } ,
18
19
debug : true ,
19
20
jsPath : '/js' ,
21
+ cssPath : '/css' ,
20
22
} ,
21
23
options
22
24
) ;
23
25
24
26
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 ) ;
27
33
28
34
// 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 ( ) ;
31
37
32
38
bundler . on ( 'error' , e => {
33
39
this . emit ( 'error' , e ) ;
@@ -36,31 +42,50 @@ module.exports = class Middleware extends EventEmitter {
36
42
bundler . pipe ( devnull ( ) ) ;
37
43
} ) ;
38
44
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
+ } ) ;
45
51
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 ( {
47
70
mergeParams : true ,
48
71
} ) ;
49
72
50
73
this . app . get ( this . options . jsPath , this . js ( ) ) ;
74
+ this . app . get ( this . options . cssPath , this . css ( ) ) ;
51
75
}
52
76
53
77
transform ( transform , options ) {
54
- this . writer . transform ( transform , options ) ;
78
+ this . writers . js . transform ( transform , options ) ;
55
79
}
56
80
57
81
plugin ( plugin , options ) {
58
- this . writer . plugin ( plugin , options ) ;
82
+ this . writers . js . plugin ( plugin , options ) ;
59
83
}
60
84
61
- middelware ( jsProp = 'js' ) {
85
+ middleware ( jsProp = 'js' , cssProp = 'css ') {
62
86
return ( req , res , next ) => {
63
87
res . locals [ jsProp ] = this . options . jsPath ;
88
+ res . locals [ cssProp ] = this . options . cssPath ;
64
89
next ( ) ;
65
90
} ;
66
91
}
@@ -72,7 +97,26 @@ module.exports = class Middleware extends EventEmitter {
72
97
js ( ) {
73
98
return ( req , res , next ) => {
74
99
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 ( ) ;
76
120
77
121
bundler . on ( 'error' , cleanup ) ;
78
122
0 commit comments