classnames-minifier 0.0.0-experimental-6b8a553
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/LICENSE +21 -0
- package/README.md +83 -0
- package/dist/cjs/ClassnamesMinifier.d.ts +21 -0
- package/dist/cjs/ClassnamesMinifier.js +83 -0
- package/dist/cjs/lib/ConverterMinified.d.ts +31 -0
- package/dist/cjs/lib/ConverterMinified.js +208 -0
- package/dist/cjs/lib/classnames-minifier-postloader.d.ts +5 -0
- package/dist/cjs/lib/classnames-minifier-postloader.js +20 -0
- package/dist/cjs/lib/classnames-minifier-preloader.d.ts +5 -0
- package/dist/cjs/lib/classnames-minifier-preloader.js +19 -0
- package/dist/cjs/lib/constants/configuration.d.ts +6 -0
- package/dist/cjs/lib/constants/configuration.js +9 -0
- package/dist/cjs/lib/removeDist.d.ts +2 -0
- package/dist/cjs/lib/removeDist.js +9 -0
- package/dist/cjs/lib/types/plugin.d.ts +60 -0
- package/dist/cjs/lib/types/plugin.js +2 -0
- package/dist/cjs/lib/validateConfig.d.ts +3 -0
- package/dist/cjs/lib/validateConfig.js +35 -0
- package/dist/cjs/lib/validateDist.d.ts +3 -0
- package/dist/cjs/lib/validateDist.js +56 -0
- package/dist/esm/ClassnamesMinifier.d.ts +21 -0
- package/dist/esm/ClassnamesMinifier.js +79 -0
- package/dist/esm/lib/ConverterMinified.d.ts +31 -0
- package/dist/esm/lib/ConverterMinified.js +207 -0
- package/dist/esm/lib/classnames-minifier-postloader.d.ts +5 -0
- package/dist/esm/lib/classnames-minifier-postloader.js +14 -0
- package/dist/esm/lib/classnames-minifier-preloader.d.ts +5 -0
- package/dist/esm/lib/classnames-minifier-preloader.js +16 -0
- package/dist/esm/lib/constants/configuration.d.ts +6 -0
- package/dist/esm/lib/constants/configuration.js +6 -0
- package/dist/esm/lib/removeDist.d.ts +2 -0
- package/dist/esm/lib/removeDist.js +7 -0
- package/dist/esm/lib/types/plugin.d.ts +60 -0
- package/dist/esm/lib/types/plugin.js +1 -0
- package/dist/esm/lib/validateConfig.d.ts +3 -0
- package/dist/esm/lib/validateConfig.js +33 -0
- package/dist/esm/lib/validateDist.d.ts +3 -0
- package/dist/esm/lib/validateDist.js +50 -0
- package/package.json +62 -0
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const fs_1 = __importDefault(require("fs"));
|
|
7
|
+
const configuration_1 = require("./constants/configuration");
|
|
8
|
+
const readManifest = (manifestPath) => {
|
|
9
|
+
try {
|
|
10
|
+
const prevData = fs_1.default.readFileSync(manifestPath, { encoding: "utf-8" });
|
|
11
|
+
return JSON.parse(prevData);
|
|
12
|
+
}
|
|
13
|
+
catch (_a) {
|
|
14
|
+
return {};
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
const validateDist = (pluginOptions, manifestPath) => {
|
|
18
|
+
var _a, _b, _c;
|
|
19
|
+
const { cacheDir, distDir, prefix, reservedNames, distDeletionPolicy, checkDistFreshness } = pluginOptions;
|
|
20
|
+
if (!cacheDir || !distDir) {
|
|
21
|
+
console.log("classnames-minifier: Failed to check the dist folder because cacheDir or distDir is not specified");
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
let configurationError = "";
|
|
25
|
+
if (fs_1.default.existsSync(manifestPath)) {
|
|
26
|
+
const prevData = readManifest(manifestPath);
|
|
27
|
+
const configDiffMessages = [];
|
|
28
|
+
if (prevData.prefix !== prefix) {
|
|
29
|
+
configDiffMessages.push(`Different "prefix": "${prevData.prefix}" -> "${prefix}"`);
|
|
30
|
+
}
|
|
31
|
+
if (prevData.cacheDir !== cacheDir) {
|
|
32
|
+
configDiffMessages.push(`Different "cacheDir": "${prevData.cacheDir}" -> "${cacheDir}"`);
|
|
33
|
+
}
|
|
34
|
+
if (prevData.distDir !== distDir) {
|
|
35
|
+
configDiffMessages.push(`Different "distDir": "${prevData.distDir}" -> "${distDir}"`);
|
|
36
|
+
}
|
|
37
|
+
if (((_a = prevData.reservedNames) === null || _a === void 0 ? void 0 : _a.length) !== (reservedNames === null || reservedNames === void 0 ? void 0 : reservedNames.length) ||
|
|
38
|
+
((_b = prevData.reservedNames) === null || _b === void 0 ? void 0 : _b.some((name) => !(reservedNames === null || reservedNames === void 0 ? void 0 : reservedNames.includes(name))))) {
|
|
39
|
+
configDiffMessages.push(`Different "reservedNames": "${(_c = prevData.reservedNames) === null || _c === void 0 ? void 0 : _c.join(", ")}" -> "${reservedNames === null || reservedNames === void 0 ? void 0 : reservedNames.join(", ")}"`);
|
|
40
|
+
}
|
|
41
|
+
if (prevData.version !== configuration_1.CODE_VERSION) {
|
|
42
|
+
configDiffMessages.push(`Different package version: "${prevData.version}" -> "${configuration_1.CODE_VERSION}"`);
|
|
43
|
+
}
|
|
44
|
+
if (prevData.distDeletionPolicy && !distDeletionPolicy) {
|
|
45
|
+
configDiffMessages.push(`"distDeletionPolicy" set to "${distDeletionPolicy}"`);
|
|
46
|
+
}
|
|
47
|
+
if (configDiffMessages.length) {
|
|
48
|
+
configurationError = `Changes found in package configuration: \n${configDiffMessages.map((message) => `- ${message};\n`).join("")}`;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
else if (!(checkDistFreshness === null || checkDistFreshness === void 0 ? void 0 : checkDistFreshness())) {
|
|
52
|
+
configurationError = `Can not find the package cache manifest at ${manifestPath}\n`;
|
|
53
|
+
}
|
|
54
|
+
return configurationError;
|
|
55
|
+
};
|
|
56
|
+
exports.default = validateDist;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { LoaderContext } from "webpack";
|
|
2
|
+
import type { Config } from "./lib/types/plugin";
|
|
3
|
+
import ConverterMinified from "./lib/ConverterMinified";
|
|
4
|
+
declare class ClassnamesMinifier {
|
|
5
|
+
converterMinified: ConverterMinified;
|
|
6
|
+
constructor(config: Config);
|
|
7
|
+
get getLocalIdent(): ({ resourcePath }: LoaderContext<any>, _localIdent: string, origName: string) => string;
|
|
8
|
+
get preLoader(): {
|
|
9
|
+
loader: string;
|
|
10
|
+
options: {
|
|
11
|
+
classnamesMinifier: ConverterMinified;
|
|
12
|
+
};
|
|
13
|
+
};
|
|
14
|
+
get postLoader(): {
|
|
15
|
+
loader: string;
|
|
16
|
+
options: {
|
|
17
|
+
classnamesMinifier: ConverterMinified;
|
|
18
|
+
};
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
export default ClassnamesMinifier;
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
import path from "path";
|
|
3
|
+
import { CODE_VERSION } from "./lib/constants/configuration";
|
|
4
|
+
import validateConfig from "./lib/validateConfig";
|
|
5
|
+
import ConverterMinified from "./lib/ConverterMinified";
|
|
6
|
+
import validateDist from "./lib/validateDist";
|
|
7
|
+
import removeDist from "./lib/removeDist";
|
|
8
|
+
class ClassnamesMinifier {
|
|
9
|
+
converterMinified;
|
|
10
|
+
constructor(config) {
|
|
11
|
+
validateConfig(config);
|
|
12
|
+
this.converterMinified = new ConverterMinified(config);
|
|
13
|
+
if (!config.cacheDir || !config.distDir) {
|
|
14
|
+
console.log("classnames-minifier: Failed to check the dist folder because cacheDir or distDir is not specified");
|
|
15
|
+
}
|
|
16
|
+
else {
|
|
17
|
+
const manifestDir = path.join(config.cacheDir, "ncm-meta");
|
|
18
|
+
const manifestPath = path.join(manifestDir, "manifest.json");
|
|
19
|
+
const { distDeletionPolicy = "error" } = config;
|
|
20
|
+
let distCleared = false;
|
|
21
|
+
if (config.cacheDir) {
|
|
22
|
+
const errors = validateDist(config, manifestPath);
|
|
23
|
+
if (errors) {
|
|
24
|
+
console.log(`classnames-minifier: "distDeletionPolicy" option was set to "${distDeletionPolicy}"`);
|
|
25
|
+
if (distDeletionPolicy === "auto") {
|
|
26
|
+
removeDist(config.distDir, errors);
|
|
27
|
+
distCleared = true;
|
|
28
|
+
}
|
|
29
|
+
else if (distDeletionPolicy === "error") {
|
|
30
|
+
throw new Error(`classnames-minifier: Please, remove dist dir manually. ${errors}`);
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
console.warn(`classnames-minifier: Please, remove dist dir manually. ${errors}`);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
const { syncFreedNames, freedNamesLimit = 100000 } = config.experimental || {};
|
|
38
|
+
if (!syncFreedNames && this.converterMinified.freeClasses.length > freedNamesLimit && config.distDir) {
|
|
39
|
+
console.log(`classnames-minifier: "distDeletionPolicy" option was set to "${distDeletionPolicy}"`);
|
|
40
|
+
if (distDeletionPolicy === "auto") {
|
|
41
|
+
removeDist(config.distDir, `Freed names exceeds the limit (${freedNamesLimit})`);
|
|
42
|
+
distCleared = true;
|
|
43
|
+
}
|
|
44
|
+
else if (distDeletionPolicy === "error") {
|
|
45
|
+
throw new Error(`Please, remove dist dir manually. Freed names exceeds the limit (${freedNamesLimit})`);
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
console.warn(`Please, remove dist dir manually. Freed names exceeds the limit (${freedNamesLimit})`);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
if (distCleared) {
|
|
52
|
+
this.converterMinified.reset();
|
|
53
|
+
}
|
|
54
|
+
if (!fs.existsSync(manifestDir))
|
|
55
|
+
fs.mkdirSync(manifestDir, { recursive: true });
|
|
56
|
+
fs.writeFileSync(manifestPath, JSON.stringify({ ...config, version: CODE_VERSION }), { encoding: "utf-8" });
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
get getLocalIdent() {
|
|
60
|
+
return this.converterMinified.getLocalIdent.bind(this.converterMinified);
|
|
61
|
+
}
|
|
62
|
+
get preLoader() {
|
|
63
|
+
return {
|
|
64
|
+
loader: path.join(__dirname, "./lib/classnames-minifier-preloader.js"),
|
|
65
|
+
options: {
|
|
66
|
+
classnamesMinifier: this.converterMinified,
|
|
67
|
+
},
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
get postLoader() {
|
|
71
|
+
return {
|
|
72
|
+
loader: path.join(__dirname, "./lib/classnames-minifier-postloader.js"),
|
|
73
|
+
options: {
|
|
74
|
+
classnamesMinifier: this.converterMinified,
|
|
75
|
+
},
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
export default ClassnamesMinifier;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { LoaderContext } from "webpack";
|
|
2
|
+
import type { Config } from "./types/plugin";
|
|
3
|
+
type CacheType = {
|
|
4
|
+
[resourcePath: string]: {
|
|
5
|
+
cachePath: string;
|
|
6
|
+
matchings: {
|
|
7
|
+
[origClass: string]: string;
|
|
8
|
+
};
|
|
9
|
+
type: "new" | "updated" | "old";
|
|
10
|
+
};
|
|
11
|
+
};
|
|
12
|
+
declare class ConverterMinified {
|
|
13
|
+
private cacheDir?;
|
|
14
|
+
private prefix;
|
|
15
|
+
private reservedNames;
|
|
16
|
+
private syncFreedNames;
|
|
17
|
+
private symbols;
|
|
18
|
+
dirtyСache: CacheType;
|
|
19
|
+
freeClasses: string[];
|
|
20
|
+
lastIndex: number;
|
|
21
|
+
private nextLoopEndsWith;
|
|
22
|
+
private currentLoopLength;
|
|
23
|
+
private nameMap;
|
|
24
|
+
constructor({ cacheDir, prefix, reservedNames, experimental }: Config);
|
|
25
|
+
reset: () => void;
|
|
26
|
+
private invalidateCache;
|
|
27
|
+
private generateClassName;
|
|
28
|
+
private getTargetClassName;
|
|
29
|
+
getLocalIdent({ resourcePath }: LoaderContext<any>, _localIdent: string, origName: string): string;
|
|
30
|
+
}
|
|
31
|
+
export default ConverterMinified;
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
import { existsSync, mkdirSync, readFileSync, readdirSync, rmSync } from "fs";
|
|
2
|
+
import { v4 as uuidv4 } from "uuid";
|
|
3
|
+
import path from "path";
|
|
4
|
+
class ConverterMinified {
|
|
5
|
+
cacheDir;
|
|
6
|
+
prefix;
|
|
7
|
+
reservedNames;
|
|
8
|
+
syncFreedNames;
|
|
9
|
+
symbols = [
|
|
10
|
+
"a",
|
|
11
|
+
"b",
|
|
12
|
+
"c",
|
|
13
|
+
"d",
|
|
14
|
+
"e",
|
|
15
|
+
"f",
|
|
16
|
+
"g",
|
|
17
|
+
"h",
|
|
18
|
+
"i",
|
|
19
|
+
"j",
|
|
20
|
+
"k",
|
|
21
|
+
"l",
|
|
22
|
+
"m",
|
|
23
|
+
"n",
|
|
24
|
+
"o",
|
|
25
|
+
"p",
|
|
26
|
+
"q",
|
|
27
|
+
"r",
|
|
28
|
+
"s",
|
|
29
|
+
"t",
|
|
30
|
+
"u",
|
|
31
|
+
"v",
|
|
32
|
+
"w",
|
|
33
|
+
"x",
|
|
34
|
+
"y",
|
|
35
|
+
"z",
|
|
36
|
+
"A",
|
|
37
|
+
"B",
|
|
38
|
+
"C",
|
|
39
|
+
"D",
|
|
40
|
+
"E",
|
|
41
|
+
"F",
|
|
42
|
+
"G",
|
|
43
|
+
"H",
|
|
44
|
+
"I",
|
|
45
|
+
"J",
|
|
46
|
+
"K",
|
|
47
|
+
"L",
|
|
48
|
+
"M",
|
|
49
|
+
"N",
|
|
50
|
+
"O",
|
|
51
|
+
"P",
|
|
52
|
+
"Q",
|
|
53
|
+
"R",
|
|
54
|
+
"S",
|
|
55
|
+
"T",
|
|
56
|
+
"U",
|
|
57
|
+
"V",
|
|
58
|
+
"W",
|
|
59
|
+
"X",
|
|
60
|
+
"Y",
|
|
61
|
+
"Z",
|
|
62
|
+
"0",
|
|
63
|
+
"1",
|
|
64
|
+
"2",
|
|
65
|
+
"3",
|
|
66
|
+
"4",
|
|
67
|
+
"5",
|
|
68
|
+
"6",
|
|
69
|
+
"7",
|
|
70
|
+
"8",
|
|
71
|
+
"9",
|
|
72
|
+
];
|
|
73
|
+
dirtyСache = {};
|
|
74
|
+
freeClasses = [];
|
|
75
|
+
lastIndex = 0;
|
|
76
|
+
nextLoopEndsWith = 26;
|
|
77
|
+
currentLoopLength = 0;
|
|
78
|
+
nameMap = [0];
|
|
79
|
+
constructor({ cacheDir, prefix = "", reservedNames = [], experimental }) {
|
|
80
|
+
this.prefix = prefix;
|
|
81
|
+
this.reservedNames = reservedNames;
|
|
82
|
+
this.syncFreedNames = Boolean(experimental?.syncFreedNames);
|
|
83
|
+
if (cacheDir)
|
|
84
|
+
this.invalidateCache(path.join(cacheDir, "ncm"));
|
|
85
|
+
}
|
|
86
|
+
reset = () => {
|
|
87
|
+
this.dirtyСache = {};
|
|
88
|
+
this.freeClasses = [];
|
|
89
|
+
this.lastIndex = 0;
|
|
90
|
+
this.nextLoopEndsWith = 26;
|
|
91
|
+
this.currentLoopLength = 0;
|
|
92
|
+
this.nameMap = [0];
|
|
93
|
+
if (this.cacheDir) {
|
|
94
|
+
this.invalidateCache(this.cacheDir);
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
invalidateCache = (cacheDir) => {
|
|
98
|
+
this.cacheDir = cacheDir;
|
|
99
|
+
if (!existsSync(cacheDir))
|
|
100
|
+
mkdirSync(cacheDir, { recursive: true });
|
|
101
|
+
const cachedFiles = readdirSync(cacheDir);
|
|
102
|
+
if (cachedFiles.length) {
|
|
103
|
+
console.log("classnames-minifier: Restoring pairs of classes...");
|
|
104
|
+
}
|
|
105
|
+
else {
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
const usedClassNames = [];
|
|
109
|
+
const dirtyСache = {};
|
|
110
|
+
let prevLastIndex = 0;
|
|
111
|
+
cachedFiles.forEach((file) => {
|
|
112
|
+
const filePath = path.join(cacheDir, file);
|
|
113
|
+
const dirtyCacheFile = readFileSync(filePath, { encoding: "utf8" });
|
|
114
|
+
const [resourcePath, lastIndex, ...classnames] = dirtyCacheFile.split(",");
|
|
115
|
+
if (lastIndex && +lastIndex > prevLastIndex)
|
|
116
|
+
prevLastIndex = +lastIndex;
|
|
117
|
+
if (existsSync(resourcePath)) {
|
|
118
|
+
const cachedMatchings = classnames.reduce((acc, cur) => {
|
|
119
|
+
const [origClass, newClass] = cur.split("=");
|
|
120
|
+
acc[origClass] = newClass;
|
|
121
|
+
if (!usedClassNames.includes(newClass)) {
|
|
122
|
+
usedClassNames.push(newClass);
|
|
123
|
+
}
|
|
124
|
+
return acc;
|
|
125
|
+
}, {});
|
|
126
|
+
dirtyСache[resourcePath] = {
|
|
127
|
+
cachePath: filePath,
|
|
128
|
+
matchings: cachedMatchings,
|
|
129
|
+
type: "old",
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
else {
|
|
133
|
+
rmSync(filePath);
|
|
134
|
+
}
|
|
135
|
+
});
|
|
136
|
+
for (let i = 0; i <= prevLastIndex; i++) {
|
|
137
|
+
const newClass = this.generateClassName();
|
|
138
|
+
this.lastIndex += 1;
|
|
139
|
+
const usedClassNameIndex = usedClassNames.indexOf(newClass);
|
|
140
|
+
if (usedClassNameIndex !== -1) {
|
|
141
|
+
usedClassNames.splice(usedClassNameIndex, 1);
|
|
142
|
+
}
|
|
143
|
+
else if (!this.reservedNames.includes(newClass)) {
|
|
144
|
+
this.freeClasses.push(newClass);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
if (cachedFiles.length) {
|
|
148
|
+
console.log("classnames-minifier: Pairs restored");
|
|
149
|
+
}
|
|
150
|
+
this.dirtyСache = dirtyСache;
|
|
151
|
+
};
|
|
152
|
+
generateClassName() {
|
|
153
|
+
const symbolsCount = 62;
|
|
154
|
+
if (this.lastIndex >= this.nextLoopEndsWith) {
|
|
155
|
+
if (this.nextLoopEndsWith === 26)
|
|
156
|
+
this.nextLoopEndsWith = 62 * symbolsCount;
|
|
157
|
+
else
|
|
158
|
+
this.nextLoopEndsWith = this.nextLoopEndsWith * symbolsCount;
|
|
159
|
+
this.nameMap.push(0);
|
|
160
|
+
this.currentLoopLength += 1;
|
|
161
|
+
}
|
|
162
|
+
const currentClassname = this.prefix + this.nameMap.map((e) => this.symbols[e]).join("");
|
|
163
|
+
for (let i = this.currentLoopLength; i >= 0; i--) {
|
|
164
|
+
if (this.nameMap[i] === symbolsCount - 1 || (i === 0 && this.nameMap[i] === 25)) {
|
|
165
|
+
this.nameMap[i] = 0;
|
|
166
|
+
}
|
|
167
|
+
else {
|
|
168
|
+
this.nameMap[i] += 1;
|
|
169
|
+
break;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
return currentClassname;
|
|
173
|
+
}
|
|
174
|
+
getTargetClassName(origName) {
|
|
175
|
+
let targetClassName;
|
|
176
|
+
if (this.freeClasses.length && this.syncFreedNames) {
|
|
177
|
+
targetClassName = this.freeClasses.shift();
|
|
178
|
+
}
|
|
179
|
+
else {
|
|
180
|
+
targetClassName = this.generateClassName();
|
|
181
|
+
}
|
|
182
|
+
if (this.reservedNames.includes(targetClassName)) {
|
|
183
|
+
targetClassName = this.getTargetClassName(origName);
|
|
184
|
+
this.lastIndex += 1;
|
|
185
|
+
}
|
|
186
|
+
return targetClassName;
|
|
187
|
+
}
|
|
188
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
189
|
+
getLocalIdent({ resourcePath }, _localIdent, origName) {
|
|
190
|
+
if (!this.dirtyСache[resourcePath]) {
|
|
191
|
+
this.dirtyСache[resourcePath] = {
|
|
192
|
+
cachePath: this.cacheDir ? path.join(this.cacheDir, uuidv4()) : "",
|
|
193
|
+
matchings: {},
|
|
194
|
+
type: "new",
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
const currentCache = this.dirtyСache[resourcePath];
|
|
198
|
+
if (currentCache.matchings[origName])
|
|
199
|
+
return currentCache.matchings[origName];
|
|
200
|
+
const targetClassName = this.getTargetClassName(origName);
|
|
201
|
+
currentCache.matchings[origName] = targetClassName;
|
|
202
|
+
currentCache.type = "updated";
|
|
203
|
+
this.lastIndex += 1;
|
|
204
|
+
return targetClassName;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
export default ConverterMinified;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
export default function (source, map, meta) {
|
|
3
|
+
const options = this.getOptions();
|
|
4
|
+
const classnamesMinifier = options.classnamesMinifier;
|
|
5
|
+
Object.entries(classnamesMinifier.dirtyСache).forEach(([resourcePath, data]) => {
|
|
6
|
+
if (data.type !== "old") {
|
|
7
|
+
fs.writeFileSync(data.cachePath, `${resourcePath},${classnamesMinifier.lastIndex},${Object.entries(data.matchings)
|
|
8
|
+
.map(([key, value]) => `${key}=${value}`)
|
|
9
|
+
.join(",")}`);
|
|
10
|
+
}
|
|
11
|
+
});
|
|
12
|
+
this.callback(null, source, map, meta);
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export default function (source, map, meta) {
|
|
2
|
+
const options = this.getOptions();
|
|
3
|
+
const classnamesMinifier = options.classnamesMinifier;
|
|
4
|
+
const maybeClassesList = source.match(/\.-?[_a-zA-Z]+[_a-zA-Z0-9-]*/g);
|
|
5
|
+
const cache = classnamesMinifier.dirtyСache[this.resourcePath];
|
|
6
|
+
/**
|
|
7
|
+
* if some class has ceased to be used since the last time the file was loaded, we remove it from the cache
|
|
8
|
+
*/
|
|
9
|
+
if (cache && cache.matchings) {
|
|
10
|
+
cache.matchings = maybeClassesList
|
|
11
|
+
? Object.fromEntries(Object.entries(cache.matchings).filter(([key]) => maybeClassesList?.includes(`.${key}`)))
|
|
12
|
+
: {};
|
|
13
|
+
}
|
|
14
|
+
this.callback(null, source, map, meta);
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
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
|
+
export declare const CODE_VERSION = "parrot";
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { rmSync } from "fs";
|
|
2
|
+
const removeDist = (distDir, message) => {
|
|
3
|
+
console.log(`classnames-minifier: ${message}Cleaning the dist folder...`);
|
|
4
|
+
rmSync(distDir, { recursive: true, force: true });
|
|
5
|
+
console.log("classnames-minifier: Dist folder cleared");
|
|
6
|
+
};
|
|
7
|
+
export default removeDist;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
export type Config = {
|
|
2
|
+
/**
|
|
3
|
+
* The directory where the package cache will be written
|
|
4
|
+
*/
|
|
5
|
+
cacheDir?: string;
|
|
6
|
+
/**
|
|
7
|
+
* Directory where the project is built
|
|
8
|
+
*/
|
|
9
|
+
distDir?: string;
|
|
10
|
+
/**
|
|
11
|
+
* Prefix which will be added to each generated name
|
|
12
|
+
*/
|
|
13
|
+
prefix?: string;
|
|
14
|
+
/**
|
|
15
|
+
* Reserved minified names. Use this option if you are adding short classes manually
|
|
16
|
+
*/
|
|
17
|
+
reservedNames?: string[];
|
|
18
|
+
/**
|
|
19
|
+
* Package policy to resolve potential problems with minified classes
|
|
20
|
+
*
|
|
21
|
+
* This may happen due to the following reasons:
|
|
22
|
+
*
|
|
23
|
+
* 1. Launching the package for the first time. Package need clean next.js cache to put everything in the correct order
|
|
24
|
+
* 2. Changing the package configuration. Package need clean next.js cache to rebuild it with classes according to the new rules
|
|
25
|
+
* 3. Exceeding the limit on freed classes (these are classes that were used before, but are now *probably* no longer used)
|
|
26
|
+
*
|
|
27
|
+
* @param "warning" - a warning message will simply be displayed.
|
|
28
|
+
* With this option, there is a high risk of errors and duplicates of generated classes.
|
|
29
|
+
*
|
|
30
|
+
* @param "error" - an error will be thrown, and as a result the build will stop.
|
|
31
|
+
* If this option occurs, delete the next.js cache manually and restart the build.
|
|
32
|
+
*
|
|
33
|
+
* @param "auto" - the package will automatically delete the next.js cache directory.
|
|
34
|
+
*
|
|
35
|
+
* @default "error"
|
|
36
|
+
*/
|
|
37
|
+
distDeletionPolicy?: "warning" | "error" | "auto";
|
|
38
|
+
/**
|
|
39
|
+
* Additional check of the dist directory for freshness
|
|
40
|
+
*/
|
|
41
|
+
checkDistFreshness?: () => boolean;
|
|
42
|
+
experimental?: {
|
|
43
|
+
/**
|
|
44
|
+
* Automatically synchronize freed classes (for example, if you deleted the original styles)
|
|
45
|
+
* Such classes will be reused in new locations. In this case, there may be situations that the package
|
|
46
|
+
* will mistakenly consider them freed and reuse the class again, thereby creating duplicates.
|
|
47
|
+
*
|
|
48
|
+
* Be careful using this option
|
|
49
|
+
* @default false
|
|
50
|
+
*/
|
|
51
|
+
syncFreedNames?: boolean;
|
|
52
|
+
/**
|
|
53
|
+
* Limit of unused minified classes. Such classes are not reused by default,
|
|
54
|
+
* since the package cannot be sure of this at this time.
|
|
55
|
+
*
|
|
56
|
+
* @default 100000
|
|
57
|
+
*/
|
|
58
|
+
freedNamesLimit?: number;
|
|
59
|
+
};
|
|
60
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
const validKeys = [
|
|
2
|
+
"prefix",
|
|
3
|
+
"reservedNames",
|
|
4
|
+
"cacheDir",
|
|
5
|
+
"distDir",
|
|
6
|
+
"distDeletionPolicy",
|
|
7
|
+
"experimental",
|
|
8
|
+
"checkDistFreshness",
|
|
9
|
+
];
|
|
10
|
+
const validateIsObject = (config) => {
|
|
11
|
+
if (!config)
|
|
12
|
+
return false;
|
|
13
|
+
if (typeof config !== "object" || Array.isArray(config)) {
|
|
14
|
+
console.error(`classnames-minifier: Invalid configuration. Expected object, received ${typeof config}. See https://github.com/alexdln/classnames-minifier#configuration`);
|
|
15
|
+
process.exit();
|
|
16
|
+
}
|
|
17
|
+
const isValidKeys = Object.keys(config).every((key) => validKeys.includes(key));
|
|
18
|
+
if (!isValidKeys) {
|
|
19
|
+
console.error(`classnames-minifier: Invalid configuration. Valid keys are: ${validKeys.join(", ")}. See https://github.com/alexdln/classnames-minifier#configuration`);
|
|
20
|
+
process.exit();
|
|
21
|
+
}
|
|
22
|
+
return true;
|
|
23
|
+
};
|
|
24
|
+
const validateConfig = (config = {}) => {
|
|
25
|
+
if (!validateIsObject(config))
|
|
26
|
+
return {};
|
|
27
|
+
if (config.prefix && !config.prefix.match(/^-?[_a-zA-Z]+[_a-zA-Z0-9-]*$/)) {
|
|
28
|
+
console.error(`classnames-minifier: Invalid prefix. It should match following rule: "^-?[_a-zA-Z]+[_a-zA-Z0-9-]*$". See https://github.com/alexdln/classnames-minifier#configuration`);
|
|
29
|
+
process.exit();
|
|
30
|
+
}
|
|
31
|
+
return config;
|
|
32
|
+
};
|
|
33
|
+
export default validateConfig;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
import { CODE_VERSION } from "./constants/configuration";
|
|
3
|
+
const readManifest = (manifestPath) => {
|
|
4
|
+
try {
|
|
5
|
+
const prevData = fs.readFileSync(manifestPath, { encoding: "utf-8" });
|
|
6
|
+
return JSON.parse(prevData);
|
|
7
|
+
}
|
|
8
|
+
catch {
|
|
9
|
+
return {};
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
const validateDist = (pluginOptions, manifestPath) => {
|
|
13
|
+
const { cacheDir, distDir, prefix, reservedNames, distDeletionPolicy, checkDistFreshness } = pluginOptions;
|
|
14
|
+
if (!cacheDir || !distDir) {
|
|
15
|
+
console.log("classnames-minifier: Failed to check the dist folder because cacheDir or distDir is not specified");
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
let configurationError = "";
|
|
19
|
+
if (fs.existsSync(manifestPath)) {
|
|
20
|
+
const prevData = readManifest(manifestPath);
|
|
21
|
+
const configDiffMessages = [];
|
|
22
|
+
if (prevData.prefix !== prefix) {
|
|
23
|
+
configDiffMessages.push(`Different "prefix": "${prevData.prefix}" -> "${prefix}"`);
|
|
24
|
+
}
|
|
25
|
+
if (prevData.cacheDir !== cacheDir) {
|
|
26
|
+
configDiffMessages.push(`Different "cacheDir": "${prevData.cacheDir}" -> "${cacheDir}"`);
|
|
27
|
+
}
|
|
28
|
+
if (prevData.distDir !== distDir) {
|
|
29
|
+
configDiffMessages.push(`Different "distDir": "${prevData.distDir}" -> "${distDir}"`);
|
|
30
|
+
}
|
|
31
|
+
if (prevData.reservedNames?.length !== reservedNames?.length ||
|
|
32
|
+
prevData.reservedNames?.some((name) => !reservedNames?.includes(name))) {
|
|
33
|
+
configDiffMessages.push(`Different "reservedNames": "${prevData.reservedNames?.join(", ")}" -> "${reservedNames?.join(", ")}"`);
|
|
34
|
+
}
|
|
35
|
+
if (prevData.version !== CODE_VERSION) {
|
|
36
|
+
configDiffMessages.push(`Different package version: "${prevData.version}" -> "${CODE_VERSION}"`);
|
|
37
|
+
}
|
|
38
|
+
if (prevData.distDeletionPolicy && !distDeletionPolicy) {
|
|
39
|
+
configDiffMessages.push(`"distDeletionPolicy" set to "${distDeletionPolicy}"`);
|
|
40
|
+
}
|
|
41
|
+
if (configDiffMessages.length) {
|
|
42
|
+
configurationError = `Changes found in package configuration: \n${configDiffMessages.map((message) => `- ${message};\n`).join("")}`;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
else if (!checkDistFreshness?.()) {
|
|
46
|
+
configurationError = `Can not find the package cache manifest at ${manifestPath}\n`;
|
|
47
|
+
}
|
|
48
|
+
return configurationError;
|
|
49
|
+
};
|
|
50
|
+
export default validateDist;
|