forked from Sight-wcg/layui-theme-dark
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathgulpfile.js
More file actions
72 lines (63 loc) · 1.85 KB
/
Copy pathgulpfile.js
File metadata and controls
72 lines (63 loc) · 1.85 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
import gulp from 'gulp';
import concat from 'gulp-concat';
import less from 'gulp-less';
import rename from 'gulp-rename';
import sourcemaps from 'gulp-sourcemaps';
import yargs from 'yargs';
import { Transform } from 'node:stream';
const { src, dest } = gulp;
// 命令行参数
const argv = yargs.default({
selector: '.dark' // 自定义选择器
}).help().argv;
// 文件名
const fileName = 'layui-theme-dark';
const commentRE = /\/\*[^*]*\*+([^/][^*]*\*+)*\//g;
/**
* 生成带自定义选择器的主题库
*/
export function buildSelector() {
return src('src/*.css')
.pipe(sourcemaps.init())
.pipe(
new Transform({
objectMode: true,
transform(chunk, enc, callback) {
if (chunk.isNull()) {
return callback(null, chunk);
}
const selector = argv.selector;
const basename = chunk.basename;
let contents = chunk.contents.toString();
contents = basename === 'css-variables.css'
? contents.replace(/:root/g, `:root${selector}`)
: `${selector}{${contents}}`;
contents = contents.replace(commentRE, '').trim();
chunk.contents = Buffer.from(contents);
callback(null, chunk);
}
})
)
.pipe(less())
.pipe(concat('full.css', { newLine: '' }))
.pipe(rename({ basename: `${fileName}-selector` }))
.pipe(sourcemaps.write('.'))
.pipe(dest('./dist'));
}
/**
* Build
* @example
* # 生成选择器为 .dark 的主题库
* gulp build --selector .dark
*/
export const build = gulp.parallel(buildSelector, () => {
return src('src/*.css')
.pipe(sourcemaps.init())
.pipe(concat('full.css', { newLine: '' }))
.pipe(rename({ basename: fileName }))
.pipe(sourcemaps.write('.'))
.pipe(dest('./dist'))
});
export function watch() {
gulp.watch('src/*.css', gulp.series(['build']));
}