Last active
May 18, 2020 09:02
-
-
Save AaronSadlerUK/a1e808d12c9481b776fc2c5a7d273903 to your computer and use it in GitHub Desktop.
Gulpfile containing scss compiler with autoprefixer, css minification, js compiler with babel, js minification and css / js versioning
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
const gulp = require("gulp"); | |
const sass = require("gulp-sass"); | |
const sourcemaps = require("gulp-sourcemaps"); | |
const autoprefixer = require("gulp-autoprefixer"); | |
const cssmin = require("gulp-cssmin"); | |
const rename = require("gulp-rename"); | |
const minify = require("gulp-minify"); | |
const babel = require("gulp-babel"); | |
var rev = require("gulp-rev"); | |
var revReplace = require("gulp-rev-rewrite"); | |
// CSS Tasks | |
gulp.task("css-compile", function () { | |
return gulp.src("./assets/scss/styles.scss") | |
.pipe(sourcemaps.init()) | |
.pipe(sass({ outputStyle: "nested" }).on("error", sass.logError)) | |
.pipe(autoprefixer({ cascade: false })) | |
.pipe(sourcemaps.write('.', { sourceRoot: '../SCSS' })) | |
.pipe(gulp.dest("./css")); | |
}); | |
gulp.task("css-minify", function () { | |
return gulp.src(["./css/**.css", "!./css/**.min.css"]) | |
.pipe(cssmin()) | |
.pipe(rename({ suffix: ".min" })) | |
.pipe(rev()) | |
.pipe(gulp.dest("./css")) | |
.pipe(rev.manifest()) | |
.pipe(gulp.dest("./css")); | |
}); | |
gulp.task("revreplacecss", function () { | |
var manifest = gulp.src("./css/rev-manifest.json"); | |
var source = './Views/_Layout.cshtml'; | |
var outputFolderHtml = './Views'; | |
return gulp.src(source) | |
.pipe(revReplace({ manifest })) | |
.pipe(gulp.dest(outputFolderHtml)); | |
}); | |
// Generate JS, and run aganst babel for cross browser | |
gulp.task('js-compile', function () { | |
return gulp.src(['./assets/js/*.js', '!./assets/js/*.min.js']) | |
.pipe(babel()) | |
.pipe(gulp.dest('./scripts')); | |
}); | |
// Minify JS | |
gulp.task('js-minify', function () { | |
return gulp.src(['./scripts/*.js', '!./scripts/*.min.js']) | |
.pipe(minify({ | |
ext: { | |
min: '.min.js' | |
}, | |
noSource: true | |
})) | |
.pipe(rev()) | |
.pipe(gulp.dest('./scripts')) | |
.pipe(rev.manifest()) | |
.pipe(gulp.dest('./scripts')); | |
}); | |
gulp.task("revreplacejs", function () { | |
var manifest = gulp.src("./scripts/rev-manifest.json"); | |
var source = './Views/_Layout.cshtml'; | |
var outputFolderHtml = './Views'; | |
return gulp.src(source) | |
.pipe(revReplace({ manifest })) | |
.pipe(gulp.dest(outputFolderHtml)); | |
}); | |
gulp.task('css-tasks', gulp.series( | |
'css-compile', | |
'css-minify')); | |
gulp.task('js-tasks', gulp.series( | |
'js-compile', | |
'js-minify')); | |
gulp.task('default', gulp.series( | |
'css-compile', | |
'css-minify', | |
'revreplacecss', | |
'js-compile', | |
'js-minify', | |
'revreplacejs')); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "projectname", | |
"version": "1.0.0", | |
"description": "", | |
"main": "gulpfile.js", | |
"dependencies": {}, | |
"devDependencies": { | |
"@babel/core": "^7.8.4", | |
"gulp": "^4.0.2", | |
"gulp-autoprefixer": "7.0.1", | |
"gulp-babel": "8.0.0", | |
"gulp-concat": "^2.6.1", | |
"gulp-cssmin": "^0.2.0", | |
"gulp-imagemin": "7.1.0", | |
"gulp-minify": "^3.1.0", | |
"gulp-rename": "2.0.0", | |
"gulp-sass": "^4.0.2", | |
"gulp-sourcemaps": "2.6.5", | |
"gulp-rev": "9.0.0", | |
"gulp-rev-rewrite": "3.0.3" | |
}, | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"author": "", | |
"license": "ISC" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment