-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathgulpfile.js
243 lines (231 loc) · 5.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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
var gulp = require("gulp");
var path = require("path");
var rename = require("gulp-rename");
var less = require("gulp-less");
var postcss = require("gulp-postcss");
var changed = require("gulp-changed");
var autoprefixer = require("autoprefixer");
var clear = require("gulp-clean");
var del = require("del");
var ts = require("gulp-typescript");
var tsProject = ts.createProject("tsconfig.json");
var sourcemaps = require("gulp-sourcemaps");
var jsonTransform = require("gulp-json-transform");
var projectConfig = require("./package.json");
// 支持 sass
var sass = require("gulp-sass");
sass.compiler = require("node-sass");
//项目路径
var option = {
base: "src",
allowEmpty: true
};
var dist = __dirname + "/dist";
var copyPath = [
"src/**/!(_)*.*",
"!src/**/*.less",
"!src/**/*.sass",
"!src/**/*.scss",
"!src/**/*.ts"
];
var lessPath = ["src/**/*.less", "src/app.less"];
var watchLessPath = ["src/**/*.less", "src/css/**/*.less", "src/app.less"];
var sassPath = [
"src/**/*.sass",
"src/app.sass",
"src/**/*.scss",
"src/app.scss"
];
var watchSassPath = [
"src/**/*.sass",
"src/app.sass",
"src/**/*.scss",
"src/app.scss"
];
var tsPath = ["src/**/*.ts", "src/app.ts"];
//清空目录
gulp.task("clear", () => {
return gulp.src(dist, { allowEmpty: true }).pipe(clear());
});
//复制不包含less和图片的文件
gulp.task("copy", () => {
return gulp.src(copyPath, option).pipe(gulp.dest(dist));
});
//复制不包含less和图片的文件(只改动有变动的文件)
gulp.task("copyChange", () => {
return gulp
.src(copyPath, option)
.pipe(changed(dist))
.pipe(gulp.dest(dist));
});
// 增加dependencies
var dependencies = projectConfig && projectConfig.dependencies; // dependencies配置
var nodeModulesCopyPath = [];
for (let d in dependencies) {
nodeModulesCopyPath.push("node_modules/" + d + "/**/*");
}
//项目路径
var copyNodeModuleOption = {
base: ".",
allowEmpty: true
};
//复制依赖的node_modules文件
gulp.task("copyNodeModules", () => {
return gulp
.src(nodeModulesCopyPath, copyNodeModuleOption)
.pipe(gulp.dest(dist));
});
//复制依赖的node_modules文件(只改动有变动的文件)
gulp.task("copyNodeModulesChange", () => {
return gulp
.src(nodeModulesCopyPath, copyNodeModuleOption)
.pipe(changed(dist))
.pipe(gulp.dest(dist));
});
// 根据denpende生成package.json
gulp.task("generatePackageJson", () => {
return gulp
.src("./package.json")
.pipe(
jsonTransform(function(data, file) {
return {
dependencies: dependencies
};
})
)
.pipe(gulp.dest(dist));
});
//编译less
gulp.task("less", () => {
return gulp
.src(lessPath, option)
.pipe(
less().on("error", function(e) {
console.error(e.message);
this.emit("end");
})
)
.pipe(postcss([autoprefixer]))
.pipe(
rename(function(path) {
path.extname = ".wxss";
})
)
.pipe(gulp.dest(dist));
});
//编译less(只改动有变动的文件)
gulp.task("lessChange", () => {
return gulp
.src(lessPath, option)
.pipe(changed(dist))
.pipe(
less().on("error", function(e) {
console.error(e.message);
this.emit("end");
})
)
.pipe(postcss([autoprefixer]))
.pipe(
rename(function(path) {
path.extname = ".wxss";
})
)
.pipe(gulp.dest(dist));
});
// 编译sass
gulp.task("sass", () => {
return gulp
.src(sassPath, option)
.pipe(
sass().on("error", function(e) {
console.error(e.message);
this.emit("end");
})
)
.pipe(postcss([autoprefixer]))
.pipe(
rename(function(path) {
path.extname = ".wxss";
})
)
.pipe(gulp.dest(dist));
});
// 编译sass(只改动有变动的文件)
gulp.task("sassChange", () => {
return gulp
.src(sassPath, option)
.pipe(changed(dist))
.pipe(
sass().on("error", function(e) {
console.error(e.message);
this.emit("end");
})
)
.pipe(postcss([autoprefixer]))
.pipe(
rename(function(path) {
path.extname = ".wxss";
})
)
.pipe(gulp.dest(dist));
});
// 编译
gulp.task("tsCompile", function() {
return tsProject
.src()
.pipe(sourcemaps.init())
.pipe(tsProject())
.js.pipe(sourcemaps.write())
.pipe(gulp.dest("dist"));
});
//监听
gulp.task("watch", () => {
gulp.watch(tsPath, gulp.series("tsCompile"));
var watcher = gulp.watch(copyPath, gulp.series("copyChange"));
gulp.watch(nodeModulesCopyPath, gulp.series("copyNodeModulesChange"));
gulp.watch(watchLessPath, gulp.series("lessChange")); //Change
gulp.watch(watchSassPath, gulp.series("sassChange")); //Change
watcher.on("change", function(event) {
if (event.type === "deleted") {
var filepath = event.path;
var filePathFromSrc = path.relative(path.resolve("src"), filepath);
// Concatenating the 'build' absolute path used by gulp.dest in the scripts task
var destFilePath = path.resolve("dist", filePathFromSrc);
// console.log({filepath, filePathFromSrc, destFilePath})
del.sync(destFilePath);
}
});
});
//开发并监听
gulp.task(
"default",
gulp.series(
// sync
gulp.parallel(
"copy",
"copyNodeModules",
"generatePackageJson",
"less",
"sass",
"tsCompile"
),
"watch"
)
);
//上线
gulp.task(
"build",
gulp.series(
// sync
"clear",
gulp.parallel(
// async
"copy",
"copyNodeModules",
"generatePackageJson",
"less",
"sass",
"tsCompile"
)
)
);