forked from readthedocs/readthedocs.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
255 lines (231 loc) · 9.41 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
var gulp = require('gulp'),
gulp_util = require('gulp-util'),
watch = require('gulp-watch'),
rename = require('gulp-rename'),
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 = {
builds: {'js/detail.js': {}},
core: {
'js/readthedocs-doc-embed.js': {expose: false},
'js/autocomplete.js': {},
'css/badge_only.css': {src: 'bower_components/sphinx-rtd-theme/sphinx_rtd_theme/static/css/badge_only.css'},
'css/theme.css': {src: 'bower_components/sphinx-rtd-theme/sphinx_rtd_theme/static/css/theme.css'},
'font/Lato-Bold.ttf': {src: 'bower_components/lato-googlefont/Lato-Bold.ttf'},
'font/Lato-Regular.ttf': {src: 'bower_components/lato-googlefont/Lato-Regular.ttf'},
'font/Inconsolata-Bold.ttf': {src: 'bower_components/inconsolata-googlefont/Inconsolata-Bold.ttf'},
'font/Inconsolata-Regular.ttf': {src: 'bower_components/inconsolata-googlefont/Inconsolata-Regular.ttf'},
'font/RobotoSlab-Bold.ttf': {src: 'bower_components/robotoslab-googlefont/RobotoSlab-Bold.ttf'},
'font/RobotoSlab-Regular.ttf': {src: 'bower_components/robotoslab-googlefont/RobotoSlab-Regular.ttf'},
'font/FontAwesome.otf': {src: 'bower_components/font-awesome/FontAwesome.otf'},
'font/fontawesome-webfont.eot': {src: 'bower_components/font-awesome/fonts/fontawesome-webfont.eot'},
'font/fontawesome-webfont.svg': {src: 'bower_components/font-awesome/fonts/fontawesome-webfont.svg'},
'font/fontawesome-webfont.ttf': {src: 'bower_components/font-awesome/fonts/fontawesome-webfont.ttf'},
'font/fontawesome-webfont.woff': {src: 'bower_components/font-awesome/fonts/fontawesome-webfont.woff'},
'font/FontAwesome.otf': {src: 'bower_components/font-awesome/fonts/FontAwesome.otf'}
},
projects: {
'js/tools.js': {},
'js/import.js': {},
'css/import.less': {},
},
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': {standalone: 'jquery'},
'knockout': {},
'jquery-migrate': {standalone: 'jquery-migrate'},
'jquery-ui': {standalone: 'jquery-ui'},
'underscore': {standalone: '_'}
};
// 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 bundles = Object.keys(sources[application]).map(function (entry_path) {
var bundle_path = path.join(
pkg_config.name, application, 'static-src', '**', entry_path),
bundle_config = sources[application][entry_path] || {},
bundle;
if (/\.js$/.test(bundle_path)) {
// Javascript sources
bundle = gulp
.src(bundle_path)
.pipe(es.map(function (file, cb) {
if (typeof(bundle_config.expose) == 'undefined') {
var parts = [
application,
path.basename(file.path, '.js')
];
bundle_config.expose = parts.join('/');
}
else if (bundle_config.expose === false) {
bundle_config.expose = undefined;
}
return browserify_stream(
file, bundle_config, cb
);
}));
if (minify) {
bundle = bundle
.pipe(vinyl_buffer())
.pipe(uglify())
.on('error', function (ev) {
gulp_util.beep();
gulp_util.log('Uglify error:', ev.message);
});
}
}
else if (/\.less$/.test(bundle_path)) {
// LESS sources
bundle = gulp.src(bundle_path)
.pipe(less({}))
.on('error', function (ev) {
gulp_util.beep();
gulp_util.log('LESS error:', ev.message);
});
}
else {
// Copy only sources, from bower_components/etc
var bundle = gulp;
if (bundle_config.src) {
bundle = bundle
.src(bundle_config.src)
.pipe(rename(application + path.sep + entry_path));
}
else {
bundle = bundle
.src(bundle_path);
}
}
return bundle;
});
return es.merge(bundles)
.pipe(gulp.dest(path.join(pkg_config.name, application, 'static')));
}
// Browserify build
function browserify_stream (file, config, cb_output) {
bower_resolve.offline = true;
bower_resolve.init(function () {
var bundle_stream = browserify({
paths: ['./']
});
Object.keys(standalone).map(function (module) {
bundle_stream = bundle_stream.external(module);
});
if (typeof(config.expose) == 'undefined') {
bundle_stream.add(file.path);
}
else {
bundle_stream = bundle_stream.require(
file.path, {expose: config.expose}
);
}
bundle_stream
.transform('debowerify', {ignoreModules: Object.keys(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 = Object.keys(standalone).map(function (module) {
var vendor_options = standalone[module] || {},
vendor_bundles = [];
// Bundle vendor libs for import via require()
vendor_bundles.push(
browserify()
.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')
))
);
// Bundle standalone for legacy use. These should only be used on
// old documentation that does not yet use the new bundles
if (typeof(vendor_options.standalone) != 'undefined') {
vendor_bundles.push(
browserify({standalone: vendor_options.standalone})
.require(bower_resolve(module))
.bundle()
.pipe(vinyl_source(module + '-standalone.js'))
.pipe(vinyl_buffer())
.pipe(uglify())
.pipe(gulp.dest(
path.join(pkg_config.name, 'static', 'vendor')
))
);
}
return es.merge(vendor_bundles);
});
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', '**', '*.less')
];
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']);