gulpfile.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. "use strict";
  2. // Load plugins
  3. const autoprefixer = require("gulp-autoprefixer");
  4. const browsersync = require("browser-sync").create();
  5. const cleanCSS = require("gulp-clean-css");
  6. const del = require("del");
  7. const gulp = require("gulp");
  8. const header = require("gulp-header");
  9. const merge = require("merge-stream");
  10. const plumber = require("gulp-plumber");
  11. const rename = require("gulp-rename");
  12. const sass = require("gulp-sass");
  13. const uglify = require("gulp-uglify");
  14. // Load package.json for banner
  15. const pkg = require('./package.json');
  16. // Set the banner content
  17. const banner = ['/*!\n',
  18. ' * Start Bootstrap - <%= pkg.title %> v<%= pkg.version %> (<%= pkg.homepage %>)\n',
  19. ' * Copyright 2013-' + (new Date()).getFullYear(), ' <%= pkg.author %>\n',
  20. ' * Licensed under <%= pkg.license %> (https://github.com/StartBootstrap/<%= pkg.name %>/blob/master/LICENSE)\n',
  21. ' */\n',
  22. '\n'
  23. ].join('');
  24. // BrowserSync
  25. function browserSync(done) {
  26. browsersync.init({
  27. server: {
  28. baseDir: "./"
  29. },
  30. port: 3000
  31. });
  32. done();
  33. }
  34. // BrowserSync reload
  35. function browserSyncReload(done) {
  36. browsersync.reload();
  37. done();
  38. }
  39. // Clean vendor
  40. function clean() {
  41. return del(["./vendor/"]);
  42. }
  43. // Bring third party dependencies from node_modules into vendor directory
  44. function modules() {
  45. // Bootstrap JS
  46. var bootstrapJS = gulp.src('./node_modules/bootstrap/dist/js/*')
  47. .pipe(gulp.dest('./vendor/bootstrap/js'));
  48. // Bootstrap SCSS
  49. var bootstrapSCSS = gulp.src('./node_modules/bootstrap/scss/**/*')
  50. .pipe(gulp.dest('./vendor/bootstrap/scss'));
  51. // ChartJS
  52. var chartJS = gulp.src('./node_modules/chart.js/dist/*.js')
  53. .pipe(gulp.dest('./vendor/chart.js'));
  54. // dataTables
  55. var dataTables = gulp.src([
  56. './node_modules/datatables.net/js/*.js',
  57. './node_modules/datatables.net-bs4/js/*.js',
  58. './node_modules/datatables.net-bs4/css/*.css'
  59. ])
  60. .pipe(gulp.dest('./vendor/datatables'));
  61. // Font Awesome
  62. var fontAwesome = gulp.src('./node_modules/@fortawesome/**/*')
  63. .pipe(gulp.dest('./vendor'));
  64. // jQuery Easing
  65. var jqueryEasing = gulp.src('./node_modules/jquery.easing/*.js')
  66. .pipe(gulp.dest('./vendor/jquery-easing'));
  67. // jQuery
  68. var jquery = gulp.src([
  69. './node_modules/jquery/dist/*',
  70. '!./node_modules/jquery/dist/core.js'
  71. ])
  72. .pipe(gulp.dest('./vendor/jquery'));
  73. return merge(bootstrapJS, bootstrapSCSS, chartJS, dataTables, fontAwesome, jquery, jqueryEasing);
  74. }
  75. // CSS task
  76. function css() {
  77. return gulp
  78. .src("./scss/**/*.scss")
  79. .pipe(plumber())
  80. .pipe(sass({
  81. outputStyle: "expanded",
  82. includePaths: "./node_modules",
  83. }))
  84. .on("error", sass.logError)
  85. .pipe(autoprefixer({
  86. cascade: false
  87. }))
  88. .pipe(header(banner, {
  89. pkg: pkg
  90. }))
  91. .pipe(gulp.dest("./css"))
  92. .pipe(rename({
  93. suffix: ".min"
  94. }))
  95. .pipe(cleanCSS())
  96. .pipe(gulp.dest("./css"))
  97. .pipe(browsersync.stream());
  98. }
  99. // JS task
  100. function js() {
  101. return gulp
  102. .src([
  103. './js/*.js',
  104. '!./js/*.min.js',
  105. ])
  106. .pipe(uglify())
  107. .pipe(header(banner, {
  108. pkg: pkg
  109. }))
  110. .pipe(rename({
  111. suffix: '.min'
  112. }))
  113. .pipe(gulp.dest('./js'))
  114. .pipe(browsersync.stream());
  115. }
  116. // Watch files
  117. function watchFiles() {
  118. gulp.watch("./scss/**/*", css);
  119. gulp.watch(["./js/**/*", "!./js/**/*.min.js"], js);
  120. gulp.watch("./**/*.html", browserSyncReload);
  121. }
  122. // Define complex tasks
  123. const vendor = gulp.series(clean, modules);
  124. const build = gulp.series(vendor, gulp.parallel(css, js));
  125. const watch = gulp.series(build, gulp.parallel(watchFiles, browserSync));
  126. // Export tasks
  127. exports.css = css;
  128. exports.js = js;
  129. exports.clean = clean;
  130. exports.vendor = vendor;
  131. exports.build = build;
  132. exports.watch = watch;
  133. exports.default = build;