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.
@@ -1,246 +0,0 @@
1
- 'use strict';
2
-
3
- var t = require('@babel/types');
4
- var core = require('@babel/core');
5
- var micromatch = require('micromatch');
6
- var fs = require('fs');
7
- var path = require('path');
8
-
9
- function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e }; }
10
-
11
- function _interopNamespace(e) {
12
- if (e && e.__esModule) return e;
13
- var n = Object.create(null);
14
- if (e) {
15
- Object.keys(e).forEach(function (k) {
16
- if (k !== 'default') {
17
- var d = Object.getOwnPropertyDescriptor(e, k);
18
- Object.defineProperty(n, k, d.get ? d : {
19
- enumerable: true,
20
- get: function () { return e[k]; }
21
- });
22
- }
23
- });
24
- }
25
- n["default"] = e;
26
- return Object.freeze(n);
27
- }
28
-
29
- var t__namespace = /*#__PURE__*/_interopNamespace(t);
30
- var micromatch__default = /*#__PURE__*/_interopDefault(micromatch);
31
- var fs__default = /*#__PURE__*/_interopDefault(fs);
32
- var path__default = /*#__PURE__*/_interopDefault(path);
33
-
34
- const pluginName = 'unplugin-tailwindcss-mangle';
35
-
36
- const { isMatch } = micromatch__default["default"];
37
- const isMangleClass = (className) => {
38
- return /[-:]/.test(className);
39
- };
40
- function groupBy(arr, cb) {
41
- if (!Array.isArray(arr)) {
42
- throw new Error('expected an array for first argument');
43
- }
44
- if (typeof cb !== 'function') {
45
- throw new Error('expected a function for second argument');
46
- }
47
- const result = {};
48
- for (let i = 0; i < arr.length; i++) {
49
- const item = arr[i];
50
- const bucketCategory = cb(item);
51
- const bucket = result[bucketCategory];
52
- if (!Array.isArray(bucket)) {
53
- result[bucketCategory] = [item];
54
- }
55
- else {
56
- result[bucketCategory].push(item);
57
- }
58
- }
59
- return result;
60
- }
61
- function getGroupedEntries(entries, options = {
62
- cssMatcher(file) {
63
- return /\.css$/.test(file);
64
- },
65
- htmlMatcher(file) {
66
- return /\.html?$/.test(file);
67
- },
68
- jsMatcher(file) {
69
- return /\.[cm]?js$/.test(file);
70
- }
71
- }) {
72
- const { cssMatcher, htmlMatcher, jsMatcher } = options;
73
- const groupedEntries = groupBy(entries, ([file]) => {
74
- if (cssMatcher(file)) {
75
- return 'css';
76
- }
77
- else if (htmlMatcher(file)) {
78
- return 'html';
79
- }
80
- else if (jsMatcher(file)) {
81
- return 'js';
82
- }
83
- else {
84
- return 'other';
85
- }
86
- });
87
- if (!groupedEntries.css) {
88
- groupedEntries.css = [];
89
- }
90
- if (!groupedEntries.html) {
91
- groupedEntries.html = [];
92
- }
93
- if (!groupedEntries.js) {
94
- groupedEntries.js = [];
95
- }
96
- if (!groupedEntries.other) {
97
- groupedEntries.other = [];
98
- }
99
- return groupedEntries;
100
- }
101
- const acceptChars = 'abcdefghijklmnopqrstuvwxyz'.split('');
102
- function stripEscapeSequence(words) {
103
- return words.replace(/\\/g, '');
104
- }
105
- function isRegexp(value) {
106
- return Object.prototype.toString.call(value) === '[object RegExp]';
107
- }
108
- function regExpTest(arr = [], str) {
109
- if (Array.isArray(arr)) {
110
- for (let i = 0; i < arr.length; i++) {
111
- const item = arr[i];
112
- if (typeof item === 'string') {
113
- if (item === str) {
114
- return true;
115
- }
116
- }
117
- else if (isRegexp(item)) {
118
- item.lastIndex = 0;
119
- if (item.test(str)) {
120
- return true;
121
- }
122
- }
123
- }
124
- return false;
125
- }
126
- throw new TypeError("paramater 'arr' should be a Array of Regexp | String !");
127
- }
128
- function escapeStringRegexp(str) {
129
- if (typeof str !== 'string') {
130
- throw new TypeError('Expected a string');
131
- }
132
- return str.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&').replace(/-/g, '\\x2d');
133
- }
134
- function createGlobMatcher(pattern, fallbackValue = false) {
135
- if (typeof pattern === 'undefined') {
136
- return function (file) {
137
- return fallbackValue;
138
- };
139
- }
140
- return function (file) {
141
- return isMatch(file, pattern);
142
- };
143
- }
144
- function getCacheDir(basedir = process.cwd()) {
145
- return path__default["default"].resolve(basedir, 'node_modules/.cache', pluginName);
146
- }
147
- function mkCacheDirectory(cwd = process.cwd()) {
148
- const cacheDirectory = getCacheDir(cwd);
149
- const exists = fs__default["default"].existsSync(cacheDirectory);
150
- if (!exists) {
151
- fs__default["default"].mkdirSync(cacheDirectory, {
152
- recursive: true
153
- });
154
- }
155
- return cacheDirectory;
156
- }
157
- function cacheDump(filename, data, basedir) {
158
- try {
159
- const dir = mkCacheDirectory(basedir);
160
- fs__default["default"].writeFileSync(path__default["default"].resolve(dir, filename), JSON.stringify(Array.from(data), null, 2), 'utf-8');
161
- }
162
- catch (error) {
163
- console.log(error);
164
- }
165
- }
166
-
167
- const validateFilterRE = /[\w\u00A0-\uFFFF-_:%-?]/;
168
- function isValidSelector(selector = '') {
169
- return validateFilterRE.test(selector);
170
- }
171
- const splitCode = (code) => code.split(/[\s"]+/).filter(isValidSelector);
172
-
173
- function makeRegex(str) {
174
- return new RegExp('(?<=^|[\\s"])' + escapeStringRegexp(str), 'g');
175
- }
176
- function handleValue(str, node, options) {
177
- const set = options.runtimeSet;
178
- const clsGen = options.classGenerator;
179
- const arr = splitCode(str);
180
- let rawStr = str;
181
- for (let i = 0; i < arr.length; i++) {
182
- const v = arr[i];
183
- if (set.has(v)) {
184
- let ignoreFlag = false;
185
- if (Array.isArray(node.leadingComments)) {
186
- ignoreFlag = node.leadingComments.findIndex((x) => x.value.includes('tw-mangle') && x.value.includes('ignore')) > -1;
187
- }
188
- if (!ignoreFlag) {
189
- rawStr = rawStr.replace(makeRegex(v), clsGen.generateClassName(v).name);
190
- }
191
- }
192
- }
193
- return rawStr;
194
- }
195
- function jsHandler(rawSource, options) {
196
- const result = core.transformSync(rawSource, {
197
- babelrc: false,
198
- ast: true,
199
- plugins: [
200
- () => {
201
- return {
202
- visitor: {
203
- StringLiteral: {
204
- enter(p) {
205
- const n = p.node;
206
- n.value = handleValue(n.value, n, options);
207
- }
208
- },
209
- TemplateElement: {
210
- enter(p) {
211
- const n = p.node;
212
- n.value.raw = handleValue(n.value.raw, n, options);
213
- }
214
- },
215
- CallExpression: {
216
- enter(p) {
217
- const n = p.node;
218
- if (t__namespace.isIdentifier(n.callee) && n.callee.name === 'eval') {
219
- if (t__namespace.isStringLiteral(n.arguments[0])) {
220
- const res = jsHandler(n.arguments[0].value, options);
221
- if (res.code) {
222
- n.arguments[0].value = res.code;
223
- }
224
- }
225
- }
226
- }
227
- }
228
- }
229
- };
230
- }
231
- ],
232
- sourceMaps: false,
233
- configFile: false
234
- });
235
- return result;
236
- }
237
-
238
- exports.acceptChars = acceptChars;
239
- exports.cacheDump = cacheDump;
240
- exports.createGlobMatcher = createGlobMatcher;
241
- exports.getGroupedEntries = getGroupedEntries;
242
- exports.isMangleClass = isMangleClass;
243
- exports.jsHandler = jsHandler;
244
- exports.pluginName = pluginName;
245
- exports.regExpTest = regExpTest;
246
- exports.stripEscapeSequence = stripEscapeSequence;
@@ -1,211 +0,0 @@
1
- import * as t from '@babel/types';
2
- import { transformSync } from '@babel/core';
3
- import micromatch from 'micromatch';
4
- import fs from 'fs';
5
- import path from 'path';
6
-
7
- const pluginName = 'unplugin-tailwindcss-mangle';
8
-
9
- const { isMatch } = micromatch;
10
- const isMangleClass = (className) => {
11
- return /[-:]/.test(className);
12
- };
13
- function groupBy(arr, cb) {
14
- if (!Array.isArray(arr)) {
15
- throw new Error('expected an array for first argument');
16
- }
17
- if (typeof cb !== 'function') {
18
- throw new Error('expected a function for second argument');
19
- }
20
- const result = {};
21
- for (let i = 0; i < arr.length; i++) {
22
- const item = arr[i];
23
- const bucketCategory = cb(item);
24
- const bucket = result[bucketCategory];
25
- if (!Array.isArray(bucket)) {
26
- result[bucketCategory] = [item];
27
- }
28
- else {
29
- result[bucketCategory].push(item);
30
- }
31
- }
32
- return result;
33
- }
34
- function getGroupedEntries(entries, options = {
35
- cssMatcher(file) {
36
- return /\.css$/.test(file);
37
- },
38
- htmlMatcher(file) {
39
- return /\.html?$/.test(file);
40
- },
41
- jsMatcher(file) {
42
- return /\.[cm]?js$/.test(file);
43
- }
44
- }) {
45
- const { cssMatcher, htmlMatcher, jsMatcher } = options;
46
- const groupedEntries = groupBy(entries, ([file]) => {
47
- if (cssMatcher(file)) {
48
- return 'css';
49
- }
50
- else if (htmlMatcher(file)) {
51
- return 'html';
52
- }
53
- else if (jsMatcher(file)) {
54
- return 'js';
55
- }
56
- else {
57
- return 'other';
58
- }
59
- });
60
- if (!groupedEntries.css) {
61
- groupedEntries.css = [];
62
- }
63
- if (!groupedEntries.html) {
64
- groupedEntries.html = [];
65
- }
66
- if (!groupedEntries.js) {
67
- groupedEntries.js = [];
68
- }
69
- if (!groupedEntries.other) {
70
- groupedEntries.other = [];
71
- }
72
- return groupedEntries;
73
- }
74
- const acceptChars = 'abcdefghijklmnopqrstuvwxyz'.split('');
75
- function stripEscapeSequence(words) {
76
- return words.replace(/\\/g, '');
77
- }
78
- function isRegexp(value) {
79
- return Object.prototype.toString.call(value) === '[object RegExp]';
80
- }
81
- function regExpTest(arr = [], str) {
82
- if (Array.isArray(arr)) {
83
- for (let i = 0; i < arr.length; i++) {
84
- const item = arr[i];
85
- if (typeof item === 'string') {
86
- if (item === str) {
87
- return true;
88
- }
89
- }
90
- else if (isRegexp(item)) {
91
- item.lastIndex = 0;
92
- if (item.test(str)) {
93
- return true;
94
- }
95
- }
96
- }
97
- return false;
98
- }
99
- throw new TypeError("paramater 'arr' should be a Array of Regexp | String !");
100
- }
101
- function escapeStringRegexp(str) {
102
- if (typeof str !== 'string') {
103
- throw new TypeError('Expected a string');
104
- }
105
- return str.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&').replace(/-/g, '\\x2d');
106
- }
107
- function createGlobMatcher(pattern, fallbackValue = false) {
108
- if (typeof pattern === 'undefined') {
109
- return function (file) {
110
- return fallbackValue;
111
- };
112
- }
113
- return function (file) {
114
- return isMatch(file, pattern);
115
- };
116
- }
117
- function getCacheDir(basedir = process.cwd()) {
118
- return path.resolve(basedir, 'node_modules/.cache', pluginName);
119
- }
120
- function mkCacheDirectory(cwd = process.cwd()) {
121
- const cacheDirectory = getCacheDir(cwd);
122
- const exists = fs.existsSync(cacheDirectory);
123
- if (!exists) {
124
- fs.mkdirSync(cacheDirectory, {
125
- recursive: true
126
- });
127
- }
128
- return cacheDirectory;
129
- }
130
- function cacheDump(filename, data, basedir) {
131
- try {
132
- const dir = mkCacheDirectory(basedir);
133
- fs.writeFileSync(path.resolve(dir, filename), JSON.stringify(Array.from(data), null, 2), 'utf-8');
134
- }
135
- catch (error) {
136
- console.log(error);
137
- }
138
- }
139
-
140
- const validateFilterRE = /[\w\u00A0-\uFFFF-_:%-?]/;
141
- function isValidSelector(selector = '') {
142
- return validateFilterRE.test(selector);
143
- }
144
- const splitCode = (code) => code.split(/[\s"]+/).filter(isValidSelector);
145
-
146
- function makeRegex(str) {
147
- return new RegExp('(?<=^|[\\s"])' + escapeStringRegexp(str), 'g');
148
- }
149
- function handleValue(str, node, options) {
150
- const set = options.runtimeSet;
151
- const clsGen = options.classGenerator;
152
- const arr = splitCode(str);
153
- let rawStr = str;
154
- for (let i = 0; i < arr.length; i++) {
155
- const v = arr[i];
156
- if (set.has(v)) {
157
- let ignoreFlag = false;
158
- if (Array.isArray(node.leadingComments)) {
159
- ignoreFlag = node.leadingComments.findIndex((x) => x.value.includes('tw-mangle') && x.value.includes('ignore')) > -1;
160
- }
161
- if (!ignoreFlag) {
162
- rawStr = rawStr.replace(makeRegex(v), clsGen.generateClassName(v).name);
163
- }
164
- }
165
- }
166
- return rawStr;
167
- }
168
- function jsHandler(rawSource, options) {
169
- const result = transformSync(rawSource, {
170
- babelrc: false,
171
- ast: true,
172
- plugins: [
173
- () => {
174
- return {
175
- visitor: {
176
- StringLiteral: {
177
- enter(p) {
178
- const n = p.node;
179
- n.value = handleValue(n.value, n, options);
180
- }
181
- },
182
- TemplateElement: {
183
- enter(p) {
184
- const n = p.node;
185
- n.value.raw = handleValue(n.value.raw, n, options);
186
- }
187
- },
188
- CallExpression: {
189
- enter(p) {
190
- const n = p.node;
191
- if (t.isIdentifier(n.callee) && n.callee.name === 'eval') {
192
- if (t.isStringLiteral(n.arguments[0])) {
193
- const res = jsHandler(n.arguments[0].value, options);
194
- if (res.code) {
195
- n.arguments[0].value = res.code;
196
- }
197
- }
198
- }
199
- }
200
- }
201
- }
202
- };
203
- }
204
- ],
205
- sourceMaps: false,
206
- configFile: false
207
- });
208
- return result;
209
- }
210
-
211
- export { acceptChars as a, cacheDump as b, createGlobMatcher as c, getGroupedEntries as g, isMangleClass as i, jsHandler as j, pluginName as p, regExpTest as r, stripEscapeSequence as s };
@@ -1,79 +0,0 @@
1
- import postcss from 'postcss';
2
- import parser from 'postcss-selector-parser';
3
-
4
- const postcssPlugin = 'postcss-mangle-tailwindcss-plugin';
5
- const postcssMangleTailwindcssPlugin = (options) => {
6
- if (options?.scene === 'loader') {
7
- let set = new Set();
8
- if (options) {
9
- if (options.runtimeSet) {
10
- set = options.runtimeSet;
11
- }
12
- }
13
- return {
14
- postcssPlugin,
15
- Rule(rule, helper) {
16
- rule.selector = parser((selectors) => {
17
- selectors.walkClasses((s) => {
18
- if (s.value) {
19
- const existed = set.has(s.value);
20
- if (existed) {
21
- if (s.parent) {
22
- const idx = s.parent.nodes.indexOf(s);
23
- if (idx > -1) {
24
- const nextNode = s.parent.nodes[idx + 1];
25
- if (nextNode && nextNode.type === 'attribute' && nextNode.attribute.indexOf('data-v-') > -1) {
26
- return;
27
- }
28
- }
29
- }
30
- s.value = options.classGenerator.generateClassName(s.value).name;
31
- }
32
- }
33
- });
34
- }).processSync(rule.selector);
35
- }
36
- };
37
- }
38
- else {
39
- let newClassMap = {};
40
- if (options) {
41
- if (options.classGenerator) {
42
- newClassMap = options.classGenerator.newClassMap;
43
- }
44
- }
45
- return {
46
- postcssPlugin,
47
- Rule(rule, helper) {
48
- rule.selector = parser((selectors) => {
49
- selectors.walkClasses((s) => {
50
- if (s.value) {
51
- const hit = newClassMap[s.value];
52
- const existed = Boolean(hit);
53
- if (existed) {
54
- if (s.parent) {
55
- const idx = s.parent.nodes.indexOf(s);
56
- if (idx > -1) {
57
- const nextNode = s.parent.nodes[idx + 1];
58
- if (nextNode && nextNode.type === 'attribute' && nextNode.attribute.indexOf('data-v-') > -1) {
59
- return;
60
- }
61
- }
62
- }
63
- s.value = hit.name;
64
- }
65
- }
66
- });
67
- }).processSync(rule.selector);
68
- }
69
- };
70
- }
71
- };
72
- postcssMangleTailwindcssPlugin.postcss = true;
73
-
74
- function cssHandler(rawSource, options) {
75
- const acceptedPlugins = [postcssMangleTailwindcssPlugin(options)];
76
- return postcss(acceptedPlugins).process(rawSource).css;
77
- }
78
-
79
- export { cssHandler as c };
@@ -1,86 +0,0 @@
1
- 'use strict';
2
-
3
- var postcss = require('postcss');
4
- var parser = require('postcss-selector-parser');
5
-
6
- function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e }; }
7
-
8
- var postcss__default = /*#__PURE__*/_interopDefault(postcss);
9
- var parser__default = /*#__PURE__*/_interopDefault(parser);
10
-
11
- const postcssPlugin = 'postcss-mangle-tailwindcss-plugin';
12
- const postcssMangleTailwindcssPlugin = (options) => {
13
- if (options?.scene === 'loader') {
14
- let set = new Set();
15
- if (options) {
16
- if (options.runtimeSet) {
17
- set = options.runtimeSet;
18
- }
19
- }
20
- return {
21
- postcssPlugin,
22
- Rule(rule, helper) {
23
- rule.selector = parser__default["default"]((selectors) => {
24
- selectors.walkClasses((s) => {
25
- if (s.value) {
26
- const existed = set.has(s.value);
27
- if (existed) {
28
- if (s.parent) {
29
- const idx = s.parent.nodes.indexOf(s);
30
- if (idx > -1) {
31
- const nextNode = s.parent.nodes[idx + 1];
32
- if (nextNode && nextNode.type === 'attribute' && nextNode.attribute.indexOf('data-v-') > -1) {
33
- return;
34
- }
35
- }
36
- }
37
- s.value = options.classGenerator.generateClassName(s.value).name;
38
- }
39
- }
40
- });
41
- }).processSync(rule.selector);
42
- }
43
- };
44
- }
45
- else {
46
- let newClassMap = {};
47
- if (options) {
48
- if (options.classGenerator) {
49
- newClassMap = options.classGenerator.newClassMap;
50
- }
51
- }
52
- return {
53
- postcssPlugin,
54
- Rule(rule, helper) {
55
- rule.selector = parser__default["default"]((selectors) => {
56
- selectors.walkClasses((s) => {
57
- if (s.value) {
58
- const hit = newClassMap[s.value];
59
- const existed = Boolean(hit);
60
- if (existed) {
61
- if (s.parent) {
62
- const idx = s.parent.nodes.indexOf(s);
63
- if (idx > -1) {
64
- const nextNode = s.parent.nodes[idx + 1];
65
- if (nextNode && nextNode.type === 'attribute' && nextNode.attribute.indexOf('data-v-') > -1) {
66
- return;
67
- }
68
- }
69
- }
70
- s.value = hit.name;
71
- }
72
- }
73
- });
74
- }).processSync(rule.selector);
75
- }
76
- };
77
- }
78
- };
79
- postcssMangleTailwindcssPlugin.postcss = true;
80
-
81
- function cssHandler(rawSource, options) {
82
- const acceptedPlugins = [postcssMangleTailwindcssPlugin(options)];
83
- return postcss__default["default"](acceptedPlugins).process(rawSource).css;
84
- }
85
-
86
- exports.cssHandler = cssHandler;
@@ -1,6 +0,0 @@
1
- import type { StringLiteral, TemplateElement } from '@babel/types';
2
- import { type BabelFileResult } from '@babel/core';
3
- import type { IHandlerOptions } from '../types';
4
- export declare function makeRegex(str: string): RegExp;
5
- export declare function handleValue(str: string, node: StringLiteral | TemplateElement, options: IHandlerOptions): string;
6
- export declare function jsHandler(rawSource: string, options: IHandlerOptions): BabelFileResult;
@@ -1,3 +0,0 @@
1
- export declare const validateFilterRE: RegExp;
2
- export declare function isValidSelector(selector?: string): selector is string;
3
- export declare const splitCode: (code: string) => string[];
@@ -1,6 +0,0 @@
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/twm-js.js DELETED
@@ -1,26 +0,0 @@
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;
package/dist/twm-js.mjs DELETED
@@ -1,24 +0,0 @@
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 };