const fs = require('fs');
const path = require('path');
const gulp = require('gulp');
const pipeIf = require('gulp-if');
const nunjucks = require('gulp-nunjucks');
const pretty = require('gulp-pretty-html');
const minify = require('gulp-htmlmin');
const replace = require('gulp-replace');
const sass = require('gulp-sass');
const sourcemaps = require('gulp-sourcemaps');
const autoprefixer = require('gulp-autoprefixer');
const clean = require('gulp-clean-css');
const rollup = require('rollup');
const resolve = require('@rollup/plugin-node-resolve');
const commonjs = require('@rollup/plugin-commonjs');
const babel = require('rollup-plugin-babel');
const { terser } = require('rollup-plugin-terser');
const image = require('gulp-image');
const clear = require('gulp-clean');
const browser = require('browser-sync').create();
const { argv } = require('yargs');
sass.compiler = require('node-sass');
const getFiles = dirname => {
const entries = fs.readdirSync(dirname);
const files = entries
.filter(entry => {
const stat = fs.statSync(path.join(dirname, entry));
return stat.isFile();
})
.reduce((accumulator, file) => {
const name = path.basename(file, path.extname(file));
accumulator[name] = path.join(dirname, file);
return accumulator;
}, {});
return files;
};
const scripts = getFiles('Sources/Scripts/');
gulp.task('server', (callback) => {
browser.init({
server: {
baseDir: 'Build'
},
ui: {
port: 8080
},
notify: false
});
callback();
});
gulp.task('reload', (callback) => {
browser.reload();
callback();
});
gulp.task('clear', () => {
return gulp.src('Build', { allowEmpty: true })
.pipe(clear({
force: true
}));
});
gulp.task('build:html', () => {
return gulp.src('Sources/**/*.html')
.pipe(nunjucks.compile())
.pipe(replace(/(\r\n|\r|\n)(\s+)?(\r\n|\r|\n)/gm, '\n'))
.pipe(pipeIf(argv.development, pretty({
indent_with_tabs: true,
extra_liners: []
})))
.pipe(pipeIf(argv.production, minify({
collapseWhitespace: true
})))
.pipe(gulp.dest(`Build/`));
});
gulp.task('build:sass', () => {
return gulp.src(['Sources/Styles/*.sass', 'Sources/Styles/*.scss'])
.pipe(pipeIf(argv.development, sourcemaps.init()))
.pipe(pipeIf(argv.development, sass({
indentWidth: 4,
outputStyle: 'expanded'
})))
.pipe(pipeIf(argv.production, sass()))
.pipe(autoprefixer({
cascade: false
}))
.pipe(pipeIf(argv.production, clean()))
.pipe(pipeIf(argv.development, sourcemaps.write('.')))
.pipe(gulp.dest('Build/Styles/'));
});
gulp.task('build:js', async (callback) => {
const bundle = await rollup.rollup({
input: scripts,
plugins: [
resolve(),
commonjs(),
babel({
exclude: 'node_modules/**',
presets: ['@babel/env']
}),
!!argv.production && terser({
keep_classnames: true
})
]
});
await bundle.write({
dir: 'Build/Scripts/',
format: 'esm',
chunkFileNames: '[name].js',
entryFileNames: '[name].js',
sourcemap: !!argv.development
});
callback();
});
gulp.task('build:images', () => {
return gulp.src(['Sources/Images/**/*.png', 'Sources/Images/**/*.jpg', 'Sources/Images/**/*.svg'])
.pipe(pipeIf(argv.production, image()))
.pipe(gulp.dest('Build/Images/'));
});
gulp.task('watch:html', () => {
gulp.watch(['Sources/**/*.html', 'Sources/**/*.njk'], gulp.series('build:html', 'reload'));
});
gulp.task('watch:sass', () => {
gulp.watch(['Sources/Styles/**/*.sass', 'Sources/Styles/**/*.scss'], gulp.series('build:sass', 'reload'));
});
gulp.task('watch:js', () => {
gulp.watch('Sources/Scripts/**/*.js', gulp.series('build:js', 'reload'));
});
gulp.task('watch:images', () => {
gulp.watch(['Sources/Images/**/*.png', 'Sources/Images/**/*.jpg', 'Sources/Images/**/*.svg'], gulp.series('build:images'));
});
gulp.task('watch:icons', () => {
gulp.watch('Sources/Icons/**/*.svg', gulp.series('build:html', 'reload'));
});
gulp.task('build', gulp.parallel('build:html', 'build:sass', 'build:js', 'build:images'));
gulp.task('watch', gulp.parallel('watch:html', 'watch:sass', 'watch:js', 'watch:images', 'watch:icons'));
gulp.task('default', gulp.series('clear', 'build', 'server', 'watch'));