Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathindex.js
More file actions
Latest commit
73 lines (60 loc) · 2.06 KB
/
Copy pathindex.js
File metadata and controls
73 lines (60 loc) · 2.06 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
conststaticModule=require('static-module')
constfrom2=require('from2-string')
constthrough=require('through2')
constassert=require('assert')
constd=require('defined')
constbl=require('bl')
constfs=require('fs')
module.exports=cssExtract
// Extract CSS from a browserify bundle
// obj -> null
functioncssExtract(bundle,opts){
opts=opts||{}
varoutFile=opts.out||opts.o||'bundle.css'
varsourceMap=d(opts.sourceMap,bundle&&bundle._options&&bundle._options.debug,false)
assert.strictEqual(typeofbundle,'object','bundle should be an object')
assert.strictEqual(typeofopts,'object','opts should be an object')
// every time .bundle is called, attach hook
bundle.on('reset',addHooks)
addHooks()
functionaddHooks(){
constextractStream=through.obj(write,flush)
constwriteStream=(typeofoutFile==='function')
? outFile()
: bl(writeComplete)
// run before the "label" step in browserify pipeline
bundle.pipeline.get('label').unshift(extractStream)
functionwrite(chunk,enc,cb){
// Performance boost: don't do ast parsing unless we know it's needed
if(!/(insert-css|sheetify\/insert)/.test(chunk.source)){
returncb(null,chunk)
}
varsource=from2(chunk.source)
varsm=staticModule({
'insert-css': function(src){
writeStream.write(String(src)+'\n')
returnfrom2('null')
},
'sheetify/insert': function(src){
writeStream.write(String(src)+'\n')
returnfrom2('null')
}
},{sourceMap: sourceMap})
source.pipe(sm).pipe(bl(complete))
functioncomplete(err,source){
if(err)returnextractStream.emit('error',err)
chunk.source=String(source)
cb(null,chunk)
}
}
// close stream and signal end
functionflush(cb){
writeStream.end()
cb()
}
functionwriteComplete(err,buffer){
if(err)returnextractStream.emit('error',err)
fs.writeFileSync(outFile,buffer)
}
}
}