-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathindex.js
80 lines (69 loc) · 2.02 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
79
80
const postcss = require('postcss')
const wxssPlugin = require('postcss-wxss')
const clean = require('postcss-clean')
const fs = require('fs')
const path = require('path')
// Todo: ignore imports in comments
const importRegx = /@import\s+(?:'(.+\.wxss)'|"(.+\.wxss)");?/g
let fileStack = []
/**
* construct fullpath from path url
*
* @param {string} file
* @returns {string} fullPath
*/
const constructPath = function (file) {
let dir
const isAbsolutePath = path.isAbsolute(file)
const lastFile = fileStack.slice(-1)[0]
if (lastFile && !isAbsolutePath) {
dir = path.dirname(lastFile)
} else {
dir = process.cwd()
}
const fullPath = isAbsolutePath
? path.join(dir, file)
: path.resolve(dir, file)
if (fileStack.indexOf(fullPath) === -1) {
fileStack.push(fullPath)
} else {
console.log('Import Stack:')
console.log(fileStack)
throw new Error('Circular Imported File: ' + fullPath)
}
return fullPath
}
/**
* getContent from first file and insert templates into it
*
* @param {string} file
* @returns {string} fileContent
*/
const getContent = function (file) {
const content = fs.readFileSync(constructPath(file))
return content.toString().replace(importRegx, function (str, p1, p2) {
const childPath = p1 || p2
const res = getContent(childPath)
fileStack.pop()
return res
})
}
module.exports = function (files, opts) {
fileStack = []
const fileList = files.slice()
const initFile = fileList.shift()
const cssFileContent = getContent(initFile)
const isAbsolutePath = path.isAbsolute(initFile)
const dir = process.cwd()
const initFilePath = isAbsolutePath
? path.join(dir, initFile)
: path.resolve(dir, initFile)
// remove "//" to remove single line comment, default: true
const cssSource = opts && opts.keepSlash
? cssFileContent
: cssFileContent.replace(/\/\//g, ' ')
return postcss([clean(), wxssPlugin(opts)])
.process(cssSource, { from: initFilePath })
.then(res => res.css)
.catch(err => console.log('Postcss Error:', err))
}