-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
91 lines (80 loc) · 2.47 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
const gulp = require('gulp');
const $ = require('gulp-load-plugins')();
const browserSync = require('browser-sync');
const autoprefixer = require('autoprefixer');
const minimist = require('minimist'); // 用來讀取指令轉成變數
const gulpSequence = require('gulp-sequence').use(gulp);
// production || development
// # gulp --env production
const envOptions = {
string: 'env',
default: {
env: 'development'
}
};
const options = minimist(process.argv.slice(2), envOptions);
console.log(options);
gulp.task('clean', () => {
return gulp.src(['./public', './.tmp'], {
read: false
}) // 選項讀取:false阻止gulp讀取文件的內容,使此任務更快。
.pipe($.clean());
});
gulp.task('vendorJs', function () {
return gulp.src([
'./node_modules/jquery/dist/jquery.slim.min.js',
'./node_modules/bootstrap/dist/js/bootstrap.bundle.min.js'
])
.pipe($.concat('vendor.js'))
.pipe(gulp.dest('./public/javascripts'))
})
gulp.task('sass', function () {
// PostCSS AutoPrefixer
var processors = [
autoprefixer({
browsers: ['last 5 version'],
})
];
return gulp
.src(['./source/stylesheets/**/*.sass', './source/stylesheets/**/*.scss'])
.pipe($.plumber())
.pipe($.sourcemaps.init())
.pipe($.sass({
outputStyle: 'nested',
includePaths: ['./node_modules/bootstrap/scss']
})
.on('error', $.sass.logError))
.pipe($.postcss(processors))
.pipe($.if(options.env === 'production', $.minifyCss())) // 假設開發環境則壓縮 CSS
.pipe($.sourcemaps.write('.'))
.pipe(gulp.dest('./public/stylesheets'))
.pipe(browserSync.reload({
stream: true
}));
});
gulp.task('copy', function () {
gulp.src(['./source/**/**', '!source/stylesheets/**/**'])
.pipe(gulp.dest('./public/'))
.pipe(browserSync.reload({
stream: true
}));
});
gulp.task('browserSync', function () {
browserSync.init({
server: {
baseDir: './public'
},
reloadDebounce: 2000
})
});
gulp.task('watch', function () {
gulp.watch(['./source/stylesheets/**/*.sass', './source/stylesheets/**/*.scss'], ['sass']);
gulp.watch(['./source/**/**', '!/source/stylesheets/**/**'], ['copy']);
});
gulp.task('deploy', function () {
return gulp.src('./public/**/*')
.pipe($.ghPages());
});
gulp.task('sequence', gulpSequence('clean', 'copy', 'sass', 'vendorJs', 'sass'));
gulp.task('default', ['copy', 'sass', 'vendorJs', 'browserSync', 'watch']);
gulp.task('build', ['sequence'])