unplugin-tailwindcss-mangle 0.1.3 → 0.1.4

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/index.mjs CHANGED
@@ -1,225 +1,107 @@
1
1
  import { createUnplugin } from 'unplugin';
2
- import { a as acceptChars, r as regExpTest, s as stripEscapeSequence, c as createGlobMatcher, b as cacheDump, i as isMangleClass, p as pluginName, g as getGroupedEntries, j as jsHandler } from './index-92f879d3.mjs';
3
- import { html, defaultTreeAdapter, parse, serialize } from 'parse5';
4
- import { c as cssHandler } from './index-b715a07f.mjs';
5
- import path from 'path';
2
+ import micromatch from 'micromatch';
6
3
  import fs from 'fs';
4
+ import path from 'path';
5
+ import { ClassGenerator, htmlHandler, jsHandler, cssHandler } from 'tailwindcss-mangle-core';
7
6
  import { TailwindcssPatcher } from 'tailwindcss-patch';
8
- import '@babel/types';
9
- import '@babel/core';
10
- import 'micromatch';
11
- import 'postcss';
12
- import 'postcss-selector-parser';
13
-
14
- ({
15
- HTML: html.NS.HTML,
16
- XML: html.NS.XML,
17
- MATHML: html.NS.MATHML,
18
- SVG: html.NS.SVG,
19
- XLINK: html.NS.XLINK,
20
- XMLNS: html.NS.XMLNS
21
- });
22
-
23
- /**
24
- * Determines if a given node is a document or not
25
- * @param {Node} node Node to test
26
- * @return {boolean}
27
- */
28
- function isDocument(node) {
29
- return node.nodeName === '#document';
30
- }
31
- /**
32
- * Determines if a given node is a document fragment or not
33
- * @param {Node} node Node to test
34
- * @return {boolean}
35
- */
36
- function isDocumentFragment(node) {
37
- return node.nodeName === '#document-fragment';
38
- }
39
- /**
40
- * Determines if a given node is a template node or not
41
- * @param {Node} node Node to test
42
- * @return {boolean}
43
- */
44
- function isTemplateNode(node) {
45
- return node.nodeName === 'template';
46
- }
47
- const isElementNode = defaultTreeAdapter.isElementNode;
48
- const isCommentNode = defaultTreeAdapter.isCommentNode;
49
- const isDocumentTypeNode = defaultTreeAdapter.isDocumentTypeNode;
50
- const isTextNode = defaultTreeAdapter.isTextNode;
51
- /**
52
- * Determines if a given node is a parent or not
53
- * @param {Node} node Node to test
54
- * @return {boolean}
55
- */
56
- function isParentNode(node) {
57
- return (isDocument(node) ||
58
- isDocumentFragment(node) ||
59
- isElementNode(node) ||
60
- isTemplateNode(node));
61
- }
62
7
 
63
- defaultTreeAdapter.appendChild;
8
+ const pluginName = 'unplugin-tailwindcss-mangle';
64
9
 
