Skip to content

Commit

Permalink
Merge pull request #51 from vordgi/cm-50
Browse files Browse the repository at this point in the history
cm-50 support checkdistfreshness option
  • Loading branch information
vordgi authored Oct 26, 2024
2 parents 63cbf39 + c9ca9a7 commit ef8fb90
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 19 deletions.
5 changes: 2 additions & 3 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import eslintPluginPrettierRecommended from "eslint-plugin-prettier/recommended";
import tseslint from "typescript-eslint";

const ignores = ["**/node_modules/**", "**/dist/**"];

export default [
{
rules: {
Expand All @@ -19,4 +17,5 @@ export default [
},
...tseslint.configs.recommended,
eslintPluginPrettierRecommended,
].map((r) => Object.assign(r, { ignores }));
{ ignores: ["**/node_modules/**", "**/dist/**"] },
];
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.2",
"version": "1.0.0",
"description": "Library for configuring style modules to generate compressed classes",
"main": "./dist/ClassnamesMinifier.js",
"types": "./dist/ClassnamesMinifier.d.ts",
Expand Down
24 changes: 15 additions & 9 deletions src/ClassnamesMinifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,33 +25,39 @@ 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: Please, remove dist dir manually. ${errors}`);
} else {
console.warn(`classnames-minifier: ${errors}"distDeletionPolicy" option was set to warning`);
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) {
if (config.distDeletionPolicy === "auto") {
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 (config.distDeletionPolicy === "error") {
throw new Error(`Freed names exceeds the limit (${freedNamesLimit})`);
} else if (distDeletionPolicy === "error") {
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) {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/constants/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
4 changes: 4 additions & 0 deletions src/lib/types/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 9 additions & 1 deletion src/lib/validateConfig.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
6 changes: 3 additions & 3 deletions src/lib/validateDist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -49,9 +49,9 @@ 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 {
} else if (!checkDistFreshness?.()) {
configurationError = `Can not find the package cache manifest at ${manifestPath}\n`;
}
return configurationError;
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"skipLibCheck": true,
"outDir": "dist",
"rootDir": "src",
"removeComments": true,
"removeComments": false,
"declaration": true
}
}

0 comments on commit ef8fb90

Please sign in to comment.