forked from jakearchibald/offline-wikipedia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
269 lines (233 loc) · 7.32 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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
var fs = require('fs');
var gulp = require('gulp');
var plugins = require('gulp-load-plugins')();
var runSequence = require('run-sequence');
var through = require('through2');
var watchify = require('watchify');
var browserify = require('browserify');
var uglifyify = require('uglifyify');
var mergeStream = require('merge-stream');
var source = require('vinyl-source-stream');
var buffer = require('vinyl-buffer');
var babelify = require('babelify');
var hbsfy = require("hbsfy");
var spawn = require('child_process').spawn;
var Promise = require('rsvp').Promise;
gulp.task('clean', function (done) {
require('del')(['dist/*', '!dist/node_modules', '!dist/.git'], done);
});
gulp.task('html', function () {
return gulp.src([
'public/*.html'
]).pipe(gulp.dest('dist/public'));
});
gulp.task('css', function () {
return gulp.src('public/css/*.scss', {base: './'})
.pipe(plugins.sourcemaps.init())
.pipe(plugins.sass({ outputStyle: 'compressed' }))
.pipe(plugins.sourcemaps.write('./'))
.pipe(gulp.dest('dist'));
});
gulp.task('misc', function () {
return gulp.src([
// Copy all files
'public/**',
// Exclude the following files
// (other tasks will handle the copying of these files)
'!public/*.html',
'!public/{css,css/**}',
'!public/{js,js/**}'
]).pipe(gulp.dest('dist/public'));
});
function createBundler(src) {
var b;
if (plugins.util.env.production) {
b = browserify();
}
else {
b = browserify({
cache: {}, packageCache: {}, fullPaths: true,
debug: true
});
}
b.transform(babelify.configure({
stage: 1
}));
b.transform(hbsfy);
if (plugins.util.env.production) {
b.transform({
global: true
}, 'uglifyify');
}
b.add(src);
return b;
}
var bundlers = {
'js/page.js': createBundler('./public/js/page/index.js'),
'js/page-framework.js': createBundler('./public/js/page-framework/index.js'),
'js/fetch.js': createBundler('./public/js/fetch/index.js'),
'js/promise-polyfill.js': createBundler('./public/js/promise-polyfill/index.js'),
'js/fastclick.js': createBundler('./public/js/fastclick/index.js'),
'sw.js': plugins.util.env['disable-sw'] ? createBundler('./public/js/sw-null/index.js') : createBundler('./public/js/sw/index.js')
};
function bundle(bundler, outputPath) {
var splitPath = outputPath.split('/');
var outputFile = splitPath[splitPath.length - 1];
var outputDir = splitPath.slice(0, -1).join('/');
return bundler.bundle()
// log errors if they happen
.on('error', plugins.util.log.bind(plugins.util, 'Browserify Error'))
.pipe(source(outputFile))
.pipe(buffer())
.pipe(plugins.sourcemaps.init({ loadMaps: true })) // loads map from browserify file
.pipe(plugins.sourcemaps.write('./')) // writes .map file
.pipe(gulp.dest('dist/public/' + outputDir));
}
gulp.task('js', function () {
return mergeStream.apply(null,
Object.keys(bundlers).map(function(key) {
return bundle(bundlers[key], key);
})
);
});
var rmOrig = function() {
return through.obj(function(file, enc, cb) {
if (file.revOrigPath) {
fs.unlink(file.revOrigPath);
}
this.push(file); // Pass file when you're done
return cb(); // notify through2 you're done
});
};
gulp.task('rev', function () {
return gulp.src([
'dist/public/{css,js,imgs}/**'
])
.pipe(plugins.rev())
.pipe(gulp.dest('dist/public'))
.pipe(rmOrig())
.pipe(plugins.rev.manifest())
.pipe(gulp.dest('dist/'));
});
gulp.task('updaterefs', function () {
var manifest = gulp.src("dist/rev-manifest.json");
return gulp.src([
'dist/index.js',
'dist/public/**',
'dist/shared-templates/**',
'dist/server-templates/**'
], {base: 'dist'})
.pipe(plugins.revReplace({
manifest: manifest,
replaceInExtensions: ['.js', '.css', '.html', '.hbs', '.json']
}))
.pipe(gulp.dest('dist'));
});
gulp.task('compress', function () {
return gulp.src([
'dist/public/**/*.{js,css}',
'dist/public/sw.js',
'dist/public/manifest.json'
], {base: 'dist'})
.pipe(plugins.gzip({append: true}))
.pipe(gulp.dest('dist'));
});
gulp.task('server:package', function () {
return gulp.src('server-package.json')
.pipe(plugins.rename(function(path) {
path.basename = 'package';
})).pipe(gulp.dest('dist'));
});
gulp.task('server:misc', function () {
return gulp.src([
'.gitignore',
'Procfile',
'wikipedia/**/*.{html,json}'
], {base: './'})
.pipe(gulp.dest('dist'));
});
gulp.task('server:js', function () {
return gulp.src([
'index.js',
'wikipedia/**/*.js',
'isojs/**/*.js'
], {base: './'})
.pipe(plugins.sourcemaps.init())
.pipe(plugins.babel({stage: 1}))
.pipe(plugins.sourcemaps.write('.'))
.pipe(gulp.dest('dist'));
});
gulp.task('server:sharedtemplates', function () {
return gulp.src('shared-templates/*.hbs')
.pipe(plugins.handlebars())
.pipe(through.obj(function(file, enc, callback) {
// Don't want the whole lib
file.defineModuleOptions.require = {Handlebars: 'handlebars/runtime'};
callback(null, file);
}))
.pipe(plugins.defineModule('commonjs'))
.pipe(plugins.rename(function(path) {
path.extname = '.js';
}))
.pipe(gulp.dest('dist/shared-templates'));
});
gulp.task('server:templates', function () {
return gulp.src('server-templates/*.dust')
.pipe(plugins.dust({
name: function(file) {
return file.relative.replace(/\.dust$/, '');
}
}))
.pipe(gulp.dest('dist/server-templates'));
});
gulp.task('watch', function () {
gulp.watch(['public/*.html'], ['html']);
gulp.watch(['public/**/*.scss'], ['css']);
gulp.watch(['index.js', 'wikipedia/**'], ['server:js']);
gulp.watch(['shared-templates/*.hbs'], ['server:sharedtemplates']);
gulp.watch(['server-templates/*.dust'], ['server:templates']);
Object.keys(bundlers).forEach(function(key) {
var watchifyBundler = watchify(bundlers[key]);
watchifyBundler.on('update', function() {
return bundle(watchifyBundler, key);
});
bundle(watchifyBundler, key);
});
});
var buildSequence = ['clean', ['css', 'misc', 'html', 'js', 'server:package', 'server:misc', 'server:js', 'server:sharedtemplates', 'server:templates']];
var productionBuildSequence = buildSequence.concat(['rev', 'updaterefs', 'compress']);
gulp.task('build', function() {
return runSequence.apply(null, buildSequence);
});
gulp.task('productionbuild', function() {
return runSequence.apply(null, productionBuildSequence);
});
gulp.task('server:serve', function() {
plugins.developServer.listen({
path: './index.js',
cwd: './dist'
});
gulp.watch([
'dist/index.js',
'dist/shared-templates/flags.js',
'dist/public/index-end.html',
'dist/public/css/head.css',
'dist/server-templates/base.js'
], plugins.developServer.restart);
});
gulp.task('serve', function() {
return runSequence.apply(null, buildSequence.concat([['server:serve', 'watch']]));
});
gulp.task('productionserve', function() {
return runSequence.apply(null, productionBuildSequence.concat([['server:serve']]));
});
gulp.task('deploy', function() {
return runSequence.apply(null, productionBuildSequence.concat([plugins.shell.task([
'git init',
'heroku git:remote -a wiki-offline',
'git add -A',
'git commit --message "Build"',
'git push heroku'
], {cwd: __dirname + '/dist'})]));
});
gulp.task('default', ['build']);