Advertisement
Guest User

Untitled

a guest
Sep 21st, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. "use strict";
  2.  
  3. var destinationFolder = 'wwwroot/';
  4. var htmlFilePath = 'App/js/components/**/*.html';
  5. var htmlFileDestination = 'templates/';
  6.  
  7. var gulp = require('gulp'),
  8.     concat = require('gulp-concat'),
  9.     sass = require('gulp-sass'),
  10.     cssmin = require('gulp-clean-css'),
  11.     htmlmin = require('gulp-htmlmin'),
  12.     uglify = require('gulp-uglify'),
  13.     merge = require('merge-stream'),
  14.     del = require('del'),
  15.     babel = require("gulp-babel"),
  16.     plumber = require('gulp-plumber'),
  17.     autoprefixer = require('gulp-autoprefixer'),
  18.     flatten = require('gulp-flatten'),
  19.     rimraf = require('rimraf');
  20.  
  21. var bundleconfig = require('./Gulp/bundle-files.json'),
  22.     filesToMove = require('./Gulp/move-files.json');
  23.  
  24. var regex = {
  25.     scss: /\.scss$/,
  26.     css: /\.css$/,
  27.     html: /\.html$/,
  28.     js: /\.js$/
  29. };
  30.  
  31.  
  32. // Tasks
  33. gulp.task('deploy', ['deploy:js', 'deploy:css', 'deploy:html', 'move:files']);
  34. gulp.task('serve', ['serve:js', 'serve:css', 'serve:html', 'move:files']);
  35.  
  36.  
  37. // Watches
  38. gulp.task('watch', function () {
  39.     getBundles(regex.css).forEach(function (bundle) {
  40.         gulp.watch(bundle.inputFiles, ['serve:css']);
  41.     });
  42.  
  43.     getBundles(regex.js).forEach(function (bundle) {
  44.         gulp.watch(bundle.inputFiles, ['serve:js']);
  45.     });
  46.  
  47.     gulp.watch(htmlFilePath, ['serve:html']);
  48.  
  49.     filesToMove.filter(function (fileToMove) {
  50.         gulp.watch(fileToMove.source, ["serve:files"]);
  51.     });
  52. });
  53.  
  54.  
  55. // Common
  56. gulp.task('clean', function (cb) {
  57.     return rimraf(destinationFolder + '**/*', cb);
  58. });
  59.  
  60. gulp.task('move:files', function () {
  61.     filesToMove.filter(function (fileToMove) {
  62.         gulp.src(fileToMove.source)
  63.             .pipe(gulp.dest(fileToMove.destination));
  64.     });
  65. });
  66.  
  67.  
  68. // Production tasks
  69. gulp.task('deploy:js', function () {
  70.     var tasks = getBundles(regex.js).map(function (bundle) {
  71.         return gulp.src(bundle.inputFiles, {
  72.             base: "."
  73.         })
  74.             .pipe(plumber())
  75.             .pipe(babel({
  76.                 presets: ['es2015'],
  77.                 compact: true,
  78.                 ignore: [
  79.                     'node_modules/**/*.js'
  80.                 ]
  81.             }))
  82.             .pipe(concat(bundle.outputFileName))
  83.             .pipe(uglify())
  84.             .pipe(gulp.dest('.'));
  85.     });
  86.  
  87.     return merge(tasks);
  88. });
  89.  
  90. gulp.task('deploy:css', function () {
  91.     var cssTask = getBundles(regex.css).map(function (bundle) {
  92.         return gulp.src(bundle.inputFiles,
  93.             {
  94.                 base: "."
  95.             })
  96.             .pipe(plumber())
  97.             .pipe(concat(bundle.outputFileName));
  98.     });
  99.  
  100.     var scssTask = getBundles(regex.css).map(function (bundle) {
  101.         return gulp.src(bundle.inputFiles,
  102.             {
  103.                 base: "."
  104.             })
  105.             .pipe(plumber())
  106.             .pipe(sass())
  107.             .pipe(concat(bundle.outputFileName));
  108.     });
  109.  
  110.     var merged = merge(cssTask, scssTask)
  111.         .pipe(plumber())
  112.         .pipe(autoprefixer({
  113.             browsers: ['last 2 versions'],
  114.             cascade: false
  115.         }))
  116.         .pipe(cssmin(), {
  117.             showLog: true
  118.         })
  119.         .pipe(gulp.dest("."));
  120.  
  121.     return merged;
  122. });
  123.  
  124. gulp.task('deploy:html', function () {
  125.     gulp.src(htmlFilePath)
  126.         .pipe(flatten())
  127.         .pipe(gulp.dest(destinationFolder + htmlFileDestination));
  128. });
  129.  
  130.  
  131. // Development tasks
  132. gulp.task('serve:js', function () {
  133.     var tasks = getBundles(regex.js).map(function (bundle) {
  134.         return gulp.src(bundle.inputFiles, {
  135.             base: "."
  136.         })
  137.             .pipe(plumber())
  138.             .pipe(concat(bundle.outputFileName))
  139.             .pipe(gulp.dest('.'));
  140.     });
  141.  
  142.     return merge(tasks);
  143. });
  144.  
  145. gulp.task('serve:css', function () {
  146.     var cssTask = getBundles(regex.css).map(function (bundle) {
  147.         return gulp.src(bundle.inputFiles,
  148.             {
  149.                 base: "."
  150.             })
  151.             .pipe(plumber())
  152.             .pipe(concat(bundle.outputFileName));
  153.     });
  154.  
  155.     var scssTask = getBundles(regex.css).map(function (bundle) {
  156.         return gulp.src(bundle.inputFiles,
  157.             {
  158.                 base: "."
  159.             })
  160.             .pipe(plumber())
  161.             .pipe(sass())
  162.             .pipe(concat(bundle.outputFileName));
  163.     });
  164.  
  165.     var merged = merge(cssTask, scssTask)
  166.         .pipe(plumber())
  167.         .pipe(gulp.dest("."));
  168.  
  169.     return merged;
  170. });
  171.  
  172. gulp.task('serve:html', function () {
  173.     gulp.src(htmlFilePath)
  174.         .pipe(flatten())
  175.         .pipe(gulp.dest(destinationFolder + htmlFileDestination));
  176. });
  177.  
  178.  
  179. // Helpers
  180. function getBundles(regexPattern) {
  181.     return bundleconfig.filter(function (bundle) {
  182.         return regexPattern.test(bundle.outputFileName);
  183.     });
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement