gulp-web/gulpfile.js

83 lines
2.4 KiB
JavaScript

const { src, dest, series, parallel, watch } = require('gulp');
const path = require('path');
const del = require('del');
const babel = require('gulp-babel');
const terser = require('gulp-terser');
const mustache = require('gulp-mustache');
const fileInclude = require('gulp-file-include');
const svgmin = require('gulp-svgmin');
const sass = require('gulp-sass');
const postcss = require('gulp-postcss');
const postcssPresetEnv = require('postcss-preset-env');
const cssnano = require('cssnano');
const browserSync = require('browser-sync');
sass.compiler = require('node-sass');
const babelConfig = require('./babel.config.js');
const postcssPlugins = [postcssPresetEnv(), cssnano()];
const srcDir = './src/';
const buildDir = './assets/';
const jsSrc = path.join(srcDir, 'js/**/*.js');
const jsDest = path.join(buildDir, 'js/');
const scssSrc = path.join(srcDir, 'scss/**/*.scss');
const cssDest = path.join(buildDir, 'css/');
const includeBaseDir = path.join(srcDir, 'includes/');
const includeSrc = path.join(includeBaseDir, '**/*');
const svgSrc = path.join(srcDir, 'svg/**/*.svg');
const svgDest = path.join(includeBaseDir, 'svg/');
const assetSrc = path.join(srcDir, 'assets/**/*');
const assetDest = path.join(buildDir, 'assets/');
function jsClean() {
return del(jsDest, { force: true });
}
function cssClean() {
return del(cssDest, { force: true });
}
function assetClean() {
return del(assetDest, { force: true });
}
function jsTranspile() {
return src(jsSrc, { sourcemaps: true })
.pipe(babel(babelConfig))
.pipe(terser())
.pipe(dest(jsDest, { sourcemaps: true }));
}
function scssCompile() {
return src(scssSrc, { sourcemaps: true })
.pipe(sass().on('error', sass.logError))
.pipe(postcss(postcssPlugins))
.pipe(dest(cssDest, { sourcemaps: true }));
}
function svgOptimise() {
return src(svgSrc)
.pipe(svgmin())
.pipe(dest(svgDest));
}
function assetCompile() {
return src(assetSrc).pipe(dest(assetDest));
}
const jsWatch = () =>
watch(jsSrc, { ignoreInitial: false }, series(jsClean, jsTranspile));
const cssWatch = () =>
watch(scssSrc, { ignoreInitial: false }, series(cssClean, scssCompile));
const svgWatch = () =>
watch(svgSrc, { ignoreInitial: false }, series(svgOptimise));
const assetWatch = () =>
watch(assetSrc, { ignoreInitial: false }, series(assetClean, assetCompile));
exports.default = parallel(jsWatch, cssWatch, svgWatch, assetWatch);