-
Notifications
You must be signed in to change notification settings - Fork 138
/
less.ts
130 lines (109 loc) Β· 5.17 KB
/
less.ts
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import path from 'path'
import { copy, copyFile, pathExists, readFile, readdirSync, remove, writeFile } from 'fs-extra'
// @ts-ignore
import less from 'less'
import { gulpConfig } from '../gulpConfig'
const { themes } = gulpConfig.build
export async function compile(content: string, savePath: string, rootPath?: string): Promise<void> {
const plugins: any[] = []
const lessOptions: any = { plugins, javascriptEnabled: true }
if (rootPath) {
lessOptions.paths = [path.dirname(rootPath)]
lessOptions.filename = rootPath
}
return less
.render(content, lessOptions)
.then(({ css }: any) => writeFile(savePath, css))
.catch((err: any) => Promise.reject(err))
}
function compileTheme(content: string, saveDirname: string, themeName: string) {
const regExp = new RegExp(themeName, 'g')
return [
writeFile(`${saveDirname}/${themeName}.js`, content),
writeFile(
`${saveDirname}/${themeName}_css.js`,
content.replace(regExp, `${themeName}_css`).replace(/_css\.less/g, '.css'),
),
remove(`${saveDirname}/${themeName}.ts`),
]
}
export async function compileLess(
targetDirname: string,
distDirname: string,
packageName: string,
): Promise<void | void[]> {
const promiseList = []
const copyAndCompile = async (styleDirname: string, outputDirname: string, themeName: string) => {
if (await pathExists(styleDirname)) {
// Copy style files for each component.
await copy(styleDirname, outputDirname)
const lessPath = `${styleDirname}/themes/${themeName}.less`
const lessContent = await readFile(lessPath, { encoding: 'utf8' })
promiseList.push(compile(lessContent, path.join(outputDirname, `themes/${themeName}.css`), lessPath))
const themePath = `${styleDirname}/themes/${themeName}.ts`
const themeContent = await readFile(themePath, { encoding: 'utf8' })
promiseList.push(...compileTheme(themeContent, `${outputDirname}/themes`, themeName))
}
}
for (const componentDirname of readdirSync(targetDirname)) {
// handler cdk
if (packageName === 'cdk') {
const styleDirname = `${targetDirname}/${componentDirname}/style`
const outputDirname = `${distDirname}/${componentDirname}/style`
if (await pathExists(styleDirname)) {
// Copy style files for each component.
await copy(styleDirname, outputDirname)
const lessPath = `${styleDirname}/index.less`
const lessContent = await readFile(lessPath, { encoding: 'utf8' })
promiseList.push(compile(lessContent, path.join(outputDirname, `index.css`), lessPath))
}
// handler private components
} else if (componentDirname === '_private') {
for (const privateComponentName of readdirSync(path.resolve(targetDirname, componentDirname))) {
const styleDirname = `${targetDirname}/${componentDirname}/${privateComponentName}/style`
const outputDirname = `${distDirname}/${componentDirname}/${privateComponentName}/style`
for (const theme of themes) {
await copyAndCompile(styleDirname, outputDirname, theme)
}
}
} else {
const styleDirname = `${targetDirname}/${componentDirname}/style`
const outputDirname = `${distDirname}/${componentDirname}/style`
for (const theme of themes) {
await copyAndCompile(styleDirname, outputDirname, theme)
}
}
}
if (packageName === 'cdk') {
await copyFile(`${targetDirname}/index.less`, `${distDirname}/index.less`)
const entryContent = `@import "${path.posix.join(distDirname, 'index.less')}";`
promiseList.push(compile(entryContent, path.join(distDirname, `index.css`)))
} else {
// Compile concentrated less file to CSS file.
await copy(path.resolve(targetDirname, 'style'), path.resolve(distDirname, 'style'))
for (const theme of themes) {
await copyFile(`${targetDirname}/${theme}.less`, `${distDirname}/${theme}.less`)
const entryContent = `@import "${path.posix.join(distDirname, theme + '.less')}";`
promiseList.push(compile(entryContent, path.join(distDirname, `${theme}.css`)))
// Compile css file that doesn't have component-specific styles.
if (packageName === 'components') {
await copyFile(`${targetDirname}/${theme}.full.less`, `${distDirname}/${theme}.full.less`)
const entryFullContent = `@import "${path.posix.join(distDirname, theme + '.full.less')}";`
promiseList.push(compile(entryFullContent, path.join(distDirname, `${theme}.full.css`)))
const resetPath = path.join(targetDirname, 'style/core', `reset.${theme}.less`)
const resetContent = await readFile(resetPath, { encoding: 'utf8' })
promiseList.push(compile(resetContent, path.join(distDirname, 'style/core', `reset.${theme}.css`), resetPath))
const resetScrollPath = path.join(targetDirname, 'style/core', `reset-scroll.${theme}.less`)
const resetScrollContent = await readFile(resetScrollPath, { encoding: 'utf8' })
promiseList.push(
compile(
resetScrollContent,
path.join(distDirname, 'style/core', `reset-scroll.${theme}.css`),
resetScrollPath,
),
)
}
}
}
return Promise.all(promiseList).catch(e => console.log(e))
}