-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathgulpfile.babel.js
More file actions
79 lines (69 loc) · 1.9 KB
/
gulpfile.babel.js
File metadata and controls
79 lines (69 loc) · 1.9 KB
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
import gulp from 'gulp';
import sass from 'gulp-sass';
import uglify from 'gulp-uglify';
import connect from 'gulp-connect';
import browserify from 'browserify';
import babelify from 'babelify';
// 轉成 gulp 讀取的 vinyl(黑膠)流
import source from 'vinyl-source-stream';
import buffer from 'vinyl-buffer';
import sourcemaps from 'gulp-sourcemaps';
import gutil from 'gulp-util';
import image from 'gulp-image';
const dirs = {
src: 'src',
dest: 'dist'
};
const stylesPaths = {
src: `${dirs.src}/styles/*.scss`,
dest: `${dirs.dest}/css`
};
const scriptsPaths = {
src: `${dirs.src}/scripts/*.js`,
dest: `${dirs.dest}/js`
};
const imagesPaths = {
src: `${dirs.src}/images/*`,
dest: `${dirs.dest}/img`
};
gulp.task('styles', () => {
gulp.src(stylesPaths.src)
.pipe(sass()) // 編譯 Scss
.pipe(gulp.dest(stylesPaths.dest)) //
.pipe(connect.reload());
});
gulp.task('scripts', function(){
return browserify({
entries: ['./src/scripts/main.js']
})
.transform(babelify)
.bundle()
.pipe(source('bundle.js'))
.pipe(buffer()) // <----- convert from streaming to buffered vinyl file object
.pipe(sourcemaps.init({loadMaps: true}))
.pipe(uglify())
.on('error', gutil.log)
.pipe(sourcemaps.write('./'))
.pipe(gulp.dest(scriptsPaths.dest))
.pipe(connect.reload());
});
gulp.task('images', function() {
gulp.src(imagesPaths.src)
.pipe(image())
.pipe(gulp.dest(imagesPaths.dest))
.pipe(connect.reload());
});
gulp.task('server', function () {
connect.server({
root: ['./'],
livereload: true,
port: 7777,
});
});
gulp.task('watch', function () {
gulp.watch(stylesPaths.src, ['styles']);
gulp.watch(scriptsPaths.src, ['scripts']);
gulp.watch(imagesPaths.src, ['images']);
});
gulp.task('default', ['scripts', 'styles', 'images', 'server', 'watch']);
gulp.task('build', ['scripts', 'styles', 'images']);