65
- /**
66
- * Traverses the tree of a given node
67
- * @param {Node} node Node to traverse
68
- * @param {Visitor} visitor Visitor to apply
69
- * @param {ParentNode=} parent Parent node of the current node
70
- * @return {void}
71
- */
72
- function traverse(node, visitor, parent) {
73
- const shouldVisitChildren = typeof visitor['pre:node'] !== 'function' ||
74
- visitor['pre:node'](node, parent) !== false;
75
- if (shouldVisitChildren && isParentNode(node)) {
76
- for (const child of node.childNodes) {
77
- traverse(child, visitor, node);
78
- }
79
- }
80
- if (typeof visitor.node === 'function') {
81
- visitor.node(node, parent);
82
- }
83
- if (typeof visitor.document === 'function' && isDocument(node)) {
84
- visitor.document(node);
10
+ const { isMatch } = micromatch;
11
+ const isMangleClass = (className) => {
12
+ return /[-:]/.test(className);
13
+ };
14
+ function groupBy(arr, cb) {
15
+ if (!Array.isArray(arr)) {
16
+ throw new Error('expected an array for first argument');
85
17
  }
86
- if (typeof visitor.documentFragment === 'function' &&
87
- isDocumentFragment(node)) {
88
- visitor.documentFragment(node, parent);
18
+ if (typeof cb !== 'function') {
19
+ throw new Error('expected a function for second argument');
89
20
  }
90
- if (typeof visitor.element === 'function' && isElementNode(node)) {
91
- visitor.element(node, parent);
92
- }
93
- if (typeof visitor.template === 'function' && isTemplateNode(node)) {
94
- visitor.template(node, parent);
95
- }
96
- if (typeof visitor.comment === 'function' && isCommentNode(node)) {
97
- visitor.comment(node, parent);
98
- }
99
- if (typeof visitor.text === 'function' && isTextNode(node)) {
100
- visitor.text(node, parent);
101
- }
102
- if (typeof visitor.documentType === 'function' && isDocumentTypeNode(node)) {
103
- visitor.documentType(node, parent);
104
- }
105
- }
106
-
107
- function htmlHandler(rawSource, options) {
108
- const { runtimeSet, classGenerator } = options;
109
- const fragment = parse(rawSource);
110
- traverse(fragment, {
111
- element(node, parent) {
112
- const attr = node.attrs.find((x) => x.name === 'class');
113
- if (attr) {
114
- const arr = attr.value.split(/\s/).filter((x) => x);
115
- attr.value = arr
116
- .map((x) => {
117
- if (runtimeSet.has(x)) {
118
- return classGenerator.generateClassName(x).name;
119
- }
120
- return x;
121
- })
122
- .join(' ');
123
- }
21
+ const result = {};
22
+ for (let i = 0; i < arr.length; i++) {
23
+ const item = arr[i];
24
+ const bucketCategory = cb(item);
25
+ const bucket = result[bucketCategory];
26
+ if (!Array.isArray(bucket)) {
27
+ result[bucketCategory] = [item];
124
28
  }
125
- });
126
- return serialize(fragment);
127
- }
128
-
129
- class ClassGenerator {
130
- newClassMap;
131
- newClassSize;
132
- context;
133
- opts;
134
- classPrefix;
135
- constructor(opts = {}) {
136
- this.newClassMap = {};
137
- this.newClassSize = 0;
138
- this.context = {};
139
- this.opts = opts;
140
- this.classPrefix = opts.classPrefix ?? 'tw-';
141
- }
142
- defaultClassGenerate() {
143
- const chars = [];
144
- let rest = (this.newClassSize - (this.newClassSize % acceptChars.length)) / acceptChars.length;
145
- if (rest > 0) {
146
- while (true) {
147
- rest -= 1;
148
- const m = rest % acceptChars.length;
149
- const c = acceptChars[m];
150
- chars.push(c);
151
- rest -= m;
152
- if (rest === 0) {
153
- break;
154
- }
155
- rest /= acceptChars.length;
156
- }
29
+ else {
30
+ result[bucketCategory].push(item);
157
31
  }
158
- const prefixIndex = this.newClassSize % acceptChars.length;
159
- const newClassName = `${this.classPrefix}${acceptChars[prefixIndex]}${chars.join('')}`;
160
- return newClassName;
161
32
  }
162
- ignoreClassName(className) {
163
- return regExpTest(this.opts.ignoreClass, className);
33
+ return result;
34
+ }
35
+ function getGroupedEntries(entries, options = {
36
+ cssMatcher(file) {
37
+ return /\.css$/.test(file);
38
+ },
39
+ htmlMatcher(file) {
40
+ return /\.html?$/.test(file);
41
+ },
42
+ jsMatcher(file) {
43
+ return /\.[cm]?js$/.test(file);
164
44
  }
165
- includeFilePath(filePath) {
166
- const { include } = this.opts;
167
- if (Array.isArray(include)) {
168
- return regExpTest(include, filePath);
45
+ }) {
46
+ const { cssMatcher, htmlMatcher, jsMatcher } = options;
47
+ const groupedEntries = groupBy(entries, ([file]) => {
48
+ if (cssMatcher(file)) {
49
+ return 'css';
169
50
  }
170
- else {
171
- return true;
51
+ else if (htmlMatcher(file)) {
52
+ return 'html';
172
53
  }
173
- }
174
- excludeFilePath(filePath) {
175
- const { exclude } = this.opts;
176
- if (Array.isArray(exclude)) {
177
- return regExpTest(exclude, filePath);
54
+ else if (jsMatcher(file)) {
55
+ return 'js';
178
56
  }
179
57
  else {
180
- return false;
58
+ return 'other';
181
59
  }
60
+ });
61
+ if (!groupedEntries.css) {
62
+ groupedEntries.css = [];
182
63
  }
183
- isFileIncluded(filePath) {
184
- return this.includeFilePath(filePath) && !this.excludeFilePath(filePath);
64
+ if (!groupedEntries.html) {
65
+ groupedEntries.html = [];
185
66
  }
186
- transformCssClass(className) {
187
- const key = stripEscapeSequence(className);
188
- const cn = this.newClassMap[key];
189
- if (cn)
190
- return cn.name;
191
- return className;
67
+ if (!groupedEntries.js) {
68
+ groupedEntries.js = [];
192
69
  }
193
- generateClassName(original) {
194
- const opts = this.opts;
195
- original = stripEscapeSequence(original);
196
- const cn = this.newClassMap[original];
197
- if (cn)
198
- return cn;
199
- let newClassName;
200
- if (opts.customGenerate && typeof opts.customGenerate === 'function') {
201
- newClassName = opts.customGenerate(original, opts, this.context);
202
- }
203
- if (!newClassName) {
204
- newClassName = this.defaultClassGenerate();
205
- }
206
- if (opts.reserveClassName && regExpTest(opts.reserveClassName, newClassName)) {
207
- if (opts.log) {
208
- console.log(`The class name has been reserved. ${newClassName}`);
209
- }
210
- this.newClassSize++;
211
- return this.generateClassName(original);
212
- }
213
- if (opts.log) {
214
- console.log(`Minify class name from ${original} to ${newClassName}`);
215
- }
216
- const newClass = {
217
- name: newClassName,
218
- usedBy: []
70
+ if (!groupedEntries.other) {
71
+ groupedEntries.other = [];
72
+ }
73
+ return groupedEntries;
74
+ }
75
+ function createGlobMatcher(pattern, fallbackValue = false) {
76
+ if (typeof pattern === 'undefined') {
77
+ return function (file) {
78
+ return fallbackValue;
219
79
  };
220
- this.newClassMap[original] = newClass;
221
- this.newClassSize++;
222
- return newClass;
80
+ }
81
+ return function (file) {
82
+ return isMatch(file, pattern);
83
+ };
84
+ }
85
+ function getCacheDir(basedir = process.cwd()) {
86
+ return path.resolve(basedir, 'node_modules/.cache', pluginName);
87
+ }
88
+ function mkCacheDirectory(cwd = process.cwd()) {
89
+ const cacheDirectory = getCacheDir(cwd);
90
+ const exists = fs.existsSync(cacheDirectory);
91
+ if (!exists) {
92
+ fs.mkdirSync(cacheDirectory, {
93
+ recursive: true
94
+ });
95
+ }
96
+ return cacheDirectory;
97
+ }
98
+ function cacheDump(filename, data, basedir) {
99
+ try {
100
+ const dir = mkCacheDirectory(basedir);
101
+ fs.writeFileSync(path.resolve(dir, filename), JSON.stringify(Array.from(data), null, 2), 'utf-8');
102
+ }
103
+ catch (error) {
104
+ console.log(error);
223
105
  }
224
106
  }
225
107
 
@@ -1,5 +1,5 @@
1
1
  import * as webpack from 'webpack';
2
- import ClassGenerator from '../classGenerator';
2
+ import { ClassGenerator } from 'tailwindcss-mangle-core';
3
3
  export default function cssloader(this: webpack.LoaderContext<{
4
4
  classGenerator: ClassGenerator;
5
5
  getCachedClassSet: (() => Set<string>) | undefined;
package/dist/nuxt.js CHANGED
@@ -2,16 +2,10 @@
2
2
 
3
3
  var index = require('./index.js');
4
4
  require('unplugin');
5
- require('./index-2edd594b.js');
6
- require('@babel/types');
7
- require('@babel/core');
8
5
  require('micromatch');
9
6
  require('fs');
10
7
  require('path');
11
- require('parse5');
12
- require('./index-c8e1bdc5.js');
13
- require('postcss');
14
- require('postcss-selector-parser');
8
+ require('tailwindcss-mangle-core');
15
9
  require('tailwindcss-patch');
16
10
 
17
11
  function nuxt (options = {}, nuxt) {
package/dist/nuxt.mjs CHANGED
@@ -1,15 +1,9 @@
1
1
  import { unplugin } from './index.mjs';
2
2
  import 'unplugin';
3
- import './index-92f879d3.mjs';
4
- import '@babel/types';
5
- import '@babel/core';
6
3
  import 'micromatch';
7
4
  import 'fs';
8
5
  import 'path';
9
- import 'parse5';
10
- import './index-b715a07f.mjs';
11
- import 'postcss';
12
- import 'postcss-selector-parser';
6
+ import 'tailwindcss-mangle-core';
13
7
  import 'tailwindcss-patch';
14
8
 
15
9
  function nuxt (options = {}, nuxt) {
package/dist/options.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import type { Options, ClassSetOutputOptions, ClassMapOutputOptions } from './types';
2
2
  import { TailwindcssPatcher } from 'tailwindcss-patch';
3
- import ClassGenerator from './classGenerator';
3
+ import { ClassGenerator } from 'tailwindcss-mangle-core';
4
4
  export declare function getOptions(options?: Options | undefined): {
5
5
  getCachedClassSet: () => Set<string>;
6
6
  classGenerator: ClassGenerator;
package/dist/rollup.js CHANGED
@@ -2,16 +2,10 @@
2
2
 
3
3
  var index = require('./index.js');
4
4
  require('unplugin');
5
- require('./index-2edd594b.js');
6
- require('@babel/types');
7
- require('@babel/core');
8
5
  require('micromatch');
9
6
  require('fs');
10
7
  require('path');
11
- require('parse5');
12
- require('./index-c8e1bdc5.js');
13
- require('postcss');
14
- require('postcss-selector-parser');
8
+ require('tailwindcss-mangle-core');
15
9
  require('tailwindcss-patch');
16
10
 
17
11
  var rollup = index.unplugin.rollup;
package/dist/rollup.mjs CHANGED
@@ -1,15 +1,9 @@
1
1
  import { unplugin } from './index.mjs';
2
2
  import 'unplugin';
3
- import './index-92f879d3.mjs';
4
- import '@babel/types';
5
- import '@babel/core';
6
3
  import 'micromatch';
7
4
  import 'fs';
8
5
  import 'path';
9
- import 'parse5';
10
- import './index-b715a07f.mjs';
11
- import 'postcss';
12
- import 'postcss-selector-parser';
6
+ import 'tailwindcss-mangle-core';
13
7
  import 'tailwindcss-patch';
14
8
 
15
9
  var rollup = unplugin.rollup;
package/dist/twm-css.js CHANGED
@@ -1,15 +1,13 @@
1
1
  'use strict';
2
2
 
3
- var index = require('./index-c8e1bdc5.js');
4
- require('postcss');
5
- require('postcss-selector-parser');
3
+ var tailwindcssMangleCore = require('tailwindcss-mangle-core');
6
4
 
7
5
  function cssloader(content) {
8
6
  this.cacheable && this.cacheable();
9
7
  const opt = this.getOptions();
10
8
  if (opt.getCachedClassSet) {
11
9
  const runtimeSet = opt.getCachedClassSet();
12
- return index.cssHandler(content, {
10
+ return tailwindcssMangleCore.cssHandler(content, {
13
11
  classGenerator: opt.classGenerator,
14
12
  runtimeSet,
15
13
  scene: 'loader'
package/dist/twm-css.mjs CHANGED
@@ -1,6 +1,4 @@
1
- import { c as cssHandler } from './index-b715a07f.mjs';
2
- import 'postcss';
3
- import 'postcss-selector-parser';
1
+ import { cssHandler } from 'tailwindcss-mangle-core';
4
2
 
5
3
  function cssloader(content) {
6
4
  this.cacheable && this.cacheable();
package/dist/types.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type ClassGenerator from './classGenerator';
1
+ import type { ClassGenerator } from 'tailwindcss-mangle-core';
2
2
  export interface IClassGeneratorContextItem {
3
3
  name: string;
4
4
  usedBy: any[];
package/dist/vite.js CHANGED
@@ -2,16 +2,10 @@
2
2
 
3
3
  var index = require('./index.js');
4
4
  require('unplugin');
5
- require('./index-2edd594b.js');
6
- require('@babel/types');
7
- require('@babel/core');
8
5
  require('micromatch');
9
6
  require('fs');
10
7
  require('path');
11
- require('parse5');
12
- require('./index-c8e1bdc5.js');
13
- require('postcss');
14
- require('postcss-selector-parser');
8
+ require('tailwindcss-mangle-core');
15
9
  require('tailwindcss-patch');
16
10
 
17
11
  var vite = index.unplugin.vite;
package/dist/vite.mjs CHANGED
@@ -1,15 +1,9 @@
1
1
  import { unplugin } from './index.mjs';
2
2
  import 'unplugin';
3
- import './index-92f879d3.mjs';
4
- import '@babel/types';
5
- import '@babel/core';
6
3
  import 'micromatch';
7
4
  import 'fs';
8
5
  import 'path';
9
- import 'parse5';
10
- import './index-b715a07f.mjs';
11
- import 'postcss';
12
- import 'postcss-selector-parser';
6
+ import 'tailwindcss-mangle-core';
13
7
  import 'tailwindcss-patch';
14
8
 
15
9
  var vite = unplugin.vite;
package/dist/webpack.js CHANGED
@@ -2,16 +2,10 @@
2
2
 
3
3
  var index = require('./index.js');
4
4
  require('unplugin');
5
- require('./index-2edd594b.js');
6
- require('@babel/types');
7
- require('@babel/core');
8
5
  require('micromatch');
9
6
  require('fs');
10
7
  require('path');
11
- require('parse5');
12
- require('./index-c8e1bdc5.js');
13
- require('postcss');
14
- require('postcss-selector-parser');
8
+ require('tailwindcss-mangle-core');
15
9
  require('tailwindcss-patch');
16
10
 
17
11
  var webpack = index.unplugin.webpack;
package/dist/webpack.mjs CHANGED
@@ -1,15 +1,9 @@
1
1
  import { unplugin } from './index.mjs';
2
2
  import 'unplugin';
3
- import './index-92f879d3.mjs';
4
- import '@babel/types';
5
- import '@babel/core';
6
3
  import 'micromatch';
7
4
  import 'fs';
8
5
  import 'path';
9
- import 'parse5';
10
- import './index-b715a07f.mjs';
11
- import 'postcss';
12
- import 'postcss-selector-parser';
6
+ import 'tailwindcss-mangle-core';
13
7
  import 'tailwindcss-patch';
14
8
 
15
9
  var webpack = unplugin.webpack;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "unplugin-tailwindcss-mangle",
3
- "version": "0.1.3",
3
+ "version": "0.1.4",
4
4
  "description": "mangle tailwindcss utilities class plugin. support vite and webpack!",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",
@@ -60,14 +60,9 @@
60
60
  "author": "SonOfMagic <qq1324318532@gmail.com>",
61
61
  "license": "MIT",
62
62
  "dependencies": {
63
- "@babel/core": "^7.21.5",
64
- "@babel/types": "^7.21.5",
65
63
  "micromatch": "^4.0.5",
66
- "parse5": "^7.1.2",
67
- "postcss": "^8.4.23",
68
- "postcss-selector-parser": "^6.0.12",
69
- "semver": "^7.5.0",
70
64
  "unplugin": "^1.3.1",
65
+ "tailwindcss-mangle-core": "^1.0.0",
71
66
  "tailwindcss-patch": "^1.1.1"
72
67
  },
73
68
  "publishConfig": {
@@ -75,17 +70,12 @@
75
70
  "registry": "https://registry.npmjs.org/"
76
71
  },
77
72
  "devDependencies": {
78
- "@parse5/tools": "^0.1.0",
79
- "@types/babel__core": "^7.20.0",
80
- "@types/escodegen": "^0.0.7",
81
73
  "@types/micromatch": "^4.0.2",
82
- "@types/semver": "^7.3.13",
83
- "pkg-types": "^1.0.2",
84
74
  "simple-functional-loader": "^1.2.1",
85
75
  "tailwindcss": "^3.3.2",
86
76
  "tslib": "^2.5.0",
87
- "vite": "^4.3.3",
88
- "webpack": "^5.81.0"
77
+ "vite": "^4.3.5",
78
+ "webpack": "^5.82.1"
89
79
  },
90
80
  "homepage": "https://github.com/sonofmagic/tailwindcss-mangle",
91
81
  "repository": {
@@ -97,7 +87,7 @@
97
87
  "build": "cross-env NODE_ENV=production rollup -c",
98
88
  "dev:tsc": "tsc -p tsconfig.json --sourceMap",
99
89
  "build:tsc": "tsc -p tsconfig.json",
100
- "test": "jest",
90
+ "_test": "jest",
101
91
  "_prepare": "tw-patch"
102
92
  }
103
93
  }
@@ -1,17 +0,0 @@
1
- import type { IClassGeneratorOptions, IClassGeneratorContextItem, IClassGenerator } from './types';
2
- declare class ClassGenerator implements IClassGenerator {
3
- newClassMap: Record<string, IClassGeneratorContextItem>;
4
- newClassSize: number;
5
- context: Record<string, any>;
6
- opts: IClassGeneratorOptions;
7
- classPrefix: string;
8
- constructor(opts?: IClassGeneratorOptions);
9
- defaultClassGenerate(): string;
10
- ignoreClassName(className: string): boolean;
11
- includeFilePath(filePath: string): boolean;
12
- excludeFilePath(filePath: string): boolean;
13
- isFileIncluded(filePath: string): boolean;
14
- transformCssClass(className: string): string;
15
- generateClassName(original: string): IClassGeneratorContextItem;
16
- }
17
- export default ClassGenerator;
@@ -1,2 +0,0 @@
1
- import { ICssHandlerOptions } from '@/types';
2
- export declare function cssHandler(rawSource: string, options: ICssHandlerOptions): string;
@@ -1,5 +0,0 @@
1
- import { ICssHandlerOptions } from '@/types';
2
- import type { PluginCreator } from 'postcss';
3
- export type PostcssMangleTailwindcssPlugin = PluginCreator<ICssHandlerOptions>;
4
- declare const postcssMangleTailwindcssPlugin: PostcssMangleTailwindcssPlugin;
5
- export { postcssMangleTailwindcssPlugin };
@@ -1,2 +0,0 @@
1
- import { IHandlerOptions } from '@/types';
2
- export declare function htmlHandler(rawSource: string, options: IHandlerOptions): string;