var gulp = require('gulp');
var concat = require('gulp-concat');
var uglify = require('gulp-uglify-es').default;
var rename = require('gulp-rename');
var inject = require('gulp-inject-string');
var config = {
jsPath: './app',
jsApps: [
"tt-copyright",
"tt-collapse",
"add-clipboard-to-element"
],
};
initGulpTaskPerApp();
function initGulpTaskPerApp(){
let jsPathsAllApps = [];
config.jsApps.forEach(function(app){
let appDir = config.jsPath + "/"+app;
let jsPathsApp = getJsPathsForApp(app);
jsPathsAllApps = jsPathsAllApps.concat(jsPathsApp);
gulp.task('concat '+app+' js', function(done) {
getGulpSrc(jsPathsApp, appDir, false);
done();
});
gulp.task('minify '+app+' js', function(done) {
getGulpSrc(jsPathsApp, appDir, true);
done();
});
//Watch Task for Minify JS
gulp.task('watch '+app+' minify', function() {
gulp.watch(jsPathsApp).on("change",gulp.series('minify '+app+' js'));
});
});
const concatSeries = minifyAllApps("concat");
gulp.task('concat all', concatSeries);
const minifySeries = minifyAllApps("minify");
gulp.task('minify all', minifySeries);
gulp.task('watch concat all', function() {
gulp.watch(jsPathsAllApps).on("change",gulp.series('concat all'));
});
gulp.task('watch minify all', function() {
gulp.watch(jsPathsAllApps).on("change", gulp.series('minify all'));
});
}
function minifyAllApps(minify = "minify"){
let gulpSeries = [];
config.jsApps.forEach(function(app){
gulpSeries.push(minify+' '+app+' js');
//gulp.series('concat '+app+' js');
});
return gulp.series(gulpSeries);
}
function getJsPathsForApp(app){
let appPath = config.jsPath + "/"+app;
//Path to all relevant js files for all.min.js
//Includes jquery first
return [ //config.jsPath + '/lib/jquery.min.js', //jQuery Core
appPath + '/lib{,/**}/*.js', //Include own libs
appPath + '/*.js', //Include js in js-root
'!' + appPath + '/extra/**', //Exclude "extra" files
'!' + appPath + '/all.min.js', //Exclude all.min.js itself
'!' + appPath + '/all.js']; //Exclude all.js itself
}
function getGulpSrc(jsPaths, appDir, minify = false) {
let gulpSrc = gulp.src(jsPaths)
.pipe(concat('all.js'))
//.pipe(inject.before("jQuery(document).ready",getInjectJSString()))
.pipe(gulp.dest(appDir))
.pipe(rename('all.min.js'));
if(minify)
gulpSrc.pipe(uglify())
gulpSrc.pipe(gulp.dest(appDir));
return gulpSrc;
}
function getInjectJSString(){
var date = new Date();
var ret = "";
ret += "//Generated by Gulp (PIC): Timestring of Generation \r\n";
ret += "pic_globals.generated = \"" + date.toISOString() + "\";";
ret += "\r\n";
ret += "//End: Generated by Gulp";
ret += "\r\n";
return ret;
}