Created
July 26, 2021 06:32
-
-
Save AndrisJefimovs/8191b6e47366f94a9e2c1ffe030cb0ae to your computer and use it in GitHub Desktop.
Gulpfile for web
This file contains hidden or 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
// Import important packages | |
const gulp = require('gulp'); | |
const plumber = require('gulp-plumber'); | |
const rename = require('gulp-rename'); | |
const browserSync = require('browser-sync').create(); | |
const sourcemaps = require('gulp-sourcemaps'); | |
const del = require('del'); | |
// SCSS -> CSS | |
const sass = require('gulp-sass'); | |
sass.compiler = require('sass'); | |
const postcss = require('gulp-postcss'); | |
const autoprefixer = require('autoprefixer'); | |
const cssnano = require('cssnano'); | |
const tildeImporter = require('node-sass-import'); | |
// HTML | |
const htmlmin = require('gulp-htmlmin'); | |
const fileinclude = require('gulp-file-include'); | |
// JS | |
const buffer = require('vinyl-buffer'); | |
const { createGulpEsbuild } = require('gulp-esbuild'); | |
const gulpEsbuild = createGulpEsbuild({ incremental: true }); | |
// ASSETS | |
const webp = require('gulp-webp'); | |
// Define important variables | |
const src = './src'; | |
const dest = './docs'; | |
// Reload the browser | |
const reload = (done) => { | |
browserSync.reload(); | |
done(); | |
}; | |
// Serve the dev-server in the browser | |
const serve = (done) => { | |
browserSync.init({ | |
server: { | |
baseDir: `${dest}`, | |
}, | |
browser: 'firefox', | |
}); | |
done(); | |
}; | |
// Compile SASS to CSS | |
const css = () => { | |
// Find SASS | |
return gulp | |
.src(`${src}/scss/**/*.scss`) | |
.pipe(plumber()) | |
.pipe(sourcemaps.init()) | |
.pipe( | |
sass({ | |
outputStyle: 'compressed', | |
includePaths: ['./node_modules'], | |
}) | |
) | |
.on('error', sass.logError) | |
.pipe(rename({ basename: 'style', suffix: '.min' })) | |
.pipe(postcss([autoprefixer(), cssnano()])) | |
.pipe(sourcemaps.write('')) | |
.pipe(gulp.dest(`${dest}/css`)) | |
.pipe(browserSync.stream()); | |
}; | |
// Compile .js to minified .js | |
const script = () => { | |
return gulp | |
.src(`${src}/js/main.js`) | |
.pipe( | |
gulpEsbuild({ | |
outfile: 'main.bundle.js', | |
bundle: true, | |
minify: true, | |
sourcemap: true, | |
platform: 'browser', | |
}) | |
) | |
.pipe(buffer()) | |
.pipe(gulp.dest(`${dest}/js`)); | |
}; | |
// Insert html components and compile .html to minified .html | |
const html = () => { | |
// Find HTML | |
return ( | |
gulp | |
.src(`${src}/*.html`) | |
// Init Plumber | |
.pipe(plumber()) | |
// Insert components | |
.pipe(fileinclude({ prefix: '@@', basepath: '@file' })) | |
// Compile HTML to minified HTML | |
.pipe( | |
htmlmin({ | |
collapseWhitespace: true, | |
removeComments: true, | |
html5: true, | |
removeEmptyAttributes: true, | |
removeTagWhitespace: true, | |
sortAttributes: true, | |
sortClassName: true, | |
}) | |
) | |
// Write everything to destination folder | |
.pipe(gulp.dest(`${dest}`)) | |
); | |
}; | |
// Minify and copy images to destination | |
const images = () => { | |
return gulp | |
.src(`${src}/img/**/*`) | |
.pipe(webp()) | |
.pipe(gulp.dest(`${dest}/img`)); | |
}; | |
// Copy assets | |
const assets = () => { | |
return gulp | |
.src( | |
[ | |
`${src}/**/*`, | |
`!${src}/includes/**/*`, | |
`!${src}/includes`, | |
`!${src}/js/**/*`, | |
`!${src}/js`, | |
`!${src}/scss/**/*`, | |
`!${src}/scss`, | |
`!${src}/*.html`, | |
], | |
{ | |
base: src, | |
} | |
) | |
.pipe(gulp.dest(`${dest}`)); | |
}; | |
// Delete old files to create a fully new build | |
const clear = () => { | |
return del(`${dest}/**`); | |
}; | |
// Watch changes and refresh page | |
const watch = () => | |
gulp.watch( | |
[ | |
`${src}/**/*.html`, | |
`${src}/js/**/*.js`, | |
`${src}/scss/**/*.scss`, | |
`${src}/**/*`, | |
], | |
gulp.series(html, css, script, reload) | |
); | |
// Development tasks | |
const dev = gulp.series(css, html, serve, watch, script, assets, images); | |
// Build tasks | |
const build = gulp.series(clear, css, html, images, assets, script); | |
// Default function (used when type "gulp") | |
exports.default = dev; | |
exports.dev = dev; | |
exports.build = build; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
package.json for this: