classnames-minifier 0.1.2-canary.1 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/ClassnamesMinifier.d.ts +3 -3
- package/dist/ClassnamesMinifier.js +2 -3
- package/dist/lib/ConverterMinified.d.ts +3 -3
- package/dist/lib/ConverterMinified.js +75 -19
- package/dist/lib/classnames-minifier-postloader.d.ts +2 -2
- package/dist/lib/classnames-minifier-postloader.js +4 -2
- package/dist/lib/classnames-minifier-preloader.d.ts +2 -2
- package/dist/lib/classnames-minifier-preloader.js +3 -4
- package/dist/lib/constants/configuration.d.ts +0 -5
- package/dist/lib/constants/configuration.js +1 -6
- package/dist/lib/validateConfig.js +4 -4
- package/dist/lib/validateDist.d.ts +1 -1
- package/dist/lib/validateDist.js +13 -13
- package/package.json +12 -3
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import type { LoaderContext } from
|
|
2
|
-
import type { Config } from
|
|
3
|
-
import ConverterMinified from
|
|
1
|
+
import type { LoaderContext } from "webpack";
|
|
2
|
+
import type { Config } from "./lib/types/plugin";
|
|
3
|
+
import ConverterMinified from "./lib/ConverterMinified";
|
|
4
4
|
declare class ClassnamesMinifier {
|
|
5
5
|
converterMinified: ConverterMinified;
|
|
6
6
|
constructor(config: Config);
|
|
@@ -20,7 +20,7 @@ class ClassnamesMinifier {
|
|
|
20
20
|
}
|
|
21
21
|
get preLoader() {
|
|
22
22
|
return {
|
|
23
|
-
loader: path_1.default.join(__dirname,
|
|
23
|
+
loader: path_1.default.join(__dirname, "./lib/classnames-minifier-preloader.js"),
|
|
24
24
|
options: {
|
|
25
25
|
classnamesMinifier: this.converterMinified,
|
|
26
26
|
},
|
|
@@ -28,12 +28,11 @@ class ClassnamesMinifier {
|
|
|
28
28
|
}
|
|
29
29
|
get postLoader() {
|
|
30
30
|
return {
|
|
31
|
-
loader: path_1.default.join(__dirname,
|
|
31
|
+
loader: path_1.default.join(__dirname, "./lib/classnames-minifier-postloader.js"),
|
|
32
32
|
options: {
|
|
33
33
|
classnamesMinifier: this.converterMinified,
|
|
34
34
|
},
|
|
35
35
|
};
|
|
36
36
|
}
|
|
37
37
|
}
|
|
38
|
-
;
|
|
39
38
|
exports.default = ClassnamesMinifier;
|
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import type { LoaderContext } from
|
|
2
|
-
import type { Config } from
|
|
1
|
+
import type { LoaderContext } from "webpack";
|
|
2
|
+
import type { Config } from "./types/plugin";
|
|
3
3
|
type CacheType = {
|
|
4
4
|
[resourcePath: string]: {
|
|
5
5
|
cachePath: string;
|
|
6
6
|
matchings: {
|
|
7
7
|
[origClass: string]: string;
|
|
8
8
|
};
|
|
9
|
-
type:
|
|
9
|
+
type: "new" | "updated" | "old";
|
|
10
10
|
};
|
|
11
11
|
};
|
|
12
12
|
declare class ConverterMinified {
|
|
@@ -7,12 +7,71 @@ const fs_1 = require("fs");
|
|
|
7
7
|
const uuid_1 = require("uuid");
|
|
8
8
|
const path_1 = __importDefault(require("path"));
|
|
9
9
|
class ConverterMinified {
|
|
10
|
-
constructor({ cacheDir, prefix =
|
|
10
|
+
constructor({ cacheDir, prefix = "", reservedNames = [] }) {
|
|
11
11
|
this.dirtyСache = {};
|
|
12
12
|
this.symbols = [
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
13
|
+
"a",
|
|
14
|
+
"b",
|
|
15
|
+
"c",
|
|
16
|
+
"d",
|
|
17
|
+
"e",
|
|
18
|
+
"f",
|
|
19
|
+
"g",
|
|
20
|
+
"h",
|
|
21
|
+
"i",
|
|
22
|
+
"j",
|
|
23
|
+
"k",
|
|
24
|
+
"l",
|
|
25
|
+
"m",
|
|
26
|
+
"n",
|
|
27
|
+
"o",
|
|
28
|
+
"p",
|
|
29
|
+
"q",
|
|
30
|
+
"r",
|
|
31
|
+
"s",
|
|
32
|
+
"t",
|
|
33
|
+
"u",
|
|
34
|
+
"v",
|
|
35
|
+
"w",
|
|
36
|
+
"x",
|
|
37
|
+
"y",
|
|
38
|
+
"z",
|
|
39
|
+
"A",
|
|
40
|
+
"B",
|
|
41
|
+
"C",
|
|
42
|
+
"D",
|
|
43
|
+
"E",
|
|
44
|
+
"F",
|
|
45
|
+
"G",
|
|
46
|
+
"H",
|
|
47
|
+
"I",
|
|
48
|
+
"J",
|
|
49
|
+
"K",
|
|
50
|
+
"L",
|
|
51
|
+
"M",
|
|
52
|
+
"N",
|
|
53
|
+
"O",
|
|
54
|
+
"P",
|
|
55
|
+
"Q",
|
|
56
|
+
"R",
|
|
57
|
+
"S",
|
|
58
|
+
"T",
|
|
59
|
+
"U",
|
|
60
|
+
"V",
|
|
61
|
+
"W",
|
|
62
|
+
"X",
|
|
63
|
+
"Y",
|
|
64
|
+
"Z",
|
|
65
|
+
"0",
|
|
66
|
+
"1",
|
|
67
|
+
"2",
|
|
68
|
+
"3",
|
|
69
|
+
"4",
|
|
70
|
+
"5",
|
|
71
|
+
"6",
|
|
72
|
+
"7",
|
|
73
|
+
"8",
|
|
74
|
+
"9",
|
|
16
75
|
];
|
|
17
76
|
this.freeClasses = [];
|
|
18
77
|
this.lastIndex = 0;
|
|
@@ -25,20 +84,20 @@ class ConverterMinified {
|
|
|
25
84
|
(0, fs_1.mkdirSync)(cacheDir, { recursive: true });
|
|
26
85
|
const cachedFiles = (0, fs_1.readdirSync)(cacheDir);
|
|
27
86
|
if (cachedFiles.length) {
|
|
28
|
-
console.log(
|
|
87
|
+
console.log("classnames-minifier: Restoring pairs of classes...");
|
|
29
88
|
}
|
|
30
89
|
const usedClassNames = [];
|
|
31
90
|
const dirtyСache = {};
|
|
32
91
|
let prevLastIndex = 0;
|
|
33
92
|
cachedFiles.forEach((file) => {
|
|
34
93
|
const filePath = path_1.default.join(cacheDir, file);
|
|
35
|
-
const dirtyCacheFile = (0, fs_1.readFileSync)(filePath, { encoding:
|
|
36
|
-
const [resourcePath, lastIndex, ...classnames] = dirtyCacheFile.split(
|
|
94
|
+
const dirtyCacheFile = (0, fs_1.readFileSync)(filePath, { encoding: "utf8" });
|
|
95
|
+
const [resourcePath, lastIndex, ...classnames] = dirtyCacheFile.split(",");
|
|
37
96
|
if (lastIndex && +lastIndex > prevLastIndex)
|
|
38
97
|
prevLastIndex = +lastIndex;
|
|
39
98
|
if ((0, fs_1.existsSync)(resourcePath)) {
|
|
40
99
|
const cachedMatchings = classnames.reduce((acc, cur) => {
|
|
41
|
-
const [origClass, newClass] = cur.split(
|
|
100
|
+
const [origClass, newClass] = cur.split("=");
|
|
42
101
|
acc[origClass] = newClass;
|
|
43
102
|
if (!usedClassNames.includes(newClass)) {
|
|
44
103
|
usedClassNames.push(newClass);
|
|
@@ -48,20 +107,18 @@ class ConverterMinified {
|
|
|
48
107
|
dirtyСache[resourcePath] = {
|
|
49
108
|
cachePath: filePath,
|
|
50
109
|
matchings: cachedMatchings,
|
|
51
|
-
type:
|
|
110
|
+
type: "old",
|
|
52
111
|
};
|
|
53
112
|
}
|
|
54
113
|
else {
|
|
55
114
|
(0, fs_1.rmSync)(filePath);
|
|
56
115
|
}
|
|
57
116
|
});
|
|
58
|
-
let unfoundClassNamesLength = usedClassNames.length;
|
|
59
117
|
for (let i = 0; i <= prevLastIndex; i++) {
|
|
60
118
|
const newClass = this.generateClassName();
|
|
61
119
|
this.lastIndex += 1;
|
|
62
120
|
const usedClassNameIndex = usedClassNames.indexOf(newClass);
|
|
63
121
|
if (usedClassNameIndex !== -1) {
|
|
64
|
-
unfoundClassNamesLength -= 1;
|
|
65
122
|
usedClassNames.splice(usedClassNameIndex, 1);
|
|
66
123
|
}
|
|
67
124
|
else if (!this.reservedNames.includes(newClass)) {
|
|
@@ -69,14 +126,14 @@ class ConverterMinified {
|
|
|
69
126
|
}
|
|
70
127
|
}
|
|
71
128
|
if (cachedFiles.length) {
|
|
72
|
-
console.log(
|
|
129
|
+
console.log("classnames-minifier: Pairs restored");
|
|
73
130
|
}
|
|
74
131
|
this.dirtyСache = dirtyСache;
|
|
75
132
|
};
|
|
76
133
|
this.prefix = prefix;
|
|
77
134
|
this.reservedNames = reservedNames;
|
|
78
135
|
if (cacheDir)
|
|
79
|
-
this.recycleCache(path_1.default.join(cacheDir,
|
|
136
|
+
this.recycleCache(path_1.default.join(cacheDir, "ncm"));
|
|
80
137
|
}
|
|
81
138
|
generateClassName() {
|
|
82
139
|
const symbolsCount = 62;
|
|
@@ -88,7 +145,7 @@ class ConverterMinified {
|
|
|
88
145
|
this.nameMap.push(0);
|
|
89
146
|
this.currentLoopLength += 1;
|
|
90
147
|
}
|
|
91
|
-
const currentClassname = this.prefix + this.nameMap.map((e) => this.symbols[e]).join(
|
|
148
|
+
const currentClassname = this.prefix + this.nameMap.map((e) => this.symbols[e]).join("");
|
|
92
149
|
for (let i = this.currentLoopLength; i >= 0; i--) {
|
|
93
150
|
if (this.nameMap[i] === symbolsCount - 1 || (i === 0 && this.nameMap[i] === 25)) {
|
|
94
151
|
this.nameMap[i] = 0;
|
|
@@ -117,20 +174,19 @@ class ConverterMinified {
|
|
|
117
174
|
getLocalIdent({ resourcePath }, _localIdent, origName) {
|
|
118
175
|
if (!this.dirtyСache[resourcePath]) {
|
|
119
176
|
this.dirtyСache[resourcePath] = {
|
|
120
|
-
cachePath: this.cacheDir ? path_1.default.join(this.cacheDir, (0, uuid_1.v4)()) :
|
|
177
|
+
cachePath: this.cacheDir ? path_1.default.join(this.cacheDir, (0, uuid_1.v4)()) : "",
|
|
121
178
|
matchings: {},
|
|
122
|
-
type:
|
|
179
|
+
type: "new",
|
|
123
180
|
};
|
|
124
181
|
}
|
|
125
182
|
const currentCache = this.dirtyСache[resourcePath];
|
|
126
183
|
if (currentCache.matchings[origName])
|
|
127
184
|
return currentCache.matchings[origName];
|
|
128
|
-
|
|
185
|
+
const targetClassName = this.getTargetClassName(origName);
|
|
129
186
|
currentCache.matchings[origName] = targetClassName;
|
|
130
|
-
currentCache.type =
|
|
187
|
+
currentCache.type = "updated";
|
|
131
188
|
this.lastIndex += 1;
|
|
132
189
|
return targetClassName;
|
|
133
190
|
}
|
|
134
191
|
}
|
|
135
|
-
;
|
|
136
192
|
exports.default = ConverterMinified;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { LoaderContext } from
|
|
2
|
-
import type ConverterMinified from
|
|
1
|
+
import type { LoaderContext } from "webpack";
|
|
2
|
+
import type ConverterMinified from "./ConverterMinified";
|
|
3
3
|
export default function (this: LoaderContext<{
|
|
4
4
|
classnamesMinifier: ConverterMinified;
|
|
5
5
|
}>, source: string, map: any, meta: any): void;
|
|
@@ -8,8 +8,10 @@ function default_1(source, map, meta) {
|
|
|
8
8
|
const options = this.getOptions();
|
|
9
9
|
const classnamesMinifier = options.classnamesMinifier;
|
|
10
10
|
Object.entries(classnamesMinifier.dirtyСache).forEach(([resourcePath, data]) => {
|
|
11
|
-
if (data.type !==
|
|
12
|
-
fs_1.default.writeFileSync(data.cachePath, `${resourcePath},${classnamesMinifier.lastIndex},${Object.entries(data.matchings)
|
|
11
|
+
if (data.type !== "old") {
|
|
12
|
+
fs_1.default.writeFileSync(data.cachePath, `${resourcePath},${classnamesMinifier.lastIndex},${Object.entries(data.matchings)
|
|
13
|
+
.map(([key, value]) => `${key}=${value}`)
|
|
14
|
+
.join(",")}`);
|
|
13
15
|
}
|
|
14
16
|
});
|
|
15
17
|
this.callback(null, source, map, meta);
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { LoaderContext } from
|
|
2
|
-
import type ConverterMinified from
|
|
1
|
+
import type { LoaderContext } from "webpack";
|
|
2
|
+
import type ConverterMinified from "./ConverterMinified";
|
|
3
3
|
export default function (this: LoaderContext<{
|
|
4
4
|
classnamesMinifier: ConverterMinified;
|
|
5
5
|
}>, source: string, map: any, meta: any): void;
|
|
@@ -5,11 +5,10 @@ function default_1(source, map, meta) {
|
|
|
5
5
|
const classnamesMinifier = options.classnamesMinifier;
|
|
6
6
|
const maybeClassesList = source.match(/\.-?[_a-zA-Z]+[_a-zA-Z0-9-]*/g);
|
|
7
7
|
const cache = classnamesMinifier.dirtyСache[this.resourcePath];
|
|
8
|
-
/**
|
|
9
|
-
* if some class has ceased to be used since the last time the file was loaded, we remove it from the cache
|
|
10
|
-
*/
|
|
11
8
|
if (cache && cache.matchings) {
|
|
12
|
-
cache.matchings = maybeClassesList
|
|
9
|
+
cache.matchings = maybeClassesList
|
|
10
|
+
? Object.fromEntries(Object.entries(cache.matchings).filter(([key]) => maybeClassesList === null || maybeClassesList === void 0 ? void 0 : maybeClassesList.includes(`.${key}`)))
|
|
11
|
+
: {};
|
|
13
12
|
}
|
|
14
13
|
this.callback(null, source, map, meta);
|
|
15
14
|
return;
|
|
@@ -1,6 +1 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Just a version of the code whose change means that the logic has a critical update
|
|
3
|
-
* and minifying the previous version is not compatible with the current one,
|
|
4
|
-
* which would mean that we should clean dist folder
|
|
5
|
-
*/
|
|
6
1
|
export declare const CODE_VERSION = "rabbit";
|
|
@@ -1,9 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.CODE_VERSION = void 0;
|
|
4
|
-
|
|
5
|
-
* Just a version of the code whose change means that the logic has a critical update
|
|
6
|
-
* and minifying the previous version is not compatible with the current one,
|
|
7
|
-
* which would mean that we should clean dist folder
|
|
8
|
-
*/
|
|
9
|
-
exports.CODE_VERSION = 'rabbit';
|
|
4
|
+
exports.CODE_VERSION = "rabbit";
|
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const validKeys = [
|
|
3
|
+
const validKeys = ["prefix", "reservedNames", "cacheDir", "distDir"];
|
|
4
4
|
const validateIsObject = (config) => {
|
|
5
5
|
if (!config)
|
|
6
6
|
return false;
|
|
7
|
-
if (typeof config !==
|
|
7
|
+
if (typeof config !== "object" || Array.isArray(config)) {
|
|
8
8
|
console.error(`classnames-minifier: Invalid configuration. Expected object, received ${typeof config}. See https://github.com/vordgi/classnames-minifier#configuration`);
|
|
9
9
|
process.exit();
|
|
10
10
|
}
|
|
11
|
-
const isValidKeys = Object.keys(config).every(key => validKeys.includes(key));
|
|
11
|
+
const isValidKeys = Object.keys(config).every((key) => validKeys.includes(key));
|
|
12
12
|
if (!isValidKeys) {
|
|
13
|
-
console.error(`classnames-minifier: Invalid configuration. Valid keys are: ${validKeys.join(
|
|
13
|
+
console.error(`classnames-minifier: Invalid configuration. Valid keys are: ${validKeys.join(", ")}. See https://github.com/vordgi/classnames-minifier#configuration`);
|
|
14
14
|
process.exit();
|
|
15
15
|
}
|
|
16
16
|
return true;
|
package/dist/lib/validateDist.js
CHANGED
|
@@ -8,7 +8,7 @@ const fs_1 = __importDefault(require("fs"));
|
|
|
8
8
|
const configuration_1 = require("./constants/configuration");
|
|
9
9
|
const readManifest = (manifestPath) => {
|
|
10
10
|
try {
|
|
11
|
-
const prevData = fs_1.default.readFileSync(manifestPath, { encoding:
|
|
11
|
+
const prevData = fs_1.default.readFileSync(manifestPath, { encoding: "utf-8" });
|
|
12
12
|
return JSON.parse(prevData);
|
|
13
13
|
}
|
|
14
14
|
catch (_a) {
|
|
@@ -19,20 +19,20 @@ const validateDist = (pluginOptions) => {
|
|
|
19
19
|
var _a, _b;
|
|
20
20
|
const { cacheDir, distDir, prefix, reservedNames } = pluginOptions;
|
|
21
21
|
if (!cacheDir || !distDir) {
|
|
22
|
-
console.log(
|
|
22
|
+
console.log("classnames-minifier: Failed to check the dist folder because cacheDir or distDir is not specified");
|
|
23
23
|
return;
|
|
24
24
|
}
|
|
25
|
-
const manifestDir = path_1.default.join(cacheDir,
|
|
26
|
-
const manifestPath = path_1.default.join(manifestDir,
|
|
25
|
+
const manifestDir = path_1.default.join(cacheDir, "ncm-meta");
|
|
26
|
+
const manifestPath = path_1.default.join(manifestDir, "manifest.json");
|
|
27
27
|
let isImpreciseDist = false;
|
|
28
28
|
if (fs_1.default.existsSync(manifestPath)) {
|
|
29
29
|
const prevData = readManifest(manifestPath);
|
|
30
|
-
if (prevData.prefix !== prefix
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
30
|
+
if (prevData.prefix !== prefix ||
|
|
31
|
+
prevData.cacheDir !== cacheDir ||
|
|
32
|
+
prevData.distDir !== distDir ||
|
|
33
|
+
((_a = prevData.reservedNames) === null || _a === void 0 ? void 0 : _a.length) !== (reservedNames === null || reservedNames === void 0 ? void 0 : reservedNames.length) ||
|
|
34
|
+
((_b = prevData.reservedNames) === null || _b === void 0 ? void 0 : _b.some((name) => !(reservedNames === null || reservedNames === void 0 ? void 0 : reservedNames.includes(name)))) ||
|
|
35
|
+
prevData.version !== configuration_1.CODE_VERSION) {
|
|
36
36
|
isImpreciseDist = true;
|
|
37
37
|
}
|
|
38
38
|
}
|
|
@@ -40,12 +40,12 @@ const validateDist = (pluginOptions) => {
|
|
|
40
40
|
isImpreciseDist = true;
|
|
41
41
|
}
|
|
42
42
|
if (isImpreciseDist) {
|
|
43
|
-
console.log(
|
|
43
|
+
console.log("classnames-minifier: Changes found in package configuration. Cleaning the dist folder...");
|
|
44
44
|
fs_1.default.rmSync(distDir, { recursive: true, force: true });
|
|
45
|
-
console.log(
|
|
45
|
+
console.log("classnames-minifier: Changes found in package configuration. Dist folder cleared");
|
|
46
46
|
}
|
|
47
47
|
if (!fs_1.default.existsSync(manifestDir))
|
|
48
48
|
fs_1.default.mkdirSync(manifestDir, { recursive: true });
|
|
49
|
-
fs_1.default.writeFileSync(manifestPath, JSON.stringify(Object.assign(Object.assign({}, pluginOptions), { version: configuration_1.CODE_VERSION })), { encoding:
|
|
49
|
+
fs_1.default.writeFileSync(manifestPath, JSON.stringify(Object.assign(Object.assign({}, pluginOptions), { version: configuration_1.CODE_VERSION })), { encoding: "utf-8" });
|
|
50
50
|
};
|
|
51
51
|
exports.default = validateDist;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "classnames-minifier",
|
|
3
|
-
"version": "0.1.2
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"description": "Library for configuring style modules to generate compressed classes",
|
|
5
5
|
"main": "./dist/ClassnamesMinifier.js",
|
|
6
6
|
"types": "./dist/ClassnamesMinifier.d.ts",
|
|
@@ -8,7 +8,10 @@
|
|
|
8
8
|
"dist"
|
|
9
9
|
],
|
|
10
10
|
"scripts": {
|
|
11
|
-
"build": "tsc"
|
|
11
|
+
"build": "tsc",
|
|
12
|
+
"lint": "eslint .",
|
|
13
|
+
"eslint": "eslint",
|
|
14
|
+
"prepare": "husky"
|
|
12
15
|
},
|
|
13
16
|
"keywords": [
|
|
14
17
|
"classname",
|
|
@@ -36,7 +39,13 @@
|
|
|
36
39
|
"@types/uuid": "9.0.7",
|
|
37
40
|
"@types/webpack": "5.28.0",
|
|
38
41
|
"css-loader": "6.9.0",
|
|
39
|
-
"typescript": "5.2.2"
|
|
42
|
+
"typescript": "5.2.2",
|
|
43
|
+
"eslint": "8.57.0",
|
|
44
|
+
"eslint-config-prettier": "^9.1.0",
|
|
45
|
+
"eslint-plugin-prettier": "^5.1.3",
|
|
46
|
+
"husky": "^9.0.11",
|
|
47
|
+
"prettier": "^3.2.5",
|
|
48
|
+
"typescript-eslint": "^7.6.0"
|
|
40
49
|
},
|
|
41
50
|
"peerDependencies": {
|
|
42
51
|
"css-loader": ">=4.0.0"
|