-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile-dev.js
111 lines (105 loc) · 3.11 KB
/
gulpfile-dev.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
// const gulp = require("gulp")//老版的
const {src,dest,series,parallel,watch} = require("gulp")
const path = require("path")
const webpackStream = require("webpack-stream")
const gulpWebserver = require("gulp-webserver")
const gulpSass = require("gulp-sass")
const proxy = require("http-proxy-middleware")
const del = require("del")
//拷贝 index.html 到 dev
function copyhtml(){
return src("./*.html")
.pipe(dest("./dev"))
}
// gulp.task("copyhtml", (cb) => {
// gulp.src("./index.html")
// .pipe(gulp.dest("./dev"))
// cb()
// })
//新版必须有返回值 或cb 调用cb
//拷贝libs下的文件到dev
function copylibs(){
return src("./src/libs/**/*")
.pipe(dest("./dev/libs"))
}
function copyimages(){
return src("./src/images/**/*")
.pipe(dest("./dev/images"))
}
function copyicons(){
return src("./src/icons/**/*")
.pipe(dest("./dev/icons"))
}
//启动一个server
function webserver(){
return src("./dev")
.pipe(gulpWebserver({
port:8000,
livereload: true,
middleware: [
proxy("/api", {
target: "'https://m.lagou.com" ,
changeOrigin: true,//localhost代理别的域必须写
pathRewrite: {
"^/api": ""
}
})
]
}))
}
//编译js模块
function packJS(){
return src("./src/**/*")
.pipe(webpackStream({
mode: "development",
entry:{
app: "./src/app.js"
},
output: {
filename: "[name].js",//[name] == app
path: path.resolve(__dirname,"./dev")
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: "babel-loader",
options: {
presets: ["@babel/preset-env"],
plugins: ['@babel/plugin-transform-runtime']
}
}
},
{
test: /\.art$/,
loader: "string-loader"
}
]
}
}))
.pipe(dest("./dev/scripts"))
}
function clear(target){
return function(){
return del(target)
}
}
//编译css
function packCSS(){
return src('./src/styles/app.scss')
.pipe(gulpSass().on('error', gulpSass.logError))//抛出错误继续运行
.pipe(dest('./dev/styles'))
}
//watcher任务
function watcher(){
watch("./*.html",series(clear("./dev/*.html"),copyhtml))
watch("./src/libs/**/*",series(clear("./dev/libs"),copylibs))
watch("./src/images/**/*",series(clear("./dev/images"),copyimages))
watch("./src/icons/**/*",series(clear("./dev/icons"),copyicons))
watch("./src/styles/**/*",series(packCSS))
watch(["./src/**/*","!src/icons/**/*","!src/images/**/*","!src/libs/**/*","!src/styles/**/*"],series(packJS))
}
// gulp.task("default", series(packjs, copyhtml, webserver))//串行
exports.default = series(parallel(packJS, packCSS, copylibs, copyimages, copyicons), copyhtml, webserver, watcher)