-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathindex.js
More file actions
65 lines (56 loc) · 1.62 KB
/
index.js
File metadata and controls
65 lines (56 loc) · 1.62 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
'use strict'
var css = require('./css')
, less = require('./less')
, fs = require('fs')
, path = require('path')
, mkdirp = require('mkdirp')
, writer = require('write-to-path')
, autoprefixer = require('autoprefixer-core')
, postcss = require('postcss')
module.exports = function atomifyCSS (opts, cb) {
if (typeof opts === 'string') opts = {entry: opts}
if (typeof cb === 'string') opts.output = cb
if (opts.entry) opts.entries = [opts.entry]
if (opts.entry && opts.entry.substr(-4) === 'less') {
less(opts, complete)
}
else {
css(opts, complete)
}
function complete (err, src) {
var outputPath
, outputDir
, writeFile
, _cb
if (opts.transform && !err) src = opts.transform(src)
if (opts.autoprefixer && !err) {
try {
src = postcss([autoprefixer(typeof opts.autoprefixer === 'object' ? opts.autoprefixer : null)])
.process(src).css
}
catch (e){
err = e
}
}
if (opts.output) {
// we definitely have to write the file
outputPath = path.resolve(process.cwd(), opts.output)
outputDir = path.dirname(outputPath)
writeFile = writer(outputPath, {debug: opts.debug})
if (!fs.existsSync(outputDir)) mkdirp.sync(outputDir)
// we might need to call a callback also
if (typeof cb === 'function') {
_cb = cb
cb = function callbackWrapper (wrapperErr, wrapperSrc) {
if (wrapperErr) return _cb(wrapperErr)
writeFile(null, wrapperSrc)
_cb(null, wrapperSrc)
}
}
else {
cb = writeFile
}
}
cb(err, src)
}
}