classnames-minifier 0.2.2 → 1.0.0
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.js +22 -10
- package/dist/lib/ConverterMinified.d.ts +1 -1
- package/dist/lib/ConverterMinified.js +3 -2
- package/dist/lib/classnames-minifier-preloader.js +3 -0
- package/dist/lib/constants/configuration.d.ts +6 -1
- package/dist/lib/constants/configuration.js +6 -1
- package/dist/lib/removeDist.d.ts +2 -0
- package/dist/lib/{rmDist.js → removeDist.js} +2 -2
- package/dist/lib/types/plugin.d.ts +52 -3
- package/dist/lib/validateConfig.js +9 -1
- package/dist/lib/validateDist.js +5 -5
- package/package.json +1 -1
- package/dist/lib/rmDist.d.ts +0 -2
|
@@ -9,7 +9,7 @@ const configuration_1 = require("./lib/constants/configuration");
|
|
|
9
9
|
const validateConfig_1 = __importDefault(require("./lib/validateConfig"));
|
|
10
10
|
const ConverterMinified_1 = __importDefault(require("./lib/ConverterMinified"));
|
|
11
11
|
const validateDist_1 = __importDefault(require("./lib/validateDist"));
|
|
12
|
-
const
|
|
12
|
+
const removeDist_1 = __importDefault(require("./lib/removeDist"));
|
|
13
13
|
class ClassnamesMinifier {
|
|
14
14
|
constructor(config) {
|
|
15
15
|
(0, validateConfig_1.default)(config);
|
|
@@ -20,25 +20,37 @@ class ClassnamesMinifier {
|
|
|
20
20
|
else {
|
|
21
21
|
const manifestDir = path_1.default.join(config.cacheDir, "ncm-meta");
|
|
22
22
|
const manifestPath = path_1.default.join(manifestDir, "manifest.json");
|
|
23
|
+
const { distDeletionPolicy = "error" } = config;
|
|
23
24
|
let distCleared = false;
|
|
24
25
|
if (config.cacheDir) {
|
|
25
26
|
const errors = (0, validateDist_1.default)(config, manifestPath);
|
|
26
27
|
if (errors) {
|
|
27
|
-
|
|
28
|
-
|
|
28
|
+
console.log(`classnames-minifier: "distDeletionPolicy" option was set to "${distDeletionPolicy}"`);
|
|
29
|
+
if (distDeletionPolicy === "auto") {
|
|
30
|
+
(0, removeDist_1.default)(config.distDir, errors);
|
|
29
31
|
distCleared = true;
|
|
30
32
|
}
|
|
33
|
+
else if (distDeletionPolicy === "error") {
|
|
34
|
+
throw new Error(`classnames-minifier: Please, remove dist dir manually. ${errors}`);
|
|
35
|
+
}
|
|
31
36
|
else {
|
|
32
|
-
console.
|
|
37
|
+
console.warn(`classnames-minifier: Please, remove dist dir manually. ${errors}`);
|
|
33
38
|
}
|
|
34
39
|
}
|
|
35
40
|
}
|
|
36
|
-
const {
|
|
37
|
-
if (
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
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
|
+
}
|
|
42
54
|
}
|
|
43
55
|
if (distCleared) {
|
|
44
56
|
this.converterMinified.reset();
|
|
@@ -146,7 +146,7 @@ class ConverterMinified {
|
|
|
146
146
|
};
|
|
147
147
|
this.prefix = prefix;
|
|
148
148
|
this.reservedNames = reservedNames;
|
|
149
|
-
this.
|
|
149
|
+
this.syncFreedNames = Boolean(experimental === null || experimental === void 0 ? void 0 : experimental.syncFreedNames);
|
|
150
150
|
if (cacheDir)
|
|
151
151
|
this.invalidateCache(path_1.default.join(cacheDir, "ncm"));
|
|
152
152
|
}
|
|
@@ -174,7 +174,7 @@ class ConverterMinified {
|
|
|
174
174
|
}
|
|
175
175
|
getTargetClassName(origName) {
|
|
176
176
|
let targetClassName;
|
|
177
|
-
if (this.freeClasses.length && this.
|
|
177
|
+
if (this.freeClasses.length && this.syncFreedNames) {
|
|
178
178
|
targetClassName = this.freeClasses.shift();
|
|
179
179
|
}
|
|
180
180
|
else {
|
|
@@ -186,6 +186,7 @@ class ConverterMinified {
|
|
|
186
186
|
}
|
|
187
187
|
return targetClassName;
|
|
188
188
|
}
|
|
189
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
189
190
|
getLocalIdent({ resourcePath }, _localIdent, origName) {
|
|
190
191
|
if (!this.dirtyСache[resourcePath]) {
|
|
191
192
|
this.dirtyСache[resourcePath] = {
|
|
@@ -5,6 +5,9 @@ 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
|
+
*/
|
|
8
11
|
if (cache && cache.matchings) {
|
|
9
12
|
cache.matchings = maybeClassesList
|
|
10
13
|
? Object.fromEntries(Object.entries(cache.matchings).filter(([key]) => maybeClassesList === null || maybeClassesList === void 0 ? void 0 : maybeClassesList.includes(`.${key}`)))
|
|
@@ -1 +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
|
+
export declare const CODE_VERSION = "parrot";
|
|
@@ -1,4 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.CODE_VERSION = void 0;
|
|
4
|
-
|
|
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";
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
const fs_1 = require("fs");
|
|
4
|
-
const
|
|
4
|
+
const removeDist = (distDir, message) => {
|
|
5
5
|
console.log(`classnames-minifier: ${message}Cleaning the dist folder...`);
|
|
6
6
|
(0, fs_1.rmSync)(distDir, { recursive: true, force: true });
|
|
7
7
|
console.log("classnames-minifier: Dist folder cleared");
|
|
8
8
|
};
|
|
9
|
-
exports.default =
|
|
9
|
+
exports.default = removeDist;
|
|
@@ -1,11 +1,60 @@
|
|
|
1
1
|
export type Config = {
|
|
2
|
+
/**
|
|
3
|
+
* The directory where the package cache will be written
|
|
4
|
+
*/
|
|
2
5
|
cacheDir?: string;
|
|
6
|
+
/**
|
|
7
|
+
* Directory where the project is built
|
|
8
|
+
*/
|
|
3
9
|
distDir?: string;
|
|
10
|
+
/**
|
|
11
|
+
* Prefix which will be added to each generated name
|
|
12
|
+
*/
|
|
4
13
|
prefix?: string;
|
|
14
|
+
/**
|
|
15
|
+
* Reserved minified names. Use this option if you are adding short classes manually
|
|
16
|
+
*/
|
|
5
17
|
reservedNames?: string[];
|
|
6
|
-
|
|
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;
|
|
7
42
|
experimental?: {
|
|
8
|
-
|
|
9
|
-
|
|
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;
|
|
10
59
|
};
|
|
11
60
|
};
|
|
@@ -1,6 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const validKeys = [
|
|
3
|
+
const validKeys = [
|
|
4
|
+
"prefix",
|
|
5
|
+
"reservedNames",
|
|
6
|
+
"cacheDir",
|
|
7
|
+
"distDir",
|
|
8
|
+
"distDeletionPolicy",
|
|
9
|
+
"experimental",
|
|
10
|
+
"checkDistFreshness",
|
|
11
|
+
];
|
|
4
12
|
const validateIsObject = (config) => {
|
|
5
13
|
if (!config)
|
|
6
14
|
return false;
|
package/dist/lib/validateDist.js
CHANGED
|
@@ -16,7 +16,7 @@ const readManifest = (manifestPath) => {
|
|
|
16
16
|
};
|
|
17
17
|
const validateDist = (pluginOptions, manifestPath) => {
|
|
18
18
|
var _a, _b, _c;
|
|
19
|
-
const { cacheDir, distDir, prefix, reservedNames,
|
|
19
|
+
const { cacheDir, distDir, prefix, reservedNames, distDeletionPolicy, checkDistFreshness } = pluginOptions;
|
|
20
20
|
if (!cacheDir || !distDir) {
|
|
21
21
|
console.log("classnames-minifier: Failed to check the dist folder because cacheDir or distDir is not specified");
|
|
22
22
|
return;
|
|
@@ -41,14 +41,14 @@ const validateDist = (pluginOptions, manifestPath) => {
|
|
|
41
41
|
if (prevData.version !== configuration_1.CODE_VERSION) {
|
|
42
42
|
configDiffMessages.push(`Different package version: "${prevData.version}" -> "${configuration_1.CODE_VERSION}"`);
|
|
43
43
|
}
|
|
44
|
-
if (prevData.
|
|
45
|
-
configDiffMessages.push(`"
|
|
44
|
+
if (prevData.distDeletionPolicy && !distDeletionPolicy) {
|
|
45
|
+
configDiffMessages.push(`"distDeletionPolicy" set to "${distDeletionPolicy}"`);
|
|
46
46
|
}
|
|
47
47
|
if (configDiffMessages.length) {
|
|
48
|
-
configurationError = `Changes found in package configuration: \n${configDiffMessages.map((message) => `- ${message};\n`)}`;
|
|
48
|
+
configurationError = `Changes found in package configuration: \n${configDiffMessages.map((message) => `- ${message};\n`).join("")}`;
|
|
49
49
|
}
|
|
50
50
|
}
|
|
51
|
-
else {
|
|
51
|
+
else if (!(checkDistFreshness === null || checkDistFreshness === void 0 ? void 0 : checkDistFreshness())) {
|
|
52
52
|
configurationError = `Can not find the package cache manifest at ${manifestPath}\n`;
|
|
53
53
|
}
|
|
54
54
|
return configurationError;
|
package/package.json
CHANGED
package/dist/lib/rmDist.d.ts
DELETED