hypothetical/gulpfile.js

213 lines
5.9 KiB
JavaScript
Raw Normal View History

// Core packages
const gulp = require("gulp"),
minimist = require("minimist"),
log = require("fancy-log"),
plumber = require("gulp-plumber"),
concat = require("gulp-concat");
// Sass packages
const sass = require("gulp-sass"),
sassGlob = require("gulp-sass-glob"),
postCSS = require("gulp-postcss"),
autoprefixer = require("autoprefixer");
// Javascript packages
const babel = require("gulp-babel"),
stripDebug = require("gulp-strip-debug"),
uglify = require("gulp-uglify");
// Vue packages
const browserify = require("browserify"),
vueify = require("vueify"),
source = require("vinyl-source-stream"),
buffer = require("vinyl-buffer");
// Determine if gulp has been run with --production
const isProduction = minimist(process.argv.slice(2)).production !== undefined;
2018-04-06 00:29:22 -04:00
// Include browsersync when gulp has not been run with --production
let browserSync = undefined;
if (!isProduction) {
browserSync = require("browser-sync").create();
}
// Declare plugin settings
const sassOutputStyle = isProduction ? "compressed" : "nested",
sassPaths = [ "bower_components", "node_modules" ],
autoprefixerSettings = { remove: false, cascade: false, browsers: [ "last 6 versions" ] },
vuePaths = [ "./bower_components", "./node_modules", "./resources/components", "./resources/assets/js" ];
2016-03-30 00:51:54 -04:00
// Javascript files for the public site
const jsPublic = [ "resources/assets/js/app.js" ];
// Javascript libraries for the public site
const jsPublicLibs = [
"node_modules/jquery/dist/jquery.js",
"node_modules/popper.js/dist/umd/popper.js",
"node_modules/bootstrap/dist/js/bootstrap.js",
2017-11-21 18:49:59 -05:00
"node_modules/gsap/src/uncompressed/TweenMax.js",
2017-11-21 18:47:31 -05:00
"node_modules/what-input/dist/what-input.js"
];
// Javascript files for the dashboard
const jsDashboard = [
"resources/assets/js/dashboard.js"
];
// Javascript libraries for the dashboard
const jsDashboardLibs = [
"bower_components/jquery/dist/jquery.js",
"bower_components/bootstrap-sass/assets/javascripts/bootstrap.js",
"bower_components/Sortable/Sortable.js",
"bower_components/pickadate/lib/picker.js",
"bower_components/pickadate/lib/picker.date.js",
"bower_components/list.js/dist/list.js",
"bower_components/simplemde/dist/simplemde.min.js"
];
// Paths to folders containing fonts that should be copied to public/fonts/
const fontPaths = [
"resources/assets/fonts/**"
2016-03-30 00:51:54 -04:00
];
// Handle errors
function handleError(err) {
log.error(err);
this.emit("end");
2016-03-30 00:51:54 -04:00
}
// Process sass
function processSass(filename) {
2018-04-06 00:29:22 -04:00
const css = gulp.src("resources/assets/sass/" + filename + ".scss")
.pipe(plumber(handleError))
.pipe(sassGlob())
.pipe(sass({ outputStyle: sassOutputStyle, includePaths: sassPaths }))
.pipe(postCSS([ autoprefixer(autoprefixerSettings) ]))
.pipe(concat(filename + ".css"))
.pipe(gulp.dest("public/css/"));
2018-04-06 00:29:22 -04:00
if (!isProduction) {
css.pipe(browserSync.stream({ match: "**/" + filename + ".css" }));
2018-04-06 00:29:22 -04:00
}
return css;
2016-03-30 00:51:54 -04:00
}
// Process vue
function processVue(ouputFilename, inputFile) {
const javascript = browserify({
entries: [ inputFile ],
paths: vuePaths
}).transform("babelify")
.transform(vueify)
.bundle()
.on("error", handleError)
.pipe(source(ouputFilename + ".js"))
.pipe(buffer());
if (isProduction) {
javascript.pipe(stripDebug()).pipe(uglify().on("error", handleError));
}
return javascript.pipe(gulp.dest("public/js/"));
}
// Process javascript
function processJavaScript(ouputFilename, inputFiles, es6) {
const javascript = gulp.src(inputFiles)
.pipe(plumber(handleError))
.pipe(concat(ouputFilename + ".js"));
2016-03-30 00:51:54 -04:00
if (es6) {
javascript.pipe(babel());
}
if (isProduction) {
javascript.pipe(stripDebug()).pipe(uglify());
}
return javascript.pipe(gulp.dest("public/js/"));
2016-03-30 00:51:54 -04:00
}
// Task for public styles
gulp.task("sass-public", () => {
return processSass("app");
2016-03-30 00:51:54 -04:00
});
// Task for dashboard styles
gulp.task("sass-dashboard", () => {
return processSass("dashboard");
});
2016-03-30 00:51:54 -04:00
// Task for error page styles
gulp.task("sass-error", () => {
return processSass("error");
});
// Task for public javascript
gulp.task("js-public", () => {
return processVue("app", jsPublic);
2016-03-30 00:51:54 -04:00
});
// Task for public javascript libraries
gulp.task("js-public-libs", () => {
return processJavaScript("lib", jsPublicLibs, false);
2016-03-30 00:51:54 -04:00
});
// Task for dashboard javascript
gulp.task("js-dashboard", () => {
return processJavaScript("dashboard", jsDashboard, true);
2016-03-30 00:51:54 -04:00
});
// Task for dashboard javascript libraries
gulp.task("js-dashboard-libs", () => {
return processJavaScript("lib-dashboard", jsDashboardLibs, false);
});
// Task to copy fonts
gulp.task("fonts", (done) => {
gulp.src(fontPaths)
.pipe(plumber(handleError))
.pipe(gulp.dest("public/fonts/"));
2018-04-06 00:29:22 -04:00
done();
});
2016-03-30 00:51:54 -04:00
// Task to watch files and run respective tasks when changes occur
gulp.task("watch", () => {
const browserSyncReload = (done) => {
browserSync.reload();
done();
};
2016-03-30 00:51:54 -04:00
2018-04-06 00:29:22 -04:00
browserSync.init({
logLevel: "silent",
baseDir: "./public",
notify: false,
ghostMode: {
clicks: true,
forms: true,
scroll: false
}
});
gulp.watch([ "app/**/*.php", "routes/**/*.php", "resources/views/**/*.blade.php" ], gulp.series(browserSyncReload));
gulp.watch([ jsPublic, "resources/assets/js/mixins/**/*.js", "resources/components/**/*.vue" ], gulp.series("js-public", browserSyncReload));
gulp.watch(jsDashboard, gulp.series("js-dashboard", browserSyncReload));
gulp.watch("resources/assets/sass/**/*.scss", gulp.parallel("sass-public", "sass-dashboard", "sass-error"));
2016-03-30 00:51:54 -04:00
});
// Task to run non-development tasks
gulp.task("default", gulp.parallel(
"sass-public",
"sass-dashboard",
"sass-error",
"js-public",
"js-public-libs",
"js-dashboard",
"js-dashboard-libs",
"fonts"
));