Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathindex.js
More file actions
Latest commit
129 lines (111 loc) · 3.92 KB
/
Copy pathindex.js
File metadata and controls
129 lines (111 loc) · 3.92 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
constcssPrefix=require('postcss-prefix')
constnodeResolve=require('resolve')
constmapLimit=require('map-limit')
constpostcss=require('postcss')
constassert=require('assert')
constcrypto=require('crypto')
conststackTrace=require('stack-trace')
constcssResolve=require('style-resolve').sync
constfs=require('fs')
constpath=require('path')
module.exports=sheetify
module.exports.getPrefix=getPrefix
// transform css
// (str, str, obj?, fn) -> str
functionsheetify(src,filename,options,done){
// browserify transform
if(typeofsrc==='string'&&!/\n/.test(src)&&filename&&filename._flags){
varargs=Array.prototype.slice.apply(arguments)
returnrequire('./transform.js').apply(this,args)
}
// handle tagged template calls directly from Node
constisTemplate=Array.isArray(src)
if(isTemplate)src=src.join('')
assert.equal(typeofsrc,'string','src must be a string')
src=src.trim()
// Ensure prefix is always correct when run from inside node
varcss
if(!isTemplate&&(!filename||typeoffilename==='object')){
// module or file name via tagged template call w or w/out options
constcallerDirname=path.dirname(stackTrace.get()[1].getFileName())
constresolved=cssResolve(src,{basedir: callerDirname})
css=fs.readFileSync(resolved,'utf8')
}else{
// it better be some css
css=src
}
constprefix=getPrefix(css)
// only parse if in a browserify transform
if(typeoffilename==='string')parseCss(src,filename,prefix,options,done)
returnprefix
}
functiongetPrefix(css){
constprefix='_'+crypto.createHash('md5')
.update(css.trim())
.digest('hex')
.slice(0,8)
returnprefix
}
// parse css
// (str, str, str, obj, fn) -> null
functionparseCss(src,filename,prefix,options,done){
assert.equal(typeoffilename,'string','filename must be a string')
assert.equal(typeofprefix,'string','prefix must be a string')
assert.equal(typeofoptions,'object','options must be a object')
assert.equal(typeofdone,'function','done must be a function')
applyTransforms(filename,String(src),Object.assign({},options),function(err,result){
if(err)returndone(err)
varp=postcss()
p=p.use(cssPrefix('.'+prefix))
try{
result.css=p.process(result.css).toString()
returndone(null,result,prefix)
}catch(e){
returndone(e)
}
})
// apply transforms to a string of css,
// one at the time
// (str, str, obj, fn) -> null
functionapplyTransforms(filename,src,options,done){
varcurrent={css: src,files: []}
mapLimit(options.transform,1,iterate,function(err){
if(err)returndone(err)
done(null,current)
})
// find and apply a transform to a string of css
// (fn, fn) -> null
functioniterate(plugin,next){
if(typeofplugin==='string'||typeofplugin==='function'){
plugin=[plugin,{}]
}elseif(!Array.isArray(plugin)){
returndone(newError('Plugin must be a string or array'))
}
constname=plugin[0]
constopts=plugin[1]||{}
if(typeofname==='function'){
returntransformLoaded(name,opts)
}
constresolveOpts={
basedir: opts.basedir||options.basedir||process.cwd()
}
nodeResolve(name,resolveOpts,function(err,transformPath){
if(err)returndone(err)
consttransform=require(transformPath)
transformLoaded(transform,opts)
})
functiontransformLoaded(transform,opts){
transform(filename,current.css,opts,function(err,result){
if(err)returnnext(err)
if(typeofresult==='string'){
current.css=result
}else{
current.css=result.css
current.files=current.files.concat(result.files)
}
next()
})
}
}
}
}