@tailwindcss-mangle/core 5.0.1 → 5.1.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.cjs +227 -9
- package/dist/index.d.cts +15 -1
- package/dist/index.d.ts +15 -1
- package/dist/index.js +224 -8
- package/package.json +14 -8
package/dist/index.cjs
CHANGED
|
@@ -36,7 +36,9 @@ __export(index_exports, {
|
|
|
36
36
|
cssHandler: () => cssHandler,
|
|
37
37
|
handleValue: () => handleValue,
|
|
38
38
|
htmlHandler: () => htmlHandler,
|
|
39
|
-
jsHandler: () => jsHandler
|
|
39
|
+
jsHandler: () => jsHandler,
|
|
40
|
+
svelteHandler: () => svelteHandler,
|
|
41
|
+
vueHandler: () => vueHandler
|
|
40
42
|
});
|
|
41
43
|
module.exports = __toCommonJS(index_exports);
|
|
42
44
|
|
|
@@ -164,11 +166,9 @@ transformSelectorPostcssPlugin.postcss = true;
|
|
|
164
166
|
async function cssHandler(rawSource, options) {
|
|
165
167
|
const acceptedPlugins = [transformSelectorPostcssPlugin(options)];
|
|
166
168
|
const { id } = options;
|
|
169
|
+
const processOptions = id === void 0 ? {} : { from: id, to: id };
|
|
167
170
|
try {
|
|
168
|
-
const { css: code, map } = await (0, import_postcss.default)(acceptedPlugins).process(rawSource,
|
|
169
|
-
from: id,
|
|
170
|
-
to: id
|
|
171
|
-
});
|
|
171
|
+
const { css: code, map } = await (0, import_postcss.default)(acceptedPlugins).process(rawSource, processOptions);
|
|
172
172
|
return {
|
|
173
173
|
code,
|
|
174
174
|
// @ts-ignore
|
|
@@ -264,7 +264,7 @@ var Context = class {
|
|
|
264
264
|
const { cwd, classList: _classList, transformerOptions } = opts;
|
|
265
265
|
const { config, cwd: configCwd } = await (0, import_config.getConfig)(cwd);
|
|
266
266
|
this.configRoot = configCwd ?? cwd ?? import_node_process.default.cwd();
|
|
267
|
-
|
|
267
|
+
const normalizedTransformer = transformerOptions ? { ...transformerOptions, registry: { ...transformerOptions.registry } } : void 0;
|
|
268
268
|
if (normalizedTransformer?.registry?.mapping === true) {
|
|
269
269
|
const fallback = config?.transformer?.registry?.mapping;
|
|
270
270
|
if (typeof fallback === "function") {
|
|
@@ -347,7 +347,9 @@ function htmlHandler(raw, options) {
|
|
|
347
347
|
if (replaceMap.has(v)) {
|
|
348
348
|
const gen = classGenerator.generateClassName(v);
|
|
349
349
|
rawValue = rawValue.replace((0, shared_exports.makeRegex)(v), gen.name);
|
|
350
|
-
|
|
350
|
+
if (id) {
|
|
351
|
+
ctx.addToUsedBy(v, id);
|
|
352
|
+
}
|
|
351
353
|
needUpdate = true;
|
|
352
354
|
}
|
|
353
355
|
}
|
|
@@ -415,7 +417,8 @@ function jsHandler(rawSource, options) {
|
|
|
415
417
|
let ast;
|
|
416
418
|
try {
|
|
417
419
|
ast = (0, import_parser.parse)(ms.original, {
|
|
418
|
-
sourceType: "unambiguous"
|
|
420
|
+
sourceType: "unambiguous",
|
|
421
|
+
plugins: ["jsx", "typescript"]
|
|
419
422
|
});
|
|
420
423
|
} catch {
|
|
421
424
|
return {
|
|
@@ -494,6 +497,219 @@ function jsHandler(rawSource, options) {
|
|
|
494
497
|
}
|
|
495
498
|
};
|
|
496
499
|
}
|
|
500
|
+
|
|
501
|
+
// src/svelte/index.ts
|
|
502
|
+
var import_magic_string3 = __toESM(require("magic-string"), 1);
|
|
503
|
+
var import_compiler = require("svelte/compiler");
|
|
504
|
+
async function svelteHandler(rawSource, options) {
|
|
505
|
+
const { ctx, id } = options;
|
|
506
|
+
const ms = new import_magic_string3.default(rawSource);
|
|
507
|
+
try {
|
|
508
|
+
const ast = (0, import_compiler.parse)(rawSource, {
|
|
509
|
+
filename: id || "unknown.svelte"
|
|
510
|
+
});
|
|
511
|
+
await processSvelteAst(ast, ms, ctx, id);
|
|
512
|
+
return {
|
|
513
|
+
code: ms.toString(),
|
|
514
|
+
get map() {
|
|
515
|
+
return ms.generateMap();
|
|
516
|
+
}
|
|
517
|
+
};
|
|
518
|
+
} catch (error) {
|
|
519
|
+
return jsHandler(rawSource, options);
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
async function processSvelteAst(ast, ms, ctx, id) {
|
|
523
|
+
const { replaceMap, classGenerator } = ctx;
|
|
524
|
+
const stylePromises = [];
|
|
525
|
+
async function walk(node) {
|
|
526
|
+
if (!node) {
|
|
527
|
+
return;
|
|
528
|
+
}
|
|
529
|
+
if (node.type === "Attribute" && node.name === "class") {
|
|
530
|
+
for (const attrValue of node.value) {
|
|
531
|
+
if (attrValue.type === "Text") {
|
|
532
|
+
const classValue = attrValue.data;
|
|
533
|
+
const arr = (0, shared_exports.splitCode)(classValue, { splitQuote: false });
|
|
534
|
+
let newValue = classValue;
|
|
535
|
+
let needUpdate = false;
|
|
536
|
+
for (const v of arr) {
|
|
537
|
+
if (replaceMap.has(v)) {
|
|
538
|
+
const gen = classGenerator.generateClassName(v);
|
|
539
|
+
newValue = newValue.replace((0, shared_exports.makeRegex)(v), gen.name);
|
|
540
|
+
if (id) {
|
|
541
|
+
ctx.addToUsedBy(v, id);
|
|
542
|
+
}
|
|
543
|
+
needUpdate = true;
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
if (needUpdate) {
|
|
547
|
+
const start = attrValue.start;
|
|
548
|
+
const end = attrValue.end;
|
|
549
|
+
ms.update(start, end, newValue);
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
if (node.type === "ClassDirective") {
|
|
555
|
+
const className = node.name;
|
|
556
|
+
if (replaceMap.has(className)) {
|
|
557
|
+
if (id) {
|
|
558
|
+
ctx.addToUsedBy(className, id);
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
if (node.type === "Script") {
|
|
563
|
+
const contentStart = node.content.start;
|
|
564
|
+
const contentEnd = node.content.end;
|
|
565
|
+
const innerContent = ms.original.slice(contentStart, contentEnd);
|
|
566
|
+
const jsHandlerOptions = id === void 0 ? { ctx } : { ctx, id };
|
|
567
|
+
const result = jsHandler(innerContent, jsHandlerOptions);
|
|
568
|
+
if (result.code !== innerContent) {
|
|
569
|
+
ms.update(contentStart, contentEnd, result.code);
|
|
570
|
+
}
|
|
571
|
+
return;
|
|
572
|
+
}
|
|
573
|
+
if (node.type === "Style") {
|
|
574
|
+
const contentStart = node.content.start;
|
|
575
|
+
const contentEnd = node.content.end;
|
|
576
|
+
const innerContent = ms.original.slice(contentStart, contentEnd);
|
|
577
|
+
const cssHandlerOptions = id === void 0 ? { ctx } : { ctx, id };
|
|
578
|
+
const promise = cssHandler(innerContent, cssHandlerOptions).then((result) => {
|
|
579
|
+
if (result.code !== innerContent) {
|
|
580
|
+
ms.update(contentStart, contentEnd, result.code);
|
|
581
|
+
}
|
|
582
|
+
});
|
|
583
|
+
stylePromises.push(promise);
|
|
584
|
+
return;
|
|
585
|
+
}
|
|
586
|
+
if (node.children) {
|
|
587
|
+
for (const child of node.children) {
|
|
588
|
+
await walk(child);
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
await walk(ast);
|
|
593
|
+
await Promise.all(stylePromises);
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
// src/vue/index.ts
|
|
597
|
+
var import_compiler_sfc = require("@vue/compiler-sfc");
|
|
598
|
+
var import_magic_string4 = __toESM(require("magic-string"), 1);
|
|
599
|
+
async function vueHandler(rawSource, options) {
|
|
600
|
+
const { ctx, id } = options;
|
|
601
|
+
const ms = new import_magic_string4.default(rawSource);
|
|
602
|
+
try {
|
|
603
|
+
const { descriptor } = (0, import_compiler_sfc.parse)(rawSource, {
|
|
604
|
+
filename: id || "unknown.vue"
|
|
605
|
+
});
|
|
606
|
+
if (descriptor.template) {
|
|
607
|
+
await processTemplate(descriptor.template, ms, ctx, id);
|
|
608
|
+
}
|
|
609
|
+
if (descriptor.script || descriptor.scriptSetup) {
|
|
610
|
+
await processScript(descriptor, ms, ctx, id);
|
|
611
|
+
}
|
|
612
|
+
if (descriptor.styles && descriptor.styles.length > 0) {
|
|
613
|
+
await processStyles(descriptor.styles, ms, ctx, id);
|
|
614
|
+
}
|
|
615
|
+
return {
|
|
616
|
+
code: ms.toString(),
|
|
617
|
+
get map() {
|
|
618
|
+
return ms.generateMap();
|
|
619
|
+
}
|
|
620
|
+
};
|
|
621
|
+
} catch (error) {
|
|
622
|
+
return jsHandler(rawSource, options);
|
|
623
|
+
}
|
|
624
|
+
}
|
|
625
|
+
async function processTemplate(template, ms, ctx, id) {
|
|
626
|
+
const { replaceMap, classGenerator } = ctx;
|
|
627
|
+
if (!template.ast) {
|
|
628
|
+
try {
|
|
629
|
+
const compiled = (0, import_compiler_sfc.compileTemplate)({
|
|
630
|
+
source: template.content,
|
|
631
|
+
filename: id || "unknown.vue",
|
|
632
|
+
id: `${id || "unknown"}?template`
|
|
633
|
+
});
|
|
634
|
+
} catch {
|
|
635
|
+
return;
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
const classAttrRegex = /\sclass\s*=\s*["']([^"']+)["']/g;
|
|
639
|
+
let match;
|
|
640
|
+
const templateStart = template.loc.start.offset;
|
|
641
|
+
const templateEnd = template.loc.end.offset;
|
|
642
|
+
const templateContent = ms.original.slice(templateStart, templateEnd);
|
|
643
|
+
const replacements = [];
|
|
644
|
+
while ((match = classAttrRegex.exec(templateContent)) !== null) {
|
|
645
|
+
const fullMatch = match[0];
|
|
646
|
+
const classValue = match[1];
|
|
647
|
+
if (classValue === void 0) {
|
|
648
|
+
continue;
|
|
649
|
+
}
|
|
650
|
+
const offset = match.index;
|
|
651
|
+
const arr = (0, shared_exports.splitCode)(classValue, { splitQuote: false });
|
|
652
|
+
let newValue = classValue;
|
|
653
|
+
let needUpdate = false;
|
|
654
|
+
for (const v of arr) {
|
|
655
|
+
if (replaceMap.has(v)) {
|
|
656
|
+
const gen = classGenerator.generateClassName(v);
|
|
657
|
+
newValue = newValue.replace((0, shared_exports.makeRegex)(v), gen.name);
|
|
658
|
+
if (id) {
|
|
659
|
+
ctx.addToUsedBy(v, id);
|
|
660
|
+
}
|
|
661
|
+
needUpdate = true;
|
|
662
|
+
}
|
|
663
|
+
}
|
|
664
|
+
if (needUpdate) {
|
|
665
|
+
const classValueStart = fullMatch.indexOf(classValue);
|
|
666
|
+
replacements.push({
|
|
667
|
+
start: templateStart + offset + classValueStart,
|
|
668
|
+
end: templateStart + offset + classValueStart + classValue.length,
|
|
669
|
+
value: newValue
|
|
670
|
+
});
|
|
671
|
+
}
|
|
672
|
+
}
|
|
673
|
+
for (const replacement of replacements) {
|
|
674
|
+
ms.update(replacement.start, replacement.end, replacement.value);
|
|
675
|
+
}
|
|
676
|
+
}
|
|
677
|
+
async function processScript(descriptor, ms, ctx, id) {
|
|
678
|
+
const script = descriptor.scriptSetup || descriptor.script;
|
|
679
|
+
if (!script) {
|
|
680
|
+
return;
|
|
681
|
+
}
|
|
682
|
+
const scriptContent = ms.original.slice(
|
|
683
|
+
script.loc.start.offset,
|
|
684
|
+
script.loc.end.offset
|
|
685
|
+
);
|
|
686
|
+
const jsHandlerOptions = id === void 0 ? { ctx } : { ctx, id };
|
|
687
|
+
const result = jsHandler(scriptContent, jsHandlerOptions);
|
|
688
|
+
if (result.code !== scriptContent) {
|
|
689
|
+
ms.update(
|
|
690
|
+
script.loc.start.offset,
|
|
691
|
+
script.loc.end.offset,
|
|
692
|
+
result.code
|
|
693
|
+
);
|
|
694
|
+
}
|
|
695
|
+
}
|
|
696
|
+
async function processStyles(styles, ms, ctx, id) {
|
|
697
|
+
for (const style of styles) {
|
|
698
|
+
const styleContent = ms.original.slice(
|
|
699
|
+
style.loc.start.offset,
|
|
700
|
+
style.loc.end.offset
|
|
701
|
+
);
|
|
702
|
+
const cssHandlerOptions = id === void 0 ? { ctx, ignoreVueScoped: style.scoped } : { ctx, id, ignoreVueScoped: style.scoped };
|
|
703
|
+
const result = await cssHandler(styleContent, cssHandlerOptions);
|
|
704
|
+
if (result.code !== styleContent) {
|
|
705
|
+
ms.update(
|
|
706
|
+
style.loc.start.offset,
|
|
707
|
+
style.loc.end.offset,
|
|
708
|
+
result.code
|
|
709
|
+
);
|
|
710
|
+
}
|
|
711
|
+
}
|
|
712
|
+
}
|
|
497
713
|
// Annotate the CommonJS export names for ESM import in node:
|
|
498
714
|
0 && (module.exports = {
|
|
499
715
|
ClassGenerator,
|
|
@@ -501,5 +717,7 @@ function jsHandler(rawSource, options) {
|
|
|
501
717
|
cssHandler,
|
|
502
718
|
handleValue,
|
|
503
719
|
htmlHandler,
|
|
504
|
-
jsHandler
|
|
720
|
+
jsHandler,
|
|
721
|
+
svelteHandler,
|
|
722
|
+
vueHandler
|
|
505
723
|
});
|
package/dist/index.d.cts
CHANGED
|
@@ -63,6 +63,11 @@ interface IJsHandlerOptions extends IHandlerOptions {
|
|
|
63
63
|
interface ICssHandlerOptions extends IHandlerOptions {
|
|
64
64
|
ignoreVueScoped?: boolean;
|
|
65
65
|
}
|
|
66
|
+
interface IVueHandlerOptions$1 extends IJsHandlerOptions {
|
|
67
|
+
preserveScoped?: boolean;
|
|
68
|
+
}
|
|
69
|
+
interface ISvelteHandlerOptions$1 extends IJsHandlerOptions {
|
|
70
|
+
}
|
|
66
71
|
|
|
67
72
|
declare function cssHandler(rawSource: string, options: ICssHandlerOptions): Promise<IHandlerTransformResult>;
|
|
68
73
|
|
|
@@ -71,4 +76,13 @@ declare function htmlHandler(raw: string | MagicString, options: IHtmlHandlerOpt
|
|
|
71
76
|
declare function handleValue(raw: string, node: StringLiteral | TemplateElement, options: IJsHandlerOptions, ms: MagicString, offset: number, escape: boolean): string;
|
|
72
77
|
declare function jsHandler(rawSource: string | MagicString, options: IJsHandlerOptions): IHandlerTransformResult;
|
|
73
78
|
|
|
74
|
-
|
|
79
|
+
interface ISvelteHandlerOptions extends IJsHandlerOptions {
|
|
80
|
+
}
|
|
81
|
+
declare function svelteHandler(rawSource: string, options: ISvelteHandlerOptions): Promise<IHandlerTransformResult>;
|
|
82
|
+
|
|
83
|
+
interface IVueHandlerOptions extends IJsHandlerOptions {
|
|
84
|
+
preserveScoped?: boolean;
|
|
85
|
+
}
|
|
86
|
+
declare function vueHandler(rawSource: string, options: IVueHandlerOptions): Promise<IHandlerTransformResult>;
|
|
87
|
+
|
|
88
|
+
export { Context, type IClassGeneratorContextItem, type IClassGeneratorOptions, type ICssHandlerOptions, type IHandler, type IHandlerOptions, type IHandlerTransformResult, type IHtmlHandlerOptions, type IJsHandlerOptions, type ISvelteHandlerOptions$1 as ISvelteHandlerOptions, type IVueHandlerOptions$1 as IVueHandlerOptions, cssHandler, handleValue, htmlHandler, jsHandler, svelteHandler, vueHandler };
|
package/dist/index.d.ts
CHANGED
|
@@ -63,6 +63,11 @@ interface IJsHandlerOptions extends IHandlerOptions {
|
|
|
63
63
|
interface ICssHandlerOptions extends IHandlerOptions {
|
|
64
64
|
ignoreVueScoped?: boolean;
|
|
65
65
|
}
|
|
66
|
+
interface IVueHandlerOptions$1 extends IJsHandlerOptions {
|
|
67
|
+
preserveScoped?: boolean;
|
|
68
|
+
}
|
|
69
|
+
interface ISvelteHandlerOptions$1 extends IJsHandlerOptions {
|
|
70
|
+
}
|
|
66
71
|
|
|
67
72
|
declare function cssHandler(rawSource: string, options: ICssHandlerOptions): Promise<IHandlerTransformResult>;
|
|
68
73
|
|
|
@@ -71,4 +76,13 @@ declare function htmlHandler(raw: string | MagicString, options: IHtmlHandlerOpt
|
|
|
71
76
|
declare function handleValue(raw: string, node: StringLiteral | TemplateElement, options: IJsHandlerOptions, ms: MagicString, offset: number, escape: boolean): string;
|
|
72
77
|
declare function jsHandler(rawSource: string | MagicString, options: IJsHandlerOptions): IHandlerTransformResult;
|
|
73
78
|
|
|
74
|
-
|
|
79
|
+
interface ISvelteHandlerOptions extends IJsHandlerOptions {
|
|
80
|
+
}
|
|
81
|
+
declare function svelteHandler(rawSource: string, options: ISvelteHandlerOptions): Promise<IHandlerTransformResult>;
|
|
82
|
+
|
|
83
|
+
interface IVueHandlerOptions extends IJsHandlerOptions {
|
|
84
|
+
preserveScoped?: boolean;
|
|
85
|
+
}
|
|
86
|
+
declare function vueHandler(rawSource: string, options: IVueHandlerOptions): Promise<IHandlerTransformResult>;
|
|
87
|
+
|
|
88
|
+
export { Context, type IClassGeneratorContextItem, type IClassGeneratorOptions, type ICssHandlerOptions, type IHandler, type IHandlerOptions, type IHandlerTransformResult, type IHtmlHandlerOptions, type IJsHandlerOptions, type ISvelteHandlerOptions$1 as ISvelteHandlerOptions, type IVueHandlerOptions$1 as IVueHandlerOptions, cssHandler, handleValue, htmlHandler, jsHandler, svelteHandler, vueHandler };
|
package/dist/index.js
CHANGED
|
@@ -136,11 +136,9 @@ transformSelectorPostcssPlugin.postcss = true;
|
|
|
136
136
|
async function cssHandler(rawSource, options) {
|
|
137
137
|
const acceptedPlugins = [transformSelectorPostcssPlugin(options)];
|
|
138
138
|
const { id } = options;
|
|
139
|
+
const processOptions = id === void 0 ? {} : { from: id, to: id };
|
|
139
140
|
try {
|
|
140
|
-
const { css: code, map } = await postcss(acceptedPlugins).process(rawSource,
|
|
141
|
-
from: id,
|
|
142
|
-
to: id
|
|
143
|
-
});
|
|
141
|
+
const { css: code, map } = await postcss(acceptedPlugins).process(rawSource, processOptions);
|
|
144
142
|
return {
|
|
145
143
|
code,
|
|
146
144
|
// @ts-ignore
|
|
@@ -237,7 +235,7 @@ var Context = class {
|
|
|
237
235
|
const { cwd, classList: _classList, transformerOptions } = opts;
|
|
238
236
|
const { config, cwd: configCwd } = await getConfig(cwd);
|
|
239
237
|
this.configRoot = configCwd ?? cwd ?? process.cwd();
|
|
240
|
-
|
|
238
|
+
const normalizedTransformer = transformerOptions ? { ...transformerOptions, registry: { ...transformerOptions.registry } } : void 0;
|
|
241
239
|
if (normalizedTransformer?.registry?.mapping === true) {
|
|
242
240
|
const fallback = config?.transformer?.registry?.mapping;
|
|
243
241
|
if (typeof fallback === "function") {
|
|
@@ -320,7 +318,9 @@ function htmlHandler(raw, options) {
|
|
|
320
318
|
if (replaceMap.has(v)) {
|
|
321
319
|
const gen = classGenerator.generateClassName(v);
|
|
322
320
|
rawValue = rawValue.replace((0, shared_exports.makeRegex)(v), gen.name);
|
|
323
|
-
|
|
321
|
+
if (id) {
|
|
322
|
+
ctx.addToUsedBy(v, id);
|
|
323
|
+
}
|
|
324
324
|
needUpdate = true;
|
|
325
325
|
}
|
|
326
326
|
}
|
|
@@ -388,7 +388,8 @@ function jsHandler(rawSource, options) {
|
|
|
388
388
|
let ast;
|
|
389
389
|
try {
|
|
390
390
|
ast = parse(ms.original, {
|
|
391
|
-
sourceType: "unambiguous"
|
|
391
|
+
sourceType: "unambiguous",
|
|
392
|
+
plugins: ["jsx", "typescript"]
|
|
392
393
|
});
|
|
393
394
|
} catch {
|
|
394
395
|
return {
|
|
@@ -467,6 +468,219 @@ function jsHandler(rawSource, options) {
|
|
|
467
468
|
}
|
|
468
469
|
};
|
|
469
470
|
}
|
|
471
|
+
|
|
472
|
+
// src/svelte/index.ts
|
|
473
|
+
import MagicString3 from "magic-string";
|
|
474
|
+
import { parse as parse2 } from "svelte/compiler";
|
|
475
|
+
async function svelteHandler(rawSource, options) {
|
|
476
|
+
const { ctx, id } = options;
|
|
477
|
+
const ms = new MagicString3(rawSource);
|
|
478
|
+
try {
|
|
479
|
+
const ast = parse2(rawSource, {
|
|
480
|
+
filename: id || "unknown.svelte"
|
|
481
|
+
});
|
|
482
|
+
await processSvelteAst(ast, ms, ctx, id);
|
|
483
|
+
return {
|
|
484
|
+
code: ms.toString(),
|
|
485
|
+
get map() {
|
|
486
|
+
return ms.generateMap();
|
|
487
|
+
}
|
|
488
|
+
};
|
|
489
|
+
} catch (error) {
|
|
490
|
+
return jsHandler(rawSource, options);
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
async function processSvelteAst(ast, ms, ctx, id) {
|
|
494
|
+
const { replaceMap, classGenerator } = ctx;
|
|
495
|
+
const stylePromises = [];
|
|
496
|
+
async function walk(node) {
|
|
497
|
+
if (!node) {
|
|
498
|
+
return;
|
|
499
|
+
}
|
|
500
|
+
if (node.type === "Attribute" && node.name === "class") {
|
|
501
|
+
for (const attrValue of node.value) {
|
|
502
|
+
if (attrValue.type === "Text") {
|
|
503
|
+
const classValue = attrValue.data;
|
|
504
|
+
const arr = (0, shared_exports.splitCode)(classValue, { splitQuote: false });
|
|
505
|
+
let newValue = classValue;
|
|
506
|
+
let needUpdate = false;
|
|
507
|
+
for (const v of arr) {
|
|
508
|
+
if (replaceMap.has(v)) {
|
|
509
|
+
const gen = classGenerator.generateClassName(v);
|
|
510
|
+
newValue = newValue.replace((0, shared_exports.makeRegex)(v), gen.name);
|
|
511
|
+
if (id) {
|
|
512
|
+
ctx.addToUsedBy(v, id);
|
|
513
|
+
}
|
|
514
|
+
needUpdate = true;
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
if (needUpdate) {
|
|
518
|
+
const start = attrValue.start;
|
|
519
|
+
const end = attrValue.end;
|
|
520
|
+
ms.update(start, end, newValue);
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
if (node.type === "ClassDirective") {
|
|
526
|
+
const className = node.name;
|
|
527
|
+
if (replaceMap.has(className)) {
|
|
528
|
+
if (id) {
|
|
529
|
+
ctx.addToUsedBy(className, id);
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
if (node.type === "Script") {
|
|
534
|
+
const contentStart = node.content.start;
|
|
535
|
+
const contentEnd = node.content.end;
|
|
536
|
+
const innerContent = ms.original.slice(contentStart, contentEnd);
|
|
537
|
+
const jsHandlerOptions = id === void 0 ? { ctx } : { ctx, id };
|
|
538
|
+
const result = jsHandler(innerContent, jsHandlerOptions);
|
|
539
|
+
if (result.code !== innerContent) {
|
|
540
|
+
ms.update(contentStart, contentEnd, result.code);
|
|
541
|
+
}
|
|
542
|
+
return;
|
|
543
|
+
}
|
|
544
|
+
if (node.type === "Style") {
|
|
545
|
+
const contentStart = node.content.start;
|
|
546
|
+
const contentEnd = node.content.end;
|
|
547
|
+
const innerContent = ms.original.slice(contentStart, contentEnd);
|
|
548
|
+
const cssHandlerOptions = id === void 0 ? { ctx } : { ctx, id };
|
|
549
|
+
const promise = cssHandler(innerContent, cssHandlerOptions).then((result) => {
|
|
550
|
+
if (result.code !== innerContent) {
|
|
551
|
+
ms.update(contentStart, contentEnd, result.code);
|
|
552
|
+
}
|
|
553
|
+
});
|
|
554
|
+
stylePromises.push(promise);
|
|
555
|
+
return;
|
|
556
|
+
}
|
|
557
|
+
if (node.children) {
|
|
558
|
+
for (const child of node.children) {
|
|
559
|
+
await walk(child);
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
await walk(ast);
|
|
564
|
+
await Promise.all(stylePromises);
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
// src/vue/index.ts
|
|
568
|
+
import { compileTemplate, parse as parse3 } from "@vue/compiler-sfc";
|
|
569
|
+
import MagicString4 from "magic-string";
|
|
570
|
+
async function vueHandler(rawSource, options) {
|
|
571
|
+
const { ctx, id } = options;
|
|
572
|
+
const ms = new MagicString4(rawSource);
|
|
573
|
+
try {
|
|
574
|
+
const { descriptor } = parse3(rawSource, {
|
|
575
|
+
filename: id || "unknown.vue"
|
|
576
|
+
});
|
|
577
|
+
if (descriptor.template) {
|
|
578
|
+
await processTemplate(descriptor.template, ms, ctx, id);
|
|
579
|
+
}
|
|
580
|
+
if (descriptor.script || descriptor.scriptSetup) {
|
|
581
|
+
await processScript(descriptor, ms, ctx, id);
|
|
582
|
+
}
|
|
583
|
+
if (descriptor.styles && descriptor.styles.length > 0) {
|
|
584
|
+
await processStyles(descriptor.styles, ms, ctx, id);
|
|
585
|
+
}
|
|
586
|
+
return {
|
|
587
|
+
code: ms.toString(),
|
|
588
|
+
get map() {
|
|
589
|
+
return ms.generateMap();
|
|
590
|
+
}
|
|
591
|
+
};
|
|
592
|
+
} catch (error) {
|
|
593
|
+
return jsHandler(rawSource, options);
|
|
594
|
+
}
|
|
595
|
+
}
|
|
596
|
+
async function processTemplate(template, ms, ctx, id) {
|
|
597
|
+
const { replaceMap, classGenerator } = ctx;
|
|
598
|
+
if (!template.ast) {
|
|
599
|
+
try {
|
|
600
|
+
const compiled = compileTemplate({
|
|
601
|
+
source: template.content,
|
|
602
|
+
filename: id || "unknown.vue",
|
|
603
|
+
id: `${id || "unknown"}?template`
|
|
604
|
+
});
|
|
605
|
+
} catch {
|
|
606
|
+
return;
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
const classAttrRegex = /\sclass\s*=\s*["']([^"']+)["']/g;
|
|
610
|
+
let match;
|
|
611
|
+
const templateStart = template.loc.start.offset;
|
|
612
|
+
const templateEnd = template.loc.end.offset;
|
|
613
|
+
const templateContent = ms.original.slice(templateStart, templateEnd);
|
|
614
|
+
const replacements = [];
|
|
615
|
+
while ((match = classAttrRegex.exec(templateContent)) !== null) {
|
|
616
|
+
const fullMatch = match[0];
|
|
617
|
+
const classValue = match[1];
|
|
618
|
+
if (classValue === void 0) {
|
|
619
|
+
continue;
|
|
620
|
+
}
|
|
621
|
+
const offset = match.index;
|
|
622
|
+
const arr = (0, shared_exports.splitCode)(classValue, { splitQuote: false });
|
|
623
|
+
let newValue = classValue;
|
|
624
|
+
let needUpdate = false;
|
|
625
|
+
for (const v of arr) {
|
|
626
|
+
if (replaceMap.has(v)) {
|
|
627
|
+
const gen = classGenerator.generateClassName(v);
|
|
628
|
+
newValue = newValue.replace((0, shared_exports.makeRegex)(v), gen.name);
|
|
629
|
+
if (id) {
|
|
630
|
+
ctx.addToUsedBy(v, id);
|
|
631
|
+
}
|
|
632
|
+
needUpdate = true;
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
if (needUpdate) {
|
|
636
|
+
const classValueStart = fullMatch.indexOf(classValue);
|
|
637
|
+
replacements.push({
|
|
638
|
+
start: templateStart + offset + classValueStart,
|
|
639
|
+
end: templateStart + offset + classValueStart + classValue.length,
|
|
640
|
+
value: newValue
|
|
641
|
+
});
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
for (const replacement of replacements) {
|
|
645
|
+
ms.update(replacement.start, replacement.end, replacement.value);
|
|
646
|
+
}
|
|
647
|
+
}
|
|
648
|
+
async function processScript(descriptor, ms, ctx, id) {
|
|
649
|
+
const script = descriptor.scriptSetup || descriptor.script;
|
|
650
|
+
if (!script) {
|
|
651
|
+
return;
|
|
652
|
+
}
|
|
653
|
+
const scriptContent = ms.original.slice(
|
|
654
|
+
script.loc.start.offset,
|
|
655
|
+
script.loc.end.offset
|
|
656
|
+
);
|
|
657
|
+
const jsHandlerOptions = id === void 0 ? { ctx } : { ctx, id };
|
|
658
|
+
const result = jsHandler(scriptContent, jsHandlerOptions);
|
|
659
|
+
if (result.code !== scriptContent) {
|
|
660
|
+
ms.update(
|
|
661
|
+
script.loc.start.offset,
|
|
662
|
+
script.loc.end.offset,
|
|
663
|
+
result.code
|
|
664
|
+
);
|
|
665
|
+
}
|
|
666
|
+
}
|
|
667
|
+
async function processStyles(styles, ms, ctx, id) {
|
|
668
|
+
for (const style of styles) {
|
|
669
|
+
const styleContent = ms.original.slice(
|
|
670
|
+
style.loc.start.offset,
|
|
671
|
+
style.loc.end.offset
|
|
672
|
+
);
|
|
673
|
+
const cssHandlerOptions = id === void 0 ? { ctx, ignoreVueScoped: style.scoped } : { ctx, id, ignoreVueScoped: style.scoped };
|
|
674
|
+
const result = await cssHandler(styleContent, cssHandlerOptions);
|
|
675
|
+
if (result.code !== styleContent) {
|
|
676
|
+
ms.update(
|
|
677
|
+
style.loc.start.offset,
|
|
678
|
+
style.loc.end.offset,
|
|
679
|
+
result.code
|
|
680
|
+
);
|
|
681
|
+
}
|
|
682
|
+
}
|
|
683
|
+
}
|
|
470
684
|
var export_ClassGenerator = shared_exports.ClassGenerator;
|
|
471
685
|
export {
|
|
472
686
|
export_ClassGenerator as ClassGenerator,
|
|
@@ -474,5 +688,7 @@ export {
|
|
|
474
688
|
cssHandler,
|
|
475
689
|
handleValue,
|
|
476
690
|
htmlHandler,
|
|
477
|
-
jsHandler
|
|
691
|
+
jsHandler,
|
|
692
|
+
svelteHandler,
|
|
693
|
+
vueHandler
|
|
478
694
|
};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tailwindcss-mangle/core",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "5.
|
|
4
|
+
"version": "5.1.1",
|
|
5
5
|
"description": "The core of tailwindcss-mangle",
|
|
6
6
|
"author": "ice breaker <1324318532@qq.com>",
|
|
7
7
|
"license": "MIT",
|
|
@@ -47,18 +47,24 @@
|
|
|
47
47
|
},
|
|
48
48
|
"dependencies": {
|
|
49
49
|
"@ast-core/escape": "^1.0.1",
|
|
50
|
-
"@babel/parser": "^7.
|
|
51
|
-
"@babel/traverse": "^7.
|
|
52
|
-
"@babel/types": "^7.
|
|
50
|
+
"@babel/parser": "^7.29.0",
|
|
51
|
+
"@babel/traverse": "^7.29.0",
|
|
52
|
+
"@babel/types": "^7.29.0",
|
|
53
|
+
"@vue/compiler-sfc": "^3.5.28",
|
|
53
54
|
"fast-sort": "^3.4.1",
|
|
54
|
-
"fs-extra": "^11.3.
|
|
55
|
-
"htmlparser2": "10.
|
|
55
|
+
"fs-extra": "^11.3.3",
|
|
56
|
+
"htmlparser2": "10.1.0",
|
|
56
57
|
"magic-string": "^0.30.21",
|
|
58
|
+
"parse5": "^8.0.0",
|
|
57
59
|
"pathe": "^2.0.3",
|
|
58
60
|
"postcss": "^8.5.6",
|
|
59
61
|
"postcss-selector-parser": "^7.1.1",
|
|
60
|
-
"
|
|
61
|
-
"@tailwindcss-mangle/
|
|
62
|
+
"svelte": "^5.53.3",
|
|
63
|
+
"@tailwindcss-mangle/config": "^6.1.2",
|
|
64
|
+
"@tailwindcss-mangle/shared": "^4.1.2"
|
|
65
|
+
},
|
|
66
|
+
"devDependencies": {
|
|
67
|
+
"@types/parse5": "^7.0.0"
|
|
62
68
|
},
|
|
63
69
|
"scripts": {
|
|
64
70
|
"dev": "tsup --watch --sourcemap",
|