From 8b1e208702deeb9ae0eea9e22b80ffc63a41b717 Mon Sep 17 00:00:00 2001 From: Vordgi Date: Thu, 4 Jul 2024 13:06:02 +0400 Subject: [PATCH 1/2] cm-43 improve cache deletion logic --- src/ClassnamesMinifier.ts | 17 +++++++++++++++-- src/lib/ConverterMinified.ts | 11 ++++++++--- src/lib/validateDist.ts | 9 +-------- 3 files changed, 24 insertions(+), 13 deletions(-) diff --git a/src/ClassnamesMinifier.ts b/src/ClassnamesMinifier.ts index 7e5a5ae..0723121 100644 --- a/src/ClassnamesMinifier.ts +++ b/src/ClassnamesMinifier.ts @@ -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 || {}; @@ -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" }); diff --git a/src/lib/ConverterMinified.ts b/src/lib/ConverterMinified.ts index 70119e2..8609ebb 100644 --- a/src/lib/ConverterMinified.ts +++ b/src/lib/ConverterMinified.ts @@ -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[] = []; diff --git a/src/lib/validateDist.ts b/src/lib/validateDist.ts index 69076e1..8c537f0 100644 --- a/src/lib/validateDist.ts +++ b/src/lib/validateDist.ts @@ -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 { @@ -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; From 95640ae726ee00c69fbbe8c7ebdd7e8b3641330e Mon Sep 17 00:00:00 2001 From: Vordgi Date: Thu, 4 Jul 2024 13:10:46 +0400 Subject: [PATCH 2/2] cm-43 release 0.2.2 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 196635e..7ed78cf 100644 --- a/package.json +++ b/package.json @@ -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",