forked from readthedocs/readthedocs.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
162 lines (144 loc) · 5.21 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
var gulp = require('gulp'),
gulp_util = require('gulp-util'),
watch = require('gulp-watch'),
run = require('gulp-run'),
less = require('gulp-less'),
bower_resolve = require('bower-resolve'),
browserify = require('browserify'),
debowerify = require('debowerify'),
uglify = require('gulp-uglify'),
vinyl_source = require('vinyl-source-stream'),
vinyl_buffer = require('vinyl-buffer'),
es = require('event-stream'),
path = require('path'),
pkg_config = require('./package.json');
// Applications with primary static sources. We define these here to avoid
// picking up dependencies of the primary entry points and putting any
// limitations on directory structure for entry points.
var sources = {
core: [
'js/readthedocs-doc-embed.js',
'js/autocomplete.js',
'js/projectimport.js',
],
projects: ['js/tools.js'],
gold: ['js/gold.js'],
donate: ['js/donate.js']
};
// Standalone application to create vendor bundles for. These can be imported
// with require in the browser or with Node during testing.
var standalone = ['jquery', 'knockout'];
// Build application call, wraps building entry point files for a single
// application. This is called by build and dev tasks.
function build_app_sources (application, minify) {
// Normalize file glob lists
var app_sources = sources[application].map(function (n) {
return path.join(pkg_config.name, application, 'static-src', '**', n)
});
var app_js_sources = app_sources.filter(function (elem, n, arr) {
return /\.js$/.test(elem);
});
var app_css_sources = app_sources.filter(function (elem, n, arr) {
return /\.less$/.test(elem);
});
// Javascript sources
var app_js = gulp
.src(app_js_sources)
.pipe(es.map(browserify_stream));
if (minify) {
app_js = app_js
.pipe(vinyl_buffer())
.pipe(uglify())
.on('error', function (ev) {
gulp_util.beep();
gulp_util.log('Uglify error:', ev.message);
});
}
// CSS sources
var app_css = gulp.src(app_css_sources)
.pipe(less({}))
.on('error', function (ev) {
gulp_util.beep();
gulp_util.log('LESS error:', ev.message);
});
return es.merge(app_js, app_css)
.pipe(gulp.dest(path.join(pkg_config.name, application, 'static')));
}
// Browserify build
function browserify_stream (file, cb_output) {
bower_resolve.offline = true;
bower_resolve.init(function () {
var bundle_stream = browserify(file.path)
standalone.map(function (module) {
bundle_stream = bundle_stream.external(module);
});
bundle_stream
.transform('debowerify', {ignoreModules: standalone})
.bundle()
.on('error', function (ev) {
gulp_util.beep();
gulp_util.log('Browserify error:', ev.message);
})
.pipe(vinyl_source(file.path, file))
.pipe(es.map(function (data, cb_inner) {
cb_output(null, data);
}));
});
}
// Build standalone vendor modules
function build_vendor_sources(data, cb_output) {
bower_resolve.offline = true;
bower_resolve.init(function () {
var standalone_modules = standalone.map(function (module) {
return browserify({standalone: module})
.require(bower_resolve(module), {expose: module})
.bundle()
.pipe(vinyl_source(module + '.js'))
.pipe(vinyl_buffer())
.pipe(uglify())
.pipe(gulp.dest(path.join(pkg_config.name, 'static', 'vendor')));
});
es
.merge(standalone_modules)
.pipe(es.wait(function (err, body) {
cb_output(null, data);
}));
});
}
/* Tasks */
gulp.task('build', function (done) {
gulp_util.log('Building source files');
es
.merge(Object.keys(sources).map(function (n) {
return build_app_sources(n, true);
}))
.pipe(es.wait(function (err, body) {
gulp_util.log('Collecting static files');
run('./manage.py collectstatic --noinput')
.exec('', function (err) { done(err); });
}));
});
gulp.task('vendor', function (done) {
build_vendor_sources(null, done);
});
gulp.task('dev', function (done) {
gulp_util.log('Continually building source files');
es
.merge(Object.keys(sources).map(function (application) {
var files = [
path.join(pkg_config.name, application, 'static-src', '**', '*.js'),
path.join(pkg_config.name, application, 'static-src', '**', '*.css')
];
return watch(files, {verbose: true, name: 'dev'}, function () {
build_app_sources(application, false)
.pipe(es.wait(function (err, body) {
gulp_util.log('Collecting static files');
run('./manage.py collectstatic --noinput').exec('');
}));
});
}))
.pipe(es.wait(function (err, body) {
done(null);
}));
});
gulp.task('default', ['build']);