-
-
Notifications
You must be signed in to change notification settings - Fork 17.5k
/
Copy pathgulpfile.ts
120 lines (111 loc) Β· 3.04 KB
/
gulpfile.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
import path from 'path'
import { Transform } from 'stream'
import chalk from 'chalk'
import { type TaskFunction, dest, parallel, series, src } from 'gulp'
import gulpSass from 'gulp-sass'
import dartSass from 'sass'
import autoprefixer from 'gulp-autoprefixer'
import rename from 'gulp-rename'
import consola from 'consola'
import postcss from 'postcss'
import cssnano from 'cssnano'
import { epOutput } from '@element-plus/build-utils'
import type Vinly from 'vinyl'
const distFolder = path.resolve(__dirname, 'dist')
const distBundle = path.resolve(epOutput, 'theme-chalk')
/**
* using `postcss` and `cssnano` to compress CSS
* @returns
*/
function compressWithCssnano() {
const processor = postcss([
cssnano({
preset: [
'default',
{
// avoid color transform
colormin: false,
// avoid font transform
minifyFontValues: false,
},
],
}),
])
return new Transform({
objectMode: true,
transform(chunk, _encoding, callback) {
const file = chunk as Vinly
if (file.isNull()) {
callback(null, file)
return
}
if (file.isStream()) {
callback(new Error('Streaming not supported'))
return
}
const cssString = file.contents!.toString()
processor.process(cssString, { from: file.path }).then((result) => {
const name = path.basename(file.path)
file.contents = Buffer.from(result.css)
consola.success(
`${chalk.cyan(name)}: ${chalk.yellow(
cssString.length / 1000
)} KB -> ${chalk.green(result.css.length / 1000)} KB`
)
callback(null, file)
})
},
})
}
/**
* compile theme-chalk scss & minify
* not use sass.sync().on('error', sass.logError) to throw exception
* @returns
*/
function buildThemeChalk() {
const sass = gulpSass(dartSass)
const noElPrefixFile = /(index|base|display)/
return src(path.resolve(__dirname, 'src/*.scss'))
.pipe(sass.sync())
.pipe(autoprefixer({ cascade: false }))
.pipe(compressWithCssnano())
.pipe(
rename((path) => {
if (!noElPrefixFile.test(path.basename)) {
path.basename = `el-${path.basename}`
}
})
)
.pipe(dest(distFolder))
}
/**
* Build dark Css Vars
* @returns
*/
function buildDarkCssVars() {
const sass = gulpSass(dartSass)
return src(path.resolve(__dirname, 'src/dark/css-vars.scss'))
.pipe(sass.sync())
.pipe(autoprefixer({ cascade: false }))
.pipe(compressWithCssnano())
.pipe(dest(`${distFolder}/dark`))
}
/**
* copy from packages/theme-chalk/dist to dist/element-plus/theme-chalk
*/
export function copyThemeChalkBundle() {
return src(`${distFolder}/**`).pipe(dest(distBundle))
}
/**
* copy source file to packages
*/
export function copyThemeChalkSource() {
return src(path.resolve(__dirname, 'src/**')).pipe(
dest(path.resolve(distBundle, 'src'))
)
}
export const build: TaskFunction = parallel(
copyThemeChalkSource,
series(buildThemeChalk, buildDarkCssVars, copyThemeChalkBundle)
)
export default build