forked from sturoscy/django_starter_kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
211 lines (182 loc) · 6.67 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
'use strict';
require('es6-promise').polyfill();
// Load required files that aren't auto-loaded by
// gulp-load-plugins (see below)
var argv = require('yargs').argv,
fs = require('fs'),
gulp = require('gulp'),
mainBowerFiles = require('main-bower-files'),
merge = require('merge-stream'),
minifyCss = require('gulp-minify-css'),
path = require('path'),
taskListing = require('gulp-task-listing');
// Browserify specific
var babelify = require('babelify'),
browserify = require('browserify'),
source = require('vinyl-source-stream');
// This will load any gulp plugins automatically that
// have this format ('gulp-pluginName' [can only have one dash])
// The plugin can used as $.pluginName
var $ = require('gulp-load-plugins')();
// Browser Sync
var browserSync = require('browser-sync'),
reload = browserSync.reload;
// Add a task to render the output
// Used for $-> gulp help
gulp.task('help', taskListing);
// Paths
var cssPath = 'static_dev/css',
javascriptsPath = 'static_dev/javascripts',
imagesPath = 'static_dev/images',
sassPath = 'static_dev/sass';
// Get Folder Function (from paths)
function getFolders(dir) {
return fs.readdirSync(dir)
.filter(function(file) {
return fs.statSync(path.join(dir, file)).isDirectory();
});
}
// Main serve task
// Watches coffee, js, and scss files for changes. Will restart
// apache and reload browser automatically
gulp.task('serve', function() {
gulp.watch([
'static_dev/javascripts/**/app.js',
'static_dev/javascripts/**/**/*.js'
], ['scripts-browserify', 'shell-apache']);
gulp.watch('static_dev/css/**/*.css', ['styles-css', 'shell-apache']);
gulp.watch('static_dev/sass/**/*.scss', ['styles-sass', 'shell-apache']);
});
/* Shell tasks */
// Quick ways of running python and client related tasks
gulp.task('shell-apache', $.shell.task(['sudo service httpd restart']))
/* Main bower tasks */
gulp.task('bower', ['bower-scripts', 'bower-styles']);
/* mainBowerFiles looks in an npm package's bower.json file
for the 'main' entry - http://bower.io/docs/creating-packages/#main
- and loops through the files listed there */
// Uglify's all bower/vendor scripts into single file
gulp.task('bower-scripts', function() {
return gulp.src(mainBowerFiles())
// Filter javascript files
.pipe($.filter('*.js'))
// Concat to single file
.pipe($.concat('vendor.js'))
// Remove whitespace and uglify
.pipe($.uglify())
// Rename the file
.pipe($.rename('vendor.min.js'))
// Copy to static folder
.pipe(gulp.dest('static/javascripts'))
});
// Concats all bower/vendor styles into single file
gulp.task('bower-styles', function() {
return gulp.src(mainBowerFiles())
// Filter CSS files
.pipe($.filter('*.css'))
// Concat to single file
.pipe($.concat('vendor.css'))
// Minify CSS
.pipe(minifyCss({ compatibility: 'ie9', rebase: false }))
// Post CSS processor
.pipe($.postcss([
require('autoprefixer')({ browsers: ['last 1 version'] })
]))
// Copy to static folder
.pipe(gulp.dest('static/stylesheets'))
});
/* Main scripts tasks */
gulp.task('scripts', ['scripts-browserify'])
// Browserify task
gulp.task('scripts-browserify', function() {
var folders = getFolders(javascriptsPath);
var tasks = folders.map(function(folder) {
return browserify({
entries: ['./static_dev/javascripts/' + folder + '/app.js'],
debug: true
})
.transform(babelify, { presets: ['es2015', 'react'] })
.bundle()
.pipe(source(folder + '.js'))
//.pipe($.uglify())
.pipe($.rename(folder + '.min.js'))
.pipe(gulp.dest('./static/javascripts/'));
});
return merge(tasks);
});
/* Main styles tasks */
gulp.task('styles', ['styles-sass']);
// CSS Task
// Concats and minifies css files
gulp.task('styles-css', function() {
var folders = getFolders(cssPath);
var tasks = folders.map(function(folder) {
return gulp.src('static_dev/css/' + folder + '/*.css',
{ base: 'static_dev/css/' + folder }
)
// Concat files
.pipe($.concat(folder + '.css'))
// Minify CSS
.pipe(minifyCss({ compatibility: 'ie9', rebase: false }))
// Post CSS processor
.pipe($.postcss([
require('autoprefixer')({ browsers: ['last 1 version'] })
]))
// Rename the file
.pipe($.rename(folder + '.min.css'))
// Copy it to static folder
.pipe(gulp.dest('static/stylesheets'))
});
return merge(tasks);
});
// SASS Task
// Compiles, concats, minifies, and versions scss files
gulp.task('styles-sass', function() {
var folders = getFolders(sassPath);
var tasks = folders.map(function(folder) {
return gulp.src(['static_dev/sass/' + folder + '/*.scss'],
{ base: 'static_dev/sass/' + folder }
)
// Compile to CSS
.pipe($.sass({
outputStyle: 'nested',
precision: 10,
includePaths: ['.'],
onError: console.error.bind(console, 'Sass error:')
}))
// Concat files
.pipe($.concat(folder + '.css'))
// Minify CSS
//.pipe(minifyCss({ compatibility: 'ie9', rebase: false }))
// Post CSS processor
.pipe($.postcss([
require('autoprefixer')({ browsers: ['last 1 version'] })
]))
// Rename the file
.pipe($.rename(folder + '.min.css'))
// Copy it to static folder
.pipe(gulp.dest('static/stylesheets'))
});
return merge(tasks);
});
/* Main image tasks */
gulp.task('media', ['images']);
/* Images Task
Optimizes images and places copies in static/app_name/*.{jpg|png}
This task needs to be run manually. It is not triggered by anything
other than 'build' */
gulp.task('images', function() {
var folders = getFolders(imagesPath);
var tasks = folders.map(function(folder) {
return gulp.src('static_dev/images/' + folder + '/*',
{ base: 'static_dev/images/' })
.pipe($.imagemin({
progressive: true,
interlaced: true,
svgoPlugins: [{cleanupIDs: false}]
}))
.pipe(gulp.dest('static/images'));
});
return merge(tasks);
});
gulp.task('build', ['bower', 'scripts', 'styles'])