forked from Tencent/wepy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
118 lines (106 loc) · 4.56 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
/**
* Tencent is pleased to support the open source community by making WePY available.
* Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
* http://opensource.org/licenses/MIT
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*/
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');
// for docs
var gulp = require('gulp'),
less = require('gulp-less');
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 docs = ['docs/less/**/*.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('doc-watch', function () {
gulp.src(['docs/less/main.less', 'docs/less/donate.less'])
.pipe(less())
.pipe(gulp.dest('docs/css'));
})
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', 'doc-watch'], function (callback) {
watch(scripts, {debounceDelay: 200}, function () {
gulp.start("build-watch");
});
watch(docs, {debounceDelay: 200}, function () {
gulp.start("doc-watch");
});
});