hugo-geekblog/gulpfile.js

300 lines
7.2 KiB
JavaScript
Raw Normal View History

const devBuild = !(
(process.env.NODE_ENV || "prod").trim().toLowerCase() === "prod"
);
2020-09-12 16:20:46 +00:00
const gulp = require("gulp");
const rename = require("gulp-rename");
const { sass } = require("@mr-hope/gulp-sass");
2020-09-12 16:20:46 +00:00
const cleanCSS = require("gulp-clean-css");
const autoprefixer = require("gulp-autoprefixer");
const iconfont = require("gulp-iconfont");
const filelist = require("gulp-filelist");
const uglify = require("gulp-uglify");
const sourcemaps = require("gulp-sourcemaps");
2020-09-12 16:20:46 +00:00
const realFavicon = require("gulp-real-favicon");
const svgSprite = require("gulp-svg-sprite");
2020-11-22 13:01:47 +00:00
const rev = require("gulp-rev");
const replace = require("gulp-replace");
2020-07-06 18:55:32 +00:00
const path = require("path");
const fs = require("fs");
const del = require("del");
const through = require("through2");
var BUILD = "build";
var CSS_BUILD = BUILD + "/assets";
var JS_BUILD = BUILD + "/assets/js";
var FAVICON_DATA_FILE = BUILD + "/faviconData.json";
2020-07-06 18:55:32 +00:00
var TIMESTAMP = Math.round(Date.now() / 1000);
function noop() {
return through.obj();
}
2020-09-12 16:20:46 +00:00
gulp.task("sass", function () {
return gulp
2020-11-08 16:08:17 +00:00
.src("src/sass/{main,print,mobile}.scss")
.pipe(devBuild ? sourcemaps.init() : noop())
.pipe(sass().on("error", sass.logError))
2020-09-12 16:20:46 +00:00
.pipe(cleanCSS({ format: "beautify" }))
.pipe(
autoprefixer({
cascade: false,
})
)
.pipe(gulp.dest(CSS_BUILD))
2020-09-12 16:20:46 +00:00
.pipe(cleanCSS())
.pipe(rename({ extname: ".min.css" }))
.pipe(devBuild ? sourcemaps.write(".") : noop())
.pipe(gulp.dest(CSS_BUILD));
2020-07-06 18:55:32 +00:00
});
2020-09-12 16:20:46 +00:00
gulp.task("favicon-generate", function (done) {
realFavicon.generateFavicon(
{
masterPicture: "src/favicon/favicon-master.svg",
dest: "static/favicon",
iconsPath: "/favicon",
design: {
ios: {
pictureAspect: "backgroundAndMargin",
backgroundColor: "#2f333e",
margin: "14%",
assets: {
ios6AndPriorIcons: false,
ios7AndLaterIcons: false,
precomposedIcons: false,
declareOnlyDefaultIcon: true,
},
},
desktopBrowser: {},
windows: {
pictureAspect: "whiteSilhouette",
backgroundColor: "#2f333e",
onConflict: "override",
assets: {
windows80Ie10Tile: false,
windows10Ie11EdgeTiles: {
small: false,
medium: true,
big: false,
rectangle: false,
2020-07-06 18:55:32 +00:00
},
2020-09-12 16:20:46 +00:00
},
},
androidChrome: {
pictureAspect: "shadow",
themeColor: "#2f333e",
manifest: {
display: "standalone",
orientation: "notSet",
onConflict: "override",
declared: true,
},
assets: {
legacyIcon: false,
lowResolutionIcons: false,
},
2020-07-06 18:55:32 +00:00
},
2020-09-12 16:20:46 +00:00
safariPinnedTab: {
pictureAspect: "silhouette",
themeColor: "#2f333e",
2020-07-06 18:55:32 +00:00
},
2020-09-12 16:20:46 +00:00
},
settings: {
scalingAlgorithm: "Mitchell",
errorOnImageTooSmall: false,
readmeFile: false,
htmlCodeFile: false,
usePathAsIs: false,
},
markupFile: FAVICON_DATA_FILE,
},
function () {
done();
}
);
2020-07-06 18:55:32 +00:00
});
2020-09-12 16:20:46 +00:00
gulp.task("favicon-check-update", function (done) {
var currentVersion = JSON.parse(fs.readFileSync(FAVICON_DATA_FILE)).version;
realFavicon.checkForUpdates(currentVersion, function (err) {
if (err) {
throw err;
}
});
done();
2020-07-06 18:55:32 +00:00
});
2020-09-12 16:20:46 +00:00
gulp.task("svg-sprite", function () {
config = {
shape: {
id: {
generator: "gblog_%s",
},
2020-09-12 16:20:46 +00:00
dimension: {
maxWidth: 22,
maxHeight: 22,
2020-09-12 16:20:46 +00:00
attributes: false,
},
spacing: {
padding: 5,
box: "content",
},
dest: BUILD + "/intermediate-svg",
2020-09-12 16:20:46 +00:00
},
svg: {
xmlDeclaration: false,
rootAttributes: {
class: "svg-sprite",
2020-09-12 16:20:46 +00:00
},
},
mode: {
inline: true,
symbol: {
dest: "assets/sprites/",
sprite: "geekblog.svg",
2020-09-12 16:20:46 +00:00
bust: false,
},
},
};
2020-07-06 18:55:32 +00:00
2020-09-12 16:20:46 +00:00
return gulp
.src("src/icons/*.svg")
.pipe(svgSprite(config))
.pipe(gulp.dest("."));
2020-07-06 18:55:32 +00:00
});
gulp.task("svg-sprite-list", function () {
config = { removeExtensions: true, flatten: true };
return gulp
.src("src/icons/*.svg")
2021-05-24 11:41:38 +00:00
.pipe(filelist("exampleSite/data/sprites/geekblog.json", config))
.pipe(gulp.dest("."));
});
2020-09-12 16:20:46 +00:00
gulp.task("iconfont", function () {
var lastUnicode = 0xea01;
var files = fs.readdirSync("src/iconfont");
2020-07-06 18:55:32 +00:00
// Filter files with containing unicode value and set last unicode
2020-09-12 16:20:46 +00:00
files.forEach(function (file) {
var basename = path.basename(file);
var matches = basename.match(/^(?:((?:u[0-9a-f]{4,6},?)+)\-)?(.+)\.svg$/i);
var currentCode = -1;
2020-07-06 18:55:32 +00:00
2020-09-12 16:20:46 +00:00
if (matches && matches[1]) {
currentCode = parseInt(matches[1].split("u")[1], 16);
}
2020-07-06 18:55:32 +00:00
2020-09-12 16:20:46 +00:00
if (currentCode >= lastUnicode) {
lastUnicode = ++currentCode;
}
});
2020-07-06 18:55:32 +00:00
2020-09-12 16:20:46 +00:00
return gulp
.src(["src/iconfont/*.svg"])
.pipe(
iconfont({
startUnicode: lastUnicode,
2020-11-22 13:01:47 +00:00
fontName: "GeekblogIcons",
prependUnicode: true,
2020-09-12 16:20:46 +00:00
normalize: true,
fontHeight: 1001,
centerHorizontally: true,
2020-11-22 13:01:47 +00:00
formats: ["woff", "woff2"],
timestamp: TIMESTAMP,
2020-09-12 16:20:46 +00:00
})
)
.pipe(gulp.dest("static/fonts/"));
2020-07-06 18:55:32 +00:00
});
gulp.task("js", function () {
return gulp
.src(["src/js/*.js"])
.pipe(devBuild ? sourcemaps.init() : noop())
.pipe(uglify())
.pipe(rename({ extname: ".min.js" }))
.pipe(devBuild ? sourcemaps.write(".") : noop())
.pipe(gulp.dest(JS_BUILD));
});
gulp.task("asset-sync", function () {
return gulp
.src([
"node_modules/clipboard/dist/clipboard.min.js",
"node_modules/flexsearch/dist/flexsearch.min.js",
"node_modules/mermaid/dist/mermaid.min.js",
])
.pipe(replace(/\/\/# sourceMappingURL=.+$/, ""))
.pipe(gulp.dest(JS_BUILD));
});
2020-11-22 13:01:47 +00:00
gulp.task("asset-rev", function () {
return gulp
.src([CSS_BUILD + "/*.min.css", JS_BUILD + "/*.min.js"], {
base: BUILD + "/assets",
2020-11-22 13:01:47 +00:00
})
.pipe(rev())
2020-11-22 13:12:04 +00:00
.pipe(gulp.dest("static"))
2020-11-22 13:01:47 +00:00
.pipe(
rev.manifest("data/assets-static.json", {
base: "data",
merge: true,
})
)
.pipe(rename("assets.json"))
.pipe(gulp.dest("data"));
});
gulp.task("asset-map", function () {
2020-11-22 14:00:45 +00:00
return gulp
.src([CSS_BUILD + "/*.min.css.map", JS_BUILD + "/*.min.js.map"], {
base: BUILD + "/assets",
2020-11-22 14:23:56 +00:00
})
.pipe(gulp.dest("static"));
2020-11-22 14:00:45 +00:00
});
gulp.task("clean", function () {
return del([
BUILD,
"layouts/partials/svg-icon-symbols.svg",
"static/js/",
"static/favicon/",
"static/*.min.css",
"static/*.css.map",
"data/assets.json",
"resources",
]);
});
/* Task series */
gulp.task("asset", gulp.series("asset-sync", "asset-rev"));
gulp.task("svg", gulp.series("svg-sprite", devBuild ? "svg-sprite-list" : []));
2020-11-22 14:23:56 +00:00
2020-09-12 16:20:46 +00:00
gulp.task(
"default",
gulp.series([
devBuild ? [] : "clean",
"sass",
"js",
"asset",
devBuild ? "asset-map" : [],
"svg",
"iconfont",
"favicon-generate",
])
2020-09-12 16:20:46 +00:00
);
2020-07-06 18:55:32 +00:00
gulp.task("watch", function () {
gulp.series("default")();
gulp.watch(
"src/sass/**/*.*css",
gulp.series("sass", "asset-rev", "asset-map")
);
gulp.watch("src/js/*.js", gulp.series("js", "asset-rev", "asset-map"));
2020-07-06 18:55:32 +00:00
});