-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile-dev.js
70 lines (57 loc) · 1.97 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
const gulp = require('gulp');
const server = require('gulp-webserver');
const sass = require('gulp-sass');
const webpack = require('webpack-stream');
const watch = require('gulp-watch');
const del = require('del');
// 全局的配置
const { dev_config } = require('./config');
// server_config 服务配置
const { server_config, sass_config, webpack_config } = dev_config;
// 开启热更新服务器
gulp.task('server', () => {
return gulp.src('./dev')
.pipe(server(server_config))
})
// 输出静态文件
gulp.task('copy:static', () => {
return gulp.src('./src/static/**/*.*')
.pipe(gulp.dest('./dev/static'));
})
// 输出html页面
gulp.task('copy:html', () => {
return gulp.src('./src/**/*.html')
.pipe(gulp.dest('./dev/'));
})
// 处理scss
gulp.task('compile:scss', () => {
console.log('handle scss')
return gulp.src('./src/stylesheets/*.scss')
.pipe(sass(sass_config).on('error', sass.logError))
.pipe(gulp.dest('./dev/stylesheets'));
})
// 模块化打包js
gulp.task('compile:js', () => {
return gulp.src('./src/javascripts/**/*.js')
.pipe(webpack(webpack_config))
.pipe(gulp.dest('./dev/javascripts'))
})
// 监听任务
gulp.task('watch', () => {
gulp.watch('./src/**/*.html', ['copy:html']);
gulp.watch('./src/javascripts/**/*', ['compile:js']);
gulp.watch('./src/stylesheets/**/*.scss', ['compile:scss']);
gulp.watch('./src/static', ['copy:static']);
watch('src/static', (v) => { // 当src/static中文件变化后执行
if ( v.event === 'unlink' ) { // 如果文件删除了
let _path = v.history[0].replace('\src', '\dev'); // 要删除的路径
del(_path);// 删除dist中的文件
}else {
gulp.start(['copy:static'])
}
})
})
// 默认任务
gulp.task('default', ['copy:static', 'copy:html', 'compile:scss', 'compile:js', 'server', 'watch'], () => {
console.log('Everything is done ...')
})