From db07f1798a6920f95023d6685b86c10710d6f19f Mon Sep 17 00:00:00 2001 From: Vordgi Date: Sat, 26 Oct 2024 19:45:59 +0400 Subject: [PATCH 1/3] cm-50 support checkdistfreshness option --- eslint.config.mjs | 5 ++--- src/ClassnamesMinifier.ts | 15 ++++++++------- src/lib/constants/configuration.ts | 2 +- src/lib/types/plugin.ts | 4 ++++ src/lib/validateConfig.ts | 10 +++++++++- src/lib/validateDist.ts | 4 ++-- tsconfig.json | 2 +- 7 files changed, 27 insertions(+), 15 deletions(-) diff --git a/eslint.config.mjs b/eslint.config.mjs index 3719f6a..bf7d93a 100644 --- a/eslint.config.mjs +++ b/eslint.config.mjs @@ -1,8 +1,6 @@ import eslintPluginPrettierRecommended from "eslint-plugin-prettier/recommended"; import tseslint from "typescript-eslint"; -const ignores = ["**/node_modules/**", "**/dist/**"]; - export default [ { rules: { @@ -19,4 +17,5 @@ export default [ }, ...tseslint.configs.recommended, eslintPluginPrettierRecommended, -].map((r) => Object.assign(r, { ignores })); + { ignores: ["**/node_modules/**", "**/dist/**"] }, +]; diff --git a/src/ClassnamesMinifier.ts b/src/ClassnamesMinifier.ts index cab20f8..ddfe77d 100644 --- a/src/ClassnamesMinifier.ts +++ b/src/ClassnamesMinifier.ts @@ -25,30 +25,31 @@ class ClassnamesMinifier { } else { const manifestDir = path.join(config.cacheDir, "ncm-meta"); const manifestPath = path.join(manifestDir, "manifest.json"); + const { distDeletionPolicy = "error" } = config; let distCleared = false; if (config.cacheDir) { const errors = validateDist(config, manifestPath); if (errors) { - if (config.distDeletionPolicy === "auto") { - console.log(`classnames-minifier: ${errors}"distDeletionPolicy" option was set to auto`); + console.log(`classnames-minifier: "distDeletionPolicy" option was set to ${distDeletionPolicy}`); + if (distDeletionPolicy === "auto") { removeDist(config.distDir, errors); distCleared = true; - } else if (config.distDeletionPolicy === "error") { - throw new Error(`classnames-minifier: ${errors}"distDeletionPolicy" option was set to error`); + } else if (distDeletionPolicy === "error") { + throw new Error(`classnames-minifier: ${errors}`); } else { - console.warn(`classnames-minifier: ${errors}"distDeletionPolicy" option was set to warning`); + console.warn(`classnames-minifier: ${errors}`); } } } const { syncFreedNames, freedNamesLimit = 100000 } = config.experimental || {}; if (!syncFreedNames && this.converterMinified.freeClasses.length > freedNamesLimit && config.distDir) { - if (config.distDeletionPolicy === "auto") { + if (distDeletionPolicy === "auto") { removeDist(config.distDir, `Freed names exceeds the limit (${freedNamesLimit})`); distCleared = true; - } else if (config.distDeletionPolicy === "error") { + } else if (distDeletionPolicy === "error") { throw new Error(`Freed names exceeds the limit (${freedNamesLimit})`); } else { console.warn(`Freed names exceeds the limit (${freedNamesLimit})`); diff --git a/src/lib/constants/configuration.ts b/src/lib/constants/configuration.ts index 03d3bb1..523b2d1 100644 --- a/src/lib/constants/configuration.ts +++ b/src/lib/constants/configuration.ts @@ -3,4 +3,4 @@ * and minifying the previous version is not compatible with the current one, * which would mean that we should clean dist folder */ -export const CODE_VERSION = "rabbit"; +export const CODE_VERSION = "parrot"; diff --git a/src/lib/types/plugin.ts b/src/lib/types/plugin.ts index 3eb5dd0..079832c 100644 --- a/src/lib/types/plugin.ts +++ b/src/lib/types/plugin.ts @@ -35,6 +35,10 @@ export type Config = { * @default "error" */ distDeletionPolicy?: "warning" | "error" | "auto"; + /** + * Additional check of the dist directory for freshness + */ + checkDistFreshness?: () => boolean; experimental?: { /** * Automatically synchronize freed classes (for example, if you deleted the original styles) diff --git a/src/lib/validateConfig.ts b/src/lib/validateConfig.ts index 1012b03..c045317 100644 --- a/src/lib/validateConfig.ts +++ b/src/lib/validateConfig.ts @@ -1,6 +1,14 @@ import type { Config } from "./types/plugin"; -const validKeys = ["prefix", "reservedNames", "cacheDir", "distDir", "distDeletionPolicy", "experimental"]; +const validKeys = [ + "prefix", + "reservedNames", + "cacheDir", + "distDir", + "distDeletionPolicy", + "experimental", + "checkDistFreshness", +]; const validateIsObject = (config: unknown): config is Config => { if (!config) return false; diff --git a/src/lib/validateDist.ts b/src/lib/validateDist.ts index 23ca556..fc5fd7d 100644 --- a/src/lib/validateDist.ts +++ b/src/lib/validateDist.ts @@ -12,7 +12,7 @@ const readManifest = (manifestPath: string) => { }; const validateDist = (pluginOptions: Config, manifestPath: string) => { - const { cacheDir, distDir, prefix, reservedNames, distDeletionPolicy } = pluginOptions; + const { cacheDir, distDir, prefix, reservedNames, distDeletionPolicy, checkDistFreshness } = pluginOptions; if (!cacheDir || !distDir) { console.log( @@ -51,7 +51,7 @@ const validateDist = (pluginOptions: Config, manifestPath: string) => { if (configDiffMessages.length) { configurationError = `Changes found in package configuration: \n${configDiffMessages.map((message) => `- ${message};\n`)}`; } - } else { + } else if (!checkDistFreshness?.()) { configurationError = `Can not find the package cache manifest at ${manifestPath}\n`; } return configurationError; diff --git a/tsconfig.json b/tsconfig.json index 0321775..e222e01 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -8,7 +8,7 @@ "skipLibCheck": true, "outDir": "dist", "rootDir": "src", - "removeComments": true, + "removeComments": false, "declaration": true } } From 2415451d32a1d1b86cda24f38be274adce3f8fe0 Mon Sep 17 00:00:00 2001 From: Vordgi Date: Sat, 26 Oct 2024 20:13:35 +0400 Subject: [PATCH 2/3] cm-50 improve error messages --- src/ClassnamesMinifier.ts | 15 ++++++++++----- src/lib/validateDist.ts | 2 +- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/ClassnamesMinifier.ts b/src/ClassnamesMinifier.ts index ddfe77d..c38b952 100644 --- a/src/ClassnamesMinifier.ts +++ b/src/ClassnamesMinifier.ts @@ -32,27 +32,32 @@ class ClassnamesMinifier { const errors = validateDist(config, manifestPath); if (errors) { - console.log(`classnames-minifier: "distDeletionPolicy" option was set to ${distDeletionPolicy}`); + console.log(`classnames-minifier: "distDeletionPolicy" option was set to "${distDeletionPolicy}"`); if (distDeletionPolicy === "auto") { removeDist(config.distDir, errors); distCleared = true; } else if (distDeletionPolicy === "error") { - throw new Error(`classnames-minifier: ${errors}`); + throw new Error(`classnames-minifier: Please, remove dist dir manually. ${errors}`); } else { - console.warn(`classnames-minifier: ${errors}`); + console.warn(`classnames-minifier: Please, remove dist dir manually. ${errors}`); } } } const { syncFreedNames, freedNamesLimit = 100000 } = config.experimental || {}; if (!syncFreedNames && this.converterMinified.freeClasses.length > freedNamesLimit && config.distDir) { + console.log(`classnames-minifier: "distDeletionPolicy" option was set to "${distDeletionPolicy}"`); if (distDeletionPolicy === "auto") { removeDist(config.distDir, `Freed names exceeds the limit (${freedNamesLimit})`); distCleared = true; } else if (distDeletionPolicy === "error") { - throw new Error(`Freed names exceeds the limit (${freedNamesLimit})`); + throw new Error( + `Please, remove dist dir manually. Freed names exceeds the limit (${freedNamesLimit})`, + ); } else { - console.warn(`Freed names exceeds the limit (${freedNamesLimit})`); + console.warn( + `Please, remove dist dir manually. Freed names exceeds the limit (${freedNamesLimit})`, + ); } } if (distCleared) { diff --git a/src/lib/validateDist.ts b/src/lib/validateDist.ts index fc5fd7d..a4485f3 100644 --- a/src/lib/validateDist.ts +++ b/src/lib/validateDist.ts @@ -49,7 +49,7 @@ const validateDist = (pluginOptions: Config, manifestPath: string) => { configDiffMessages.push(`"distDeletionPolicy" set to "${distDeletionPolicy}"`); } if (configDiffMessages.length) { - configurationError = `Changes found in package configuration: \n${configDiffMessages.map((message) => `- ${message};\n`)}`; + configurationError = `Changes found in package configuration: \n${configDiffMessages.map((message) => `- ${message};\n`).join("")}`; } } else if (!checkDistFreshness?.()) { configurationError = `Can not find the package cache manifest at ${manifestPath}\n`; From c9ca9a7f1060dd90caa0d0cfb60c1072a4baa9c5 Mon Sep 17 00:00:00 2001 From: Vordgi Date: Sat, 26 Oct 2024 20:18:31 +0400 Subject: [PATCH 3/3] cm-50 release 1.0.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 7ed78cf..1d9aac1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "classnames-minifier", - "version": "0.2.2", + "version": "1.0.0", "description": "Library for configuring style modules to generate compressed classes", "main": "./dist/ClassnamesMinifier.js", "types": "./dist/ClassnamesMinifier.d.ts",