Last active
September 29, 2021 19:13
-
-
Save JoshuaFrontEnd/a13bc2d0bdaba6f469dcfd1adf92f8fe to your computer and use it in GitHub Desktop.
Gulpfile with gulp 4
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
const gulp = require('gulp'); | |
const sass = require('gulp-sass'); | |
const postcss = require("gulp-postcss"); | |
const sortmediaqueries = require('postcss-sort-media-queries'); | |
const autoprefixer = require("autoprefixer"); | |
const cssnano = require("cssnano"); | |
const browserSync = require('browser-sync').create(); | |
const sourcemaps = require('gulp-sourcemaps'); | |
//compile scss into css | |
function style() { | |
//1.where is my scss | |
return gulp | |
//gets all files ending with .scss in src/assets/scss | |
.src('src/assets/scss/**/*.scss') | |
// init sourcemaps | |
.pipe(sourcemaps.init()) | |
//pass that file through sass compiler | |
.pipe(sass({outputStyle: 'expanded'}).on('error', sass.logError)) | |
// Use postcss with autoprefixer | |
.pipe(postcss([ | |
sortmediaqueries(), | |
autoprefixer(), | |
cssnano() | |
])) | |
// sourcemaps write | |
.pipe(sourcemaps.write('./sourcemap')) | |
//where do I save the compiled css file | |
.pipe(gulp.dest('src/assets/css')) | |
//stream change to all browsers | |
.pipe(browserSync.stream()); | |
} | |
function watch() { | |
browserSync.init({ | |
server: { | |
baseDir: "./src", | |
index: "/index.html" | |
} | |
}); | |
gulp.watch('src/assets/scss/**/*.scss', style); | |
gulp.watch('src/*.html').on('change', browserSync.reload); | |
gulp.watch('src/assets/js/**/*.js').on('change', browserSync.reload); | |
} | |
exports.style = style; | |
exports.watch = watch; | |
/* | |
References: | |
https://levelup.gitconnected.com/how-to-setup-your-workflow-using-gulp-v4-0-0-5450e3d7c512 | |
Resolver error de node-sass en mac: | |
https://stackoverflow.com/questions/49348482/npm-install-with-error-gyp-failed-with-exit-code-1 | |
Si al instalar no reconoce Gulp se debe instalar, de forma global, el Gulp CLI con el comando: | |
npm install --global gulp-cli | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment