unplugin-tailwindcss-mangle 0.1.2 → 0.1.3

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,145 +1,15 @@
1
1
  import { createUnplugin } from 'unplugin';
2
- import micromatch from 'micromatch';
3
- import fs from 'fs';
4
- import path from 'path';
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';
5
3
  import { html, defaultTreeAdapter, parse, serialize } from 'parse5';
6
- import { transformSync } from '@babel/core';
7
- import postcss from 'postcss';
8
- import parser from 'postcss-selector-parser';
9
- import { getClassCacheSet } from 'tailwindcss-patch';
10
-
11
- const pluginName = 'unplugin-tailwindcss-mangle';
12
-
13
- const { isMatch } = micromatch;
14
- const isMangleClass = (className) => {
15
- return /[-:]/.test(className);
16
- };
17
- function groupBy(arr, cb) {
18
- if (!Array.isArray(arr)) {
19
- throw new Error('expected an array for first argument');
20
- }
21
- if (typeof cb !== 'function') {
22
- throw new Error('expected a function for second argument');
23
- }
24
- const result = {};
25
- for (let i = 0; i < arr.length; i++) {
26
- const item = arr[i];
27
- const bucketCategory = cb(item);
28
- const bucket = result[bucketCategory];
29
- if (!Array.isArray(bucket)) {
30
- result[bucketCategory] = [item];
31
- }
32
- else {
33
- result[bucketCategory].push(item);
34
- }
35
- }
36
- return result;
37
- }
38
- function getGroupedEntries(entries, options = {
39
- cssMatcher(file) {
40
- return /\.css$/.test(file);
41
- },
42
- htmlMatcher(file) {
43
- return /\.html?$/.test(file);
44
- },
45
- jsMatcher(file) {
46
- return /\.[cm]?js$/.test(file);
47
- }
48
- }) {
49
- const { cssMatcher, htmlMatcher, jsMatcher } = options;
50
- const groupedEntries = groupBy(entries, ([file]) => {
51
- if (cssMatcher(file)) {
52
- return 'css';
53
- }
54
- else if (htmlMatcher(file)) {
55
- return 'html';
56
- }
57
- else if (jsMatcher(file)) {
58
- return 'js';
59
- }
60
- else {
61
- return 'other';
62
- }
63
- });
64
- if (!groupedEntries.css) {
65
- groupedEntries.css = [];
66
- }
67
- if (!groupedEntries.html) {
68
- groupedEntries.html = [];
69
- }
70
- if (!groupedEntries.js) {
71
- groupedEntries.js = [];
72
- }
73
- if (!groupedEntries.other) {
74
- groupedEntries.other = [];
75
- }
76
- return groupedEntries;
77
- }
78
- const acceptChars = 'abcdefghijklmnopqrstuvwxyz'.split('');
79
- function stripEscapeSequence(words) {
80
- return words.replace(/\\/g, '');
81
- }
82
- function isRegexp(value) {
83
- return Object.prototype.toString.call(value) === '[object RegExp]';
84
- }
85
- function regExpTest(arr = [], str) {
86
- if (Array.isArray(arr)) {
87
- for (let i = 0; i < arr.length; i++) {
88
- const item = arr[i];
89
- if (typeof item === 'string') {
90
- if (item === str) {
91
- return true;
92
- }
93
- }
94
- else if (isRegexp(item)) {
95
- item.lastIndex = 0;
96
- if (item.test(str)) {
97
- return true;
98
- }
99
- }
100
- }
101
- return false;
102
- }
103
- throw new TypeError("paramater 'arr' should be a Array of Regexp | String !");
104
- }
105
- function escapeStringRegexp(str) {
106
- if (typeof str !== 'string') {
107
- throw new TypeError('Expected a string');
108
- }
109
- return str.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&').replace(/-/g, '\\x2d');
110
- }
111
- function createGlobMatcher(pattern, fallbackValue = false) {
112
- if (typeof pattern === 'undefined') {
113
- return function (file) {
114
- return fallbackValue;
115
- };
116
- }
117
- return function (file) {
118
- return isMatch(file, pattern);
119
- };
120
- }
121
- function getCacheDir(basedir = process.cwd()) {
122
- return path.resolve(basedir, 'node_modules/.cache', pluginName);
123
- }
124
- function mkCacheDirectory(cwd = process.cwd()) {
125
- const cacheDirectory = getCacheDir(cwd);
126
- const exists = fs.existsSync(cacheDirectory);
127
- if (!exists) {
128
- fs.mkdirSync(cacheDirectory, {
129
- recursive: true
130
- });
131
- }
132
- return cacheDirectory;
133
- }
134
- function cacheDump(filename, data, basedir) {
135
- try {
136
- const dir = mkCacheDirectory(basedir);
137
- fs.writeFileSync(path.resolve(dir, filename), JSON.stringify(Array.from(data), null, 2), 'utf-8');
138
- }
139
- catch (error) {
140
- console.log(error);
141
- }
142
- }
4
+ import { c as cssHandler } from './index-b715a07f.mjs';
5
+ import path from 'path';
6
+ import fs from 'fs';
7
+ import { TailwindcssPatcher } from 'tailwindcss-patch';
8
+ import '@babel/types';
9
+ import '@babel/core';
10
+ import 'micromatch';
11
+ import 'postcss';
12
+ import 'postcss-selector-parser';
143
13
 
