forked from Elantris/ntuosc-course-20160603
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
158 lines (118 loc) · 4.09 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
var fs = require('fs');
var gulp = require('gulp'); // https://github.com/gulpjs/gulp
var moment = require('moment'); // https://github.com/moment/moment/
var clc = require('cli-color'); // https://github.com/medikoo/cli-color
var rename = require('gulp-rename'); // https://github.com/hparra/gulp-rename
var sourcemaps = require('gulp-sourcemaps'); // https://github.com/floridoo/gulp-sourcemaps
var gulpif = require('gulp-if'); // https://github.com/robrich/gulp-if
var production = (process.env.NODE_ENV === 'production');
gulp.task('default', ['move', 'pug', 'sass', 'react']);
/*====================================
= Browser Sync =
====================================*/
var browserSync = require('browser-sync').create(); // https://github.com/Browsersync/browser-sync
var reload = browserSync.reload;
gulp.task('browser-sync', function() {
browserSync.init({
server: {
baseDir: "./"
}
});
});
gulp.task('watch', ['pug:watch', 'sass:watch', 'react:watch', 'browser-sync']);
/*============================
= Move =
============================*/
var cssmin = require('gulp-cssmin'); // https://github.com/chilijung/gulp-cssmin
var uglify = require('gulp-uglify'); // https://github.com/terinjokes/gulp-uglify
var concat = require('gulp-concat'); // https://github.com/contra/gulp-concat
gulp.task('move', function() {
gulp.src([
'./bower_components/skeleton/css/normalize.css',
'./bower_components/skeleton/css/skeleton.css',
])
.pipe(concat('lib.min.css'))
.pipe(cssmin())
.pipe(gulp.dest('./public/css/'));
gulp.src([
'./node_modules/jquery/dist/jquery.min.js',
'./bower_components/color-thief/dist/color-thief.min.js',
])
.pipe(concat('lib.min.js'))
.pipe(uglify({ mangle: false }))
.pipe(gulp.dest('./public/js/'));
});
/*===========================
= Pug =
===========================*/
var pug = require('gulp-pug'); // https://github.com/jamen/gulp-pug
gulp.task('pug', function() {
gulp.src('./src/views/*.pug')
.pipe(pug())
.pipe(gulp.dest('./'));
});
gulp.task('pug:watch', ['pug'], function() {
gulp.watch('./src/views/**/*.pug', ['pug', reload]);
});
/*============================
= Sass =
============================*/
'use strict';
var sass = require('gulp-sass'); // https://github.com/dlmanning/gulp-sass
gulp.task('sass', function() {
gulp.src('./src/styles/*.scss')
.pipe(gulpif(production, sourcemaps.init()))
.pipe(sass({ outputStyle: 'compressed' }).on('error', sass.logError))
.pipe(gulpif(production, sourcemaps.write()))
.pipe(rename({ suffix: '.min' }))
.pipe(gulp.dest('./public/css/'));
});
gulp.task('sass:watch', ['sass'], function() {
gulp.watch('./src/styles/**/*.scss', ['sass', reload]);
});
/*=============================
= React =
=============================*/
var browserify = require('browserify'); // https://github.com/substack/node-browserify
var watchify = require('watchify'); // https://github.com/substack/watchify
var source = require('vinyl-source-stream'); // https://github.com/hughsk/vinyl-source-stream
var buffer = require('vinyl-buffer'); // https://github.com/hughsk/vinyl-buffer
var reactify = require('reactify'); // https://github.com/andreypopp/reactify
var b = browserify('./src/scripts/main.jsx', {
cache: {},
packageCache: {},
debug: !production,
transform: [reactify]
});
gulp.task('react', function() {
bundle();
});
gulp.task('react:watch', ['react'], function() {
b.plugin(watchify);
b.on('log', function(msg) {
colorLog('yellowBright', msg);
reload();
});
b.on('update', bundle);
});
function bundle() {
b.bundle()
.on('error', errorLog)
.pipe(source('main.min.js'))
.pipe(buffer())
.pipe(gulpif(production, uglify()))
.on('error', errorLog)
.pipe(gulp.dest('./public/js/'));
}
/*===============================
= Utility =
===============================*/
function colorLog(color, message) {
console.log('[%s] %s',
clc.blackBright(moment().format('HH:mm:ss')),
clc[color](message)
);
}
function errorLog(err) {
colorLog('redBright', err.stack);
}