forked from jdf2e/nutui-react
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathreplace-css-var.js
More file actions
64 lines (58 loc) · 1.7 KB
/
replace-css-var.js
File metadata and controls
64 lines (58 loc) · 1.7 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
/*
* 通过 dist 目录下的 style/index.js 构建每个组件的 css 文件
* */
const path = require('path')
const fs = require('fs')
const postcss = require('postcss')
const sass = require('sass')
const cssvariables = require('postcss-css-variables')
const config = require('../src/config.json')
const components = config.nav.reduce(
(prev, nav) => [...prev, ...nav.packages],
[],
)
const mixin = fs
.readFileSync(path.join(__dirname, '../src/styles/mixins/text-ellipsis.scss'))
.toString()
const variables = fs.readFileSync(
path.join(__dirname, '../src/styles/variables.scss'),
).toString()
const theme = fs.readFileSync(
path.join(__dirname, '../src/styles/theme-default.scss'),
).toString().replace('@import "./jd-font";', '').replace(`@import './jd-font';`, '')
const exclude = ['icon']
components.forEach((component) => {
const componentName = component.name.toLowerCase()
if (exclude.includes(componentName)) return
let content = fs
.readFileSync(
path.join(
__dirname,
`../src/packages/${componentName}/${componentName}.scss`,
),
)
.toString()
let to = path.join(
__dirname,
`../src/packages/${componentName}/${componentName}.harmony.css`,
)
const matched = content.match(/@import.*?[;][\n\r]?/gi)
if (matched) {
matched.forEach((m) => {
if (m.indexOf('styles') > -1) {
content = content.replace(m, mixin)
} else {
content = content.replace(m, '')
}
})
}
const res = sass.compileString(theme + variables + content)
postcss([
cssvariables(/*options*/),
])
.process(res.css, { from: undefined, to })
.then((result) => {
fs.writeFile(to, result.css, () => {
})
})
})