-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
279 lines (215 loc) · 6.75 KB
/
index.js
File metadata and controls
279 lines (215 loc) · 6.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
'use strict';
//
var fs = require('fs');
var trumpet = require('trumpet');
var path = require('path');
var Transform = require('readable-stream/transform');
var PassThrough = require('readable-stream/passthrough');
var CleanCSS = require('clean-css');
var UglifyJS = require('uglify-js');
var pipeline = require('./lib/pipeline/pipeline.js');
var request = require('request');
var externalReg = /^(\/\/|http:|https:)/;
var ArrProto = Array.prototype;
var slice = ArrProto.slice;
var concat = ArrProto.concat;
// discern if file is on disk or on a server, and return a stream
// representation in either case
function resolveFile( uri, folder ) {
var p = new PassThrough();
var resource;
if( uri.match( externalReg ) ) {
uri = uri.replace(/^\/\//, 'http://');
resource = request({
uri: uri,
gzip: true
});
} else {
// we don't know the host! so urls starting with / are meaningless!
if( uri.match(/^\//) ) {
// TODO: add debug tools to dump this data.
// as a warning.
}
// replace the first slash to the path can be correctly found
uri = uri.replace(/^\//, '');
resource = fs.createReadStream( path.resolve( process.cwd(), folder, uri ) );
}
resource
.on('error', function() {
// TODO: add debug tools to dump this data.
p.end('/*RESOURCE NOT FOUND : ' + uri + ' */');
})
.on('end', p.end.bind(p));
return resource.pipe(p, {end: false});
}
function base64() {
var t = new Transform();
var buf = new Buffer(0);
t._transform = function( buffer, enc, next ) {
buf = Buffer.concat( [buf, buffer] );
next();
};
// perhaps we need to buffer the data up
t._flush = function( done ) {
this.push( buf.toString('base64') );
done();
};
return t;
}
// TODO: get sourcemaps working
// return a transform stream that will minify the piped content
function mini( type, opts ) {
if( opts && typeof opts !== 'object' ) opts = {};
var data = '';
var types = {
script: function( next ) {
var o = opts || {};
o.fromString = true;
//o.dead_code = true;
this.push( UglifyJS.minify( data, o ).code );
next();
},
style: function( next ) {
this.push( new CleanCSS( opts ).minify( data ) );
next();
}
};
// some resources can get data in multiple chunks
var transform = new Transform();
transform._transform = function( chunk, enc, next ) {
data += chunk.toString();
next();
};
transform._flush = types[ type ];
return transform;
}
/**
* styles function
*
* returns a stream which will parse for link tag and fetch styles from link href
*/
function styles( dir, opts ) {
var tr = trumpet();
tr.selectAll('link[href]', function( link ) {
if( link.getAttribute('packagify-ignore') ) {
link.removeAttribute('packagify-ignore');
return;
}
var href = link.getAttribute('href');
var type = link.getAttribute('type');
var rel = link.getAttribute('rel');
if( rel === 'stylesheet' || type === 'text/css' ) {
var file = resolveFile( href, dir );
var ws = link.createWriteStream({
outer: true
});
ws.write('<style type="text/css">');
// if opts is truthy, pipe the file through the minfication
( opts ? file.pipe( mini('style', opts ) ) : file )
.on('end', function() {
ws.end('</style>');
})
.pipe( ws, {end: false} );
}
});
return tr;
}
function scripts( dir, opts ) {
var tr = trumpet();
tr.selectAll('script[type="text/javascript"][src]', function( script ) {
if( script.getAttribute('packagify-ignore') ) {
script.removeAttribute('packagify-ignore');
return;
}
var src = script.getAttribute('src');
script.removeAttribute( 'src' );
var file = resolveFile( src, dir );
// if opts is truthy, pipe the file through the uglifier
( opts ? file.pipe( mini( 'script', opts ) ) : file )
.pipe( script.createWriteStream() );
});
return tr;
}
function images( dir ) {
var tr = trumpet();
tr.selectAll('img[src]', function( img ) {
if( img.getAttribute('packagify-ignore') ) {
img.removeAttribute('packagify-ignore');
return;
}
var imgSrc = img.getAttribute('src');
var file = resolveFile( imgSrc, dir );
var ws = img.createWriteStream({outer: true});
// we need to use the attributes from the old one
ws.write('<img src="data:image/png;base64,');
file
.pipe( base64() )
.on('end', function() {ws.end('">');})
.pipe( ws, {end: false} );
});
return tr;
}
// TODO:
// maybe..
function stripWhiteSpace() {
}
// skinny shallow extend function
function extend( src ) {
if( !src ) src = {};
var obs = slice.call( arguments, 1 );
var i = 0;
var len = obs.length;
var cur, key;
for (; i < len; i++) {
cur = obs[i];
if( !cur ) break;
for( key in cur ) {
if( cur.hasOwnProperty( key ) && typeof cur[key] !== undefined ) {
src[ key ] = cur[ key ];
}
}
}
return src;
}
var defaultOptions = {
scripts: true,
styles: true,
uglify: true,
minifyCss: true,
images: true,
external: true
};
var packagify = {
pkg: function( filePath, options ) {
var opts = extend( {}, defaultOptions, options );
var folder = path.dirname( filePath );
var parse = [];
if( opts.scripts ) {
parse.push( scripts( folder, opts.uglify) );
}
if( opts.styles ) {
parse.push( styles( folder, opts.minifyCss ) );
}
if( opts.images ) {
parse.push( images( folder ) );
}
return pipeline( parse );
},
pkgFile: function( file, opts ) {
var con = fs.createReadStream( file );
return con.pipe( this.pkg( file, opts ) );
},
// this will save the file
pkgWrite: function( src, opts, dest ) {
if( !dest ) {
dest = opts;
opts = null;
}
this.pkgFile( src, opts ).pipe( fs.createWriteStream( path.resolve( process.cwd(), dest ) ) );
},
// this will take a src and pipe it to stdout
pkgSync: function( src, opts ) {
return this.pkgFile( src, opts ).pipe( process.stdout );
}
};
module.exports = packagify;