-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
235 lines (207 loc) · 6.52 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
/*!
* Facebook React Starter Kit | https://github.com/kriasoft/react-starter-kit
* Copyright (c) KriaSoft, LLC. All rights reserved. See LICENSE.txt
*/
'use strict';
// Include Gulp and other build automation tools and utilities
// See: https://github.com/gulpjs/gulp/blob/master/docs/API.md
var gulp = require('gulp');
var $ = require('gulp-load-plugins')();
var del = require('del');
var path = require('path');
var merge = require('merge-stream');
var runSequence = require('run-sequence');
var webpack = require('webpack');
var browserSync = require('browser-sync');
var pagespeed = require('psi');
var argv = require('minimist')(process.argv.slice(2));
// Settings
var DEST = './build'; // The build output folder
var RELEASE = !!argv.release; // Minimize and optimize during a build?
var GOOGLE_ANALYTICS_ID = 'UA-XXXXX-X'; // https://www.google.com/analytics/web/
var AUTOPREFIXER_BROWSERS = [ // https://github.com/ai/autoprefixer
'ie >= 10',
'ie_mob >= 10',
'ff >= 30',
'chrome >= 34',
'safari >= 7',
'opera >= 23',
'ios >= 7',
'android >= 4.4',
'bb >= 10'
];
var src = {};
var watch = false;
var pkgs = (function () {
var temp = {};
var map = function (source) {
for (var key in source) {
temp[key.replace(/[^a-z0-9]/gi, '')] = source[key].substring(1);
}
};
map(require('./package.json').dependencies);
return temp;
}());
// The default task
gulp.task('default', ['serve']);
// Clean up
gulp.task('clean', del.bind(null, [DEST]));
var concat = require('gulp-concat');
var sourcemaps = require('gulp-sourcemaps');
var ts = require('gulp-typescript');
var merge = require('merge2');
var tsProject = ts.createProject({
declarationFiles: false,
noExternalResolve: true,
target: 'ES5',
module: 'CommonJS',
sortOutput: true
});
gulp.task('models', function() {
src.models = '{models,repositories}/*.ts';
var tsResult = gulp.src(src.models)
.pipe(sourcemaps.init())
.pipe(ts(tsProject));
return merge([ // Merge the two output streams, so this task is finished when the IO of both operations are done.
//tsResult.dts.pipe(gulp.dest('build/models/definitions')),
tsResult.js.pipe(gulp.dest('build/js'))
])
.pipe(concat('models.js')) // You can use other plugins that also support gulp-sourcemaps
.pipe(sourcemaps.write()) // Now the sourcemaps are added to the .js file
.pipe(gulp.dest('build/js'));
});
// 3rd party libraries
gulp.task('vendor', function () {
return merge(
gulp.src('./node_modules/jquery/dist/**')
.pipe(gulp.dest(DEST + '/vendor/jquery-' + pkgs.jquery)),
gulp.src('./node_modules/bootstrap/dist/fonts/**')
.pipe(gulp.dest(DEST + '/fonts'))
);
});
// Static files
gulp.task('assets', function () {
src.assets = 'src/assets/**';
return gulp.src(src.assets)
.pipe($.changed(DEST))
.pipe(gulp.dest(DEST))
.pipe($.size({title: 'assets'}));
});
// Images
gulp.task('images', function () {
src.images = 'src/images/**';
return gulp.src(src.images)
.pipe($.changed(DEST + '/images'))
.pipe($.cache($.imagemin({
progressive: true,
interlaced: true
})))
.pipe(gulp.dest(DEST + '/images'))
.pipe($.size({title: 'images'}));
});
// HTML pages
gulp.task('pages', function () {
src.pages = 'src/pages/**/*.html';
return gulp.src(src.pages)
.pipe($.changed(DEST))
.pipe($.if(RELEASE, $.htmlmin({
removeComments: true,
collapseWhitespace: true,
minifyJS: true
})))
.pipe(gulp.dest(DEST))
.pipe($.size({title: 'pages'}));
});
// CSS style sheets
gulp.task('styles', function () {
src.styles = 'src/styles/**/*.{css,less}';
return gulp.src('src/styles/bootstrap.less')
.pipe($.plumber())
.pipe($.less({
sourceMap: !RELEASE,
sourceMapBasepath: __dirname
}))
.on('error', console.error.bind(console))
.pipe($.autoprefixer({browsers: AUTOPREFIXER_BROWSERS}))
.pipe($.csscomb())
.pipe($.if(RELEASE, $.minifyCss()))
.pipe(gulp.dest(DEST + '/css'))
.pipe($.size({title: 'styles'}));
});
// Bundle
gulp.task('bundle', function (cb) {
var started = false;
var config = require('./config/webpack.js')(RELEASE);
var bundler = webpack(config);
function bundle(err, stats) {
if (err) {
throw new $.util.PluginError('webpack', err);
}
!!argv.verbose && $.util.log('[webpack]', stats.toString({colors: true}));
if (!started) {
started = true;
return cb();
}
}
if (watch) {
bundler.watch(200, bundle);
} else {
bundler.run(bundle);
}
});
// Build the app from source code
gulp.task('build', ['clean'], function (cb) {
runSequence('models',['vendor', 'assets', 'images', 'pages', 'styles'],'bundle', cb);
});
// Launch a lightweight HTTP Server
gulp.task('serve', function (cb) {
watch = true;
runSequence('build', function () {
browserSync({
notify: false,
// Customize the BrowserSync console logging prefix
logPrefix: 'RSK',
// Run as an https by uncommenting 'https: true'
// Note: this uses an unsigned certificate which on first access
// will present a certificate warning in the browser.
// https: true,
server: DEST
});
gulp.watch(src.assets, ['assets']);
gulp.watch(src.images, ['images']);
gulp.watch(src.pages, ['pages']);
gulp.watch(src.models, ['models']);
gulp.watch(src.styles, ['styles']);
gulp.watch(DEST + '/**/*.*', function (file) {
browserSync.reload(path.relative(__dirname, file.path));
});
cb();
});
});
// Deploy to GitHub Pages
gulp.task('deploy', function () {
// Remove temp folder
if (argv.clean) {
var os = require('os');
var path = require('path');
var repoPath = path.join(os.tmpdir(), 'tmpRepo');
$.util.log('Delete ' + $.util.colors.magenta(repoPath));
del.sync(repoPath, {force: true});
}
return gulp.src(DEST + '/**/*')
.pipe($.if('**/robots.txt', !argv.production ? $.replace('Disallow:', 'Disallow: /') : $.util.noop()))
.pipe($.ghPages({
remoteUrl: 'https://github.com/{name}/{name}.github.io.git',
branch: 'master'
}));
});
// Run PageSpeed Insights
// Update `url` below to the public URL for your site
gulp.task('pagespeed', pagespeed.bind(null, {
// By default, we use the PageSpeed Insights
// free (no API key) tier. You can use a Google
// Developer API key if you have one. See
// http://goo.gl/RkN0vE for info key: 'YOUR_API_KEY'
url: 'https://example.com',
strategy: 'mobile'
}));