Skip to content

Commit

Permalink
Merge pull request #38 from vordgi/cm-37
Browse files Browse the repository at this point in the history
cm-37 release 0.2.0
  • Loading branch information
vordgi authored Jun 27, 2024
2 parents 54a580a + 4c4083c commit 329179b
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 20 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "classnames-minifier",
"version": "0.1.5",
"version": "0.2.0",
"description": "Library for configuring style modules to generate compressed classes",
"main": "./dist/ClassnamesMinifier.js",
"types": "./dist/ClassnamesMinifier.d.ts",
Expand Down Expand Up @@ -53,4 +53,4 @@
"dependencies": {
"uuid": "9.0.1"
}
}
}
32 changes: 28 additions & 4 deletions src/ClassnamesMinifier.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,46 @@
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import type { LoaderContext } from "webpack";
import fs from "fs";
import path from "path";
import type { Config } from "./lib/types/plugin";
import { CODE_VERSION } from "./lib/constants/configuration";
import validateConfig from "./lib/validateConfig";
import ConverterMinified from "./lib/ConverterMinified";
import validateDist from "./lib/validateDist";
import rmDist from "./lib/rmDist";

class ClassnamesMinifier {
converterMinified: ConverterMinified;

constructor(config: Config) {
validateConfig(config);

if (config.cacheDir) {
validateDist(config);
}

this.converterMinified = new ConverterMinified(config);

if (!config.cacheDir || !config.distDir) {
console.log(
"classnames-minifier: Failed to check the dist folder because cacheDir or distDir is not specified",
);
} else {
const manifestDir = path.join(config.cacheDir, "ncm-meta");
const manifestPath = path.join(manifestDir, "manifest.json");

if (config.cacheDir) {
validateDist(config, manifestPath);
}

const { freedNamesPolicy = "transmit", blockLimit = 100000 } = config.experimental || {};
if (
freedNamesPolicy === "block" &&
this.converterMinified.freeClasses.length > blockLimit &&
config.distDir
) {
rmDist(config.distDir, `Freed names exceeds the limit (${blockLimit})`);
this.converterMinified.clean();
}
if (!fs.existsSync(manifestDir)) fs.mkdirSync(manifestDir, { recursive: true });
fs.writeFileSync(manifestPath, JSON.stringify({ ...config, version: CODE_VERSION }), { encoding: "utf-8" });
}
}

get getLocalIdent() {
Expand Down
20 changes: 16 additions & 4 deletions src/lib/ConverterMinified.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class ConverterMinified {

private reservedNames: string[];

dirtyСache: CacheType = {};
private freedNamesPolicy: "transmit" | "block";

private symbols: string[] = [
"a",
Expand Down Expand Up @@ -86,7 +86,9 @@ class ConverterMinified {
"9",
];

private freeClasses: string[] = [];
dirtyСache: CacheType = {};

freeClasses: string[] = [];

lastIndex = 0;

Expand All @@ -96,13 +98,23 @@ class ConverterMinified {

private nameMap = [0];

constructor({ cacheDir, prefix = "", reservedNames = [] }: Config) {
constructor({ cacheDir, prefix = "", reservedNames = [], experimental }: Config) {
this.prefix = prefix;
this.reservedNames = reservedNames;
this.freedNamesPolicy = experimental?.freedNamesPolicy || "transmit";

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

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

private recycleCache = (cacheDir: string) => {
this.cacheDir = cacheDir;
if (!existsSync(cacheDir)) mkdirSync(cacheDir, { recursive: true });
Expand Down Expand Up @@ -185,7 +197,7 @@ class ConverterMinified {

private getTargetClassName(origName: string) {
let targetClassName: string;
if (this.freeClasses.length) {
if (this.freeClasses.length && this.freedNamesPolicy === "transmit") {
targetClassName = this.freeClasses.shift() as string;
} else {
targetClassName = this.generateClassName();
Expand Down
9 changes: 9 additions & 0 deletions src/lib/rmDist.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { rmSync } from "fs";

const rmDist = (distDir: string, message: string) => {
console.log(`classnames-minifier: ${message}Cleaning the dist folder...`);
rmSync(distDir, { recursive: true, force: true });
console.log("classnames-minifier: Dist folder cleared");
};

export default rmDist;
4 changes: 4 additions & 0 deletions src/lib/types/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@ export type Config = {
prefix?: string;
reservedNames?: string[];
disableDistDeletion?: boolean;
experimental?: {
freedNamesPolicy?: "transmit" | "block";
blockLimit?: number;
};
};
13 changes: 3 additions & 10 deletions src/lib/validateDist.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import path from "path";
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 All @@ -12,7 +12,7 @@ const readManifest = (manifestPath: string) => {
}
};

const validateDist = (pluginOptions: Config) => {
const validateDist = (pluginOptions: Config, manifestPath: string) => {
const { cacheDir, distDir, prefix, reservedNames, disableDistDeletion } = pluginOptions;

if (!cacheDir || !distDir) {
Expand All @@ -21,9 +21,6 @@ const validateDist = (pluginOptions: Config) => {
);
return;
}

const manifestDir = path.join(cacheDir, "ncm-meta");
const manifestPath = path.join(manifestDir, "manifest.json");
let configurationError = "";

if (fs.existsSync(manifestPath)) {
Expand Down Expand Up @@ -60,15 +57,11 @@ const validateDist = (pluginOptions: Config) => {
}
if (configurationError) {
if (!disableDistDeletion) {
console.log(`classnames-minifier: ${configurationError}Cleaning the dist folder...`);
fs.rmSync(distDir, { recursive: true, force: true });
console.log("classnames-minifier: Dist folder cleared");
rmDist(distDir, configurationError);
} else {
console.log(`classnames-minifier: ${configurationError}"disableDistDeletion" option was set to true`);
}
}
if (!fs.existsSync(manifestDir)) fs.mkdirSync(manifestDir, { recursive: true });
fs.writeFileSync(manifestPath, JSON.stringify({ ...pluginOptions, version: CODE_VERSION }), { encoding: "utf-8" });
};

export default validateDist;

0 comments on commit 329179b

Please sign in to comment.