@tailwindcss-mangle/core 5.0.0 → 5.1.0
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 +222 -8
- package/dist/index.d.cts +15 -1
- package/dist/index.d.ts +15 -1
- package/dist/index.js +219 -7
- 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
|
|
@@ -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,215 @@ function jsHandler(rawSource, options) {
|
|
|
494
497
|
}
|
|
495
498
|
};
|
|
496
499
|
}
|
|
500
|
+
|
|
501
|
+
// src/svelte/index.ts
|
|
502
|
+
var import_compiler = require("svelte/compiler");
|
|
503
|
+
var import_magic_string3 = __toESM(require("magic-string"), 1);
|
|
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) return;
|
|
527
|
+
if (node.type === "Attribute" && node.name === "class") {
|
|
528
|
+
for (const attrValue of node.value) {
|
|
529
|
+
if (attrValue.type === "Text") {
|
|
530
|
+
const classValue = attrValue.data;
|
|
531
|
+
const arr = (0, shared_exports.splitCode)(classValue, { splitQuote: false });
|
|
532
|
+
let newValue = classValue;
|
|
533
|
+
let needUpdate = false;
|
|
534
|
+
for (const v of arr) {
|
|
535
|
+
if (replaceMap.has(v)) {
|
|
536
|
+
const gen = classGenerator.generateClassName(v);
|
|
537
|
+
newValue = newValue.replace((0, shared_exports.makeRegex)(v), gen.name);
|
|
538
|
+
if (id) {
|
|
539
|
+
ctx.addToUsedBy(v, id);
|
|
540
|
+
}
|
|
541
|
+
needUpdate = true;
|
|
542
|
+
}
|
|
543
|
+
}
|
|
544
|
+
if (needUpdate) {
|
|
545
|
+
const start = attrValue.start;
|
|
546
|
+
const end = attrValue.end;
|
|
547
|
+
ms.update(start, end, newValue);
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
if (node.type === "ClassDirective") {
|
|
553
|
+
const className = node.name;
|
|
554
|
+
if (replaceMap.has(className)) {
|
|
555
|
+
if (id) {
|
|
556
|
+
ctx.addToUsedBy(className, id);
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
if (node.type === "Script") {
|
|
561
|
+
const contentStart = node.content.start;
|
|
562
|
+
const contentEnd = node.content.end;
|
|
563
|
+
const innerContent = ms.original.slice(contentStart, contentEnd);
|
|
564
|
+
const jsHandlerOptions = id === void 0 ? { ctx } : { ctx, id };
|
|
565
|
+
const result = jsHandler(innerContent, jsHandlerOptions);
|
|
566
|
+
if (result.code !== innerContent) {
|
|
567
|
+
ms.update(contentStart, contentEnd, result.code);
|
|
568
|
+
}
|
|
569
|
+
return;
|
|
570
|
+
}
|
|
571
|
+
if (node.type === "Style") {
|
|
572
|
+
const contentStart = node.content.start;
|
|
573
|
+
const contentEnd = node.content.end;
|
|
574
|
+
const innerContent = ms.original.slice(contentStart, contentEnd);
|
|
575
|
+
const cssHandlerOptions = id === void 0 ? { ctx } : { ctx, id };
|
|
576
|
+
const promise = cssHandler(innerContent, cssHandlerOptions).then((result) => {
|
|
577
|
+
if (result.code !== innerContent) {
|
|
578
|
+
ms.update(contentStart, contentEnd, result.code);
|
|
579
|
+
}
|
|
580
|
+
});
|
|
581
|
+
stylePromises.push(promise);
|
|
582
|
+
return;
|
|
583
|
+
}
|
|
584
|
+
if (node.children) {
|
|
585
|
+
for (const child of node.children) {
|
|
586
|
+
await walk(child);
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
await walk(ast);
|
|
591
|
+
await Promise.all(stylePromises);
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
// src/vue/index.ts
|
|
595
|
+
var import_compiler_sfc = require("@vue/compiler-sfc");
|
|
596
|
+
var import_magic_string4 = __toESM(require("magic-string"), 1);
|
|
597
|
+
async function vueHandler(rawSource, options) {
|
|
598
|
+
const { ctx, id } = options;
|
|
599
|
+
const ms = new import_magic_string4.default(rawSource);
|
|
600
|
+
try {
|
|
601
|
+
const { descriptor } = (0, import_compiler_sfc.parse)(rawSource, {
|
|
602
|
+
filename: id || "unknown.vue"
|
|
603
|
+
});
|
|
604
|
+
if (descriptor.template) {
|
|
605
|
+
await processTemplate(descriptor.template, ms, ctx, id);
|
|
606
|
+
}
|
|
607
|
+
if (descriptor.script || descriptor.scriptSetup) {
|
|
608
|
+
await processScript(descriptor, ms, ctx, id);
|
|
609
|
+
}
|
|
610
|
+
if (descriptor.styles && descriptor.styles.length > 0) {
|
|
611
|
+
await processStyles(descriptor.styles, ms, ctx, id);
|
|
612
|
+
}
|
|
613
|
+
return {
|
|
614
|
+
code: ms.toString(),
|
|
615
|
+
get map() {
|
|
616
|
+
return ms.generateMap();
|
|
617
|
+
}
|
|
618
|
+
};
|
|
619
|
+
} catch (error) {
|
|
620
|
+
return jsHandler(rawSource, options);
|
|
621
|
+
}
|
|
622
|
+
}
|
|
623
|
+
async function processTemplate(template, ms, ctx, id) {
|
|
624
|
+
const { replaceMap, classGenerator } = ctx;
|
|
625
|
+
if (!template.ast) {
|
|
626
|
+
try {
|
|
627
|
+
const compiled = (0, import_compiler_sfc.compileTemplate)({
|
|
628
|
+
source: template.content,
|
|
629
|
+
filename: id || "unknown.vue",
|
|
630
|
+
id: (id || "unknown") + "?template"
|
|
631
|
+
});
|
|
632
|
+
} catch {
|
|
633
|
+
return;
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
const classAttrRegex = /\sclass\s*=\s*["']([^"']+)["']/g;
|
|
637
|
+
let match;
|
|
638
|
+
const templateStart = template.loc.start.offset;
|
|
639
|
+
const templateEnd = template.loc.end.offset;
|
|
640
|
+
const templateContent = ms.original.slice(templateStart, templateEnd);
|
|
641
|
+
const replacements = [];
|
|
642
|
+
while ((match = classAttrRegex.exec(templateContent)) !== null) {
|
|
643
|
+
const fullMatch = match[0];
|
|
644
|
+
const classValue = match[1];
|
|
645
|
+
if (classValue === void 0) {
|
|
646
|
+
continue;
|
|
647
|
+
}
|
|
648
|
+
const offset = match.index;
|
|
649
|
+
const arr = (0, shared_exports.splitCode)(classValue, { splitQuote: false });
|
|
650
|
+
let newValue = classValue;
|
|
651
|
+
let needUpdate = false;
|
|
652
|
+
for (const v of arr) {
|
|
653
|
+
if (replaceMap.has(v)) {
|
|
654
|
+
const gen = classGenerator.generateClassName(v);
|
|
655
|
+
newValue = newValue.replace((0, shared_exports.makeRegex)(v), gen.name);
|
|
656
|
+
if (id) {
|
|
657
|
+
ctx.addToUsedBy(v, id);
|
|
658
|
+
}
|
|
659
|
+
needUpdate = true;
|
|
660
|
+
}
|
|
661
|
+
}
|
|
662
|
+
if (needUpdate) {
|
|
663
|
+
const classValueStart = fullMatch.indexOf(classValue);
|
|
664
|
+
replacements.push({
|
|
665
|
+
start: templateStart + offset + classValueStart,
|
|
666
|
+
end: templateStart + offset + classValueStart + classValue.length,
|
|
667
|
+
value: newValue
|
|
668
|
+
});
|
|
669
|
+
}
|
|
670
|
+
}
|
|
671
|
+
for (const replacement of replacements) {
|
|
672
|
+
ms.update(replacement.start, replacement.end, replacement.value);
|
|
673
|
+
}
|
|
674
|
+
}
|
|
675
|
+
async function processScript(descriptor, ms, ctx, id) {
|
|
676
|
+
const script = descriptor.scriptSetup || descriptor.script;
|
|
677
|
+
if (!script) return;
|
|
678
|
+
const scriptContent = ms.original.slice(
|
|
679
|
+
script.loc.start.offset,
|
|
680
|
+
script.loc.end.offset
|
|
681
|
+
);
|
|
682
|
+
const jsHandlerOptions = id === void 0 ? { ctx } : { ctx, id };
|
|
683
|
+
const result = jsHandler(scriptContent, jsHandlerOptions);
|
|
684
|
+
if (result.code !== scriptContent) {
|
|
685
|
+
ms.update(
|
|
686
|
+
script.loc.start.offset,
|
|
687
|
+
script.loc.end.offset,
|
|
688
|
+
result.code
|
|
689
|
+
);
|
|
690
|
+
}
|
|
691
|
+
}
|
|
692
|
+
async function processStyles(styles, ms, ctx, id) {
|
|
693
|
+
for (const style of styles) {
|
|
694
|
+
const styleContent = ms.original.slice(
|
|
695
|
+
style.loc.start.offset,
|
|
696
|
+
style.loc.end.offset
|
|
697
|
+
);
|
|
698
|
+
const cssHandlerOptions = id === void 0 ? { ctx, ignoreVueScoped: style.scoped } : { ctx, id, ignoreVueScoped: style.scoped };
|
|
699
|
+
const result = await cssHandler(styleContent, cssHandlerOptions);
|
|
700
|
+
if (result.code !== styleContent) {
|
|
701
|
+
ms.update(
|
|
702
|
+
style.loc.start.offset,
|
|
703
|
+
style.loc.end.offset,
|
|
704
|
+
result.code
|
|
705
|
+
);
|
|
706
|
+
}
|
|
707
|
+
}
|
|
708
|
+
}
|
|
497
709
|
// Annotate the CommonJS export names for ESM import in node:
|
|
498
710
|
0 && (module.exports = {
|
|
499
711
|
ClassGenerator,
|
|
@@ -501,5 +713,7 @@ function jsHandler(rawSource, options) {
|
|
|
501
713
|
cssHandler,
|
|
502
714
|
handleValue,
|
|
503
715
|
htmlHandler,
|
|
504
|
-
jsHandler
|
|
716
|
+
jsHandler,
|
|
717
|
+
svelteHandler,
|
|
718
|
+
vueHandler
|
|
505
719
|
});
|
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
|
|
@@ -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,215 @@ function jsHandler(rawSource, options) {
|
|
|
467
468
|
}
|
|
468
469
|
};
|
|
469
470
|
}
|
|
471
|
+
|
|
472
|
+
// src/svelte/index.ts
|
|
473
|
+
import { parse as parse2 } from "svelte/compiler";
|
|
474
|
+
import MagicString3 from "magic-string";
|
|
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) return;
|
|
498
|
+
if (node.type === "Attribute" && node.name === "class") {
|
|
499
|
+
for (const attrValue of node.value) {
|
|
500
|
+
if (attrValue.type === "Text") {
|
|
501
|
+
const classValue = attrValue.data;
|
|
502
|
+
const arr = (0, shared_exports.splitCode)(classValue, { splitQuote: false });
|
|
503
|
+
let newValue = classValue;
|
|
504
|
+
let needUpdate = false;
|
|
505
|
+
for (const v of arr) {
|
|
506
|
+
if (replaceMap.has(v)) {
|
|
507
|
+
const gen = classGenerator.generateClassName(v);
|
|
508
|
+
newValue = newValue.replace((0, shared_exports.makeRegex)(v), gen.name);
|
|
509
|
+
if (id) {
|
|
510
|
+
ctx.addToUsedBy(v, id);
|
|
511
|
+
}
|
|
512
|
+
needUpdate = true;
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
if (needUpdate) {
|
|
516
|
+
const start = attrValue.start;
|
|
517
|
+
const end = attrValue.end;
|
|
518
|
+
ms.update(start, end, newValue);
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
if (node.type === "ClassDirective") {
|
|
524
|
+
const className = node.name;
|
|
525
|
+
if (replaceMap.has(className)) {
|
|
526
|
+
if (id) {
|
|
527
|
+
ctx.addToUsedBy(className, id);
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
if (node.type === "Script") {
|
|
532
|
+
const contentStart = node.content.start;
|
|
533
|
+
const contentEnd = node.content.end;
|
|
534
|
+
const innerContent = ms.original.slice(contentStart, contentEnd);
|
|
535
|
+
const jsHandlerOptions = id === void 0 ? { ctx } : { ctx, id };
|
|
536
|
+
const result = jsHandler(innerContent, jsHandlerOptions);
|
|
537
|
+
if (result.code !== innerContent) {
|
|
538
|
+
ms.update(contentStart, contentEnd, result.code);
|
|
539
|
+
}
|
|
540
|
+
return;
|
|
541
|
+
}
|
|
542
|
+
if (node.type === "Style") {
|
|
543
|
+
const contentStart = node.content.start;
|
|
544
|
+
const contentEnd = node.content.end;
|
|
545
|
+
const innerContent = ms.original.slice(contentStart, contentEnd);
|
|
546
|
+
const cssHandlerOptions = id === void 0 ? { ctx } : { ctx, id };
|
|
547
|
+
const promise = cssHandler(innerContent, cssHandlerOptions).then((result) => {
|
|
548
|
+
if (result.code !== innerContent) {
|
|
549
|
+
ms.update(contentStart, contentEnd, result.code);
|
|
550
|
+
}
|
|
551
|
+
});
|
|
552
|
+
stylePromises.push(promise);
|
|
553
|
+
return;
|
|
554
|
+
}
|
|
555
|
+
if (node.children) {
|
|
556
|
+
for (const child of node.children) {
|
|
557
|
+
await walk(child);
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
await walk(ast);
|
|
562
|
+
await Promise.all(stylePromises);
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
// src/vue/index.ts
|
|
566
|
+
import { compileTemplate, parse as parse3 } from "@vue/compiler-sfc";
|
|
567
|
+
import MagicString4 from "magic-string";
|
|
568
|
+
async function vueHandler(rawSource, options) {
|
|
569
|
+
const { ctx, id } = options;
|
|
570
|
+
const ms = new MagicString4(rawSource);
|
|
571
|
+
try {
|
|
572
|
+
const { descriptor } = parse3(rawSource, {
|
|
573
|
+
filename: id || "unknown.vue"
|
|
574
|
+
});
|
|
575
|
+
if (descriptor.template) {
|
|
576
|
+
await processTemplate(descriptor.template, ms, ctx, id);
|
|
577
|
+
}
|
|
578
|
+
if (descriptor.script || descriptor.scriptSetup) {
|
|
579
|
+
await processScript(descriptor, ms, ctx, id);
|
|
580
|
+
}
|
|
581
|
+
if (descriptor.styles && descriptor.styles.length > 0) {
|
|
582
|
+
await processStyles(descriptor.styles, ms, ctx, id);
|
|
583
|
+
}
|
|
584
|
+
return {
|
|
585
|
+
code: ms.toString(),
|
|
586
|
+
get map() {
|
|
587
|
+
return ms.generateMap();
|
|
588
|
+
}
|
|
589
|
+
};
|
|
590
|
+
} catch (error) {
|
|
591
|
+
return jsHandler(rawSource, options);
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
async function processTemplate(template, ms, ctx, id) {
|
|
595
|
+
const { replaceMap, classGenerator } = ctx;
|
|
596
|
+
if (!template.ast) {
|
|
597
|
+
try {
|
|
598
|
+
const compiled = compileTemplate({
|
|
599
|
+
source: template.content,
|
|
600
|
+
filename: id || "unknown.vue",
|
|
601
|
+
id: (id || "unknown") + "?template"
|
|
602
|
+
});
|
|
603
|
+
} catch {
|
|
604
|
+
return;
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
const classAttrRegex = /\sclass\s*=\s*["']([^"']+)["']/g;
|
|
608
|
+
let match;
|
|
609
|
+
const templateStart = template.loc.start.offset;
|
|
610
|
+
const templateEnd = template.loc.end.offset;
|
|
611
|
+
const templateContent = ms.original.slice(templateStart, templateEnd);
|
|
612
|
+
const replacements = [];
|
|
613
|
+
while ((match = classAttrRegex.exec(templateContent)) !== null) {
|
|
614
|
+
const fullMatch = match[0];
|
|
615
|
+
const classValue = match[1];
|
|
616
|
+
if (classValue === void 0) {
|
|
617
|
+
continue;
|
|
618
|
+
}
|
|
619
|
+
const offset = match.index;
|
|
620
|
+
const arr = (0, shared_exports.splitCode)(classValue, { splitQuote: false });
|
|
621
|
+
let newValue = classValue;
|
|
622
|
+
let needUpdate = false;
|
|
623
|
+
for (const v of arr) {
|
|
624
|
+
if (replaceMap.has(v)) {
|
|
625
|
+
const gen = classGenerator.generateClassName(v);
|
|
626
|
+
newValue = newValue.replace((0, shared_exports.makeRegex)(v), gen.name);
|
|
627
|
+
if (id) {
|
|
628
|
+
ctx.addToUsedBy(v, id);
|
|
629
|
+
}
|
|
630
|
+
needUpdate = true;
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
if (needUpdate) {
|
|
634
|
+
const classValueStart = fullMatch.indexOf(classValue);
|
|
635
|
+
replacements.push({
|
|
636
|
+
start: templateStart + offset + classValueStart,
|
|
637
|
+
end: templateStart + offset + classValueStart + classValue.length,
|
|
638
|
+
value: newValue
|
|
639
|
+
});
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
for (const replacement of replacements) {
|
|
643
|
+
ms.update(replacement.start, replacement.end, replacement.value);
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
async function processScript(descriptor, ms, ctx, id) {
|
|
647
|
+
const script = descriptor.scriptSetup || descriptor.script;
|
|
648
|
+
if (!script) return;
|
|
649
|
+
const scriptContent = ms.original.slice(
|
|
650
|
+
script.loc.start.offset,
|
|
651
|
+
script.loc.end.offset
|
|
652
|
+
);
|
|
653
|
+
const jsHandlerOptions = id === void 0 ? { ctx } : { ctx, id };
|
|
654
|
+
const result = jsHandler(scriptContent, jsHandlerOptions);
|
|
655
|
+
if (result.code !== scriptContent) {
|
|
656
|
+
ms.update(
|
|
657
|
+
script.loc.start.offset,
|
|
658
|
+
script.loc.end.offset,
|
|
659
|
+
result.code
|
|
660
|
+
);
|
|
661
|
+
}
|
|
662
|
+
}
|
|
663
|
+
async function processStyles(styles, ms, ctx, id) {
|
|
664
|
+
for (const style of styles) {
|
|
665
|
+
const styleContent = ms.original.slice(
|
|
666
|
+
style.loc.start.offset,
|
|
667
|
+
style.loc.end.offset
|
|
668
|
+
);
|
|
669
|
+
const cssHandlerOptions = id === void 0 ? { ctx, ignoreVueScoped: style.scoped } : { ctx, id, ignoreVueScoped: style.scoped };
|
|
670
|
+
const result = await cssHandler(styleContent, cssHandlerOptions);
|
|
671
|
+
if (result.code !== styleContent) {
|
|
672
|
+
ms.update(
|
|
673
|
+
style.loc.start.offset,
|
|
674
|
+
style.loc.end.offset,
|
|
675
|
+
result.code
|
|
676
|
+
);
|
|
677
|
+
}
|
|
678
|
+
}
|
|
679
|
+
}
|
|
470
680
|
var export_ClassGenerator = shared_exports.ClassGenerator;
|
|
471
681
|
export {
|
|
472
682
|
export_ClassGenerator as ClassGenerator,
|
|
@@ -474,5 +684,7 @@ export {
|
|
|
474
684
|
cssHandler,
|
|
475
685
|
handleValue,
|
|
476
686
|
htmlHandler,
|
|
477
|
-
jsHandler
|
|
687
|
+
jsHandler,
|
|
688
|
+
svelteHandler,
|
|
689
|
+
vueHandler
|
|
478
690
|
};
|
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.0",
|
|
5
5
|
"description": "The core of tailwindcss-mangle",
|
|
6
6
|
"author": "ice breaker <1324318532@qq.com>",
|
|
7
7
|
"license": "MIT",
|
|
@@ -47,19 +47,25 @@
|
|
|
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
|
-
"postcss-selector-parser": "^7.1.
|
|
60
|
-
"
|
|
61
|
+
"postcss-selector-parser": "^7.1.1",
|
|
62
|
+
"svelte": "^5.51.1",
|
|
63
|
+
"@tailwindcss-mangle/config": "^6.1.1",
|
|
61
64
|
"@tailwindcss-mangle/shared": "^4.1.1"
|
|
62
65
|
},
|
|
66
|
+
"devDependencies": {
|
|
67
|
+
"@types/parse5": "^7.0.0"
|
|
68
|
+
},
|
|
63
69
|
"scripts": {
|
|
64
70
|
"dev": "tsup --watch --sourcemap",
|
|
65
71
|
"build": "tsup",
|