-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuildcss.js
More file actions
79 lines (70 loc) · 2.35 KB
/
buildcss.js
File metadata and controls
79 lines (70 loc) · 2.35 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
// npm install less
// npm install less-plugin-clean-css
// ./node_modules/less/bin/lessc --rootpath=/css/build/ --relative-urls --clean-css="--s1" build/bootstrap.less build/main.min.css
// const buildcss = require('buildcss')
// buildcss(srcPath, dstPath, overwrite, extFilter, options)
// buildcss('_posts', '_site', true)
const less = require('less')
const fs = require('fs')
const path = require('path')
const CleanCSS = require('clean-css')
const mm = require('micromatch')
const options = {
srcPath: './',
dstPath: './',
overwrite: false,
extFilter: ['*.less'],
// filename: path.join(__dirname, 'build/bootstrap.less'),
// filename: path.join(__dirname, 'build/bootstrap-vita.less'),
// outFile: path.join('build/', 'main.min.css'),
// outFile: path.join('build/', 'vita.css'),
// rootpath: '/css/build/',
relativeUrls: true,
matchBase: false,
ignore: ['.git', 'node_modules', 'README.md', 'test', 'lib', 'css', 'bin']
}
// getFile: reads utf8 content from a file
const readFile = (fileName) => new Promise((resolve, reject) => {
fs.readFile(fileName, 'utf8', (err, data) => {
if (err) {
return reject(err)
}
resolve(data)
})
})
const writeFile = (fileName, datain) => new Promise((resolve, reject) => {
fs.writeFile(fileName, datain, 'utf8', (err, data) => {
if (err) reject(err)
else resolve(data)
})
})
const readdir = (srcPath) => new Promise((resolve, reject) => {
fs.readdir(srcPath, (err, files) => {
if (err) reject(err)
else resolve(files)
})
})
const processFiles = (options) => {
//async process chain begin
readdir(options.srcPath)
.then(allfiles => { return mm(allfiles,options.extFilter,options) })
.then(files => {
files.forEach(file => {
readFile(path.join(options.srcPath,file))
.then(str => { return less.render(str, options) })
.then( data => {
// new CleanCSS({level: 1, returnPromise: true, format: 'beautify'})
new CleanCSS({level: 1, returnPromise: true})
.minify(data.css)
.then(output => {
writeFile(path.join(options.dstPath, path.parse(file).name + '.min.css'), output.styles)
// console.log(data.imports)
})
.catch (err => { console.log(err) })
})
.catch (err => { console.log(err) })
})
})
.catch (err => { console.log(err) })
}
processFiles(options)