classnames-minifier 0.1.5 → 0.2.0-canary.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.
@@ -3,17 +3,37 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
+ const fs_1 = __importDefault(require("fs"));
6
7
  const path_1 = __importDefault(require("path"));
8
+ const configuration_1 = require("./lib/constants/configuration");
7
9
  const validateConfig_1 = __importDefault(require("./lib/validateConfig"));
8
10
  const ConverterMinified_1 = __importDefault(require("./lib/ConverterMinified"));
9
11
  const validateDist_1 = __importDefault(require("./lib/validateDist"));
12
+ const rmDist_1 = __importDefault(require("./lib/rmDist"));
10
13
  class ClassnamesMinifier {
11
14
  constructor(config) {
12
15
  (0, validateConfig_1.default)(config);
13
- if (config.cacheDir) {
14
- (0, validateDist_1.default)(config);
15
- }
16
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
+ if (config.cacheDir) {
24
+ (0, validateDist_1.default)(config, manifestPath);
25
+ }
26
+ const { freedNamesPolicy = "transmit", blockLimit = 100000 } = config.experimental || {};
27
+ if (freedNamesPolicy === "block" &&
28
+ this.converterMinified.freeClasses.length > blockLimit &&
29
+ config.distDir) {
30
+ (0, rmDist_1.default)(config.distDir, `Freed names exceeds the limit (${blockLimit})`);
31
+ this.converterMinified.clean();
32
+ }
33
+ if (!fs_1.default.existsSync(manifestDir))
34
+ fs_1.default.mkdirSync(manifestDir, { recursive: true });
35
+ fs_1.default.writeFileSync(manifestPath, JSON.stringify(Object.assign(Object.assign({}, config), { version: configuration_1.CODE_VERSION })), { encoding: "utf-8" });
36
+ }
17
37
  }
