web/gulpfile.js/tasks/js.js

87 lines
1.8 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
const JS_ROOT = 'assets/js';
2021-01-23 10:07:18 +03:00
const jsDest = `${ JS_ROOT }/dist/`;
function concatJs(files, output) {
return src(files)
.pipe(concat(output))
.pipe(rename({ extname: '.min.js' }))
.pipe(dest(jsDest));
}
function minifyJs() {
return src(`${ jsDest }/*.js`)
2021-04-02 10:35:36 +03:00
.pipe(insert.prepend(fs.readFileSync(`${ JS_ROOT }/_copyright`, 'utf8')))
2021-01-25 01:20:51 +03:00
.pipe(uglify({output: {comments: /^!|@preserve|@license|@cc_on/i}}))
2021-01-23 10:07:18 +03:00
.pipe(dest(jsDest));
}
const homeJs = () => {
return concatJs([
`${JS_ROOT}/_commons/*.js`,
`${JS_ROOT}/_utils/timeago.js`
],
'home'
);
};
const postJs = () => {
return concatJs([
`${JS_ROOT}/_commons/*.js`,
`${JS_ROOT}/_utils/timeago.js`,
`${JS_ROOT}/_utils/lang-badge.js`,
// 'smooth-scroll.js' must be called after ToC is ready
`${JS_ROOT}/_utils/smooth-scroll.js`
], 'post'
);
};
const categoriesJs = () => {
return concatJs([
`${JS_ROOT}/_commons/*.js`,
`${JS_ROOT}/_utils/category-collapse.js`
], 'categories'
);
};
const pageJs = () => {
return concatJs([
`${JS_ROOT}/_commons/*.js`,
`${JS_ROOT}/_utils/smooth-scroll.js`
], 'page'
);
};
// GA pageviews report
const pvreportJs = () => {
2021-01-25 01:20:51 +03:00
return concatJs(`${JS_ROOT}/_utils/pageviews.js`, 'pvreport');
2021-01-23 10:07:18 +03:00
};
const buildJs = parallel(homeJs, postJs, categoriesJs, pageJs, pvreportJs);
exports.build = series(buildJs, minifyJs);
exports.liveRebuild = () => {
buildJs();
watch([
`${ JS_ROOT }/_commons/*.js`,
2021-01-25 01:20:51 +03:00
`${ JS_ROOT }/_utils/*.js`,
`${ JS_ROOT }/lib/*.js`
2021-01-23 10:07:18 +03:00
],
buildJs
)
}