Skip to content

Commit

Permalink
Merge pull request #44 from vordgi/cm-43
Browse files Browse the repository at this point in the history
cm-43 improve cache deletion logic
  • Loading branch information
vordgi authored Jul 4, 2024
2 parents 0dc97ba + 95640ae commit 953db31
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 14 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "classnames-minifier",
"version": "0.2.1",
"version": "0.2.2",
"description": "Library for configuring style modules to generate compressed classes",
"main": "./dist/ClassnamesMinifier.js",
"types": "./dist/ClassnamesMinifier.d.ts",
Expand Down
17 changes: 15 additions & 2 deletions src/ClassnamesMinifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,18 @@ class ClassnamesMinifier {
const manifestDir = path.join(config.cacheDir, "ncm-meta");
const manifestPath = path.join(manifestDir, "manifest.json");

let distCleared = false;
if (config.cacheDir) {
validateDist(config, manifestPath);
const errors = validateDist(config, manifestPath);

if (errors) {
if (!config.disableDistDeletion) {
rmDist(config.distDir, errors);
distCleared = true;
} else {
console.log(`classnames-minifier: ${errors}"disableDistDeletion" option was set to true`);
}
}
}

const { freedNamesPolicy = "transmit", blockLimit = 100000 } = config.experimental || {};
Expand All @@ -35,8 +45,11 @@ class ClassnamesMinifier {
this.converterMinified.freeClasses.length > blockLimit &&
config.distDir
) {
distCleared = true;
rmDist(config.distDir, `Freed names exceeds the limit (${blockLimit})`);
this.converterMinified.clean();
}
if (distCleared) {
this.converterMinified.reset();
}
if (!fs.existsSync(manifestDir)) fs.mkdirSync(manifestDir, { recursive: true });
fs.writeFileSync(manifestPath, JSON.stringify({ ...config, version: CODE_VERSION }), { encoding: "utf-8" });
Expand Down
11 changes: 8 additions & 3 deletions src/lib/ConverterMinified.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,25 +103,30 @@ class ConverterMinified {
this.reservedNames = reservedNames;
this.freedNamesPolicy = experimental?.freedNamesPolicy || "transmit";

if (cacheDir) this.recycleCache(path.join(cacheDir, "ncm"));
if (cacheDir) this.invalidateCache(path.join(cacheDir, "ncm"));
}

clean = () => {
reset = () => {
this.dirtyСache = {};
this.freeClasses = [];
this.lastIndex = 0;
this.nextLoopEndsWith = 26;
this.currentLoopLength = 0;
this.nameMap = [0];
if (this.cacheDir) {
this.invalidateCache(this.cacheDir);
}
};

private recycleCache = (cacheDir: string) => {
private invalidateCache = (cacheDir: string) => {
this.cacheDir = cacheDir;
if (!existsSync(cacheDir)) mkdirSync(cacheDir, { recursive: true });

const cachedFiles = readdirSync(cacheDir);
if (cachedFiles.length) {
console.log("classnames-minifier: Restoring pairs of classes...");
} else {
return;
}

const usedClassNames: string[] = [];
Expand Down
9 changes: 1 addition & 8 deletions src/lib/validateDist.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import fs from "fs";
import type { Config } from "./types/plugin";
import { CODE_VERSION } from "./constants/configuration";
import rmDist from "./rmDist";

const readManifest = (manifestPath: string) => {
try {
Expand Down Expand Up @@ -55,13 +54,7 @@ const validateDist = (pluginOptions: Config, manifestPath: string) => {
} else {
configurationError = `Can not find the package cache manifest at ${manifestPath}\n`;
}
if (configurationError) {
if (!disableDistDeletion) {
rmDist(distDir, configurationError);
} else {
console.log(`classnames-minifier: ${configurationError}"disableDistDeletion" option was set to true`);
}
}
return configurationError;
};

export default validateDist;

0 comments on commit 953db31

Please sign in to comment.