This repository was archived by the owner on Jan 18, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 150
/
Copy pathindex.js
78 lines (62 loc) · 1.8 KB
/
index.js
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
import { writeFile, mkdirSync as mkdir, existsSync as exists } from 'fs'
import { dirname, isAbsolute, resolve as resolvePath } from 'path'
import compileCSS from './css'
import compileSCSS from './scss'
import compileLESS from './less/index'
import compileSTYLUS from './stylus'
const compilers = {
scss: compileSCSS,
sass: compileSCSS,
less: compileLESS,
stylus: compileSTYLUS
}
export async function compile (style, options) {
let output
if (style.lang === 'css') {
output = await compileCSS(style, options)
} else {
output = await compileCSS(await compilers[style.lang].call(null, style, options), options)
}
return output
}
function ensureDirectory (directory) {
if (!exists(directory)) {
ensureDirectory(dirname(directory))
mkdir(directory)
}
}
/* eslint-disable complexity */
export default function (files, options) {
if (typeof (options.css) === 'boolean') {
return
}
// Combine all stylesheets.
let css = ''
const allStyles = []
Object.keys(files).forEach((file) => {
files[file].forEach((style) => {
css += ('$compiled' in style) ? `${style.$compiled.code}\n` : `${style.code}\n`
allStyles.push(style)
})
})
// Emit styles through callback
if (typeof (options.css) === 'function') {
options.css(css, allStyles, compile)
return
}
// Don't generate empty style file.
if (!css.trim().length) {
return
}
let dest = options.css
if (typeof dest !== 'string') {
return
}
dest = isAbsolute(dest) ? dest : resolvePath(process.cwd(), dest)
// Emit styles to file
ensureDirectory(dirname(dest))
writeFile(dest, css, (err) => {
if (err) throw err
})
}
/* eslint-enable complexity */