forked from Tencent/wepy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
95 lines (86 loc) · 3.53 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
var fs = require('fs');
var path = require("path");
var watch = require("gulp-watch");
var newer = require('gulp-newer');
var lec = require('gulp-line-ending-corrector');
var plumber = require("gulp-plumber");
var babel = require("gulp-babel");
var gutil = require("gulp-util");
var through = require("through2");
var gulp = require("gulp");
var chalk = require("chalk");
var gulpif = require('gulp-if');
var mkdirp = require('mkpath');
var scripts = ['./packages/*/src/**/*.js', './packages/wepy-web/src/components/*.vue', './packages/wepy-web/src/apis/*.vue', './packages/wepy-web/src/apis/*.js', './packages/wepy-web/src/components/styles/*/*.less'];
var bins = "./packages/*/bin/**/*";
var srcEx, libFragment;
if (path.win32 === path) {
srcEx = /(packages\\[^\\]+)\\src\\/;
libFragment = "$1\\lib\\";
} else {
srcEx = new RegExp("(packages/[^/]+)/src/");
libFragment = "$1/lib/";
}
var mapToDest = function (path) { return path.replace(srcEx, libFragment); };
var dest = "packages";
gulp.task("default", ["build"]);
gulp.task('lec', function () {
return gulp.src(bins, { base: "./" }).pipe(lec({eolc: 'LF', encoding:'utf8'})).pipe(gulp.dest('.'));
});
gulp.task("build", ['lec'], function () {
return gulp.src(scripts)
.pipe(plumber({
errorHandler: function (err) {
gutil.log(err.stack);
}
}))
.pipe(newer({map: mapToDest}))
.pipe(through.obj(function (file, enc, callback) {
file._path = file.path;
file.path = file.path.replace(srcEx, libFragment);
callback(null, file);
}))
.pipe(gulpif((file) => !/\.vue|\.less/.test(path.parse(file.path).ext), through.obj(function (file, enc, callback) {
gutil.log("Compiling", "'" + chalk.cyan(file.path) + "'...");
callback(null, file);
})))
// If it's vue, then copy only, else babel it.
.pipe(gulpif((file) => /\.vue|\.less/.test(path.parse(file.path).ext), through.obj(function (file, enc, callback) {
gutil.log("Copy Vue Component", "'" + chalk.cyan(file._path) + "'...");
mkdirp.sync(path.parse(file.path).dir);
fs.createReadStream(file._path).pipe(fs.createWriteStream(file.path));
callback(null, file);
}), babel()))
.pipe(gulp.dest(dest));
});
gulp.task("build-watch", function () {
return gulp.src(scripts)
.pipe(plumber({
errorHandler: function (err) {
gutil.log(err.stack);
}
}))
.pipe(through.obj(function (file, enc, callback) {
file._path = file.path;
file.path = file.path.replace(srcEx, libFragment);
callback(null, file);
}))
.pipe(newer(dest))
.pipe(gulpif((file) => !/\.vue|\.less/.test(path.parse(file.path).ext), through.obj(function (file, enc, callback) {
gutil.log("Compiling", "'" + chalk.cyan(file._path) + "'...");
callback(null, file);
})))
// If it's vue, then copy only, else babel it.
.pipe(gulpif((file) => /\.vue|\.less/.test(path.parse(file.path).ext), through.obj(function (file, enc, callback) {
gutil.log("Copy Vue Component", "'" + chalk.cyan(file._path) + "'...");
mkdirp.sync(path.parse(file.path).dir);
fs.createReadStream(file._path).pipe(fs.createWriteStream(file.path));
callback(null, file);
}), babel()))
.pipe(gulp.dest(dest));
});
gulp.task("watch", ["build-watch"], function (callback) {
watch(scripts, {debounceDelay: 200}, function () {
gulp.start("build-watch");
});
});