144
14
  ({
145
15
  HTML: html.NS.HTML,
@@ -256,107 +126,6 @@ function htmlHandler(rawSource, options) {
256
126
  return serialize(fragment);
257
127
  }
258
128
 
259
- const validateFilterRE = /[\w\u00A0-\uFFFF-_:%-?]/;
260
- function isValidSelector(selector = '') {
261
- return validateFilterRE.test(selector);
262
- }
263
- const splitCode = (code) => code.split(/[\s"]+/).filter(isValidSelector);
264
-
265
- function makeRegex(str) {
266
- return new RegExp('(?<=^|[\\s"])' + escapeStringRegexp(str), 'g');
267
- }
268
- function handleValue(str, node, options) {
269
- const set = options.runtimeSet;
270
- const clsGen = options.classGenerator;
271
- const arr = splitCode(str);
272
- let rawStr = str;
273
- for (let i = 0; i < arr.length; i++) {
274
- const v = arr[i];
275
- if (set.has(v)) {
276
- let ignoreFlag = false;
277
- if (Array.isArray(node.leadingComments)) {
278
- ignoreFlag = node.leadingComments.findIndex((x) => x.value.includes('tw-mangle') && x.value.includes('ignore')) > -1;
279
- }
280
- if (!ignoreFlag) {
281
- rawStr = rawStr.replace(makeRegex(v), clsGen.generateClassName(v).name);
282
- }
283
- }
284
- }
285
- return rawStr;
286
- }
287
- function jsHandler(rawSource, options) {
288
- const result = transformSync(rawSource, {
289
- babelrc: false,
290
- ast: true,
291
- plugins: [
292
- () => {
293
- return {
294
- visitor: {
295
- StringLiteral: {
296
- enter(p) {
297
- const n = p.node;
298
- n.value = handleValue(n.value, n, options);
299
- }
300
- },
301
- TemplateElement: {
302
- enter(p) {
303
- const n = p.node;
304
- n.value.raw = handleValue(n.value.raw, n, options);
305
- }
306
- }
307
- }
308
- };
309
- }
310
- ],
311
- sourceMaps: false,
312
- configFile: false
313
- });
314
- return result;
315
- }
316
-
317
- const postcssPlugin = 'postcss-mangle-tailwindcss-plugin';
318
- const postcssMangleTailwindcssPlugin = (options) => {
319
- let newClassMap = {};
320
- if (options) {
321
- if (options.newClassMap) {
322
- newClassMap = options.newClassMap;
323
- }
324
- }
325
- return {
326
- postcssPlugin,
327
- Rule(rule, helper) {
328
- rule.selector = parser((selectors) => {
329
- selectors.walkClasses((s) => {
330
- if (s.value) {
331
- const hit = newClassMap[s.value];
332
- if (hit) {
333
- if (s.parent) {
334
- const idx = s.parent.nodes.indexOf(s);
335
- if (idx > -1) {
336
- const nextNode = s.parent.nodes[idx + 1];
337
- if (nextNode && nextNode.type === 'attribute' && nextNode.attribute.indexOf('data-v-') > -1) {
338
- return;
339
- }
340
- }
341
- }
342
- s.value = hit.name;
343
- }
344
- }
345
- });
346
- }).processSync(rule.selector);
347
- }
348
- };
349
- };
350
- postcssMangleTailwindcssPlugin.postcss = true;
351
-
352
- function cssHandler(rawSource, options) {
353
- return postcss([
354
- postcssMangleTailwindcssPlugin({
355
- newClassMap: options.classGenerator.newClassMap
356
- })
357
- ]).process(rawSource).css;
358
- }
359
-
360
129
  class ClassGenerator {
361
130
  newClassMap;
362
131
  newClassSize;
@@ -461,6 +230,7 @@ function getOptions(options = {}) {
461
230
  return includeMatcher(file) && !excludeMatcher(file);
462
231
  }
463
232
  let classSet;
233
+ const twPatcher = new TailwindcssPatcher();
464
234
  const classSetOutputOptions = {
465
235
  filename: 'classSet.json',
466
236
  type: 'partial'
@@ -476,7 +246,7 @@ function getOptions(options = {}) {
476
246
  }
477
247
  const classGenerator = new ClassGenerator(options.classGenerator);
478
248
  function getCachedClassSet() {
479
- const set = getClassCacheSet();
249
+ const set = twPatcher.getClassSet();
480
250
  const isOutput = set.size && options.classSetOutput;
481
251
  if (isOutput && classSetOutputOptions.type === 'all') {
482
252
  cacheDump(classSetOutputOptions.filename, set, classSetOutputOptions.dir);
@@ -499,7 +269,8 @@ function getOptions(options = {}) {
499
269
  excludeMatcher,
500
270
  isInclude,
501
271
  classSetOutputOptions,
502
- classMapOutputOptions
272
+ classMapOutputOptions,
273
+ twPatcher
503
274
  };
504
275
  }
505
276
 
@@ -557,7 +328,7 @@ const unplugin = createUnplugin((options = {}, meta) => {
557
328
  }
558
329
  },
559
330
  webpack(compiler) {
560
- const Compilation = compiler.webpack.Compilation;
331
+ const { NormalModule, Compilation } = compiler.webpack;
561
332
  const { ConcatSource } = compiler.webpack.sources;
562
333
  function getAssetPath(outputPath, file, abs = true) {
563
334
  const fn = abs ? path.resolve : path.relative;
@@ -575,7 +346,22 @@ const unplugin = createUnplugin((options = {}, meta) => {
575
346
  console.log(error);
576
347
  }
577
348
  }
349
+ const twmCssloader = path.resolve(__dirname, 'twm-css.js');
578
350
  compiler.hooks.compilation.tap(pluginName, (compilation) => {
351
+ NormalModule.getCompilationHooks(compilation).loader.tap(pluginName, (loaderContext, module) => {
352
+ const idx = module.loaders.findIndex((x) => x.loader.includes('css-loader'));
353
+ if (idx > -1) {
354
+ module.loaders.splice(idx + 1, 0, {
355
+ loader: twmCssloader,
356
+ options: {
357
+ classGenerator,
358
+ getCachedClassSet
359
+ },
360
+ ident: null,
361
+ type: null
362
+ });
363
+ }
364
+ });
579
365
  compilation.hooks.processAssets.tap({
580
366
  name: pluginName,
581
367
  stage: Compilation.PROCESS_ASSETS_STAGE_SUMMARIZE
@@ -0,0 +1,6 @@
1
+ import * as webpack from 'webpack';
2
+ import ClassGenerator from '../classGenerator';
3
+ export default function cssloader(this: webpack.LoaderContext<{
4
+ classGenerator: ClassGenerator;
5
+ getCachedClassSet: (() => Set<string>) | undefined;
6
+ }>, content: string): string;
@@ -0,0 +1,6 @@
1
+ import * as webpack from 'webpack';
2
+ import ClassGenerator from '../classGenerator';
3
+ export default function cssloader(this: webpack.LoaderContext<{
4
+ classGenerator: ClassGenerator;
5
+ getCachedClassSet: (() => Set<string>) | undefined;
6
+ }>, content: string): string;
package/dist/nuxt.js CHANGED
@@ -2,11 +2,14 @@
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');
5
8
  require('micromatch');
6
9
  require('fs');
7
10
  require('path');
8
11
  require('parse5');
9
- require('@babel/core');
12
+ require('./index-c8e1bdc5.js');
10
13
  require('postcss');
11
14
  require('postcss-selector-parser');
12
15
  require('tailwindcss-patch');
package/dist/nuxt.mjs CHANGED
@@ -1,10 +1,13 @@
1
1
  import { unplugin } from './index.mjs';
2
2
  import 'unplugin';
3
+ import './index-92f879d3.mjs';
4
+ import '@babel/types';
5
+ import '@babel/core';
3
6
  import 'micromatch';
4
7
  import 'fs';
5
8
  import 'path';
6
9
  import 'parse5';
7
- import '@babel/core';
10
+ import './index-b715a07f.mjs';
8
11
  import 'postcss';
9
12
  import 'postcss-selector-parser';
10
13
  import 'tailwindcss-patch';
package/dist/options.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import type { Options, ClassSetOutputOptions, ClassMapOutputOptions } from './types';
2
+ import { TailwindcssPatcher } from 'tailwindcss-patch';
2
3
  import ClassGenerator from './classGenerator';
3
4
  export declare function getOptions(options?: Options | undefined): {
4
5
  getCachedClassSet: () => Set<string>;
@@ -8,4 +9,5 @@ export declare function getOptions(options?: Options | undefined): {
8
9
  isInclude: (file: string) => boolean;
9
10
  classSetOutputOptions: ClassSetOutputOptions;
10
11
  classMapOutputOptions: ClassMapOutputOptions;
12
+ twPatcher: TailwindcssPatcher;
11
13
  };
package/dist/rollup.js CHANGED
@@ -2,11 +2,14 @@
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');
5
8
  require('micromatch');
6
9
  require('fs');
7
10
  require('path');
8
11
  require('parse5');
9
- require('@babel/core');
12
+ require('./index-c8e1bdc5.js');
10
13
  require('postcss');
11
14
  require('postcss-selector-parser');
12
15
  require('tailwindcss-patch');
package/dist/rollup.mjs CHANGED
@@ -1,10 +1,13 @@
1
1
  import { unplugin } from './index.mjs';
2
2
  import 'unplugin';
3
+ import './index-92f879d3.mjs';
4
+ import '@babel/types';
5
+ import '@babel/core';
3
6
  import 'micromatch';
4
7
  import 'fs';
5
8
  import 'path';
6
9
  import 'parse5';
7
- import '@babel/core';
10
+ import './index-b715a07f.mjs';
8
11
  import 'postcss';
9
12
  import 'postcss-selector-parser';
10
13
  import 'tailwindcss-patch';
@@ -0,0 +1,21 @@
1
+ 'use strict';
2
+
3
+ var index = require('./index-c8e1bdc5.js');
4
+ require('postcss');
5
+ require('postcss-selector-parser');
6
+
7
+ function cssloader(content) {
8
+ this.cacheable && this.cacheable();
9
+ const opt = this.getOptions();
10
+ if (opt.getCachedClassSet) {
11
+ const runtimeSet = opt.getCachedClassSet();
12
+ return index.cssHandler(content, {
13
+ classGenerator: opt.classGenerator,
14
+ runtimeSet,
15
+ scene: 'loader'
16
+ });
17
+ }
18
+ return content;
19
+ }
20
+
21
+ module.exports = cssloader;
@@ -0,0 +1,19 @@
1
+ import { c as cssHandler } from './index-b715a07f.mjs';
2
+ import 'postcss';
3
+ import 'postcss-selector-parser';
4
+
5
+ function cssloader(content) {
6
+ this.cacheable && this.cacheable();
7
+ const opt = this.getOptions();
8
+ if (opt.getCachedClassSet) {
9
+ const runtimeSet = opt.getCachedClassSet();
10
+ return cssHandler(content, {
11
+ classGenerator: opt.classGenerator,
12
+ runtimeSet,
13
+ scene: 'loader'
14
+ });
15
+ }
16
+ return content;
17
+ }
18
+
19
+ export { cssloader as default };
package/dist/twm-js.js ADDED
@@ -0,0 +1,26 @@
1
+ 'use strict';
2
+
3
+ var index = require('./index-2edd594b.js');
4
+ require('@babel/types');
5
+ require('@babel/core');
6
+ require('micromatch');
7
+ require('fs');
8
+ require('path');
9
+
10
+ function cssloader(content) {
11
+ this.cacheable && this.cacheable();
12
+ const opt = this.getOptions();
13
+ if (opt.getCachedClassSet) {
14
+ const runtimeSet = opt.getCachedClassSet();
15
+ const code = index.jsHandler(content, {
16
+ runtimeSet,
17
+ classGenerator: opt.classGenerator
18
+ }).code;
19
+ if (code) {
20
+ return code;
21
+ }
22
+ }
23
+ return content;
24
+ }
25
+
26
+ module.exports = cssloader;
@@ -0,0 +1,24 @@
1
+ import { j as jsHandler } from './index-92f879d3.mjs';
2
+ import '@babel/types';
3
+ import '@babel/core';
4
+ import 'micromatch';
5
+ import 'fs';
6
+ import 'path';
7
+
8
+ function cssloader(content) {
9
+ this.cacheable && this.cacheable();
10
+ const opt = this.getOptions();
11
+ if (opt.getCachedClassSet) {
12
+ const runtimeSet = opt.getCachedClassSet();
13
+ const code = jsHandler(content, {
14
+ runtimeSet,
15
+ classGenerator: opt.classGenerator
16
+ }).code;
17
+ if (code) {
18
+ return code;
19
+ }
20
+ }
21
+ return content;
22
+ }
23
+
24
+ export { cssloader as default };
package/dist/types.d.ts CHANGED
@@ -21,6 +21,9 @@ export interface IHandlerOptions {
21
21
  runtimeSet: Set<string>;
22
22
  classGenerator: ClassGenerator;
23
23
  }
24
+ export interface ICssHandlerOptions extends IHandlerOptions {
25
+ scene?: 'loader' | 'process';
26
+ }
24
27
  export interface ClassSetOutputOptions {
25
28
  filename: string;
26
29
  dir?: string;
package/dist/vite.js CHANGED
@@ -2,11 +2,14 @@
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');
5
8
  require('micromatch');
6
9
  require('fs');
7
10
  require('path');
8
11
  require('parse5');
9
- require('@babel/core');
12
+ require('./index-c8e1bdc5.js');
10
13
  require('postcss');
11
14
  require('postcss-selector-parser');
12
15
  require('tailwindcss-patch');
package/dist/vite.mjs CHANGED
@@ -1,10 +1,13 @@
1
1
  import { unplugin } from './index.mjs';
2
2
  import 'unplugin';
3
+ import './index-92f879d3.mjs';
4
+ import '@babel/types';
5
+ import '@babel/core';
3
6
  import 'micromatch';
4
7
  import 'fs';
5
8
  import 'path';
6
9
  import 'parse5';
7
- import '@babel/core';
10
+ import './index-b715a07f.mjs';
8
11
  import 'postcss';
9
12
  import 'postcss-selector-parser';
10
13
  import 'tailwindcss-patch';
package/dist/webpack.js CHANGED
@@ -2,11 +2,14 @@
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');
5
8
  require('micromatch');
6
9
  require('fs');
7
10
  require('path');
8
11
  require('parse5');
9
- require('@babel/core');
12
+ require('./index-c8e1bdc5.js');
10
13
  require('postcss');
11
14
  require('postcss-selector-parser');
12
15
  require('tailwindcss-patch');
package/dist/webpack.mjs CHANGED
@@ -1,10 +1,13 @@
1
1
  import { unplugin } from './index.mjs';
2
2
  import 'unplugin';
3
+ import './index-92f879d3.mjs';
4
+ import '@babel/types';
5
+ import '@babel/core';
3
6
  import 'micromatch';
4
7
  import 'fs';
5
8
  import 'path';
6
9
  import 'parse5';
7
- import '@babel/core';
10
+ import './index-b715a07f.mjs';
8
11
  import 'postcss';
9
12
  import 'postcss-selector-parser';
10
13
  import 'tailwindcss-patch';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "unplugin-tailwindcss-mangle",
3
- "version": "0.1.2",
3
+ "version": "0.1.3",
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,15 +60,15 @@
60
60
  "author": "SonOfMagic <qq1324318532@gmail.com>",
61
61
  "license": "MIT",
62
62
  "dependencies": {
63
- "@babel/core": "^7.21.4",
64
- "@babel/types": "^7.21.4",
63
+ "@babel/core": "^7.21.5",
64
+ "@babel/types": "^7.21.5",
65
65
  "micromatch": "^4.0.5",
66
66
  "parse5": "^7.1.2",
67
67
  "postcss": "^8.4.23",
68
- "postcss-selector-parser": "^6.0.11",
68
+ "postcss-selector-parser": "^6.0.12",
69
69
  "semver": "^7.5.0",
70
70
  "unplugin": "^1.3.1",
71
- "tailwindcss-patch": "^1.1.0"
71
+ "tailwindcss-patch": "^1.1.1"
72
72
  },
73
73
  "publishConfig": {
74
74
  "access": "public",
@@ -81,6 +81,7 @@
81
81
  "@types/micromatch": "^4.0.2",
82
82
  "@types/semver": "^7.3.13",
83
83
  "pkg-types": "^1.0.2",
84
+ "simple-functional-loader": "^1.2.1",
84
85
  "tailwindcss": "^3.3.2",
85
86
  "tslib": "^2.5.0",
86
87
  "vite": "^4.3.3",