forked from TerriaJS/nationalmap
-
Notifications
You must be signed in to change notification settings - Fork 1
/
gulpfile.js
172 lines (136 loc) · 4.86 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
"use strict";
/*global require*/
var fs = require('fs');
var glob = require('glob-all');
var gulp = require('gulp');
var gutil = require('gulp-util');
var browserify = require('browserify');
var jshint = require('gulp-jshint');
var jsdoc = require('gulp-jsdoc');
var less = require('gulp-less');
var uglify = require('gulp-uglify');
var rename = require('gulp-rename');
var sourcemaps = require('gulp-sourcemaps');
var exorcist = require('exorcist');
var buffer = require('vinyl-buffer');
var transform = require('vinyl-transform');
var source = require('vinyl-source-stream');
var watchify = require('watchify');
var NpmImportPlugin = require('less-plugin-npm-import');
var appJSName = 'nationalmap.js';
var appCssName = 'nationalmap.css';
var specJSName = 'nationalmap-tests.js';
var appEntryJSName = './index.js';
var testGlob = './test/**/*.js';
// Create the build directory, because browserify flips out if the directory that might
// contain an existing source map doesn't exist.
if (!fs.existsSync('wwwroot/build')) {
fs.mkdirSync('wwwroot/build');
}
gulp.task('build-app', ['prepare-terriajs'], function() {
return build(appJSName, appEntryJSName, false);
});
gulp.task('build-specs', ['prepare-terriajs'], function() {
return build(specJSName, glob.sync(testGlob), false);
});
gulp.task('build-css', function() {
return gulp.src('./index.less')
.pipe(less({
plugins: [
new NpmImportPlugin()
]
}))
.pipe(rename(appCssName))
.pipe(gulp.dest('./wwwroot/build/'));
});
gulp.task('build', ['build-css', 'build-app', 'build-specs']);
gulp.task('release-app', ['prepare'], function() {
return build(appJSName, appEntryJSName, true);
});
gulp.task('release-specs', ['prepare'], function() {
return build(specJSName, glob.sync(testGlob), true);
});
gulp.task('release', ['build-css', 'release-app', 'release-specs']);
gulp.task('watch-app', ['prepare'], function() {
return watch(appJSName, appEntryJSName, false);
});
gulp.task('watch-specs', ['prepare'], function() {
return watch(specJSName, glob.sync(testGlob), false);
});
gulp.task('watch-css', ['build-css'], function() {
return gulp.watch(['./index.less', './node_modules/terriajs/lib/Styles/*.less'], ['build-css']);
});
gulp.task('watch', ['watch-app', 'watch-specs', 'watch-css']);
gulp.task('lint', function(){
return gulp.src(['lib/**/*.js', 'test/**/*.js'])
.pipe(jshint())
.pipe(jshint.reporter('default'))
.pipe(jshint.reporter('fail'));
});
gulp.task('docs', function(){
return gulp.src('lib/**/*.js')
.pipe(jsdoc('./wwwroot/doc', undefined, {
plugins : ['plugins/markdown']
}));
});
gulp.task('prepare', ['prepare-terriajs']);
gulp.task('prepare-terriajs', function() {
return gulp.src([
'node_modules/terriajs/wwwroot/**'
], { base: 'node_modules/terriajs/wwwroot' })
.pipe(gulp.dest('wwwroot/build/TerriaJS'));
});
gulp.task('default', ['lint', 'build']);
function bundle(name, bundler, minify, catchErrors) {
// Combine main.js and its dependencies into a single file.
// The poorly-named "debug: true" causes Browserify to generate a source map.
var result = bundler.bundle();
if (catchErrors) {
// Display errors to the user, and don't let them propagate.
result = result.on('error', function(e) {
gutil.log('Browserify Error', e);
});
}
result = result
.pipe(source(name))
.pipe(buffer());
if (minify) {
// Minify the combined source.
// sourcemaps.init/write maintains a working source map after minification.
// "preserveComments: 'some'" preserves JSDoc-style comments tagged with @license or @preserve.
result = result
.pipe(sourcemaps.init({ loadMaps: true }))
.pipe(uglify({preserveComments: 'some', mangle: true, compress: true}))
.pipe(sourcemaps.write());
}
result = result
// Extract the embedded source map to a separate file.
.pipe(transform(function () { return exorcist('wwwroot/build/' + name + '.map'); }))
// Write the finished product.
.pipe(gulp.dest('wwwroot/build'));
return result;
}
function build(name, files, minify) {
return bundle(name, browserify({
entries: files,
debug: true
}), minify, false);
}
function watch(name, files, minify) {
var bundler = watchify(browserify({
entries: files,
debug: true,
cache: {},
packageCache: {}
}));
function rebundle() {
var start = new Date();
var result = bundle(name, bundler, minify, true);
result.on('end', function() {
console.log('Rebuilt ' + name + ' in ' + (new Date() - start) + ' milliseconds.');
});
return result;
}
bundler.on('update', rebundle);
return rebundle();
}