-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgulpfile.js
182 lines (161 loc) · 5.02 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// Import all packages needed for the build
var gulp = require('gulp');
var connect = require('gulp-connect');
var ghPages = require('gulp-gh-pages');
var browserify = require('browserify');
var rename = require('gulp-rename');
var bulkify = require('bulkify');
var concat = require('gulp-concat');
var sass = require('gulp-sass');
var autoprefixer = require('gulp-autoprefixer');
var hashify = require('gulp-hashify');
var source = require('vinyl-source-stream');
var buffer = require('vinyl-buffer');
var tap = require('gulp-tap');
var del = require('del');
var watch = require('gulp-watch');
var sourcemaps = require('gulp-sourcemaps');
// Common patterns used throughout the gulp configuration
var src = {
allHtml: './src/**/*.html',
allViews: './src/views/**/*.html',
allJs: './src/**/*.js',
allFont: './src/**/*.{ttf,woff,otf,eot}',
allScss: './src/**/*.scss',
allImg: './src/**/*.{jpg,png,svg,gif,ico}'
};
// The default task is what runs when you type 'gulp' in the terminal
gulp.task('default', ['clean'], function () {
return gulp.start('html', 'img', 'font', 'js:views', 'js:vendor', 'js', 'scss', 'watch', 'reload', 'serve');
});
// Serve is a name I made up. You could call it 'dostuff' or whatever.
// The task starts a connect server on port 8000 if you go to
// http://localhost:8000, you can see what is being served.
gulp.task('serve', function () {
connect.server({
root: './dist', // Serve content out of the ./src folder
port: 8000, // Serve on port 8000
livereload: true // Allow us to reload the app in the browser at will
});
});
// The watch task watches a directory for changes and tells the
// browser(s) connected to the server to refresh. I also made this name
// up. In fact, the only name that has intrinsic meaning to gulp is the
// 'default' task.
gulp.task('watch', function () {
watch(src.allHtml, function () {
gulp.start('html', 'js:views');
});
watch(src.allJs, function () {
gulp.start('js');
});
watch(src.allScss, function () {
gulp.start('scss');
});
watch(src.allImg, function () {
gulp.start('img');
});
watch(src.allFont, function () {
gulp.start('font');
});
});
// The reload task tells the connect server to reload all browsers
gulp.task('reload', function () {
watch('./dist/**/*', function () {
gulp.src('./dist/**/*').pipe(connect.reload());
});
});
// Deploy our src folder to gh-pages
gulp.task('deploy', function() {
return gulp.src('./dist/**/*').pipe(ghPages());
});
// Adding the CSS task
gulp.task('scss', function () {
return gulp.src('./src/css/main.scss')
.on('error', swallowError)
.pipe(sass().on('error', sass.logError))
.pipe(rename('main.css'))
.pipe(autoprefixer({
browsers: ['last 2 versions'],
cascade: false
}))
.pipe(gulp.dest('./dist/css'));
});
// Build our JavaScript files using browserify
gulp.task('js', function () {
var stream;
try {
stream = browserify('./src/js/init.js', { debug: true })
.transform('bulkify')
.transform({ global: true }, 'uglifyify')
.external('views')
.external('jquery')
.external('underscore')
.external('backbone')
.external('parsleyjs')
.bundle();
} catch (ex) {
console.error(ex);
return;
}
return stream
.on('error', swallowError)
.pipe(source('app.js'))
.pipe(buffer())
.pipe(sourcemaps.init({ loadMaps: true }))
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest('./dist/js'));
});
// Bundle vendor scripts (jQuery, Backbone, etc) into one script (vendor.js)
gulp.task('js:vendor', function () {
return browserify({ debug: true })
.transform({ global: true }, 'uglifyify')
.require('jquery')
.require('underscore')
.require('backbone')
.require('parsleyjs')
.bundle()
.pipe(source('vendor.js'))
.pipe(buffer())
.pipe(sourcemaps.init({ loadMaps: true }))
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest('./dist/js'));
});
// Turn all views into a JavaScript object
gulp.task('js:views', function () {
return gulp.src(src.allViews)
.pipe(hashify('bundled-views.js'))
.pipe(tap(function(file) {
return browserify()
.require(file, { expose: 'views' })
.bundle()
.pipe(source('views.js'))
.pipe(buffer())
.pipe(gulp.dest('./dist/js'));
}));
});
// Let's move our html files into dest, too... Sometime, we'll modify this
// to do minification, cache-busting, etc...
gulp.task('html', function () {
return gulp.src([src.allHtml, '!' + src.allViews])
.pipe(gulp.dest('./dist'));
});
// Move any images to the dist folder
gulp.task('img', function () {
return gulp.src(src.allImg)
.pipe(gulp.dest('./dist'));
});
// Move any fonts to the dist folder
gulp.task('font', function () {
return gulp.src(src.allFont)
.pipe(gulp.dest('./dist'));
});
// Clean the destination directory
gulp.task('clean', function (cb) {
del('./dist', cb);
});
// Prevent gulp from crashing and leaving a running Node process behind
function swallowError (error) {
console.log(error.toString());
this.emit('end');
}