18
38
  get getLocalIdent() {
19
39
  return this.converterMinified.getLocalIdent.bind(this.converterMinified);
@@ -13,14 +13,16 @@ declare class ConverterMinified {
13
13
  private cacheDir?;
14
14
  private prefix;
15
15
  private reservedNames;
16
- dirtyСache: CacheType;
16
+ private freedNamesPolicy;
17
17
  private symbols;
18
- private freeClasses;
18
+ dirtyСache: CacheType;
19
+ freeClasses: string[];
19
20
  lastIndex: number;
20
21
  private nextLoopEndsWith;
21
22
  private currentLoopLength;
22
23
  private nameMap;
23
- constructor({ cacheDir, prefix, reservedNames }: Config);
24
+ constructor({ cacheDir, prefix, reservedNames, experimental }: Config);
25
+ clean: () => void;
24
26
  private recycleCache;
25
27
  private generateClassName;
26
28
  private getTargetClassName;
@@ -7,8 +7,7 @@ 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 = "", reservedNames = [] }) {
11
- this.dirtyСache = {};
10
+ constructor({ cacheDir, prefix = "", reservedNames = [], experimental }) {
12
11
  this.symbols = [
13
12
  "a",
14
13
  "b",
@@ -73,11 +72,20 @@ class ConverterMinified {
73
72
  "8",
74
73
  "9",
75
74
  ];
75
+ this.dirtyСache = {};
76
76
  this.freeClasses = [];
77
77
  this.lastIndex = 0;
78
78
  this.nextLoopEndsWith = 26;
79
79
  this.currentLoopLength = 0;
80
80
  this.nameMap = [0];
81
+ this.clean = () => {
82
+ this.dirtyСache = {};
83
+ this.freeClasses = [];
84
+ this.lastIndex = 0;
85
+ this.nextLoopEndsWith = 26;
86
+ this.currentLoopLength = 0;
87
+ this.nameMap = [0];
88
+ };
81
89
  this.recycleCache = (cacheDir) => {
82
90
  this.cacheDir = cacheDir;
83
91
  if (!(0, fs_1.existsSync)(cacheDir))
@@ -132,6 +140,7 @@ class ConverterMinified {
132
140
  };
133
141
  this.prefix = prefix;
134
142
  this.reservedNames = reservedNames;
143
+ this.freedNamesPolicy = (experimental === null || experimental === void 0 ? void 0 : experimental.freedNamesPolicy) || "transmit";
135
144
  if (cacheDir)
136
145
  this.recycleCache(path_1.default.join(cacheDir, "ncm"));
137
146
  }
@@ -159,7 +168,7 @@ class ConverterMinified {
159
168
  }
160
169
  getTargetClassName(origName) {
161
170
  let targetClassName;
162
- if (this.freeClasses.length) {
171
+ if (this.freeClasses.length && this.freedNamesPolicy === "transmit") {
163
172
  targetClassName = this.freeClasses.shift();
164
173
  }
165
174
  else {
@@ -0,0 +1,2 @@
1
+ declare const rmDist: (distDir: string, message: string) => void;
2
+ export default rmDist;
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const fs_1 = require("fs");
4
+ const rmDist = (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 = rmDist;
@@ -4,4 +4,8 @@ export type Config = {
4
4
  prefix?: string;
5
5
  reservedNames?: string[];
6
6
  disableDistDeletion?: boolean;
7
+ experimental?: {
8
+ freedNamesPolicy?: "transmit" | "block";
9
+ blockLimit?: number;
10
+ };
7
11
  };
@@ -1,3 +1,3 @@
1
1
  import type { Config } from "./types/plugin";
2
- declare const validateDist: (pluginOptions: Config) => void;
2
+ declare const validateDist: (pluginOptions: Config, manifestPath: string) => void;
3
3
  export default validateDist;
@@ -3,9 +3,9 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- const path_1 = __importDefault(require("path"));
7
6
  const fs_1 = __importDefault(require("fs"));
8
7
  const configuration_1 = require("./constants/configuration");
8
+ const rmDist_1 = __importDefault(require("./rmDist"));
9
9
  const readManifest = (manifestPath) => {
10
10
  try {
11
11
  const prevData = fs_1.default.readFileSync(manifestPath, { encoding: "utf-8" });
@@ -15,15 +15,13 @@ const readManifest = (manifestPath) => {
15
15
  return {};
16
16
  }
17
17
  };
18
- const validateDist = (pluginOptions) => {
18
+ const validateDist = (pluginOptions, manifestPath) => {
19
19
  var _a, _b, _c;
20
20
  const { cacheDir, distDir, prefix, reservedNames, disableDistDeletion } = pluginOptions;
21
21
  if (!cacheDir || !distDir) {
22
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, "ncm-meta");
26
- const manifestPath = path_1.default.join(manifestDir, "manifest.json");
27
25
  let configurationError = "";
28
26
  if (fs_1.default.existsSync(manifestPath)) {
29
27
  const prevData = readManifest(manifestPath);
@@ -56,16 +54,11 @@ const validateDist = (pluginOptions) => {
56
54
  }
57
55
  if (configurationError) {
58
56
  if (!disableDistDeletion) {
59
- console.log(`classnames-minifier: ${configurationError}Cleaning the dist folder...`);
60
- fs_1.default.rmSync(distDir, { recursive: true, force: true });
61
- console.log("classnames-minifier: Dist folder cleared");
57
+ (0, rmDist_1.default)(distDir, configurationError);
62
58
  }
63
59
  else {
64
60
  console.log(`classnames-minifier: ${configurationError}"disableDistDeletion" option was set to true`);
65
61
  }
66
62
  }
67
- if (!fs_1.default.existsSync(manifestDir))
68
- fs_1.default.mkdirSync(manifestDir, { recursive: true });
69
- fs_1.default.writeFileSync(manifestPath, JSON.stringify(Object.assign(Object.assign({}, pluginOptions), { version: configuration_1.CODE_VERSION })), { encoding: "utf-8" });
70
63
  };
71
64
  exports.default = validateDist;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "classnames-minifier",
3
- "version": "0.1.5",
3
+ "version": "0.2.0-canary.0",
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",