1
0
mirror of https://github.com/tobspr/shapez.io.git synced 2025-12-11 09:11:50 +00:00
tobspr_shapez.io/gulp/image-resources.js
Даниїл Григор'єв 9906397170
Build tools cleanup pass (#81)
* Fix tsconfig scopes affecting html.js

Since it's quite hard to use a DOM library type there, remove the type
entirely.

* Remove environment variables check

Nothing is using them anymore. It can be added back if needed later.

* Refactor Texture Packer downloading

Refactor local-config.js tasks file into a generic "environment"
category consisting of checking if Java is installed, downloading the
runnable Texture Packer if it's not yet downloaded and copying the local
configuration template; update README accordingly.

* Prepare environment only at postinstall

Remove environment.prepare task from default build pipelines, add a
postinstall script that calls the task, using environment.js as the
gulpfile to speed it up.

* Remove "docs" tasks and types generation script

Remove tasks from docs.js as they are unlikely to do anything meaningful
nowadays. Also remove the buildTypes script as it doesn't work anymore.
A better solution will be provided in the future.

* Simplify some globs

Use additional gulp.src options instead of specifying more or complex
globs.

* Extract built-temp location to a variable

Add the src/js/built-temp directory as a new variable in config.js,
replace all existing references to built-temp with this variable.
2025-06-19 04:51:34 +03:00

163 lines
4.6 KiB
JavaScript

import gulp from "gulp";
import path from "path/posix";
import atlas2Json from "./atlas2json.js";
import { buildFolder } from "./config.js";
import childProcess from "child_process";
import { promisify } from "util";
const exec = promisify(childProcess.exec);
const execute = command => {
const promise = exec(command, {
encoding: "utf-8",
});
promise.child.stderr.pipe(process.stderr);
return promise;
};
import gulpCached from "gulp-cached";
import gulpClean from "gulp-clean";
import gulpIf from "gulp-if";
import gulpImagemin from "gulp-imagemin";
import imageminGifsicle from "imagemin-gifsicle";
import imageminJpegtran from "imagemin-jpegtran";
import imageminPngquant from "imagemin-pngquant";
import { imageResourcesGlobs, nonImageResourcesGlobs } from "./config.js";
// Lossless options
const minifyImagesOptsLossless = () => [
imageminJpegtran({
progressive: true,
}),
gulpImagemin.svgo({}),
gulpImagemin.optipng({
optimizationLevel: 3,
}),
imageminGifsicle({
optimizationLevel: 3,
colors: 128,
}),
];
// Lossy options
const minifyImagesOpts = () => [
gulpImagemin.mozjpeg({
quality: 80,
maxMemory: 1024 * 1024 * 8,
}),
gulpImagemin.svgo({}),
imageminPngquant({
speed: 1,
strip: true,
quality: [0.65, 0.9],
dithering: false,
verbose: false,
}),
gulpImagemin.optipng({
optimizationLevel: 3,
}),
imageminGifsicle({
optimizationLevel: 3,
colors: 128,
}),
];
// Where the resources folder are
const resourcesDestFolder = path.join(buildFolder, "res");
/**
* Determines if an atlas must use lossless compression
* @param {string} fname
*/
function fileMustBeLossless(fname) {
return fname.indexOf("lossless") >= 0;
}
/////////////// ATLAS /////////////////////
export async function buildAtlas() {
const config = JSON.stringify("../res_raw/atlas.json");
const source = JSON.stringify("../res_raw");
const dest = JSON.stringify("../res_built/atlas");
try {
await execute(`java -jar runnable-texturepacker.jar ${source} ${dest} atlas0 ${config}`);
} catch {
console.warn("Building atlas failed. Java not found / unsupported version?");
}
}
// Converts .atlas LibGDX files to JSON
export async function atlasToJson() {
atlas2Json("../res_built/atlas");
}
// Copies the atlas to the final destination
export function atlas() {
return gulp.src("../res_built/atlas/*.png").pipe(gulp.dest(resourcesDestFolder));
}
// Copies the atlas to the final destination after optimizing it (lossy compression)
export function atlasOptimized() {
return gulp
.src(["../res_built/atlas/*.png"])
.pipe(
gulpIf(
fname => fileMustBeLossless(fname.history[0]),
gulpImagemin(minifyImagesOptsLossless()),
gulpImagemin(minifyImagesOpts())
)
)
.pipe(gulp.dest(resourcesDestFolder));
}
//////////////////// RESOURCES //////////////////////
// Copies all resources which are no ui resources
export function copyNonImageResources() {
return gulp.src(nonImageResourcesGlobs).pipe(gulp.dest(resourcesDestFolder));
}
// Copies all ui resources
export function copyImageResources() {
return gulp
.src(imageResourcesGlobs)
.pipe(gulpCached("imgres.copyImageResources"))
.pipe(gulp.dest(path.join(resourcesDestFolder)));
}
// Copies all ui resources and optimizes them
export function copyImageResourcesOptimized() {
return gulp
.src(imageResourcesGlobs)
.pipe(
gulpIf(
fname => fileMustBeLossless(fname.history[0]),
gulpImagemin(minifyImagesOptsLossless()),
gulpImagemin(minifyImagesOpts())
)
)
.pipe(gulp.dest(path.join(resourcesDestFolder)));
}
// Copies all resources and optimizes them
export const allOptimized = gulp.parallel(
gulp.series(buildAtlas, atlasToJson, atlasOptimized),
copyNonImageResources,
copyImageResourcesOptimized
);
// Cleans up unused images which are instead inline into the css
export function cleanupUnusedCssInlineImages() {
return gulp
.src(
[
path.join(buildFolder, "res", "ui", "**", "*.png"),
path.join(buildFolder, "res", "ui", "**", "*.jpg"),
path.join(buildFolder, "res", "ui", "**", "*.svg"),
path.join(buildFolder, "res", "ui", "**", "*.gif"),
],
{ read: false }
)
.pipe(gulpIf(fname => fname.history[0].indexOf("noinline") < 0, gulpClean({ force: true })));
}