unplugin-tailwindcss-mangle
Advanced tools
Comparing version 0.0.3 to 0.0.4
@@ -115,3 +115,3 @@ 'use strict'; | ||
} | ||
defaultClassGenerator() { | ||
defaultClassGenerate() { | ||
const chars = []; | ||
@@ -174,7 +174,7 @@ let rest = (this.newClassSize - (this.newClassSize % acceptChars.length)) / acceptChars.length; | ||
let newClassName; | ||
if (opts.classGenerator) { | ||
newClassName = opts.classGenerator(original, opts, this.context); | ||
if (opts.customGenerate && typeof opts.customGenerate === 'function') { | ||
newClassName = opts.customGenerate(original, opts, this.context); | ||
} | ||
if (!newClassName) { | ||
newClassName = this.defaultClassGenerator(); | ||
newClassName = this.defaultClassGenerate(); | ||
} | ||
@@ -327,6 +327,23 @@ if (opts.reserveClassName && regExpTest(opts.reserveClassName, newClassName)) { | ||
const traverse = getDefaultExportFromNamespaceIfPresent(_traverse__default["default"]); | ||
function handleValue(str, node, options) { | ||
const set = options.runtimeSet; | ||
const clsGen = options.classGenerator; | ||
const arr = splitCode(str); | ||
let rawStr = str; | ||
for (let i = 0; i < arr.length; i++) { | ||
const v = arr[i]; | ||
if (set.has(v)) { | ||
let ignoreFlag = false; | ||
if (Array.isArray(node.leadingComments)) { | ||
ignoreFlag = node.leadingComments.findIndex((x) => x.value.includes('tw-mangle') && x.value.includes('ignore')) > -1; | ||
} | ||
if (!ignoreFlag) { | ||
rawStr = rawStr.replace(new RegExp('(?<="|\\s)' + escapeStringRegexp(v), 'g'), clsGen.generateClassName(v).name); | ||
} | ||
} | ||
} | ||
return rawStr; | ||
} | ||
function jsHandler(rawSource, options) { | ||
const ast = parser.parse(rawSource); | ||
const set = options.runtimeSet; | ||
const clsGen = options.classGenerator; | ||
const topt = { | ||
@@ -336,19 +353,11 @@ StringLiteral: { | ||
const n = p.node; | ||
const arr = splitCode(n.value); | ||
let rawStr = n.value; | ||
for (let i = 0; i < arr.length; i++) { | ||
const v = arr[i]; | ||
if (set.has(v)) { | ||
let ignoreFlag = false; | ||
if (Array.isArray(n.leadingComments)) { | ||
ignoreFlag = n.leadingComments.findIndex((x) => x.value.includes('tw-mangle') && x.value.includes('ignore')) > -1; | ||
} | ||
if (!ignoreFlag) { | ||
rawStr = rawStr.replace(new RegExp(escapeStringRegexp(v), 'g'), clsGen.generateClassName(v).name); | ||
} | ||
} | ||
} | ||
n.value = rawStr; | ||
n.value = handleValue(n.value, n, options); | ||
} | ||
}, | ||
TemplateElement: { | ||
enter(p) { | ||
const n = p.node; | ||
n.value.raw = handleValue(n.value.raw, n, options); | ||
} | ||
}, | ||
noScope: true | ||
@@ -399,3 +408,3 @@ }; | ||
let classSet; | ||
const classGenerator = new ClassGenerator(); | ||
const classGenerator = new ClassGenerator(options.classGenerator); | ||
function getCachedClassSet() { | ||
@@ -402,0 +411,0 @@ const set = tailwindcssPatch.getClassCacheSet(); |
@@ -1,10 +0,10 @@ | ||
import type { IMangleOptions, IMangleContextClass, IClassGenerator } from './types'; | ||
import type { IClassGeneratorOptions, IClassGeneratorContextItem, IClassGenerator } from './types'; | ||
declare class ClassGenerator implements IClassGenerator { | ||
newClassMap: Record<string, IMangleContextClass>; | ||
newClassMap: Record<string, IClassGeneratorContextItem>; | ||
newClassSize: number; | ||
context: Record<string, any>; | ||
opts: IMangleOptions; | ||
opts: IClassGeneratorOptions; | ||
classPrefix: string; | ||
constructor(opts?: IMangleOptions); | ||
defaultClassGenerator(): string; | ||
constructor(opts?: IClassGeneratorOptions); | ||
defaultClassGenerate(): string; | ||
ignoreClassName(className: string): boolean; | ||
@@ -15,4 +15,4 @@ includeFilePath(filePath: string): boolean; | ||
transformCssClass(className: string): string; | ||
generateClassName(original: string): IMangleContextClass; | ||
generateClassName(original: string): IClassGeneratorContextItem; | ||
} | ||
export default ClassGenerator; |
@@ -1,7 +0,7 @@ | ||
import { IMangleContextClass } from '@/types'; | ||
import { IClassGeneratorContextItem } from '@/types'; | ||
import type { PluginCreator } from 'postcss'; | ||
export type PostcssMangleTailwindcssPlugin = PluginCreator<{ | ||
newClassMap: Record<string, IMangleContextClass>; | ||
newClassMap: Record<string, IClassGeneratorContextItem>; | ||
}>; | ||
declare const postcssMangleTailwindcssPlugin: PostcssMangleTailwindcssPlugin; | ||
export { postcssMangleTailwindcssPlugin }; |
@@ -115,3 +115,3 @@ 'use strict'; | ||
} | ||
defaultClassGenerator() { | ||
defaultClassGenerate() { | ||
const chars = []; | ||
@@ -174,7 +174,7 @@ let rest = (this.newClassSize - (this.newClassSize % acceptChars.length)) / acceptChars.length; | ||
let newClassName; | ||
if (opts.classGenerator) { | ||
newClassName = opts.classGenerator(original, opts, this.context); | ||
if (opts.customGenerate && typeof opts.customGenerate === 'function') { | ||
newClassName = opts.customGenerate(original, opts, this.context); | ||
} | ||
if (!newClassName) { | ||
newClassName = this.defaultClassGenerator(); | ||
newClassName = this.defaultClassGenerate(); | ||
} | ||
@@ -327,6 +327,23 @@ if (opts.reserveClassName && regExpTest(opts.reserveClassName, newClassName)) { | ||
const traverse = getDefaultExportFromNamespaceIfPresent(_traverse__default["default"]); | ||
function handleValue(str, node, options) { | ||
const set = options.runtimeSet; | ||
const clsGen = options.classGenerator; | ||
const arr = splitCode(str); | ||
let rawStr = str; | ||
for (let i = 0; i < arr.length; i++) { | ||
const v = arr[i]; | ||
if (set.has(v)) { | ||
let ignoreFlag = false; | ||
if (Array.isArray(node.leadingComments)) { | ||
ignoreFlag = node.leadingComments.findIndex((x) => x.value.includes('tw-mangle') && x.value.includes('ignore')) > -1; | ||
} | ||
if (!ignoreFlag) { | ||
rawStr = rawStr.replace(new RegExp('(?<="|\\s)' + escapeStringRegexp(v), 'g'), clsGen.generateClassName(v).name); | ||
} | ||
} | ||
} | ||
return rawStr; | ||
} | ||
function jsHandler(rawSource, options) { | ||
const ast = parser.parse(rawSource); | ||
const set = options.runtimeSet; | ||
const clsGen = options.classGenerator; | ||
const topt = { | ||
@@ -336,19 +353,11 @@ StringLiteral: { | ||
const n = p.node; | ||
const arr = splitCode(n.value); | ||
let rawStr = n.value; | ||
for (let i = 0; i < arr.length; i++) { | ||
const v = arr[i]; | ||
if (set.has(v)) { | ||
let ignoreFlag = false; | ||
if (Array.isArray(n.leadingComments)) { | ||
ignoreFlag = n.leadingComments.findIndex((x) => x.value.includes('tw-mangle') && x.value.includes('ignore')) > -1; | ||
} | ||
if (!ignoreFlag) { | ||
rawStr = rawStr.replace(new RegExp(escapeStringRegexp(v), 'g'), clsGen.generateClassName(v).name); | ||
} | ||
} | ||
} | ||
n.value = rawStr; | ||
n.value = handleValue(n.value, n, options); | ||
} | ||
}, | ||
TemplateElement: { | ||
enter(p) { | ||
const n = p.node; | ||
n.value.raw = handleValue(n.value.raw, n, options); | ||
} | ||
}, | ||
noScope: true | ||
@@ -399,3 +408,3 @@ }; | ||
let classSet; | ||
const classGenerator = new ClassGenerator(); | ||
const classGenerator = new ClassGenerator(options.classGenerator); | ||
function getCachedClassSet() { | ||
@@ -402,0 +411,0 @@ const set = tailwindcssPatch.getClassCacheSet(); |
@@ -0,2 +1,4 @@ | ||
import type { StringLiteral, TemplateElement } from '@babel/types'; | ||
import type { IHandlerOptions } from '../types'; | ||
export declare function handleValue(str: string, node: StringLiteral | TemplateElement, options: IHandlerOptions): string; | ||
export declare function jsHandler(rawSource: string, options: IHandlerOptions): import("@babel/generator").GeneratorResult; |
import type ClassGenerator from './classGenerator'; | ||
export interface Options { | ||
} | ||
export interface IMangleContextClass { | ||
export interface IClassGeneratorContextItem { | ||
name: string; | ||
usedBy: any[]; | ||
} | ||
export interface IMangleOptions { | ||
export interface IClassGeneratorOptions { | ||
reserveClassName?: (string | RegExp)[]; | ||
classGenerator?: (original: string, opts: IMangleOptions, context: Record<string, any>) => string | undefined; | ||
customGenerate?: (original: string, opts: IClassGeneratorOptions, context: Record<string, any>) => string | undefined; | ||
log?: boolean; | ||
@@ -18,3 +16,3 @@ exclude?: (string | RegExp)[]; | ||
export interface IClassGenerator { | ||
newClassMap: Record<string, IMangleContextClass>; | ||
newClassMap: Record<string, IClassGeneratorContextItem>; | ||
newClassSize: number; | ||
@@ -28,1 +26,4 @@ context: Record<string, any>; | ||
} | ||
export interface Options { | ||
classGenerator?: IClassGeneratorOptions; | ||
} |
@@ -1,2 +0,2 @@ | ||
import type { IMangleOptions, IClassGenerator } from './types'; | ||
import type { IClassGeneratorOptions, IClassGenerator } from './types'; | ||
export declare function groupBy<T>(arr: T[], cb: (arg: T) => string): Record<string, T[]>; | ||
@@ -10,3 +10,3 @@ export declare function getGroupedEntries<T>(entries: [string, T][], options?: { | ||
export declare function stripEscapeSequence(words: string): string; | ||
export declare const validate: (opts: IMangleOptions, classGenerator: IClassGenerator) => void; | ||
export declare const validate: (opts: IClassGeneratorOptions, classGenerator: IClassGenerator) => void; | ||
export declare function isRegexp(value: unknown): boolean; | ||
@@ -13,0 +13,0 @@ export declare function isMap(value: unknown): boolean; |
{ | ||
"name": "unplugin-tailwindcss-mangle", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "mangle tailwindcss utilities class", | ||
@@ -5,0 +5,0 @@ "main": "./dist/index.js", |
@@ -16,2 +16,4 @@ # unplugin-tailwindcss-mangle | ||
- [webpack](#webpack) | ||
- [Options](#options) | ||
- [classGenerator](#classgenerator) | ||
- [Notice](#notice) | ||
@@ -78,2 +80,4 @@ | ||
> Experiment, not work right now | ||
```js | ||
@@ -88,2 +92,20 @@ // esm | ||
## Options | ||
### classGenerator | ||
custom class generator, if you want to custom class name (default 'tw-*'), use this options | ||
```js | ||
export interface IClassGeneratorOptions { | ||
reserveClassName?: (string | RegExp)[] | ||
customGenerate?: (original: string, opts: IClassGeneratorOptions, context: Record<string, any>) => string | undefined | ||
log?: boolean | ||
exclude?: (string | RegExp)[] | ||
include?: (string | RegExp)[] | ||
ignoreClass?: (string | RegExp)[] | ||
classPrefix?: string | ||
} | ||
``` | ||
## Notice | ||
@@ -90,0 +112,0 @@ |
Sorry, the diff of this file is not supported yet
69519
1739
122