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.
Files changed (39) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +83 -0
  3. package/dist/cjs/ClassnamesMinifier.d.ts +21 -0
  4. package/dist/cjs/ClassnamesMinifier.js +83 -0
  5. package/dist/cjs/lib/ConverterMinified.d.ts +31 -0
  6. package/dist/cjs/lib/ConverterMinified.js +208 -0
  7. package/dist/cjs/lib/classnames-minifier-postloader.d.ts +5 -0
  8. package/dist/cjs/lib/classnames-minifier-postloader.js +20 -0
  9. package/dist/cjs/lib/classnames-minifier-preloader.d.ts +5 -0
  10. package/dist/cjs/lib/classnames-minifier-preloader.js +19 -0
  11. package/dist/cjs/lib/constants/configuration.d.ts +6 -0
  12. package/dist/cjs/lib/constants/configuration.js +9 -0
  13. package/dist/cjs/lib/removeDist.d.ts +2 -0
  14. package/dist/cjs/lib/removeDist.js +9 -0
  15. package/dist/cjs/lib/types/plugin.d.ts +60 -0
  16. package/dist/cjs/lib/types/plugin.js +2 -0
  17. package/dist/cjs/lib/validateConfig.d.ts +3 -0
  18. package/dist/cjs/lib/validateConfig.js +35 -0
  19. package/dist/cjs/lib/validateDist.d.ts +3 -0
  20. package/dist/cjs/lib/validateDist.js +56 -0
  21. package/dist/esm/ClassnamesMinifier.d.ts +21 -0
  22. package/dist/esm/ClassnamesMinifier.js +79 -0
  23. package/dist/esm/lib/ConverterMinified.d.ts +31 -0
  24. package/dist/esm/lib/ConverterMinified.js +207 -0
  25. package/dist/esm/lib/classnames-minifier-postloader.d.ts +5 -0
  26. package/dist/esm/lib/classnames-minifier-postloader.js +14 -0
  27. package/dist/esm/lib/classnames-minifier-preloader.d.ts +5 -0
  28. package/dist/esm/lib/classnames-minifier-preloader.js +16 -0
  29. package/dist/esm/lib/constants/configuration.d.ts +6 -0
  30. package/dist/esm/lib/constants/configuration.js +6 -0
  31. package/dist/esm/lib/removeDist.d.ts +2 -0
  32. package/dist/esm/lib/removeDist.js +7 -0
  33. package/dist/esm/lib/types/plugin.d.ts +60 -0
  34. package/dist/esm/lib/types/plugin.js +1 -0
  35. package/dist/esm/lib/validateConfig.d.ts +3 -0
  36. package/dist/esm/lib/validateConfig.js +33 -0
  37. package/dist/esm/lib/validateDist.d.ts +3 -0
  38. package/dist/esm/lib/validateDist.js +50 -0
  39. package/package.json +62 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2021 Alex Savelyev <dev@alexdln.com>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,83 @@
