unplugin-tailwindcss-mangle 0.1.3 → 1.2.1

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/README.md CHANGED
@@ -105,11 +105,13 @@ custom class generator, if you want to custom class name (default 'tw-*'), use t
105
105
  ```js
106
106
  export interface IClassGeneratorOptions {
107
107
  reserveClassName?: (string | RegExp)[]
108
+ // custom generate class name
108
109
  customGenerate?: (original: string, opts: IClassGeneratorOptions, context: Record<string, any>) => string | undefined
109
110
  log?: boolean
110
111
  exclude?: (string | RegExp)[]
111
112
  include?: (string | RegExp)[]
112
113
  ignoreClass?: (string | RegExp)[]
114
+ // default 'tw-',for example: tw-a,tw-b, you can set any you want, like '','ice-'
113
115
  classPrefix?: string
114
116
  }
115
117
  ```
package/dist/esbuild.js CHANGED
@@ -2,16 +2,11 @@
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-shared');
9
+ require('tailwindcss-mangle-core');
15
10
  require('tailwindcss-patch');
16
11
 
17
12
  var esbuild = index.unplugin.esbuild;
package/dist/esbuild.mjs CHANGED
@@ -1,15 +1,10 @@
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-shared';
7
+ import 'tailwindcss-mangle-core';
13
8
  import 'tailwindcss-patch';
14
9
 
15
10
  var esbuild = unplugin.esbuild;
package/dist/index.js CHANGED
@@ -3,238 +3,99 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var unplugin$1 = require('unplugin');
6
- var index = require('./index-2edd594b.js');
7
- var parse5 = require('parse5');
8
- var index$1 = require('./index-c8e1bdc5.js');
9
- var path = require('path');
6
+ var micromatch = require('micromatch');
10
7
  var fs = require('fs');
8
+ var path = require('path');
9
+ var tailwindcssMangleShared = require('tailwindcss-mangle-shared');
10
+ var tailwindcssMangleCore = require('tailwindcss-mangle-core');
11
11
  var tailwindcssPatch = require('tailwindcss-patch');
12
- require('@babel/types');
13
- require('@babel/core');
14
- require('micromatch');
15
- require('postcss');
16
- require('postcss-selector-parser');
17
12
 
18
13
  function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e }; }
19
14
 
20
- var path__default = /*#__PURE__*/_interopDefault(path);
15
+ var micromatch__default = /*#__PURE__*/_interopDefault(micromatch);
21
16
  var fs__default = /*#__PURE__*/_interopDefault(fs);
17
+ var path__default = /*#__PURE__*/_interopDefault(path);
22
18
 
23
- ({
24
- HTML: parse5.html.NS.HTML,
25
- XML: parse5.html.NS.XML,
26
- MATHML: parse5.html.NS.MATHML,
27
- SVG: parse5.html.NS.SVG,
28
- XLINK: parse5.html.NS.XLINK,
29
- XMLNS: parse5.html.NS.XMLNS
30
- });
31
-
32
- /**
33
- * Determines if a given node is a document or not
34
- * @param {Node} node Node to test
35
- * @return {boolean}
36
- */
37
- function isDocument(node) {
38
- return node.nodeName === '#document';
39
- }
40
- /**
41
- * Determines if a given node is a document fragment or not
42
- * @param {Node} node Node to test
43
- * @return {boolean}
44
- */
45
- function isDocumentFragment(node) {
46
- return node.nodeName === '#document-fragment';
47
- }
48
- /**
49
- * Determines if a given node is a template node or not
50
- * @param {Node} node Node to test
51
- * @return {boolean}
52
- */
53
- function isTemplateNode(node) {
54
- return node.nodeName === 'template';
55
- }
56
- const isElementNode = parse5.defaultTreeAdapter.isElementNode;
57
- const isCommentNode = parse5.defaultTreeAdapter.isCommentNode;
58
- const isDocumentTypeNode = parse5.defaultTreeAdapter.isDocumentTypeNode;
59
- const isTextNode = parse5.defaultTreeAdapter.isTextNode;
60
- /**
61
- * Determines if a given node is a parent or not
62
- * @param {Node} node Node to test
63
- * @return {boolean}
64
- */
65
- function isParentNode(node) {
66
- return (isDocument(node) ||
67
- isDocumentFragment(node) ||
68
- isElementNode(node) ||
69
- isTemplateNode(node));
70
- }
71
-
72
- parse5.defaultTreeAdapter.appendChild;
19
+ const pluginName = 'unplugin-tailwindcss-mangle';
73
20
 
