Skip to content

Commit

Permalink
feat: add type definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
cossssmin committed Aug 29, 2024
1 parent db28c1f commit 1957026
Show file tree
Hide file tree
Showing 8 changed files with 216 additions and 12 deletions.
1 change: 1 addition & 0 deletions build.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ export default defineBuildConfig({
rootDir: './lib',
outDir: '../dist',
entries: ['index.js'],
declaration: true,
})
12 changes: 6 additions & 6 deletions dist/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -1158,14 +1158,14 @@ const nameGenerators = {
};

class MinifyClassnames {
constructor({ genNameClass, genNameId, filter, customAttributes = [], removeUnfound = true } = {}) {
constructor({ genNameClass, genNameId, filter, customAttributes = [], removeUnused = true } = {}) {
this.filter = filter || /^.js-/;
this.genNameClass = this.getNameGenerator(genNameClass, 7);
this.genNameId = this.getNameGenerator(genNameId, 5);
this.classMap = {};
this.idMap = {};
this.customAttributes = customAttributes;
this.removeUnfound = removeUnfound;
this.removeUnused = removeUnused;
}
getNameGenerator(value, seed) {
if (value === false) {
Expand Down Expand Up @@ -1210,7 +1210,7 @@ class MinifyClassnames {
if (this.isClassFiltered(value)) {
return value;
}
return this.classMap[value] || (this.removeUnfound ? "" : value);
return this.classMap[value] || (this.removeUnused ? "" : value);
}).filter(Boolean).join(" ").trim();
}
}
Expand All @@ -1220,7 +1220,7 @@ class MinifyClassnames {
if (this.isClassFiltered(value)) {
return value;
}
return this.classMap[value] || (this.removeUnfound ? "" : value);
return this.classMap[value] || (this.removeUnused ? "" : value);
}).filter(Boolean).join(" ").trim();
}
if (node.attrs?.id) {
Expand All @@ -1229,10 +1229,10 @@ class MinifyClassnames {
if (this.isIdFiltered(value)) {
return value;
}
if (!this.idMap[value] && this.removeUnfound) {
if (!this.idMap[value] && this.removeUnused) {
this.idMap[value] = this.genNameId.next().value;
}
return this.idMap[value] || (this.removeUnfound ? "" : value);
return this.idMap[value] || (this.removeUnused ? "" : value);
}).join(" ");
}
if (node.attrs?.for) {
Expand Down
51 changes: 51 additions & 0 deletions dist/index.d.cts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
type MinifyOptions = {
/**
* Regular expression used to exclude names from processing.
*
* @default /^.js-/
*/
filter?: RegExp;

/**
* The name generator to use for classes and IDs.
*
* Possible values:
* - `'genName'` - generates the smallest possible names
* - `'genNameEmoji'` - generates small emoji based names
* - `'genNameEmojiString'` - generates random emoji with 3 emojis in each
* - `false` - preserves names, use this to ignore ids or classes
*
* @default 'genName'
*/
genNameClass?: string | boolean;

/**
* The name generator to use for classes and IDs.
*
* Possible values:
* - `'genName'` - generates the smallest possible names
* - `'genNameEmoji'` - generates small emoji based names
* - `'genNameEmojiString'` - generates random emoji with 3 emojis in each
* - `false` - preserves names, use this to ignore ids or classes
*
* @default 'genName'
*/
genNameId?: string | boolean;

/**
* Array of strings containing custom attribute names that will have their values minified.
*
* @default []
*/
customAttributes?: string[];

/**
* Whether to remove classes, attributes and other identifiers from the HTML
* that are not defined in the CSS.
*
* @default true
*/
removeUnused?: boolean;
};

export type { MinifyOptions };
51 changes: 51 additions & 0 deletions dist/index.d.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
type MinifyOptions = {
/**
* Regular expression used to exclude names from processing.
*
* @default /^.js-/
*/
filter?: RegExp;

/**
* The name generator to use for classes and IDs.
*
* Possible values:
* - `'genName'` - generates the smallest possible names
* - `'genNameEmoji'` - generates small emoji based names
* - `'genNameEmojiString'` - generates random emoji with 3 emojis in each
* - `false` - preserves names, use this to ignore ids or classes
*
* @default 'genName'
*/
genNameClass?: string | boolean;

/**
* The name generator to use for classes and IDs.
*
* Possible values:
* - `'genName'` - generates the smallest possible names
* - `'genNameEmoji'` - generates small emoji based names
* - `'genNameEmojiString'` - generates random emoji with 3 emojis in each
* - `false` - preserves names, use this to ignore ids or classes
*
* @default 'genName'
*/
genNameId?: string | boolean;

/**
* Array of strings containing custom attribute names that will have their values minified.
*
* @default []
*/
customAttributes?: string[];

/**
* Whether to remove classes, attributes and other identifiers from the HTML
* that are not defined in the CSS.
*
* @default true
*/
removeUnused?: boolean;
};

export type { MinifyOptions };
51 changes: 51 additions & 0 deletions dist/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
type MinifyOptions = {
/**
* Regular expression used to exclude names from processing.
*
* @default /^.js-/
*/
filter?: RegExp;

/**
* The name generator to use for classes and IDs.
*
* Possible values:
* - `'genName'` - generates the smallest possible names
* - `'genNameEmoji'` - generates small emoji based names
* - `'genNameEmojiString'` - generates random emoji with 3 emojis in each
* - `false` - preserves names, use this to ignore ids or classes
*
* @default 'genName'
*/
genNameClass?: string | boolean;

/**
* The name generator to use for classes and IDs.
*
* Possible values:
* - `'genName'` - generates the smallest possible names
* - `'genNameEmoji'` - generates small emoji based names
* - `'genNameEmojiString'` - generates random emoji with 3 emojis in each
* - `false` - preserves names, use this to ignore ids or classes
*
* @default 'genName'
*/
genNameId?: string | boolean;

/**
* Array of strings containing custom attribute names that will have their values minified.
*
* @default []
*/
customAttributes?: string[];

/**
* Whether to remove classes, attributes and other identifiers from the HTML
* that are not defined in the CSS.
*
* @default true
*/
removeUnused?: boolean;
};

export type { MinifyOptions };
12 changes: 6 additions & 6 deletions dist/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1151,14 +1151,14 @@ const nameGenerators = {
};

class MinifyClassnames {
constructor({ genNameClass, genNameId, filter, customAttributes = [], removeUnfound = true } = {}) {
constructor({ genNameClass, genNameId, filter, customAttributes = [], removeUnused = true } = {}) {
this.filter = filter || /^.js-/;
this.genNameClass = this.getNameGenerator(genNameClass, 7);
this.genNameId = this.getNameGenerator(genNameId, 5);
this.classMap = {};
this.idMap = {};
this.customAttributes = customAttributes;
this.removeUnfound = removeUnfound;
this.removeUnused = removeUnused;
}
getNameGenerator(value, seed) {
if (value === false) {
Expand Down Expand Up @@ -1203,7 +1203,7 @@ class MinifyClassnames {
if (this.isClassFiltered(value)) {
return value;
}
return this.classMap[value] || (this.removeUnfound ? "" : value);
return this.classMap[value] || (this.removeUnused ? "" : value);
}).filter(Boolean).join(" ").trim();
}
}
Expand All @@ -1213,7 +1213,7 @@ class MinifyClassnames {
if (this.isClassFiltered(value)) {
return value;
}
return this.classMap[value] || (this.removeUnfound ? "" : value);
return this.classMap[value] || (this.removeUnused ? "" : value);
}).filter(Boolean).join(" ").trim();
}
if (node.attrs?.id) {
Expand All @@ -1222,10 +1222,10 @@ class MinifyClassnames {
if (this.isIdFiltered(value)) {
return value;
}
if (!this.idMap[value] && this.removeUnfound) {
if (!this.idMap[value] && this.removeUnused) {
this.idMap[value] = this.genNameId.next().value;
}
return this.idMap[value] || (this.removeUnfound ? "" : value);
return this.idMap[value] || (this.removeUnused ? "" : value);
}).join(" ");
}
if (node.attrs?.for) {
Expand Down
49 changes: 49 additions & 0 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
export type MinifyOptions = {
/**
* Regular expression used to exclude names from processing.
*
* @default /^.js-/
*/
filter?: RegExp;

/**
* The name generator to use for classes and IDs.
*
* Possible values:
* - `'genName'` - generates the smallest possible names
* - `'genNameEmoji'` - generates small emoji based names
* - `'genNameEmojiString'` - generates random emoji with 3 emojis in each
* - `false` - preserves names, use this to ignore ids or classes
*
* @default 'genName'
*/
genNameClass?: string | boolean;

/**
* The name generator to use for classes and IDs.
*
* Possible values:
* - `'genName'` - generates the smallest possible names
* - `'genNameEmoji'` - generates small emoji based names
* - `'genNameEmojiString'` - generates random emoji with 3 emojis in each
* - `false` - preserves names, use this to ignore ids or classes
*
* @default 'genName'
*/
genNameId?: string | boolean;

/**
* Array of strings containing custom attribute names that will have their values minified.
*
* @default []
*/
customAttributes?: string[];

/**
* Whether to remove classes, attributes and other identifiers from the HTML
* that are not defined in the CSS.
*
* @default true
*/
removeUnused?: boolean;
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"require": "./dist/index.cjs"
}
},
"types": "./dist/index.d.ts",
"main": "dist/index.cjs",
"module": "./dist/index.mjs",
"files": [
Expand Down

0 comments on commit 1957026

Please sign in to comment.