forked from JamBrain/JamBrain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
306 lines (262 loc) · 9.15 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
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
'use strict';
require('es6-promise').polyfill();
require('es6');
// Our Local Configuration //
var config = require('./config.js');
// Gulp Includes //
var gulp = require('gulp');
var debug = require('gulp-debug');
var newer = require('gulp-newer');
var rename = require('gulp-rename');
var cat = require('gulp-concat');
var notify = require('gulp-notify');
var gzip = require('gulp-gzip');
var size = require('gulp-size');
// Other Includes //
var glob = require("glob")
var ignore_folders = glob.sync('src/**/')
.filter(function(val){
return val.indexOf('/_') >= 0;
})
.map(function(el){
return '!'+el+'**/*.*';
})
.concat(['src/embed/']);
var es6ignore_files = glob.sync('src/**/.es6ignore')
.map(function(el){
return el.replace('.es6ignore','');
});
var build_files = glob.sync('src/**/build.json');
var build_data = build_files.map(function(file){
return require('./'+file);
});
//console.log(build_data);
// Ignore any minified files, or files/folders prefixed with an underscore //
var less_files = ['src/**/*.less','!src/**/_*.*']
.concat(ignore_folders);
var css_files = ['src/**/*.css','!src/**/*.min.css','!src/**/_*.*']
.concat(ignore_folders);
var js_in_files = ['src/**/*.js','!src/**/*.min.js','!src/**/_*.*']
.concat(es6ignore_files.map(function(el){
return '!'+el+'**/*.*';
}))
.concat(ignore_folders);
var raw_js_in_files = es6ignore_files.map(function(el){
return el+'**/*.js';
})
.concat(es6ignore_files.map(function(el){
return '!'+el+'**/*.min.js';
}))
.concat(es6ignore_files.map(function(el){
return '!'+el+'**/_*.*';
}))
.concat(ignore_folders);
var build_folder = 'output';
var release_folder = 'public-static/output';
// Ignore any min files, and the output file //
var css_output = 'all.css';
var css_min_output = 'all.min.css';
var css_min_gz_output = 'all.min.css.gz';
var css_out_files = [
build_folder+'/**/*.css',
'!'+build_folder+'/'+css_output
];
var js_output = 'all.js';
var js_min_output = 'all.min.js';
var js_min_gz_output = 'all.min.js.gz';
var js_out_files = [
build_folder+'/**/*.js',
'!'+build_folder+'/**/*.o.js',
'!'+build_folder+'/'+js_output
];
/* LESS files to CSS */
gulp.task('less', function() {
var less = require('gulp-less');
// NOTE: Sourcemaps is surpressing errors, so it's disabled for now
// var sourcemaps = require('gulp-sourcemaps');
// var less = require('gulp-less-sourcemap');
// NOTE: We're running autoprefixer as a LESS plugin, due to problems with postcss sourcemaps
var autoprefix = require('less-plugin-autoprefix');
var AUTOPREFIX_CONFIG = {
// browsers: ["last 2 versions"]
};
return gulp.src( less_files )
.pipe( newer({dest:build_folder,ext:".css"}) )
.pipe( debug({title:'less:'}) )
// .pipe( sourcemaps.init() )
.pipe( less({
'globalVars': config.LESS_GLOBALVARS,
plugins: [
new autoprefix(AUTOPREFIX_CONFIG)
]
}) )
// .pipe( sourcemaps.write() )
.pipe( gulp.dest(build_folder+'/') );
});
/* Unprocessed CSS files */
gulp.task('css', function() {
return gulp.src( css_files )
.pipe( newer({dest:build_folder}) )
.pipe( debug({title:'css:'}) )
.pipe( gulp.dest(build_folder+'/') );
});
/* Concatenate all CSS files */
gulp.task('css-cat', ['less','css'], function() {
//gulp.task('css-cat', gulp.series(['less','css'], function() {
return gulp.src( css_out_files )
.pipe( newer({dest:build_folder+'/'+css_output}) )
.pipe( cat( css_output ) )
.pipe( size({title:'css-cat:',showFiles:true}) )
.pipe( gulp.dest( build_folder+'/' ) );
});
/* Minifiy the concatenated CSS file */
gulp.task('css-min', ['css-cat'], function() {
//gulp.task('css-min', gulp.series(['css-cat'], function() {
// Benchmarks: http://goalsmashers.github.io/css-minification-benchmark/
var cleancss = require('gulp-cleancss'); // Faster, similar results
// var cssnano = require('gulp-cssnano');
return gulp.src( build_folder+'/'+css_output )
.pipe( newer({dest:release_folder+'/'+css_min_output}) )
.pipe( cleancss() )
// .pipe( cssnano() )
.pipe( cat( css_min_output ) )
.pipe( size({title:'css-min:',showFiles:true}) )
.pipe( gulp.dest( release_folder+'/' ) );
});
/* GZIP minified (for reference) */
gulp.task('css-min-gz', ['css-min'], function() {
//gulp.task('css-min-gz', gulp.series(['css-min'], function() {
return gulp.src( release_folder+'/'+css_min_output )
.pipe( newer({dest:build_folder+'/'+css_min_gz_output}) )
.pipe( gzip() )
.pipe( size({title:'css-min-gz:',showFiles:true}) )
.pipe( gulp.dest( build_folder+'/' ) );
});
/* Use Buble to compile ES2015 files to JS */
gulp.task('buble', function() {
var buble = require("gulp-buble");
var BUBLE_CONFIG = {
transforms: {
modules: false, // ignore import/export's
// generator: false
},
jsx: "h"
};
return gulp.src( js_in_files, {base:'src'} )
.pipe( newer({dest:build_folder,ext:".o.js"}) )
.pipe( debug({title:'js (buble):'}) )
.pipe( buble(BUBLE_CONFIG) )
.pipe( rename({extname: ".o.js"}) )
.pipe( gulp.dest( build_folder+'/' ) );
});
gulp.task('buble-rollup', ['buble'], function() {
var rollup = require('rollup-stream');
var source = require('vinyl-source-stream');
var includePaths = require("rollup-plugin-includepaths");
// var nodeResolve = require("rollup-plugin-node-resolve");
return rollup({
entry: './output/main/main.o.js',
plugins: [
includePaths({
paths: [
'output/external',
'output/custom',
],
// include: {
// 'preact':'output/external/preact/preact.o.js'
// },
extensions:['.js','.o.js']
}),
// nodeResolve({
// jsnext: true
// }),
]
})
.pipe(source('buble.js'))
.pipe(gulp.dest('./output'));
});
/* Unprocessed JS files */
gulp.task('js', function() {
return gulp.src( raw_js_in_files, {base:'src'} )
.pipe( newer({dest:build_folder}) )
.pipe( debug({title:'js (raw):'}) )
.pipe( gulp.dest( build_folder+'/' ) );
});
/* Concatenate all JS files */
gulp.task('js-cat', ['buble-rollup','js'], function() {
//gulp.task('js-cat', gulp.series(['buble-rollup','js'], function() {
return gulp.src( js_out_files )
.pipe( newer({dest:build_folder+'/'+js_output}) )
.pipe( cat( js_output ) )
.pipe( size({title:'js-cat:',showFiles:true}) )
.pipe( gulp.dest( build_folder+'/' ) );
});
/* Minifiy the concatenated JS file */
gulp.task('js-min', ['js-cat'], function() {
//gulp.task('js-min', gulp.series(['js-cat'], function() {
var uglify = require('gulp-uglify');
return gulp.src( build_folder+'/'+js_output )
.pipe( newer({dest:release_folder+'/'+js_min_output}) )
.pipe( uglify() )
.pipe( cat( js_min_output ) )
.pipe( size({title:'js-min:',showFiles:true}) )
.pipe( gulp.dest( release_folder+'/' ) );
});
/* GZIP minified (for reference) */
gulp.task('js-min-gz', ['js-min'], function() {
//gulp.task('js-min-gz', gulp.series(['js-min'], function() {
return gulp.src( release_folder+'/'+js_min_output )
.pipe( newer({dest:build_folder+'/'+js_min_gz_output}) )
.pipe( gzip() )
.pipe( size({title:'js-min-gz:',showFiles:true}) )
.pipe( gulp.dest( build_folder+'/' ) );
});
/* Nuke the output folders */
gulp.task('clean', function() {
var del = require('del');
return del( [build_folder+'/**/*',release_folder+'/**/*'] );
});
// Testing generation of a file used by PHP
gulp.task('git-version.php', function() {
var fs = require('fs');
require('shelljs/global');
var Hash = exec('git describe --always',{silent:true}).stdout.trim();
var Commit = exec('git rev-list HEAD | wc -l',{silent:true}).stdout.trim();
fs.writeFileSync('output/git-version.php', "<?php\n// WARNING! DO NOT MODIFY! This file is automatically generated by gulpfile.js!\n\nconst GIT_VERSION = '"+Commit+"-"+Hash+"';\n");
});
// Testing generation of a file used by PHP
//gulp.task('php-com', function() {
// var fs = require('fs');
//
// fs.writeFileSync('src/com/list.gen.php', "<?php\n// WARNING! DO NOT MODIFY! This file is automatically generated!\n\n");
//});
// TODO: Popup notifications when a watch catches an error/linting error
// .pipe( notify("hello") )
// NOTE: Use gulp-watch instead: https://www.npmjs.com/package/gulp-watch
//gulp.task('less-watch', ['css','js'] function () {
// gulp.watch(less_files, ['css'])
// gulp.watch(js_out_files, ['js'])
//});
// By default, GZIP the files, to report roughly how large things are when GZIPPED
gulp.task('default', ['css-min-gz','js-min-gz'], function(done) {
//gulp.task('default', gulp.series(['css-min-gz','js-min-gz'], function(done) {
var symlink = require('gulp-symlink'); // Depricated in Gulp 4
var IS_DEBUG = glob.sync('.gulpdebug').length > 0;
if ( IS_DEBUG ) {
// Only create symlinks if they don't exist //
if ( glob.sync(release_folder+'/'+js_output).length === 0 ) {
gulp.src( build_folder+'/'+css_output )
.pipe( symlink( release_folder+'/'+css_output ) );
// .pipe( gulp.symlink( release_folder+'/'+css_output ) );
}
if ( glob.sync(release_folder+'/'+js_output).length === 0 ) {
gulp.src( build_folder+'/'+js_output )
.pipe( symlink( release_folder+'/'+js_output ) );
// .pipe( gulp.symlink( release_folder+'/'+js_output ) );
}
}
if ( glob.sync('output/git-version.php').length === 0 ) {
gulp.start('git-version.php');
}
return done();
});