74
- /**
75
- * Traverses the tree of a given node
76
- * @param {Node} node Node to traverse
77
- * @param {Visitor} visitor Visitor to apply
78
- * @param {ParentNode=} parent Parent node of the current node
79
- * @return {void}
80
- */
81
- function traverse(node, visitor, parent) {
82
- const shouldVisitChildren = typeof visitor['pre:node'] !== 'function' ||
83
- visitor['pre:node'](node, parent) !== false;
84
- if (shouldVisitChildren && isParentNode(node)) {
85
- for (const child of node.childNodes) {
86
- traverse(child, visitor, node);
87
- }
88
- }
89
- if (typeof visitor.node === 'function') {
90
- visitor.node(node, parent);
21
+ const { isMatch } = micromatch__default["default"];
22
+ function getGroupedEntries(entries, options = {
23
+ cssMatcher(file) {
24
+ return /\.css$/.test(file);
25
+ },
26
+ htmlMatcher(file) {
27
+ return /\.html?$/.test(file);
28
+ },
29
+ jsMatcher(file) {
30
+ return /\.[cm]?js$/.test(file);
91
31
  }
92
- if (typeof visitor.document === 'function' && isDocument(node)) {
93
- visitor.document(node);
94
- }
95
- if (typeof visitor.documentFragment === 'function' &&
96
- isDocumentFragment(node)) {
97
- visitor.documentFragment(node, parent);
98
- }
99
- if (typeof visitor.element === 'function' && isElementNode(node)) {
100
- visitor.element(node, parent);
101
- }
102
- if (typeof visitor.template === 'function' && isTemplateNode(node)) {
103
- visitor.template(node, parent);
104
- }
105
- if (typeof visitor.comment === 'function' && isCommentNode(node)) {
106
- visitor.comment(node, parent);
107
- }
108
- if (typeof visitor.text === 'function' && isTextNode(node)) {
109
- visitor.text(node, parent);
110
- }
111
- if (typeof visitor.documentType === 'function' && isDocumentTypeNode(node)) {
112
- visitor.documentType(node, parent);
113
- }
114
- }
115
-
116
- function htmlHandler(rawSource, options) {
117
- const { runtimeSet, classGenerator } = options;
118
- const fragment = parse5.parse(rawSource);
119
- traverse(fragment, {
120
- element(node, parent) {
121
- const attr = node.attrs.find((x) => x.name === 'class');
122
- if (attr) {
123
- const arr = attr.value.split(/\s/).filter((x) => x);
124
- attr.value = arr
125
- .map((x) => {
126
- if (runtimeSet.has(x)) {
127
- return classGenerator.generateClassName(x).name;
128
- }
129
- return x;
130
- })
131
- .join(' ');
132
- }
32
+ }) {
33
+ const { cssMatcher, htmlMatcher, jsMatcher } = options;
34
+ const groupedEntries = tailwindcssMangleShared.groupBy(entries, ([file]) => {
35
+ if (cssMatcher(file)) {
36
+ return 'css';
133
37
  }
134
- });
135
- return parse5.serialize(fragment);
136
- }
137
-
138
- class ClassGenerator {
139
- newClassMap;
140
- newClassSize;
141
- context;
142
- opts;
143
- classPrefix;
144
- constructor(opts = {}) {
145
- this.newClassMap = {};
146
- this.newClassSize = 0;
147
- this.context = {};
148
- this.opts = opts;
149
- this.classPrefix = opts.classPrefix ?? 'tw-';
150
- }
151
- defaultClassGenerate() {
152
- const chars = [];
153
- let rest = (this.newClassSize - (this.newClassSize % index.acceptChars.length)) / index.acceptChars.length;
154
- if (rest > 0) {
155
- while (true) {
156
- rest -= 1;
157
- const m = rest % index.acceptChars.length;
158
- const c = index.acceptChars[m];
159
- chars.push(c);
160
- rest -= m;
161
- if (rest === 0) {
162
- break;
163
- }
164
- rest /= index.acceptChars.length;
165
- }
38
+ else if (htmlMatcher(file)) {
39
+ return 'html';
166
40
  }
167
- const prefixIndex = this.newClassSize % index.acceptChars.length;
168
- const newClassName = `${this.classPrefix}${index.acceptChars[prefixIndex]}${chars.join('')}`;
169
- return newClassName;
170
- }
171
- ignoreClassName(className) {
172
- return index.regExpTest(this.opts.ignoreClass, className);
173
- }
174
- includeFilePath(filePath) {
175
- const { include } = this.opts;
176
- if (Array.isArray(include)) {
177
- return index.regExpTest(include, filePath);
41
+ else if (jsMatcher(file)) {
42
+ return 'js';
178
43
  }
179
44
  else {
180
- return true;
45
+ return 'other';
181
46
  }
47
+ });
48
+ if (!groupedEntries.css) {
49
+ groupedEntries.css = [];
182
50
  }
183
- excludeFilePath(filePath) {
184
- const { exclude } = this.opts;
185
- if (Array.isArray(exclude)) {
186
- return index.regExpTest(exclude, filePath);
187
- }
188
- else {
189
- return false;
190
- }
51
+ if (!groupedEntries.html) {
52
+ groupedEntries.html = [];
191
53
  }
192
- isFileIncluded(filePath) {
193
- return this.includeFilePath(filePath) && !this.excludeFilePath(filePath);
54
+ if (!groupedEntries.js) {
55
+ groupedEntries.js = [];
194
56
  }
195
- transformCssClass(className) {
196
- const key = index.stripEscapeSequence(className);
197
- const cn = this.newClassMap[key];
198
- if (cn)
199
- return cn.name;
200
- return className;
57
+ if (!groupedEntries.other) {
58
+ groupedEntries.other = [];
201
59
  }
202
- generateClassName(original) {
203
- const opts = this.opts;
204
- original = index.stripEscapeSequence(original);
205
- const cn = this.newClassMap[original];
206
- if (cn)
207
- return cn;
208
- let newClassName;
209
- if (opts.customGenerate && typeof opts.customGenerate === 'function') {
210
- newClassName = opts.customGenerate(original, opts, this.context);
211
- }
212
- if (!newClassName) {
213
- newClassName = this.defaultClassGenerate();
214
- }
215
- if (opts.reserveClassName && index.regExpTest(opts.reserveClassName, newClassName)) {
216
- if (opts.log) {
217
- console.log(`The class name has been reserved. ${newClassName}`);
218
- }
219
- this.newClassSize++;
220
- return this.generateClassName(original);
221
- }
222
- if (opts.log) {
223
- console.log(`Minify class name from ${original} to ${newClassName}`);
224
- }
225
- const newClass = {
226
- name: newClassName,
227
- usedBy: []
60
+ return groupedEntries;
61
+ }
62
+ function createGlobMatcher(pattern, fallbackValue = false) {
63
+ if (typeof pattern === 'undefined') {
64
+ return function (file) {
65
+ return fallbackValue;
228
66
  };
229
- this.newClassMap[original] = newClass;
230
- this.newClassSize++;
231
- return newClass;
67
+ }
68
+ return function (file) {
69
+ return isMatch(file, pattern);
70
+ };
71
+ }
72
+ function getCacheDir(basedir = process.cwd()) {
73
+ return path__default["default"].resolve(basedir, 'node_modules/.cache', pluginName);
74
+ }
75
+ function mkCacheDirectory(cwd = process.cwd()) {
76
+ const cacheDirectory = getCacheDir(cwd);
77
+ const exists = fs__default["default"].existsSync(cacheDirectory);
78
+ if (!exists) {
79
+ fs__default["default"].mkdirSync(cacheDirectory, {
80
+ recursive: true
81
+ });
82
+ }
83
+ return cacheDirectory;
84
+ }
85
+ function cacheDump(filename, data, basedir) {
86
+ try {
87
+ const dir = mkCacheDirectory(basedir);
88
+ fs__default["default"].writeFileSync(path__default["default"].resolve(dir, filename), JSON.stringify(Array.from(data), null, 2), 'utf-8');
89
+ }
90
+ catch (error) {
91
+ console.log(error);
232
92
  }
233
93
  }
234
94
 
235
95
  function getOptions(options = {}) {
236
- const includeMatcher = index.createGlobMatcher(options.include, true);
237
- const excludeMatcher = index.createGlobMatcher(options.exclude, false);
96
+ const includeMatcher = createGlobMatcher(options.include, true);
97
+ const excludeMatcher = createGlobMatcher(options.exclude, false);
98
+ const currentMangleClassFilter = options.mangleClassFilter ?? tailwindcssMangleShared.defaultMangleClassFilter;
238
99
  function isInclude(file) {
239
100
  return includeMatcher(file) && !excludeMatcher(file);
240
101
  }
@@ -253,20 +114,20 @@ function getOptions(options = {}) {
253
114
  if (typeof options.classMapOutput === 'object') {
254
115
  Object.assign(classMapOutputOptions, options.classMapOutput);
255
116
  }
256
- const classGenerator = new ClassGenerator(options.classGenerator);
117
+ const classGenerator = new tailwindcssMangleCore.ClassGenerator(options.classGenerator);
257
118
  function getCachedClassSet() {
258
119
  const set = twPatcher.getClassSet();
259
120
  const isOutput = set.size && options.classSetOutput;
260
121
  if (isOutput && classSetOutputOptions.type === 'all') {
261
- index.cacheDump(classSetOutputOptions.filename, set, classSetOutputOptions.dir);
122
+ cacheDump(classSetOutputOptions.filename, set, classSetOutputOptions.dir);
262
123
  }
263
124
  set.forEach((c) => {
264
- if (!index.isMangleClass(c)) {
125
+ if (!currentMangleClassFilter(c)) {
265
126
  set.delete(c);
266
127
  }
267
128
  });
268
129
  if (isOutput && classSetOutputOptions.type === 'partial') {
269
- index.cacheDump(classSetOutputOptions.filename, set, classSetOutputOptions.dir);
130
+ cacheDump(classSetOutputOptions.filename, set, classSetOutputOptions.dir);
270
131
  }
271
132
  classSet = set;
272
133
  return classSet;
@@ -287,7 +148,7 @@ const outputCachedMap = new Map();
287
148
  const unplugin = unplugin$1.createUnplugin((options = {}, meta) => {
288
149
  const { classGenerator, getCachedClassSet, isInclude, classMapOutputOptions } = getOptions(options);
289
150
  return {
290
- name: index.pluginName,
151
+ name: pluginName,
291
152
  enforce: 'post',
292
153
  vite: {
293
154
  generateBundle: {
@@ -296,12 +157,12 @@ const unplugin = unplugin$1.createUnplugin((options = {}, meta) => {
296
157
  if (!runtimeSet.size) {
297
158
  return;
298
159
  }
299
- const groupedEntries = index.getGroupedEntries(Object.entries(bundle));
160
+ const groupedEntries = getGroupedEntries(Object.entries(bundle));
300
161
  if (Array.isArray(groupedEntries.html) && groupedEntries.html.length) {
301
162
  for (let i = 0; i < groupedEntries.html.length; i++) {
302
163
  const [file, asset] = groupedEntries.html[i];
303
164
  if (isInclude(file)) {
304
- asset.source = htmlHandler(asset.source.toString(), {
165
+ asset.source = tailwindcssMangleCore.htmlHandler(asset.source.toString(), {
305
166
  classGenerator,
306
167
  runtimeSet
307
168
  });
@@ -312,7 +173,7 @@ const unplugin = unplugin$1.createUnplugin((options = {}, meta) => {
312
173
  for (let i = 0; i < groupedEntries.js.length; i++) {
313
174
  const [file, chunk] = groupedEntries.js[i];
314
175
  if (isInclude(file)) {
315
- const code = index.jsHandler(chunk.code, {
176
+ const code = tailwindcssMangleCore.jsHandler(chunk.code, {
316
177
  runtimeSet,
317
178
  classGenerator
318
179
  }).code;
@@ -326,7 +187,7 @@ const unplugin = unplugin$1.createUnplugin((options = {}, meta) => {
326
187
  for (let i = 0; i < groupedEntries.css.length; i++) {
327
188
  const [file, css] = groupedEntries.css[i];
328
189
  if (isInclude(file)) {
329
- css.source = index$1.cssHandler(css.source.toString(), {
190
+ css.source = tailwindcssMangleCore.cssHandler(css.source.toString(), {
330
191
  classGenerator,
331
192
  runtimeSet
332
193
  });
@@ -356,8 +217,8 @@ const unplugin = unplugin$1.createUnplugin((options = {}, meta) => {
356
217
  }
357
218
  }
358
219
  const twmCssloader = path__default["default"].resolve(__dirname, 'twm-css.js');
359
- compiler.hooks.compilation.tap(index.pluginName, (compilation) => {
360
- NormalModule.getCompilationHooks(compilation).loader.tap(index.pluginName, (loaderContext, module) => {
220
+ compiler.hooks.compilation.tap(pluginName, (compilation) => {
221
+ NormalModule.getCompilationHooks(compilation).loader.tap(pluginName, (loaderContext, module) => {
361
222
  const idx = module.loaders.findIndex((x) => x.loader.includes('css-loader'));
362
223
  if (idx > -1) {
363
224
  module.loaders.splice(idx + 1, 0, {
@@ -372,11 +233,11 @@ const unplugin = unplugin$1.createUnplugin((options = {}, meta) => {
372
233
  }
373
234
  });
374
235
  compilation.hooks.processAssets.tap({
375
- name: index.pluginName,
236
+ name: pluginName,
376
237
  stage: Compilation.PROCESS_ASSETS_STAGE_SUMMARIZE
377
238
  }, (assets) => {
378
239
  const runtimeSet = getCachedClassSet();
379
- const groupedEntries = index.getGroupedEntries(Object.entries(assets));
240
+ const groupedEntries = getGroupedEntries(Object.entries(assets));
380
241
  if (!runtimeSet.size) {
381
242
  const css = new Map();
382
243
  const html = new Map();
@@ -403,7 +264,7 @@ const unplugin = unplugin$1.createUnplugin((options = {}, meta) => {
403
264
  for (let i = 0; i < groupedEntries.html.length; i++) {
404
265
  const [file, asset] = groupedEntries.html[i];
405
266
  if (isInclude(file)) {
406
- const html = htmlHandler(asset.source().toString(), {
267
+ const html = tailwindcssMangleCore.htmlHandler(asset.source().toString(), {
407
268
  classGenerator,
408
269
  runtimeSet
409
270
  });
@@ -416,7 +277,7 @@ const unplugin = unplugin$1.createUnplugin((options = {}, meta) => {
416
277
  for (let i = 0; i < groupedEntries.js.length; i++) {
417
278
  const [file, chunk] = groupedEntries.js[i];
418
279
  if (isInclude(file)) {
419
- const code = index.jsHandler(chunk.source().toString(), {
280
+ const code = tailwindcssMangleCore.jsHandler(chunk.source().toString(), {
420
281
  runtimeSet,
421
282
  classGenerator
422
283
  }).code;
@@ -431,7 +292,7 @@ const unplugin = unplugin$1.createUnplugin((options = {}, meta) => {
431
292
  for (let i = 0; i < groupedEntries.css.length; i++) {
432
293
  const [file, css] = groupedEntries.css[i];
433
294
  if (isInclude(file)) {
434
- const newCss = index$1.cssHandler(css.source().toString(), {
295
+ const newCss = tailwindcssMangleCore.cssHandler(css.source().toString(), {
435
296
  classGenerator,
436
297
  runtimeSet
437
298
  });
@@ -444,7 +305,7 @@ const unplugin = unplugin$1.createUnplugin((options = {}, meta) => {
444
305
  if (html.size) {
445
306
  html.forEach((asset, file) => {
446
307
  if (isInclude(file)) {
447
- const html = htmlHandler(asset.source().toString(), {
308
+ const html = tailwindcssMangleCore.htmlHandler(asset.source().toString(), {
448
309
  classGenerator,
449
310
  runtimeSet
450
311
  });
@@ -457,7 +318,7 @@ const unplugin = unplugin$1.createUnplugin((options = {}, meta) => {
457
318
  js.forEach((chunk, file) => {
458
319
  if (isInclude(file)) {
459
320
  const rawCode = chunk.source().toString();
460
- const code = index.jsHandler(rawCode, {
321
+ const code = tailwindcssMangleCore.jsHandler(rawCode, {
461
322
  runtimeSet,
462
323
  classGenerator
463
324
  }).code;
@@ -471,7 +332,7 @@ const unplugin = unplugin$1.createUnplugin((options = {}, meta) => {
471
332
  if (css.size) {
472
333
  css.forEach((style, file) => {
473
334
  if (isInclude(file)) {
474
- const newCss = index$1.cssHandler(style.source().toString(), {
335
+ const newCss = tailwindcssMangleCore.cssHandler(style.source().toString(), {
475
336
  classGenerator,
476
337
  runtimeSet
477
338
  });
@@ -487,7 +348,7 @@ const unplugin = unplugin$1.createUnplugin((options = {}, meta) => {
487
348
  writeBundle() {
488
349
  const entries = Object.entries(classGenerator.newClassMap);
489
350
  if (entries.length && classMapOutputOptions) {
490
- index.cacheDump(classMapOutputOptions.filename, entries.map((x) => {
351
+ cacheDump(classMapOutputOptions.filename, entries.map((x) => {
491
352
  return [x[0], x[1].name];
492
353
  }), classMapOutputOptions.dir);
493
354
  }