web/gulpfile.js/tasks/js.js

94 lines
2 KiB
JavaScript
Raw Normal View History

2021-01-23 10:07:18 +03:00
#!/usr/bin/env node
"use strict";
const { src, dest, watch, series, parallel} = require('gulp');
const concat = require('gulp-concat');
const rename = require("gulp-rename");
const uglify = require('gulp-uglify');
2021-01-25 01:20:51 +03:00
const insert = require('gulp-insert');
const fs = require('fs');
2021-01-23 10:07:18 +03:00
2021-04-19 09:51:45 +03:00
const JS_SRC = '_javascript';
const JS_DEST = `assets/js/dist/`;
2021-01-23 10:07:18 +03:00
function concatJs(files, output) {
return src(files)
.pipe(concat(output))
.pipe(rename({ extname: '.min.js' }))
2021-04-19 09:51:45 +03:00
.pipe(dest(JS_DEST));
2021-01-23 10:07:18 +03:00
}
function minifyJs() {
2021-04-19 09:51:45 +03:00
return src(`${ JS_DEST }/*.js`)
.pipe(insert.prepend(fs.readFileSync(`${ JS_SRC }/copyright`, 'utf8')))
2021-01-25 01:20:51 +03:00
.pipe(uglify({output: {comments: /^!|@preserve|@license|@cc_on/i}}))
2021-04-19 09:51:45 +03:00
.pipe(dest(JS_DEST));
2021-01-23 10:07:18 +03:00
}
2021-04-21 18:17:34 +03:00
const commonsJs = () => {
return concatJs(`${JS_SRC}/commons/*.js`, 'commons');
};
2021-01-23 10:07:18 +03:00
const homeJs = () => {
return concatJs([
2021-04-19 09:51:45 +03:00
`${JS_SRC}/commons/*.js`,
`${JS_SRC}/utils/timeago.js`
2021-01-23 10:07:18 +03:00
],
'home'
);
};
const postJs = () => {
return concatJs([
2021-04-19 09:51:45 +03:00
`${JS_SRC}/commons/*.js`,
2021-04-21 18:17:34 +03:00
`${JS_SRC}/utils/img-extra.js`,
2021-04-19 09:51:45 +03:00
`${JS_SRC}/utils/timeago.js`,
2021-04-21 18:17:34 +03:00
`${JS_SRC}/utils/checkbox.js`,
2021-09-10 17:01:18 +03:00
`${JS_SRC}/utils/clipboard.js`,
2021-01-23 10:07:18 +03:00
// 'smooth-scroll.js' must be called after ToC is ready
2021-04-19 09:51:45 +03:00
`${JS_SRC}/utils/smooth-scroll.js`
2021-01-23 10:07:18 +03:00
], 'post'
);
};
const categoriesJs = () => {
return concatJs([
2021-04-19 09:51:45 +03:00
`${JS_SRC}/commons/*.js`,
`${JS_SRC}/utils/category-collapse.js`
2021-01-23 10:07:18 +03:00
], 'categories'
);
};
const pageJs = () => {
return concatJs([
2021-04-19 09:51:45 +03:00
`${JS_SRC}/commons/*.js`,
2021-04-21 18:17:34 +03:00
`${JS_SRC}/utils/checkbox.js`,
`${JS_SRC}/utils/img-extra.js`,
2021-09-10 17:01:18 +03:00
`${JS_SRC}/utils/clipboard.js`
2021-01-23 10:07:18 +03:00
], 'page'
);
};
// GA pageviews report
const pvreportJs = () => {
2021-04-19 09:51:45 +03:00
return concatJs(`${JS_SRC}/utils/pageviews.js`, 'pvreport');
2021-01-23 10:07:18 +03:00
};
2021-04-21 18:17:34 +03:00
const buildJs = parallel(commonsJs, homeJs, postJs, categoriesJs, pageJs, pvreportJs);
2021-01-23 10:07:18 +03:00
exports.build = series(buildJs, minifyJs);
exports.liveRebuild = () => {
buildJs();
watch([
2021-04-19 09:51:45 +03:00
`${ JS_SRC }/commons/*.js`,
`${ JS_SRC }/utils/*.js`,
`${ JS_SRC }/lib/*.js`
2021-01-23 10:07:18 +03:00
],
buildJs
2021-10-25 21:33:32 +03:00
);
};