|
const { series, src, dest, watch } = require('gulp'); |
|
|
|
const rename = require('gulp-rename'); |
|
const sourcemaps = require('gulp-sourcemaps'); |
|
const rev = require("gulp-rev"); |
|
const gulpif = require('gulp-if'); |
|
const rimraf = require('rimraf'); |
|
const revDel = require('rev-del'); |
|
|
|
const assets_basepath = './assets'; |
|
const output_basepath = './dist'; |
|
const manifest_name = 'assets-manifest.json'; |
|
const manifest_path = `../${manifest_name}`; |
|
|
|
function clean(cb) { |
|
const is_production = process.argv.indexOf('--production') !== -1; |
|
|
|
if(!is_production) { |
|
rimraf(`${output_basepath}/**/main-*`, cb); |
|
rimraf(`${output_basepath}/${manifest_name}`, cb); |
|
} |
|
|
|
cb(); |
|
} |
|
|
|
function scripts(cb) { |
|
const eslint = require('gulp-eslint'); |
|
const babel = require('gulp-babel'); |
|
const uglify = require('gulp-uglify'); |
|
const is_production = process.argv.indexOf('--production') !== -1; |
|
|
|
return src(`${assets_basepath}/scripts/[^_]*.js`) |
|
.pipe(eslint()) |
|
.pipe(babel()) |
|
.pipe(sourcemaps.init({loadMaps: true})) |
|
.pipe(uglify()) |
|
.pipe(rename({ extname: '.min.js' })) |
|
.pipe(sourcemaps.write('./')) |
|
.pipe(gulpif(is_production, rev())) |
|
.pipe(dest(`${output_basepath}/scripts`)) |
|
.pipe(gulpif(is_production, rev.manifest(manifest_path))) |
|
.pipe(gulpif(is_production, revDel({ dest: 'dist', oldManifest: manifest_path }))) |
|
.pipe(gulpif(is_production, dest(`${output_basepath}/scripts`))) |
|
} |
|
|
|
function styles(cb) { |
|
const cssnano = require('gulp-cssnano'); |
|
const gulpStylelint = require('gulp-stylelint'); |
|
const is_production = process.argv.indexOf('--production') !== -1; |
|
|
|
return src(`${assets_basepath}/styles/[^_]*.scss`) |
|
.pipe(sourcemaps.init({loadMaps: true})) |
|
.pipe(gulpStylelint({ |
|
reporters: [ |
|
{formatter: 'string', console: true} |
|
] |
|
})) |
|
.pipe(cssnano()) |
|
.pipe(rename({ extname: '.min.css' })) |
|
.pipe(sourcemaps.write(`./`)) |
|
.pipe(gulpif(is_production, rev())) |
|
.pipe(dest(`${output_basepath}/styles`)) |
|
.pipe(gulpif(is_production, rev.manifest(manifest_path, {merge: true}))) |
|
.pipe(gulpif(is_production, revDel({ dest: 'dist', oldManifest: manifest_path }))) |
|
.pipe(gulpif(is_production, dest(`${output_basepath}/styles`))) |
|
} |
|
|
|
function images() { |
|
const imageCompress = require('gulp-image'); |
|
|
|
return src(`${assets_basepath}/images/*`) |
|
.pipe(imageCompress({ |
|
pngquant: true, |
|
optipng: true, |
|
zopflipng: true, |
|
jpegRecompress: false, |
|
mozjpeg: true, |
|
svgo: true, |
|
concurrent: 10 |
|
})) |
|
.pipe(dest(`${output_basepath}/images/`)); |
|
} |
|
|
|
function filesWatch() { |
|
watch(`${assets_basepath}/scripts`, scripts); |
|
watch(`${assets_basepath}/styles`, styles); |
|
watch(`${assets_basepath}/images`, images); |
|
watch(`${assets_basepath}/fonts`, fonts); |
|
} |
|
|
|
function fonts(cb) { |
|
const fse = require('fs-extra'); |
|
|
|
fse.copySync(`${assets_basepath}/fonts`, `${output_basepath}/fonts`, { overwrite: true }); |
|
|
|
cb(); |
|
} |
|
|
|
exports.default = series(clean, scripts, styles, images, fonts); |
|
exports.watch = filesWatch; |
|
exports.clean = clean; |