-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
120 lines (112 loc) · 3.8 KB
/
gulpfile.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
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
const path = require("path");
const { series, parallel } = require("gulp");
const { execSync } = require('child_process');
var shell = require('shelljs');
const gulp = require("gulp");
const argv = require('minimist')(process.argv.slice(2));
const babel = require("gulp-babel");
const rimraf = require("rimraf");
const webpack = require("webpack");
const through2 = require('through2');
const merge2 = require("merge2");
const transformLess = require('./utils/transformLess');
const getBabelCommonConfig = require("./utils/getBabelCommonConfig");
const cwd = process.cwd();
const libDir = path.join.apply(path, [cwd, "lib/"]);
const esDir = path.join.apply(path, [cwd, "es/"]);
const getProjectPath = require("./utils/getProjectPath");
const packageJson = require(getProjectPath('package.json'));
const runCmd = require("./utils/runCmd");
const checkDiff = require("./utils/checkDiff");
// 在Babel中默认为modules,只有明确设置为false才会关闭
function compile(modules) {
rimraf.sync(modules === false ? esDir : libDir);
// 移动less文件并转译less文件成css文件
const less = gulp.src(['components/**/*.less', 'components/*.less', 'components/**/**/*.less']).pipe(
through2.obj(function (file, encoding, next) {
this.push(file.clone());
transformLess(file.path)
.then(css => {
file.contents = Buffer.from(css);
// convert less files into css files
file.path = file.path.replace(/\.less$/, '.css');
this.push(file);
next();
}).catch(err => {
console.error(err);
})
})
).pipe(gulp.dest(modules === false ? esDir : libDir));
// clone svg files into target diractories
const svg = gulp.src(['components/**/*.svg']).pipe(
through2.obj(function (file, encoding, next) {
this.push(file.clone())
})
).pipe(gulp.dest(modules === false ? esDir : libDir));
const resource = ['components/**/*.jsx', 'components/*.js', 'components/**/*.js'];
let jscode = gulp.src(resource);
// compile Javascript files
let jsStream = babelify(jscode, modules);
return merge2([less, jsStream, svg]);
};
function babelify(js, modules) {
const babelConfig = getBabelCommonConfig(modules);
let stream = js.pipe(babel(babelConfig)).pipe(
through2.obj(function (file, encoding, next) {
this.push(file.clone());
const content = file.contents.toString(encoding);
let newContent = content.replace(/\.less/g, '.css')
file.contents = Buffer.from(newContent);
this.push(file)
next();
})
);
return stream.pipe(gulp.dest(modules === false ? esDir : libDir))
}
gulp.task("compile-es", done => {
console.log("生成es文件夹...");
compile(false);
done()
});
gulp.task("compile-lib", done => {
console.log("生成lib文件夹...");
compile()
done()
});
/**
* 发布NPM新版本
* npm run pub --npm-tag=0.2.1
* */
gulp.task("pub", done => {
// 匹配当前版本号
let { version } = packageJson;
const versionFine = version.match(/^\d+\.\d+\.\d+$/);
let tagString;
if (argv['npm-tag']) {
tagString = argv['npm-tag'];
}
if (!tagString && versionFine) {
let args = ['publish'].concat(['--tag', tagString])
shell.exec(`npm publish --tag=${tagString}`);
shell.exec(`git tag ${version}`);
shell.exec('git push origin master:master');
done();
} else {
shell.exec(`npm publish`);
shell.exec(`git tag ${version}`);
shell.exec('git push origin master:master');
done();
}
})
gulp.task("compile", series(parallel("compile-es", "compile-lib")));
gulp.task("diff", series(done => {
let { name, version } = packageJson;
checkDiff(name, version, done)
}));
exports.compile = gulp.task("compile");
exports.pub = gulp.task("pub");
exports.diff = gulp.task("diff");
exports.default = (done) => {
console.log("请指定gulp任务");
done();
};