forked from google/incremental-dom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
181 lines (161 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
173
174
175
176
177
178
179
180
181
/**
* Copyright 2015 The Incremental DOM Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var babel = require('gulp-babel');
var del = require('del');
var rollup = require('rollup');
var file = require('gulp-file');
var fs = require('fs');
var gjslint = require('gulp-gjslint');
var gulp = require('gulp');
var gutil = require('gulp-util');
var karma = require('karma').server;
var path = require('path');
var replace = require('gulp-replace');
var source = require('vinyl-source-stream');
var sourcemaps = require('gulp-sourcemaps');
var uglify = require('gulp-uglify');
var entryFileName = 'index.js';
var artifactName = 'incremental-dom';
var googModuleName = 'incrementaldom';
var jsFileName = 'incremental-dom.js';
var srcs = [jsFileName, 'src/**/*.js'];
var tests = ['test/**/*.js'];
var karmaConfig = path.resolve('conf/karma.conf.js');
function clean() {
return del(['dist/']);
}
function unit(done) {
karma.start({
configFile: karmaConfig,
singleRun: true,
browsers: ['Chrome', 'Firefox']
}, done);
}
function unitPhantom(done) {
karma.start({
configFile: karmaConfig,
singleRun: true,
browsers: ['PhantomJS']
}, done);
}
function unitWatch(done) {
karma.start({
configFile: karmaConfig,
browsers: ['Chrome']
}, done);
}
function lint() {
return gulp.src(srcs, tests)
.pipe(gjslint())
.pipe(gjslint.reporter('console'))
.pipe(gjslint.reporter('fail'));
}
function inlineEnv() {
return babel({
whitelist: [
'utility.inlineEnvironmentVariables'
]
});
}
function bundle(format) {
return rollup.rollup({
entry: entryFileName,
}).then(function(bundle) {
return bundle.generate({
format: format,
banner: fs.readFileSync('conf/license_header.txt', 'utf8'),
sourceMap: true,
moduleName: 'IncrementalDOM'
});
}).then(function(gen) {
gen.code += '\n//# sourceMappingURL=' + gen.map.toUrl();
return gen;
});
}
function js() {
process.env.NODE_ENV = 'development';
return bundle('umd').then(function(gen) {
return file(artifactName + '.js', gen.code, {src: true})
.pipe(sourcemaps.init({loadMaps: true}))
.pipe(inlineEnv())
.pipe(babel())
.pipe(sourcemaps.write('./'))
.pipe(gulp.dest('dist'));
});
}
function jsWatch() {
gulp.watch(srcs, ['js']);
}
function jsMin() {
process.env.NODE_ENV = 'production';
return bundle('umd').then(function(gen) {
return file(artifactName + '-min.js', gen.code, {src: true})
.pipe(sourcemaps.init({loadMaps: true}))
.pipe(inlineEnv())
.pipe(babel())
.pipe(uglify({
preserveComments: 'some'
}))
.pipe(sourcemaps.write('./'))
.pipe(gulp.dest('dist'));
});
}
function jsClosure(done) {
return bundle('cjs').then(function(gen) {
// Replace the first line with a goog.module declaration.
var moduleDeclaration = 'goog.module(\'' + googModuleName + '\');';
var code = gen.code.replace(/.*/, moduleDeclaration)
.replace(/'use strict';/, '');
return file(artifactName + '-closure.js', code, {src: true})
.pipe(sourcemaps.init({loadMaps: true}))
.pipe(replace('process.env.NODE_ENV !== \'production\'', 'goog.DEBUG'))
.pipe(babel())
.pipe(sourcemaps.write('./'))
.pipe(gulp.dest('dist'));
});
}
function jsCommonJS() {
return bundle('cjs').then(function(gen) {
return file(artifactName + '-cjs.js', gen.code, {src: true})
.pipe(sourcemaps.init({loadMaps: true}))
.pipe(babel())
.pipe(sourcemaps.write('./'))
.pipe(gulp.dest('dist'));
});
}
function jsDist() {
// These must be run serially: clean must complete before any of the js
// targets run. The js and jsMin targets cannot run in parallel as they both
// change process.env.NODE_ENV. The CommonJS target could run in parallel
// with the js and jsMin targets, but currently is not.
return clean()
.then(jsCommonJS)
.then(js)
.then(jsMin);
}
gulp.task('clean', clean);
gulp.task('unit', unit);
gulp.task('unit-phantom', unitPhantom);
gulp.task('unit-watch', unitWatch);
gulp.task('lint', lint);
gulp.task('js', js);
gulp.task('js-watch', jsWatch);
gulp.task('js-min', jsMin);
gulp.task('js-dist', jsDist);
gulp.task('js-closure', jsClosure);
gulp.task('build', ['lint', 'unit', 'js']);
gulp.task('dist', ['lint', 'unit-phantom', 'js-dist']);
gulp.task('default', ['build']);