1
+ # classnames-minifier
2
+
3
+ [![npm version](https://badge.fury.io/js/classnames-minifier.svg)](https://badge.fury.io/js/classnames-minifier)
4
+
5
+ Library for configuring style _(css/scss/sass)_ modules to generate compressed classes (`.header` -> `.a`, `.nav` -> `.b`, ..., `.footer` -> `.aad`, etc.) with support for changes and rebuilding without clearing the built application.
6
+
7
+ ## Reasons
8
+
9
+ _Compressing classes_ can reduce the size of the generated html and css by up to _20%_, which will have a positive effect on page rendering and metrics (primarily [FCP](https://web.dev/first-contentful-paint/))
10
+
11
+ ## Installation
12
+
13
+ **Using npm:**
14
+
15
+ ```bash
16
+ npm i classnames-minifier
17
+ ```
18
+
19
+ **Using yarn:**
20
+
21
+ ```bash
22
+ yarn add classnames-minifier
23
+ ```
24
+
25
+ ## Configuration
26
+
27
+ ### Options
28
+
29
+ - `prefix` - custom prefix that will be added to each updated class;
30
+ - `reservedNames` - array of reserved names that should not be used by this package (must include prefix);
31
+ - `cacheDir` - directory where this library will write the cache. Passing this parameter will enable caching. Use this option only if your framework really needs it;
32
+ - `distDir` - directory where the project is being assembled. Used only when caching is enabled to synchronize caches between this library and the project;
33
+ - `disableDistDeletion` - option that allows you to disable the automatic deletion of the dist folder if necessary (_f.e. differences in package setup in cache and now or first launch_);
34
+
35
+ Configuration example:
36
+
37
+ ```js
38
+ // webpack.config.js
39
+ const classnamesMinifier = new ClassnamesMinifier({
40
+ prefix: "_",
41
+ reservedNames: ["_en", "_de"],
42
+ });
43
+ // ...
44
+ loaders.push({
45
+ loader: require.resolve("css-loader"),
46
+ options: {
47
+ importLoaders: 3,
48
+ modules: {
49
+ mode: "local",
50
+ getLocalIdent: classnamesMinifier.getLocalIdent,
51
+ },
52
+ },
53
+ });
54
+ ```
55
+
56
+ If the framework you are using utilizes component caching between builds, you can configure caching in classnames-minifier as well. As a result, module classes between builds will use the same compressed classes.
57
+
58
+ ```js
59
+ // webpack.config.js
60
+ const classnamesMinifier = new ClassnamesMinifier({
61
+ distDir: path.join(process.cwd(), "build"),
62
+ cacheDir: path.join(process.cwd(), "build/cache"),
63
+ });
64
+ // ...
65
+ loaders.push(
66
+ classnamesMinifier.postLoader,
67
+ {
68
+ loader: require.resolve("css-loader"),
69
+ options: {
70
+ importLoaders: 3,
71
+ modules: {
72
+ mode: "local",
73
+ getLocalIdent: classnamesMinifier.getLocalIdent,
74
+ },
75
+ },
76
+ },
77
+ classnamesMinifier.preLoader
78
+ );
79
+ ```
80
+
81
+ ## License
82
+
83
+ [MIT](https://github.com/alexdln/classnames-minifier/blob/main/LICENSE)
@@ -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,83 @@
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 path_1 = __importDefault(require("path"));
8
+ const configuration_1 = require("./lib/constants/configuration");
9
+ const validateConfig_1 = __importDefault(require("./lib/validateConfig"));
10
+ const ConverterMinified_1 = __importDefault(require("./lib/ConverterMinified"));
11
+ const validateDist_1 = __importDefault(require("./lib/validateDist"));
12
+ const removeDist_1 = __importDefault(require("./lib/removeDist"));
13
+ class ClassnamesMinifier {
14
+ constructor(config) {
15
+ (0, validateConfig_1.default)(config);
16
+ this.converterMinified = new ConverterMinified_1.default(config);
17
+ if (!config.cacheDir || !config.distDir) {
18
+ console.log("classnames-minifier: Failed to check the dist folder because cacheDir or distDir is not specified");
19
+ }
20
+ else {
21
+ const manifestDir = path_1.default.join(config.cacheDir, "ncm-meta");
22
+ const manifestPath = path_1.default.join(manifestDir, "manifest.json");
23
+ const { distDeletionPolicy = "error" } = config;
24
+ let distCleared = false;
25
+ if (config.cacheDir) {
26
+ const errors = (0, validateDist_1.default)(config, manifestPath);
27
+ if (errors) {
28
+ console.log(`classnames-minifier: "distDeletionPolicy" option was set to "${distDeletionPolicy}"`);
29
+ if (distDeletionPolicy === "auto") {
30
+ (0, removeDist_1.default)(config.distDir, errors);
31
+ distCleared = true;
32
+ }
33
+ else if (distDeletionPolicy === "error") {
34
+ throw new Error(`classnames-minifier: Please, remove dist dir manually. ${errors}`);
35
+ }
36
+ else {
37
+ console.warn(`classnames-minifier: Please, remove dist dir manually. ${errors}`);
38
+ }
39
+ }
40
+ }
41
+ const { syncFreedNames, freedNamesLimit = 100000 } = config.experimental || {};
42
+ if (!syncFreedNames && this.converterMinified.freeClasses.length > freedNamesLimit && config.distDir) {
43
+ console.log(`classnames-minifier: "distDeletionPolicy" option was set to "${distDeletionPolicy}"`);
44
+ if (distDeletionPolicy === "auto") {
45
+ (0, removeDist_1.default)(config.distDir, `Freed names exceeds the limit (${freedNamesLimit})`);
46
+ distCleared = true;
47
+ }
48
+ else if (distDeletionPolicy === "error") {
49
+ throw new Error(`Please, remove dist dir manually. Freed names exceeds the limit (${freedNamesLimit})`);
50
+ }
51
+ else {
52
+ console.warn(`Please, remove dist dir manually. Freed names exceeds the limit (${freedNamesLimit})`);
53
+ }
54
+ }
55
+ if (distCleared) {
56
+ this.converterMinified.reset();
57
+ }
58
+ if (!fs_1.default.existsSync(manifestDir))
59
+ fs_1.default.mkdirSync(manifestDir, { recursive: true });
60
+ fs_1.default.writeFileSync(manifestPath, JSON.stringify(Object.assign(Object.assign({}, config), { version: configuration_1.CODE_VERSION })), { encoding: "utf-8" });
61
+ }
62
+ }
63
+ get getLocalIdent() {
64
+ return this.converterMinified.getLocalIdent.bind(this.converterMinified);
65
+ }
66
+ get preLoader() {
67
+ return {
68
+ loader: path_1.default.join(__dirname, "./lib/classnames-minifier-preloader.js"),
69
+ options: {
70
+ classnamesMinifier: this.converterMinified,
71
+ },
72
+ };
73
+ }
74
+ get postLoader() {
75
+ return {
76
+ loader: path_1.default.join(__dirname, "./lib/classnames-minifier-postloader.js"),
77
+ options: {
78
+ classnamesMinifier: this.converterMinified,
79
+ },
80
+ };
81
+ }
82
+ }
83
+ exports.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,208 @@
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 = require("fs");
7
+ const uuid_1 = require("uuid");
8
+ const path_1 = __importDefault(require("path"));
9
+ class ConverterMinified {
10
+ constructor({ cacheDir, prefix = "", reservedNames = [], experimental }) {
11
+ this.symbols = [
12
+ "a",
13
+ "b",
14
+ "c",
15
+ "d",
16
+ "e",
17
+ "f",
18
+ "g",
19
+ "h",
20
+ "i",
21
+ "j",
22
+ "k",
23
+ "l",
24
+ "m",
25
+ "n",
26
+ "o",
27
+ "p",
28
+ "q",
29
+ "r",
30
+ "s",
31
+ "t",
32
+ "u",
33
+ "v",
34
+ "w",
35
+ "x",
36
+ "y",
37
+ "z",
38
+ "A",
39
+ "B",
40
+ "C",
41
+ "D",
42
+ "E",
43
+ "F",
44
+ "G",
45
+ "H",
46
+ "I",
47
+ "J",
48
+ "K",
49
+ "L",
50
+ "M",
51
+ "N",
52
+ "O",
53
+ "P",
54
+ "Q",
55
+ "R",
56
+ "S",
57
+ "T",
58
+ "U",
59
+ "V",
60
+ "W",
61
+ "X",
62
+ "Y",
63
+ "Z",
64
+ "0",
65
+ "1",
66
+ "2",
67
+ "3",
68
+ "4",
69
+ "5",
70
+ "6",
71
+ "7",
72
+ "8",
73
+ "9",
74
+ ];
75
+ this.dirtyСache = {};
76
+ this.freeClasses = [];
77
+ this.lastIndex = 0;
78
+ this.nextLoopEndsWith = 26;
79
+ this.currentLoopLength = 0;
80
+ this.nameMap = [0];
81
+ this.reset = () => {
82
+ this.dirtyСache = {};
83
+ this.freeClasses = [];
84
+ this.lastIndex = 0;
85
+ this.nextLoopEndsWith = 26;
86
+ this.currentLoopLength = 0;
87
+ this.nameMap = [0];
88
+ if (this.cacheDir) {
89
+ this.invalidateCache(this.cacheDir);
90
+ }
91
+ };
92
+ this.invalidateCache = (cacheDir) => {
93
+ this.cacheDir = cacheDir;
94
+ if (!(0, fs_1.existsSync)(cacheDir))
95
+ (0, fs_1.mkdirSync)(cacheDir, { recursive: true });
96
+ const cachedFiles = (0, fs_1.readdirSync)(cacheDir);
97
+ if (cachedFiles.length) {
98
+ console.log("classnames-minifier: Restoring pairs of classes...");
99
+ }
100
+ else {
101
+ return;
102
+ }
103
+ const usedClassNames = [];
104
+ const dirtyСache = {};
105
+ let prevLastIndex = 0;
106
+ cachedFiles.forEach((file) => {
107
+ const filePath = path_1.default.join(cacheDir, file);
108
+ const dirtyCacheFile = (0, fs_1.readFileSync)(filePath, { encoding: "utf8" });
109
+ const [resourcePath, lastIndex, ...classnames] = dirtyCacheFile.split(",");
110
+ if (lastIndex && +lastIndex > prevLastIndex)
111
+ prevLastIndex = +lastIndex;
112
+ if ((0, fs_1.existsSync)(resourcePath)) {
113
+ const cachedMatchings = classnames.reduce((acc, cur) => {
114
+ const [origClass, newClass] = cur.split("=");
115
+ acc[origClass] = newClass;
116
+ if (!usedClassNames.includes(newClass)) {
117
+ usedClassNames.push(newClass);
118
+ }
119
+ return acc;
120
+ }, {});
121
+ dirtyСache[resourcePath] = {
122
+ cachePath: filePath,
123
+ matchings: cachedMatchings,
124
+ type: "old",
125
+ };
126
+ }
127
+ else {
128
+ (0, fs_1.rmSync)(filePath);
129
+ }
130
+ });
131
+ for (let i = 0; i <= prevLastIndex; i++) {
132
+ const newClass = this.generateClassName();
133
+ this.lastIndex += 1;
134
+ const usedClassNameIndex = usedClassNames.indexOf(newClass);
135
+ if (usedClassNameIndex !== -1) {
136
+ usedClassNames.splice(usedClassNameIndex, 1);
137
+ }
138
+ else if (!this.reservedNames.includes(newClass)) {
139
+ this.freeClasses.push(newClass);
140
+ }
141
+ }
142
+ if (cachedFiles.length) {
143
+ console.log("classnames-minifier: Pairs restored");
144
+ }
145
+ this.dirtyСache = dirtyСache;
146
+ };
147
+ this.prefix = prefix;
148
+ this.reservedNames = reservedNames;
149
+ this.syncFreedNames = Boolean(experimental === null || experimental === void 0 ? void 0 : experimental.syncFreedNames);
150
+ if (cacheDir)
151
+ this.invalidateCache(path_1.default.join(cacheDir, "ncm"));
152
+ }
153
+ generateClassName() {
154
+ const symbolsCount = 62;
155
+ if (this.lastIndex >= this.nextLoopEndsWith) {
156
+ if (this.nextLoopEndsWith === 26)
157
+ this.nextLoopEndsWith = 62 * symbolsCount;
158
+ else
159
+ this.nextLoopEndsWith = this.nextLoopEndsWith * symbolsCount;
160
+ this.nameMap.push(0);
161
+ this.currentLoopLength += 1;
162
+ }
163
+ const currentClassname = this.prefix + this.nameMap.map((e) => this.symbols[e]).join("");
164
+ for (let i = this.currentLoopLength; i >= 0; i--) {
165
+ if (this.nameMap[i] === symbolsCount - 1 || (i === 0 && this.nameMap[i] === 25)) {
166
+ this.nameMap[i] = 0;
167
+ }
168
+ else {
169
+ this.nameMap[i] += 1;
170
+ break;
171
+ }
172
+ }
173
+ return currentClassname;
174
+ }
175
+ getTargetClassName(origName) {
176
+ let targetClassName;
177
+ if (this.freeClasses.length && this.syncFreedNames) {
178
+ targetClassName = this.freeClasses.shift();
179
+ }
180
+ else {
181
+ targetClassName = this.generateClassName();
182
+ }
183
+ if (this.reservedNames.includes(targetClassName)) {
184
+ targetClassName = this.getTargetClassName(origName);
185
+ this.lastIndex += 1;
186
+ }
187
+ return targetClassName;
188
+ }
189
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
190
+ getLocalIdent({ resourcePath }, _localIdent, origName) {
191
+ if (!this.dirtyСache[resourcePath]) {
192
+ this.dirtyСache[resourcePath] = {
193
+ cachePath: this.cacheDir ? path_1.default.join(this.cacheDir, (0, uuid_1.v4)()) : "",
194
+ matchings: {},
195
+ type: "new",
196
+ };
197
+ }
198
+ const currentCache = this.dirtyСache[resourcePath];
199
+ if (currentCache.matchings[origName])
200
+ return currentCache.matchings[origName];
201
+ const targetClassName = this.getTargetClassName(origName);
202
+ currentCache.matchings[origName] = targetClassName;
203
+ currentCache.type = "updated";
204
+ this.lastIndex += 1;
205
+ return targetClassName;
206
+ }
207
+ }
208
+ exports.default = ConverterMinified;
@@ -0,0 +1,5 @@
1
+ import type { LoaderContext } from "webpack";
2
+ import type ConverterMinified from "./ConverterMinified";
3
+ export default function (this: LoaderContext<{
4
+ classnamesMinifier: ConverterMinified;
5
+ }>, source: string, map: any, meta: any): void;
@@ -0,0 +1,20 @@
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
+ exports.default = default_1;
7
+ const fs_1 = __importDefault(require("fs"));
8
+ function default_1(source, map, meta) {
9
+ const options = this.getOptions();
10
+ const classnamesMinifier = options.classnamesMinifier;
11
+ Object.entries(classnamesMinifier.dirtyСache).forEach(([resourcePath, data]) => {
12
+ if (data.type !== "old") {
13
+ fs_1.default.writeFileSync(data.cachePath, `${resourcePath},${classnamesMinifier.lastIndex},${Object.entries(data.matchings)
14
+ .map(([key, value]) => `${key}=${value}`)
15
+ .join(",")}`);
16
+ }
17
+ });
18
+ this.callback(null, source, map, meta);
19
+ return;
20
+ }
@@ -0,0 +1,5 @@
1
+ import type { LoaderContext } from "webpack";
2
+ import type ConverterMinified from "./ConverterMinified";
3
+ export default function (this: LoaderContext<{
4
+ classnamesMinifier: ConverterMinified;
5
+ }>, source: string, map: any, meta: any): void;
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.default = default_1;
4
+ function default_1(source, map, meta) {
5
+ const options = this.getOptions();
6
+ const classnamesMinifier = options.classnamesMinifier;
7
+ const maybeClassesList = source.match(/\.-?[_a-zA-Z]+[_a-zA-Z0-9-]*/g);
8
+ const cache = classnamesMinifier.dirtyСache[this.resourcePath];
9
+ /**
10
+ * if some class has ceased to be used since the last time the file was loaded, we remove it from the cache
11
+ */
12
+ if (cache && cache.matchings) {
13
+ cache.matchings = maybeClassesList
14
+ ? Object.fromEntries(Object.entries(cache.matchings).filter(([key]) => maybeClassesList === null || maybeClassesList === void 0 ? void 0 : maybeClassesList.includes(`.${key}`)))
15
+ : {};
16
+ }
17
+ this.callback(null, source, map, meta);
18
+ return;
19
+ }
@@ -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,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
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 = "parrot";
@@ -0,0 +1,2 @@
1
+ declare const removeDist: (distDir: string, message: string) => void;
2
+ export default removeDist;
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const fs_1 = require("fs");
4
+ const removeDist = (distDir, message) => {
5
+ console.log(`classnames-minifier: ${message}Cleaning the dist folder...`);
6
+ (0, fs_1.rmSync)(distDir, { recursive: true, force: true });
7
+ console.log("classnames-minifier: Dist folder cleared");
8
+ };
9
+ exports.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,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,3 @@
1
+ import type { Config } from "./types/plugin";
2
+ declare const validateConfig: (config?: unknown) => Config;
3
+ export default validateConfig;
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const validKeys = [
4
+ "prefix",
5
+ "reservedNames",
6
+ "cacheDir",
7
+ "distDir",
8
+ "distDeletionPolicy",
9
+ "experimental",
10
+ "checkDistFreshness",
11
+ ];
12
+ const validateIsObject = (config) => {
13
+ if (!config)
14
+ return false;
15
+ if (typeof config !== "object" || Array.isArray(config)) {
16
+ console.error(`classnames-minifier: Invalid configuration. Expected object, received ${typeof config}. See https://github.com/alexdln/classnames-minifier#configuration`);
17
+ process.exit();
18
+ }
19
+ const isValidKeys = Object.keys(config).every((key) => validKeys.includes(key));
20
+ if (!isValidKeys) {
21
+ console.error(`classnames-minifier: Invalid configuration. Valid keys are: ${validKeys.join(", ")}. See https://github.com/alexdln/classnames-minifier#configuration`);
22
+ process.exit();
23
+ }
24
+ return true;
25
+ };
26
+ const validateConfig = (config = {}) => {
27
+ if (!validateIsObject(config))
28
+ return {};
29
+ if (config.prefix && !config.prefix.match(/^-?[_a-zA-Z]+[_a-zA-Z0-9-]*$/)) {
30
+ 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`);
31
+ process.exit();
32
+ }
33
+ return config;
34
+ };
35
+ exports.default = validateConfig;
@@ -0,0 +1,3 @@
1
+ import type { Config } from "./types/plugin";
2
+ declare const validateDist: (pluginOptions: Config, manifestPath: string) => string | undefined;
3
+ export default validateDist;