-
Notifications
You must be signed in to change notification settings - Fork 22
/
gulpfile.js
213 lines (179 loc) · 5.31 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
212
213
'use strict';
var _ = require('lodash');
var autoprefixer = require('autoprefixer');
var browserify = require('browserify');
var buffer = require('vinyl-buffer');
var bundleLogger = require('./gulp/bundle-logger.js');
var color = require('postcss-color-function');
var gulp = require('gulp');
var path = require('path');
var precss = require('precss');
var reporter = require('postcss-reporter');
var rimraf = require('rimraf');
var through2 = require('through2');
var watchify = require('watchify');
var plugins = require('gulp-load-plugins')();
var paths = {
js: [
'./static/js/**.js',
'./**/static/js/**.js'
],
jsEntries: [
'./static/js/*.js',
'./**/static/js/*.js'
],
css: [
'./static/css/**/*.css',
'!./static/css/**/_*.css'
],
cssAll: [
'./static/css/**/*.css'
],
python: [
'**/*.py',
'!**/migrations/*.py',
'!./scripts/**/*.py',
'!./tmp/**/*.py',
'!./node_modules/**/*.py'
],
select2Files: [
'./node_modules/select2/dist/css/select2.min.css'
],
bootstrapFiles: [
'./node_modules/bootstrap/dist/css/bootstrap.css',
'./node_modules/bootstrap/dist/css/bootstrap.css.map',
'./node_modules/bootstrap/dist/css/bootstrap-theme.css',
'./node_modules/bootstrap/dist/css/bootstrap-theme.css.map'
],
webshimFiles: [
'./node_modules/webshim/js-webshim/minified/shims/**/*'
]
};
// Clean up files
gulp.task('clean', function (cb) {
rimraf('./build', cb);
});
// Lint JavaScript code
gulp.task('lint-js-eslint', function () {
return gulp.src(paths.js)
.pipe(plugins.eslint({
useEslintrc: true,
rulePaths: [
path.join(process.env.HOME, '.eslint')
]
}))
.pipe(plugins.eslint.format());
});
gulp.task('lint-js-jscs', function () {
return gulp.src(paths.js)
.pipe(plugins.jscs());
});
gulp.task('lint-js', gulp.parallel(['lint-js-eslint', 'lint-js-jscs']));
// Lint Python code
gulp.task('lint-python', function () {
var shellOptions = {ignoreErrors: true};
return gulp.src(paths.python)
.pipe(plugins.shell([
'flake8 <%= file.path %> | awk \'$0="flake8: "$0\''
], shellOptions))
.pipe(plugins.shell([
'pylint --rcfile=.pylintrc -r no -f colorized <%= file.path %> ' +
'--msg-template "{C}: {path}:{line}:{column} {msg} ({symbol})" ' +
'| awk \'$0="pylint: "$0\''
], shellOptions))
.pipe(plugins.shell([
'pydocstyle <%= file.path %> 2>&1 | awk \'$0="pydocstyle: "$0\''
], shellOptions));
});
gulp.task('lint', gulp.parallel(['lint-js', 'lint-python']));
gulp.task('bootstrap-files', function () {
return gulp.src(paths.bootstrapFiles)
.pipe(gulp.dest('./build/vendor'));
});
gulp.task('select2-files', function () {
return gulp.src(paths.select2Files)
.pipe(gulp.dest('./build/vendor'));
});
gulp.task('webshim-files', function () {
return gulp.src(paths.webshimFiles)
.pipe(gulp.dest('./build/vendor/shims'));
});
// Collect Bootstrap and other frontend files
gulp.task('frontend-files', gulp.parallel([
'bootstrap-files',
'select2-files',
'webshim-files'
]));
function browserifyTask(options) {
options = options || {};
function bundleEntry(cbUpdate) {
return through2.obj(function (file, enc, next) {
var browserifyOptions = {debug: true};
if (options.development) {
// Add watchify args
_.assignIn(browserifyOptions, watchify.args);
}
// Log when bundling starts
bundleLogger.start(file.path);
var bundler = browserify(file.path, browserifyOptions);
if (options.development) {
// Wrap with watchify and rebundle on changes
bundler = watchify(bundler);
// Rebundle on update
bundler.on('update', _.partial(cbUpdate, file.path));
bundleLogger.watch(file.path);
}
bundler.bundle(function (err, res) {
file.contents = res;
bundleLogger.end(file.path);
next(err, file);
});
});
}
function bundle(files) {
return gulp.src(files)
.pipe(bundleEntry(bundle))
.pipe(buffer())
.pipe(plugins.sourcemaps.init({loadMaps: true}))
.pipe(plugins.uglify())
.on('error', function (err) {
console.error(err.toString());
process.exit(1);
})
.pipe(plugins.sourcemaps.write('./'))
.pipe(gulp.dest('./build/js'));
}
return bundle(paths.jsEntries);
}
// Browserify all of our JavaScript entry points
gulp.task('browserify', gulp.parallel(['frontend-files'], function () {
return browserifyTask();
}));
// Watchify all of our JavaScript entry points
gulp.task('watchify', gulp.parallel(['frontend-files'], function () {
return browserifyTask({development: true});
}));
gulp.task('postcss', function () {
return gulp.src(paths.css)
.pipe(plugins.postcss([
precss(),
color(),
autoprefixer({browsers: ['last 2 versions']}),
reporter()
]))
.pipe(plugins.cssnano())
.pipe(gulp.dest('./build/css'));
});
// Run browserify on JS changes, postcss on css changes
gulp.task('watch', gulp.parallel(['frontend-files'], function () {
return gulp.watch(paths.cssAll, gulp.parallel(['postcss']));
}));
// Just build the files in ./build
gulp.task('build', gulp.parallel(['frontend-files', 'postcss', 'browserify']));
// Build and watch
gulp.task('default', gulp.parallel([
'frontend-files',
'postcss',
'watch',
'watchify'
]));