-
Notifications
You must be signed in to change notification settings - Fork 8
/
gulpfile.babel.js
357 lines (313 loc) · 10.5 KB
/
gulpfile.babel.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/*jslint node: true */
'use strict';
/*
This gulpfile was built for a few internal projects i use and is probably massivly overcomplicated
It's meant to be run on a fairly powerful machine, and it does all it can to speed up the compilation
process (at the expense of CPU, RAM, disk usage, etc...).
*/
import fs from 'fs';
import del from 'del';
import path from 'path';
import gulp from 'gulp';
import merge from 'merge-stream';
import glp from 'gulp-load-plugins';
var $ = glp({
pattern: ['*'],
rename: {
'lodash': '_',
'babel': 'babelProper',
'vulcanize': 'vulcanizeProper'
}
});
$.webComponentTester.gulp.init(gulp);
var PROD = false;
var DEPLOY = false;
var folderCache = new Map();
const SASS_OPTIONS = {
outputStyle: 'expanded',
includePaths: [$.nodeBourbon.includePaths, path.join('app', 'styles')]
};
const AUTOPREFIXER_OPTIONS = {
cascade: true,
remove: true,
browsers: [
'ie >= 10',
'ie_mob >= 10',
'ff >= 35',
'chrome >= 40',
'opera >= 26',
'safari >= 7',
'ios >= 7',
'android >= 4.4',
'bb >= 10'
]
};
const PLUMBER_OPTIONS = {
errorHandler: $.notify.onError("Error: <%= error.message %>")
};
const BABEL_OPTIONS = {
compact: false,
blacklist: [
'strict',
'jscript',
],
optional: [
'es7.functionBind'
],
externalHelpers: true,
loose: 'all'
};
const COFFEE_OPTIONS = {
bare: true
};
const UGLIFY_OPTIONS = {
'screw-ie8': true,
compress: {
unsafe: true
},
// Enable these when support for them drops in gulp-uglify
//'mangle-props': true,
//'mangle-regex': '/^_/'
};
const CLOSURE_OPTIONS = {
compilation_level: 'SIMPLE_OPTIMIZATIONS'
};
const CSSLINT_OPTIONS = {
ids: false,
'box-sizing': false,
'fallback-colors': false,
'compatible-vendor-prefixes': false
};
const MIN_CSS_OPTIONS = {
keepSpecialComments: 0
};
const IMAGEMIN_OPTIONS = {
optimizationLevel: 7,
progressive: true,
multipass: true,
use: [
$.imageminMozjpeg({
quality: 100,
}),
$.imageminPngcrush({
reduce: true
})
]
};
const VULCANIZE_OPTIONS = {
dest: 'build',
inlineCss: true,
inlineScripts: true,
stripExcludes: false,
excludes: [
'//fonts.googleapis.com/*',
'//pagead2.googlesyndication.com/*'
]
};
const MINIFY_INLINE_OPTIONS = {
css: false, // Shit's broken yo
jsSelector: 'script[type!="text/markdown"]'
};
const MINIFY_HTML_OPTIONS = {
quotes: true,
cdata: true,
empty: true,
spare: true
};
var compileCss = function compileCss(name, folder){
return [
$.plumber(PLUMBER_OPTIONS),
$.if(!PROD, $.sourcemaps.init()),
$.cached(name + '|css|' + folder),
$.if('*.sass', $.sass.sync(SASS_OPTIONS).on('error', $.sass.logError)),
$.autoprefixer(AUTOPREFIXER_OPTIONS),
$.csslint(CSSLINT_OPTIONS),
$.csslint.reporter($.csslintStylish),
$.remember(name + '|css|' + folder),
'concat',
$.if(PROD, $.cached(name + '|minifycss|' + folder)),
$.if(PROD, $.minifyCss(MIN_CSS_OPTIONS)),
$.if(PROD, $.remember(name + '|minifycss|' + folder)),
$.if(!PROD, $.sourcemaps.write()),
$.plumber.stop()
];
};
var compileJs = function compileJs(name, folder){
return [
$.plumber(PLUMBER_OPTIONS),
$.if(!PROD, $.sourcemaps.init()),
$.if('*.coffee', $.cached(name + '|coffeescript|' + folder)),
$.if('*.coffee', $.coffee(COFFEE_OPTIONS)),
$.if('*.coffee', $.remember(name + '|coffeescript|' + folder)),
$.if('*/elements/*', $.cached(name + '|jshint|' + folder)),
$.if('*/elements/*', $.jshint()),
$.if('*/elements/*', $.jshint.reporter($.jshintStylish)),
$.if('*/elements/*', $.remember(name + '|jshint|' + folder)),
$.cached(name + '|babel|' + folder),
$.babel(BABEL_OPTIONS),
$.remember(name + '|babel|' + folder),
'concat',
$.if(PROD, $.cached(name + '|uglify|' + folder)),
//$.if(DEPLOY, $.closureCompilerService(CLOSURE_OPTIONS)),
$.if(PROD, $.uglify(UGLIFY_OPTIONS)),
// Because of a bug in some minifier somewhere, i need to do this...
$.if(PROD, $.replace('\'"></script>', '\'"><\\/script>')),
$.if(PROD, $.replace('0);</script>"', '0);<\\/script>"')),
$.if(PROD, $.remember(name + '|uglify|' + folder)),
$.stripComments({safe: false, line: true}),
$.if(!PROD, $.sourcemaps.write()),
$.plumber.stop()
];
};
gulp.task('compileAssets', ['copy'], () => {
return merge(getFolders(path.join('app', 'elements')).concat('').map((folder) => {
let src = path.join(...(folder ? ['app', 'elements', folder, '*.html'] : ['app', 'index.html']));
let dest = path.join(...(folder ? ['build', 'elements', folder] : ['build']));
let useminOptions = {};
buildUseminLoops(['css'], 1).forEach((name) => useminOptions[name] = compileCss(name, folder));
buildUseminLoops(['js'], 2).forEach((name) => useminOptions[name] = compileJs(name, folder));
return gulp.src(src)
.pipe($.duration('compile ' + (folder ? folder : 'index.html')))
.pipe($.plumber(PLUMBER_OPTIONS))
.pipe($.cached('htmlAutoprefixer|' + folder))
.pipe($.htmlAutoprefixer(AUTOPREFIXER_OPTIONS))
.pipe($.remember('htmlAutoprefixer|' + folder))
.pipe($.usemin(useminOptions))
.pipe($.cached('writeHtml|' + folder))
.pipe(gulp.dest(dest))
.pipe($.plumber.stop());
}));
});
gulp.task('minifyIndex', ['copy', 'copyBowerComponents', 'compileAssets', 'vulcanize'], () => {
return gulp.src(path.join('build', 'index.html'))
.pipe($.cached('minifyIndex'))
.pipe($.if(PROD, $.minifyInline(MINIFY_INLINE_OPTIONS)))
.pipe($.if(PROD, $.minifyHtml(MINIFY_HTML_OPTIONS)))
.pipe(gulp.dest('build'));
});
gulp.task('vulcanize', ['copy', 'copyBowerComponents', 'compileAssets'], () => {
return gulp.src(path.join('build', 'elements', 'elements.html'))
.pipe($.if(PROD, $.vulcanize(VULCANIZE_OPTIONS)))
.pipe($.cached('vulcanize'))
.pipe($.if(PROD, $.minifyInline(MINIFY_INLINE_OPTIONS)))
// Because of a bug in some minifier somewhere, i need to do this...
.pipe($.if(PROD, $.replace('\'"></script>', '\'"><\\/script>')))
.pipe($.if(PROD, $.replace('0);</script>"', '0);<\\/script>"')))
//.pipe($.if(PROD, $.minifyHtml(MINIFY_HTML_OPTIONS)))
.pipe($.size())
.pipe(gulp.dest(path.join('build', 'elements')));
});
// Copy everything over
// Do this before any other compilation passes because it copies EVERYTHING
// This is to ensure that nothing that isn't compiled isn't missed
gulp.task('copy', () => {
return gulp.src(path.join('app', '**', '*'), {
base: 'app'
})
.pipe($.cached('copy'))
.pipe($.if(DEPLOY, $.imagemin(IMAGEMIN_OPTIONS)))
.pipe(gulp.dest(path.join('build')));
});
gulp.task('copyBowerComponents', () => {
return gulp.src(path.join('bower_components', '**', '*'))
.pipe($.cached('copyBowerComponents'))
.pipe($.if(DEPLOY, $.imagemin(IMAGEMIN_OPTIONS)))
.pipe(gulp.dest(path.join('build', 'bower_components')));
});
gulp.task('serve', ['build'], () => {
$.browserSync({
notify: false,
server: {
baseDir: 'build',
routes: {
'/bower_components': 'bower_components',
'/app': 'app'
}
}
});
gulp.watch(path.join('app', '**', '*'), ['build', $.browserSync.reload]);
});
gulp.task('serve:dist', ['build:dist'], () => {
$.browserSync({
notify: false,
server: {
baseDir: 'build'
}
});
gulp.watch(path.join('app', '**', '*'), ['build:dist', $.browserSync.reload]);
});
gulp.task('production', ()=> PROD = true);
gulp.task('deploy_flag', ()=> DEPLOY = true);
gulp.task('clean', del.bind(null, ['build']));
gulp.task('uninstall', ['clean'], del.bind(null, ['node_modules', 'bower_components']));
gulp.task('build', ['compileAssets']);
gulp.task('build:dist', ['production','build','copyBowerComponents','vulcanize','minifyIndex']);
gulp.task('deploy', ['deploy_flag', 'build:dist'], (cb)=>{
$.ghPages.publish(path.join(process.cwd(), 'build'), {clone: '.publish'}, cb);
});
gulp.task('devdeploy', (cb)=>{
$.ghPages.publish(path.join(process.cwd(), 'build'), {clone: '.publish'}, cb);
});
// Below this are just helper functions...
// Recursively gets all folders in the directory given and returns them in an array
// Won't return any folders that haven't changed since the last time this was run
var getFolders = function getFolders(dir, rootDir = dir) {
let folderArray = [];
fs.readdirSync(dir).forEach((file) => {
let dirName = path.join(dir, file);
if(fs.statSync(dirName).isDirectory()){
[].push.apply(folderArray, getFolders(dirName, rootDir).concat(dirName));
}
});
let fixedRootPathFolderArray = folderArray.map((filePath)=> filePath.replace(rootDir + '\\', ''));
if(rootDir === dir){
// if we are here we are in the first getFolders call...
return onlyCompileChanged(fixedRootPathFolderArray, dir);
}else{
return fixedRootPathFolderArray;
}
};
var onlyCompileChanged = function onlyCompileChanged(folderArray, dir){
var forceRecompile = new Set();
// get all folders and don't return them if they haven't changed
let foldersToCompile = folderArray.filter((partialDirectoryPath)=>{
let directoryPath = path.join(dir, partialDirectoryPath);
let totalMTime = fs.readdirSync(directoryPath).reduce((runningMTime, fileName) => {
let individualFile = path.join(directoryPath, fileName);
return (runningMTime * 1) + (fs.statSync(individualFile).mtime.getTime() * 1);
}, 0);
if(
forceRecompile.has(partialDirectoryPath) ||
!folderCache.has(directoryPath) ||
folderCache.get(directoryPath) !== totalMTime
){
//set the cache to the new mtime for this folder
folderCache.set(directoryPath, totalMTime);
//Now set the forceRecompile set to have all parents as well
partialDirectoryPath.split(path.sep).forEach((value, index, arr)=>{
let pathToForce = '';
$._.times(index, (x)=>{
pathToForce = path.join(pathToForce, arr[x]);
});
forceRecompile.add(pathToForce);
});
return true;
}
return false;
});
return foldersToCompile;
};
// Builds the usemin arrays fn(['js', 'coffee'], 2) becomes ['js', 'coffee', 'inlinejs', 'inlinecoffee', 'js2', 'coffee2']
var buildUseminLoops = function buildUseminLoops(types, number){
return $._.flattenDeep($._.map(new Array(number), (value, index)=>{
return $._.map(types, (type)=> {
return $._.map(['', 'inline'], (inline)=> {
if(index > 0 && inline === 'inline'){
return null;
}else{
return inline + type + (index > 0 ? index : '');
}
});
});
})).filter((item)=> item);
};