unplugin-tailwindcss-mangle 1.2.7 → 2.0.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/dist/index.js DELETED
@@ -1,379 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- var unplugin$1 = require('unplugin');
6
- var micromatch = require('micromatch');
7
- var fs = require('node:fs');
8
- var path = require('node:path');
9
- var tailwindcssMangleShared = require('tailwindcss-mangle-shared');
10
- var tailwindcssMangleCore = require('tailwindcss-mangle-core');
11
- var tailwindcssPatch = require('tailwindcss-patch');
12
-
13
- function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e }; }
14
-
15
- var micromatch__default = /*#__PURE__*/_interopDefault(micromatch);
16
- var fs__default = /*#__PURE__*/_interopDefault(fs);
17
- var path__default = /*#__PURE__*/_interopDefault(path);
18
-
19
- const pluginName = 'unplugin-tailwindcss-mangle';
20
-
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);
31
- }
32
- }) {
33
- const { cssMatcher, htmlMatcher, jsMatcher } = options;
34
- const groupedEntries = tailwindcssMangleShared.groupBy(entries, ([file]) => {
35
- if (cssMatcher(file)) {
36
- return 'css';
37
- }
38
- else if (htmlMatcher(file)) {
39
- return 'html';
40
- }
41
- else if (jsMatcher(file)) {
42
- return 'js';
43
- }
44
- else {
45
- return 'other';
46
- }
47
- });
48
- if (!groupedEntries.css) {
49
- groupedEntries.css = [];
50
- }
51
- if (!groupedEntries.html) {
52
- groupedEntries.html = [];
53
- }
54
- if (!groupedEntries.js) {
55
- groupedEntries.js = [];
56
- }
57
- if (!groupedEntries.other) {
58
- groupedEntries.other = [];
59
- }
60
- return groupedEntries;
61
- }
62
- function createGlobMatcher(pattern, fallbackValue = false) {
63
- if (pattern === undefined) {
64
- return function (file) {
65
- return fallbackValue;
66
- };
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([...data], undefined, 2), 'utf8');
89
- }
90
- catch (error) {
91
- console.log(error);
92
- }
93
- }
94
-
95
- function getOptions(options = {}) {
96
- const includeMatcher = createGlobMatcher(options.include, true);
97
- const excludeMatcher = createGlobMatcher(options.exclude, false);
98
- const currentMangleClassFilter = options.mangleClassFilter ?? tailwindcssMangleShared.defaultMangleClassFilter;
99
- function isInclude(file) {
100
- return includeMatcher(file) && !excludeMatcher(file);
101
- }
102
- let classSet;
103
- const twPatcher = new tailwindcssPatch.TailwindcssPatcher();
104
- const classSetOutputOptions = {
105
- filename: 'classSet.json',
106
- type: 'partial'
107
- };
108
- const classMapOutputOptions = {
109
- filename: 'classMap.json'
110
- };
111
- if (typeof options.classSetOutput === 'object') {
112
- Object.assign(classSetOutputOptions, options.classSetOutput);
113
- }
114
- if (typeof options.classMapOutput === 'object') {
115
- Object.assign(classMapOutputOptions, options.classMapOutput);
116
- }
117
- const classGenerator = new tailwindcssMangleCore.ClassGenerator(options.classGenerator);
118
- function getCachedClassSet() {
119
- const set = twPatcher.getClassSet();
120
- const isOutput = set.size > 0 && options.classSetOutput;
121
- if (isOutput && classSetOutputOptions.type === 'all') {
122
- cacheDump(classSetOutputOptions.filename, set, classSetOutputOptions.dir);
123
- }
124
- for (const c of set) {
125
- if (!currentMangleClassFilter(c)) {
126
- set.delete(c);
127
- }
128
- }
129
- if (isOutput && classSetOutputOptions.type === 'partial') {
130
- cacheDump(classSetOutputOptions.filename, set, classSetOutputOptions.dir);
131
- }
132
- classSet = set;
133
- return classSet;
134
- }
135
- return {
136
- getCachedClassSet,
137
- classGenerator,
138
- includeMatcher,
139
- excludeMatcher,
140
- isInclude,
141
- classSetOutputOptions,
142
- classMapOutputOptions,
143
- twPatcher,
144
- jsHandlerOptions: (options.jsHandlerOptions ?? {}),
145
- htmlHandlerOptions: (options.htmlHandlerOptions ?? {}),
146
- cssHandlerOptions: (options.cssHandlerOptions ?? {})
147
- };
148
- }
149
-
150
- const outputCachedMap = new Map();
151
- const unplugin = unplugin$1.createUnplugin((options = {}, meta) => {
152
- const { classGenerator, getCachedClassSet, isInclude, classMapOutputOptions, htmlHandlerOptions, jsHandlerOptions, cssHandlerOptions } = getOptions(options);
153
- return {
154
- name: pluginName,
155
- enforce: 'post',
156
- vite: {
157
- generateBundle: {
158
- handler(options, bundle, isWrite) {
159
- const runtimeSet = getCachedClassSet();
160
- if (runtimeSet.size === 0) {
161
- return;
162
- }
163
- const groupedEntries = getGroupedEntries(Object.entries(bundle));
164
- if (Array.isArray(groupedEntries.html) && groupedEntries.html.length > 0) {
165
- for (let i = 0; i < groupedEntries.html.length; i++) {
166
- const [file, asset] = groupedEntries.html[i];
167
- if (isInclude(file)) {
168
- asset.source = tailwindcssMangleCore.htmlHandler(asset.source.toString(), {
169
- ...htmlHandlerOptions,
170
- classGenerator,
171
- runtimeSet
172
- });
173
- }
174
- }
175
- }
176
- if (Array.isArray(groupedEntries.js) && groupedEntries.js.length > 0) {
177
- for (let i = 0; i < groupedEntries.js.length; i++) {
178
- const [file, chunk] = groupedEntries.js[i];
179
- if (isInclude(file)) {
180
- const code = tailwindcssMangleCore.jsHandler(chunk.code, {
181
- ...jsHandlerOptions,
182
- runtimeSet,
183
- classGenerator
184
- }).code;
185
- if (code) {
186
- chunk.code = code;
187
- }
188
- }
189
- }
190
- }
191
- if (Array.isArray(groupedEntries.css) && groupedEntries.css.length > 0) {
192
- for (let i = 0; i < groupedEntries.css.length; i++) {
193
- const [file, css] = groupedEntries.css[i];
194
- if (isInclude(file)) {
195
- css.source = tailwindcssMangleCore.cssHandler(css.source.toString(), {
196
- ...cssHandlerOptions,
197
- classGenerator,
198
- runtimeSet
199
- });
200
- }
201
- }
202
- }
203
- }
204
- }
205
- },
206
- webpack(compiler) {
207
- const { NormalModule, Compilation } = compiler.webpack;
208
- const { ConcatSource } = compiler.webpack.sources;
209
- function getAssetPath(outputPath, file, abs = true) {
210
- const fn = abs ? path__default["default"].resolve : path__default["default"].relative;
211
- return fn(compiler.context, path__default["default"].resolve(outputPath, file));
212
- }
213
- function overwriteServerSideAsset(outputPath, file, data) {
214
- const abs = getAssetPath(outputPath, file);
215
- const rel = getAssetPath(outputPath, file, false);
216
- try {
217
- fs__default["default"].writeFileSync(abs, data, 'utf8');
218
- console.log('[tailwindcss-mangle]: ' + rel + ' overwrited successfully');
219
- }
220
- catch (error) {
221
- console.log('[tailwindcss-mangle]: ' + rel + ' overwrited fail!');
222
- console.log(error);
223
- }
224
- }
225
- const twmCssloader = path__default["default"].resolve(__dirname, 'twm-css.js');
226
- compiler.hooks.compilation.tap(pluginName, (compilation) => {
227
- NormalModule.getCompilationHooks(compilation).loader.tap(pluginName, (loaderContext, module) => {
228
- const idx = module.loaders.findIndex((x) => x.loader.includes('css-loader'));
229
- if (idx > -1) {
230
- module.loaders.splice(idx + 1, 0, {
231
- loader: twmCssloader,
232
- options: {
233
- classGenerator,
234
- getCachedClassSet
235
- },
236
- ident: null,
237
- type: null
238
- });
239
- }
240
- });
241
- compilation.hooks.processAssets.tap({
242
- name: pluginName,
243
- stage: Compilation.PROCESS_ASSETS_STAGE_SUMMARIZE
244
- }, (assets) => {
245
- const runtimeSet = getCachedClassSet();
246
- const groupedEntries = getGroupedEntries(Object.entries(assets));
247
- if (runtimeSet.size === 0) {
248
- const css = new Map();
249
- const html = new Map();
250
- const js = new Map();
251
- for (const [file, source] of groupedEntries.css) {
252
- css.set(file, source);
253
- }
254
- for (const [file, source] of groupedEntries.html) {
255
- html.set(file, source);
256
- }
257
- for (const [file, source] of groupedEntries.js) {
258
- js.set(file, source);
259
- }
260
- if (js.size > 0 || css.size > 0 || html.size > 0) {
261
- outputCachedMap.set(compiler.outputPath, {
262
- css,
263
- html,
264
- js
265
- });
266
- }
267
- return;
268
- }
269
- if (groupedEntries.html.length > 0) {
270
- for (let i = 0; i < groupedEntries.html.length; i++) {
271
- const [file, asset] = groupedEntries.html[i];
272
- if (isInclude(file)) {
273
- const html = tailwindcssMangleCore.htmlHandler(asset.source().toString(), {
274
- ...htmlHandlerOptions,
275
- classGenerator,
276
- runtimeSet
277
- });
278
- const source = new ConcatSource(html);
279
- compilation.updateAsset(file, source);
280
- }
281
- }
282
- }
283
- if (groupedEntries.js.length > 0) {
284
- for (let i = 0; i < groupedEntries.js.length; i++) {
285
- const [file, chunk] = groupedEntries.js[i];
286
- if (isInclude(file)) {
287
- const code = tailwindcssMangleCore.jsHandler(chunk.source().toString(), {
288
- ...jsHandlerOptions,
289
- runtimeSet,
290
- classGenerator
291
- }).code;
292
- if (code) {
293
- const source = new ConcatSource(code);
294
- compilation.updateAsset(file, source);
295
- }
296
- }
297
- }
298
- }
299
- if (groupedEntries.css.length > 0) {
300
- for (let i = 0; i < groupedEntries.css.length; i++) {
301
- const [file, css] = groupedEntries.css[i];
302
- if (isInclude(file)) {
303
- const newCss = tailwindcssMangleCore.cssHandler(css.source().toString(), {
304
- ...cssHandlerOptions,
305
- classGenerator,
306
- runtimeSet
307
- });
308
- const source = new ConcatSource(newCss);
309
- compilation.updateAsset(file, source);
310
- }
311
- }
312
- }
313
- for (const [key, { js, html, css }] of outputCachedMap.entries()) {
314
- if (html.size > 0) {
315
- for (const [file, asset] of html.entries()) {
316
- if (isInclude(file)) {
317
- const html = tailwindcssMangleCore.htmlHandler(asset.source().toString(), {
318
- ...htmlHandlerOptions,
319
- classGenerator,
320
- runtimeSet
321
- });
322
- overwriteServerSideAsset(key, file, html);
323
- }
324
- }
325
- html.clear();
326
- }
327
- if (js.size > 0) {
328
- for (const [file, chunk] of js.entries()) {
329
- if (isInclude(file)) {
330
- const rawCode = chunk.source().toString();
331
- const code = tailwindcssMangleCore.jsHandler(rawCode, {
332
- ...jsHandlerOptions,
333
- runtimeSet,
334
- classGenerator
335
- }).code;
336
- if (code) {
337
- overwriteServerSideAsset(key, file, code);
338
- }
339
- }
340
- }
341
- js.clear();
342
- }
343
- if (css.size > 0) {
344
- for (const [file, style] of css.entries()) {
345
- if (isInclude(file)) {
346
- const newCss = tailwindcssMangleCore.cssHandler(style.source().toString(), {
347
- ...cssHandlerOptions,
348
- classGenerator,
349
- runtimeSet
350
- });
351
- overwriteServerSideAsset(key, file, newCss);
352
- }
353
- }
354
- css.clear();
355
- }
356
- }
357
- });
358
- });
359
- },
360
- writeBundle() {
361
- const entries = Object.entries(classGenerator.newClassMap);
362
- if (entries.length > 0 && classMapOutputOptions) {
363
- cacheDump(classMapOutputOptions.filename, entries.map((x) => {
364
- return [x[0], x[1].name];
365
- }), classMapOutputOptions.dir);
366
- }
367
- }
368
- };
369
- });
370
- const vitePlugin = unplugin.vite;
371
- const webpackPlugin = unplugin.webpack;
372
-
373
- Object.defineProperty(exports, 'defaultMangleClassFilter', {
374
- enumerable: true,
375
- get: function () { return tailwindcssMangleShared.defaultMangleClassFilter; }
376
- });
377
- exports.unplugin = unplugin;
378
- exports.vitePlugin = vitePlugin;
379
- exports.webpackPlugin = webpackPlugin;
@@ -1,6 +0,0 @@
1
- import * as webpack from 'webpack';
2
- import { ClassGenerator } from 'tailwindcss-mangle-core';
3
- export default function cssloader(this: webpack.LoaderContext<{
4
- classGenerator: ClassGenerator;
5
- getCachedClassSet: (() => Set<string>) | undefined;
6
- }>, content: string): string;
package/dist/nuxt.js DELETED
@@ -1,23 +0,0 @@
1
- 'use strict';
2
-
3
- var index = require('./index.js');
4
- require('unplugin');
5
- require('micromatch');
6
- require('node:fs');
7
- require('node:path');
8
- require('tailwindcss-mangle-shared');
9
- require('tailwindcss-mangle-core');
10
- require('tailwindcss-patch');
11
-
12
- function nuxt (options = {}, nuxt) {
13
- nuxt.hook('webpack:config', async (config) => {
14
- config.plugins = config.plugins || [];
15
- config.plugins.unshift(index.unplugin.webpack(options));
16
- });
17
- nuxt.hook('vite:extendConfig', async (config) => {
18
- config.plugins = config.plugins || [];
19
- config.plugins.push(index.unplugin.vite(options));
20
- });
21
- }
22
-
23
- module.exports = nuxt;
package/dist/options.d.ts DELETED
@@ -1,17 +0,0 @@
1
- import type { Options, ClassSetOutputOptions, ClassMapOutputOptions } from './types';
2
- import { TailwindcssPatcher } from 'tailwindcss-patch';
3
- import { ClassGenerator } from 'tailwindcss-mangle-core';
4
- import type { IHtmlHandlerOptions, IJsHandlerOptions, ICssHandlerOptions } from 'tailwindcss-mangle-core';
5
- export declare function getOptions(options?: Options | undefined): {
6
- getCachedClassSet: () => Set<string>;
7
- classGenerator: ClassGenerator;
8
- includeMatcher: (file: string) => boolean;
9
- excludeMatcher: (file: string) => boolean;
10
- isInclude: (file: string) => boolean;
11
- classSetOutputOptions: ClassSetOutputOptions;
12
- classMapOutputOptions: ClassMapOutputOptions;
13
- twPatcher: TailwindcssPatcher;
14
- jsHandlerOptions: IJsHandlerOptions;
15
- htmlHandlerOptions: IHtmlHandlerOptions;
16
- cssHandlerOptions: ICssHandlerOptions;
17
- };
package/dist/rollup.js DELETED
@@ -1,14 +0,0 @@
1
- 'use strict';
2
-
3
- var index = require('./index.js');
4
- require('unplugin');
5
- require('micromatch');
6
- require('node:fs');
7
- require('node:path');
8
- require('tailwindcss-mangle-shared');
9
- require('tailwindcss-mangle-core');
10
- require('tailwindcss-patch');
11
-
12
- var rollup = index.unplugin.rollup;
13
-
14
- module.exports = rollup;
package/dist/twm-css.js DELETED
@@ -1,19 +0,0 @@
1
- 'use strict';
2
-
3
- var tailwindcssMangleCore = require('tailwindcss-mangle-core');
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 tailwindcssMangleCore.cssHandler(content, {
11
- classGenerator: opt.classGenerator,
12
- runtimeSet,
13
- scene: 'loader'
14
- });
15
- }
16
- return content;
17
- }
18
-
19
- module.exports = cssloader;
package/dist/twm-css.mjs DELETED
@@ -1,17 +0,0 @@
1
- import { cssHandler } from 'tailwindcss-mangle-core';
2
-
3
- function cssloader(content) {
4
- this.cacheable && this.cacheable();
5
- const opt = this.getOptions();
6
- if (opt.getCachedClassSet) {
7
- const runtimeSet = opt.getCachedClassSet();
8
- return cssHandler(content, {
9
- classGenerator: opt.classGenerator,
10
- runtimeSet,
11
- scene: 'loader'
12
- });
13
- }
14
- return content;
15
- }
16
-
17
- export { cssloader as default };
package/dist/types.d.ts DELETED
@@ -1,31 +0,0 @@
1
- import type { IHandlerOptions, IHtmlHandlerOptions, IJsHandlerOptions, ICssHandlerOptions } from 'tailwindcss-mangle-core';
2
- export interface IClassGeneratorOptions {
3
- reserveClassName?: (string | RegExp)[];
4
- customGenerate?: (original: string, opts: IClassGeneratorOptions, context: Record<string, any>) => string | undefined;
5
- log?: boolean;
6
- exclude?: (string | RegExp)[];
7
- include?: (string | RegExp)[];
8
- ignoreClass?: (string | RegExp)[];
9
- classPrefix?: string;
10
- }
11
- export interface ClassSetOutputOptions {
12
- filename: string;
13
- dir?: string;
14
- type: 'all' | 'partial';
15
- }
16
- export interface ClassMapOutputOptions {
17
- filename: string;
18
- dir?: string;
19
- }
20
- export type PartialHandlerOptions<T extends IHandlerOptions> = Partial<Omit<T, 'runtimeSet' | 'classGenerator'>>;
21
- export interface Options {
22
- mangleClassFilter?: (className: string) => boolean;
23
- classGenerator?: IClassGeneratorOptions;
24
- exclude?: string[];
25
- include?: string[];
26
- classSetOutput?: boolean | ClassSetOutputOptions;
27
- classMapOutput?: boolean | ClassMapOutputOptions;
28
- htmlHandlerOptions?: PartialHandlerOptions<IHtmlHandlerOptions>;
29
- jsHandlerOptions?: PartialHandlerOptions<IJsHandlerOptions>;
30
- cssHandlerOptions?: PartialHandlerOptions<ICssHandlerOptions>;
31
- }
package/dist/utils.d.ts DELETED
@@ -1,10 +0,0 @@
1
- export declare function getGroupedEntries<T>(entries: [string, T][], options?: {
2
- cssMatcher(file: string): boolean;
3
- htmlMatcher(file: string): boolean;
4
- jsMatcher(file: string): boolean;
5
- }): Record<"css" | "html" | "js" | "other", [string, T][]>;
6
- export declare function createGlobMatcher(pattern: string | string[] | undefined, fallbackValue?: boolean): (file: string) => boolean;
7
- export declare function getCacheDir(basedir?: string): string;
8
- export declare function mkCacheDirectory(cwd?: string): string;
9
- export declare function cacheDump(filename: string, data: any[] | Set<any>, basedir?: string): void;
10
- export { defaultMangleClassFilter, isMap, isRegexp } from 'tailwindcss-mangle-shared';
package/dist/vite.js DELETED
@@ -1,14 +0,0 @@
1
- 'use strict';
2
-
3
- var index = require('./index.js');
4
- require('unplugin');
5
- require('micromatch');
6
- require('node:fs');
7
- require('node:path');
8
- require('tailwindcss-mangle-shared');
9
- require('tailwindcss-mangle-core');
10
- require('tailwindcss-patch');
11
-
12
- var vite = index.unplugin.vite;
13
-
14
- module.exports = vite;
package/dist/webpack.js DELETED
@@ -1,14 +0,0 @@
1
- 'use strict';
2
-
3
- var index = require('./index.js');
4
- require('unplugin');
5
- require('micromatch');
6
- require('node:fs');
7
- require('node:path');
8
- require('tailwindcss-mangle-shared');
9
- require('tailwindcss-mangle-core');
10
- require('tailwindcss-patch');
11
-
12
- var webpack = index.unplugin.webpack;
13
-
14
- module.exports = webpack;