weapp-tailwindcss 4.9.2 → 4.9.4
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/{chunk-WGBJY2H3.mjs → chunk-3IXWQT3G.mjs} +646 -565
- package/dist/{chunk-5O6237HX.js → chunk-4RUFPPI6.js} +400 -377
- package/dist/{chunk-2L6NDZIQ.mjs → chunk-6HOLMRXS.mjs} +2 -2
- package/dist/{chunk-ZE7JKKFT.mjs → chunk-7D27E7TU.mjs} +438 -415
- package/dist/chunk-AJDZJQNR.mjs +444 -0
- package/dist/{chunk-U7BH6QHE.js → chunk-DKX737YI.js} +277 -236
- package/dist/{chunk-VU2GIXVH.js → chunk-EZWWOZAH.js} +7 -7
- package/dist/{chunk-3SKEY32E.mjs → chunk-F2CKKG6Q.mjs} +67 -67
- package/dist/{chunk-DNRNFN4I.js → chunk-KHBSYJ76.js} +44 -44
- package/dist/{chunk-XXJNXESJ.js → chunk-MVOIVNKL.js} +635 -554
- package/dist/{chunk-G5OV7IOU.mjs → chunk-QAEALIIZ.mjs} +2 -2
- package/dist/{chunk-FFGR5V3H.mjs → chunk-SPH6SCTM.mjs} +1 -1
- package/dist/{chunk-2WPTDMN3.js → chunk-VPHA77FH.js} +61 -61
- package/dist/{chunk-DSD5JRCQ.mjs → chunk-WCM6ODS4.mjs} +265 -224
- package/dist/{chunk-CBOVUIXD.mjs → chunk-XI2H73CF.mjs} +41 -41
- package/dist/{chunk-UAEME5HT.js → chunk-Y6F6NBA7.js} +1 -1
- package/dist/chunk-Z3A2XYUL.js +441 -0
- package/dist/{chunk-W3MTTQ5M.js → chunk-ZQ7ET56G.js} +5 -5
- package/dist/cli.js +182 -136
- package/dist/cli.mjs +194 -148
- package/dist/core.js +10 -10
- package/dist/core.mjs +4 -4
- package/dist/css-macro/postcss.js +1 -1
- package/dist/css-macro/postcss.mjs +1 -1
- package/dist/css-macro.js +1 -1
- package/dist/css-macro.mjs +1 -1
- package/dist/defaults.js +1 -1
- package/dist/defaults.mjs +1 -1
- package/dist/gulp.js +6 -6
- package/dist/gulp.mjs +5 -5
- package/dist/index.js +10 -10
- package/dist/index.mjs +9 -9
- package/dist/postcss-html-transform.js +1 -1
- package/dist/postcss-html-transform.mjs +1 -1
- package/dist/presets.js +5 -5
- package/dist/presets.mjs +2 -2
- package/dist/reset.d.mts +2 -1
- package/dist/reset.d.ts +2 -1
- package/dist/reset.js +25 -21
- package/dist/reset.mjs +25 -21
- package/dist/types.d.mts +503 -474
- package/dist/types.d.ts +503 -474
- package/dist/types.js +1 -1
- package/dist/types.mjs +1 -1
- package/dist/vite.js +7 -7
- package/dist/vite.mjs +6 -6
- package/dist/weapp-tw-css-import-rewrite-loader.js +2 -4
- package/dist/webpack.js +8 -8
- package/dist/webpack.mjs +7 -7
- package/dist/webpack4.js +351 -307
- package/dist/webpack4.mjs +341 -297
- package/package.json +10 -10
- package/dist/chunk-5AKI6FQP.mjs +0 -397
- package/dist/chunk-SNEMGP42.js +0 -394
|
@@ -3,7 +3,7 @@ import {
|
|
|
3
3
|
createTailwindcssPatcherFromContext,
|
|
4
4
|
findNearestPackageRoot,
|
|
5
5
|
warnMissingCssEntries
|
|
6
|
-
} from "./chunk-
|
|
6
|
+
} from "./chunk-7D27E7TU.mjs";
|
|
7
7
|
import {
|
|
8
8
|
getDefaultOptions
|
|
9
9
|
} from "./chunk-HWF5U4CI.mjs";
|
|
@@ -12,9 +12,6 @@ import {
|
|
|
12
12
|
isMap
|
|
13
13
|
} from "./chunk-OOHJLO5M.mjs";
|
|
14
14
|
|
|
15
|
-
// src/tailwindcss/runtime.ts
|
|
16
|
-
import { statSync } from "fs";
|
|
17
|
-
|
|
18
15
|
// src/debug/index.ts
|
|
19
16
|
import _createDebug from "debug";
|
|
20
17
|
var _debug = _createDebug("weapp-tw");
|
|
@@ -25,9 +22,8 @@ function createDebug(prefix) {
|
|
|
25
22
|
return debug2;
|
|
26
23
|
}
|
|
27
24
|
|
|
28
|
-
// src/tailwindcss/runtime.ts
|
|
29
|
-
|
|
30
|
-
var refreshTailwindcssPatcherSymbol = /* @__PURE__ */ Symbol.for("weapp-tailwindcss.refreshTailwindcssPatcher");
|
|
25
|
+
// src/tailwindcss/runtime/cache.ts
|
|
26
|
+
import { statSync } from "fs";
|
|
31
27
|
var runtimeClassSetCache = /* @__PURE__ */ new WeakMap();
|
|
32
28
|
function getCacheEntry(twPatcher) {
|
|
33
29
|
let entry = runtimeClassSetCache.get(twPatcher);
|
|
@@ -55,8 +51,18 @@ function invalidateRuntimeClassSet(twPatcher) {
|
|
|
55
51
|
}
|
|
56
52
|
runtimeClassSetCache.delete(twPatcher);
|
|
57
53
|
}
|
|
54
|
+
function getRuntimeClassSetCacheEntry(twPatcher) {
|
|
55
|
+
return getCacheEntry(twPatcher);
|
|
56
|
+
}
|
|
57
|
+
function getRuntimeClassSetSignature(twPatcher) {
|
|
58
|
+
return getTailwindConfigSignature(twPatcher);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// src/tailwindcss/runtime.ts
|
|
62
|
+
var debug = createDebug("[tailwindcss:runtime] ");
|
|
63
|
+
var refreshTailwindcssPatcherSymbol = /* @__PURE__ */ Symbol.for("weapp-tailwindcss.refreshTailwindcssPatcher");
|
|
58
64
|
function createTailwindPatchPromise(twPatcher, onPatched) {
|
|
59
|
-
return Promise.resolve(twPatcher.patch()).then(async (
|
|
65
|
+
return Promise.resolve(twPatcher.patch()).then(async () => {
|
|
60
66
|
invalidateRuntimeClassSet(twPatcher);
|
|
61
67
|
if (onPatched) {
|
|
62
68
|
try {
|
|
@@ -65,7 +71,6 @@ function createTailwindPatchPromise(twPatcher, onPatched) {
|
|
|
65
71
|
debug("failed to persist patch target after patch(): %O", error);
|
|
66
72
|
}
|
|
67
73
|
}
|
|
68
|
-
return result;
|
|
69
74
|
});
|
|
70
75
|
}
|
|
71
76
|
async function refreshTailwindRuntimeState(state, force) {
|
|
@@ -135,8 +140,8 @@ async function collectRuntimeClassSet(twPatcher, options = {}) {
|
|
|
135
140
|
}
|
|
136
141
|
}
|
|
137
142
|
}
|
|
138
|
-
const entry =
|
|
139
|
-
const signature =
|
|
143
|
+
const entry = getRuntimeClassSetCacheEntry(activePatcher);
|
|
144
|
+
const signature = getRuntimeClassSetSignature(activePatcher);
|
|
140
145
|
if (!options.force) {
|
|
141
146
|
if (entry.value && entry.signature === signature) {
|
|
142
147
|
return entry.value;
|
|
@@ -187,7 +192,7 @@ async function collectRuntimeClassSet(twPatcher, options = {}) {
|
|
|
187
192
|
// package.json
|
|
188
193
|
var package_default = {
|
|
189
194
|
name: "weapp-tailwindcss",
|
|
190
|
-
version: "4.9.
|
|
195
|
+
version: "4.9.4",
|
|
191
196
|
description: "\u628A tailwindcss \u539F\u5B50\u5316\u6837\u5F0F\u601D\u60F3\uFF0C\u5E26\u7ED9\u5C0F\u7A0B\u5E8F\u5F00\u53D1\u8005\u4EEC! bring tailwindcss to miniprogram developers!",
|
|
192
197
|
author: "ice breaker <1324318532@qq.com>",
|
|
193
198
|
license: "MIT",
|
|
@@ -343,7 +348,7 @@ var package_default = {
|
|
|
343
348
|
"with-layer.css"
|
|
344
349
|
],
|
|
345
350
|
engines: {
|
|
346
|
-
node: "^
|
|
351
|
+
node: "^20.19.0 || >=22.12.0"
|
|
347
352
|
},
|
|
348
353
|
scripts: {
|
|
349
354
|
dev: "tsup --watch --sourcemap",
|
|
@@ -374,9 +379,9 @@ var package_default = {
|
|
|
374
379
|
},
|
|
375
380
|
dependencies: {
|
|
376
381
|
"@ast-core/escape": "~1.0.1",
|
|
377
|
-
"@babel/parser": "~7.28.
|
|
378
|
-
"@babel/traverse": "~7.28.
|
|
379
|
-
"@babel/types": "~7.28.
|
|
382
|
+
"@babel/parser": "~7.28.6",
|
|
383
|
+
"@babel/traverse": "~7.28.6",
|
|
384
|
+
"@babel/types": "~7.28.6",
|
|
380
385
|
"@tailwindcss-mangle/config": "^6.1.0",
|
|
381
386
|
"@vue/compiler-dom": "catalog:vue3",
|
|
382
387
|
"@vue/compiler-sfc": "catalog:vue3",
|
|
@@ -388,7 +393,7 @@ var package_default = {
|
|
|
388
393
|
cac: "^6.7.14",
|
|
389
394
|
debug: "~4.4.3",
|
|
390
395
|
"fast-glob": "^3.3.3",
|
|
391
|
-
htmlparser2: "10.
|
|
396
|
+
htmlparser2: "10.1.0",
|
|
392
397
|
"loader-utils": "2.0.4",
|
|
393
398
|
"local-pkg": "^1.1.2",
|
|
394
399
|
"lru-cache": "10.4.3",
|
|
@@ -413,7 +418,7 @@ var DEFAULT_RUNTIME_PACKAGE_REPLACEMENTS = {
|
|
|
413
418
|
|
|
414
419
|
// src/context/index.ts
|
|
415
420
|
import { rm } from "fs/promises";
|
|
416
|
-
import { logger as
|
|
421
|
+
import { logger as logger5, pc } from "@weapp-tailwindcss/logger";
|
|
417
422
|
|
|
418
423
|
// src/cache/index.ts
|
|
419
424
|
import { LRUCache } from "lru-cache";
|
|
@@ -519,15 +524,15 @@ function initializeCache(cacheConfig) {
|
|
|
519
524
|
}
|
|
520
525
|
|
|
521
526
|
// src/tailwindcss/targets.ts
|
|
522
|
-
import
|
|
523
|
-
import {
|
|
527
|
+
import process3 from "process";
|
|
528
|
+
import { logger as logger2 } from "@weapp-tailwindcss/logger";
|
|
529
|
+
|
|
530
|
+
// src/tailwindcss/targets/paths.ts
|
|
531
|
+
import { existsSync } from "fs";
|
|
524
532
|
import path from "path";
|
|
525
|
-
import process from "process";
|
|
526
|
-
import { logger } from "@weapp-tailwindcss/logger";
|
|
527
533
|
var PATCH_INFO_FILENAME = "tailwindcss-target.json";
|
|
528
534
|
var PATCH_INFO_CACHE_RELATIVE_PATH = path.join("node_modules", ".cache", "weapp-tailwindcss", PATCH_INFO_FILENAME);
|
|
529
535
|
var PATCH_INFO_LEGACY_RELATIVE_PATH = path.join(".tw-patch", PATCH_INFO_FILENAME);
|
|
530
|
-
var loggedInvalidPatchRecords = /* @__PURE__ */ new Set();
|
|
531
536
|
function formatRelativeToBase(targetPath, baseDir) {
|
|
532
537
|
if (!baseDir) {
|
|
533
538
|
return path.normalize(targetPath);
|
|
@@ -569,29 +574,22 @@ function getRecordFileCandidates(baseDir) {
|
|
|
569
574
|
]);
|
|
570
575
|
return [...candidates];
|
|
571
576
|
}
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
);
|
|
581
|
-
return;
|
|
582
|
-
}
|
|
583
|
-
const displayPath = formatRelativeToBase(packageInfo.rootPath, baseDir);
|
|
584
|
-
const version = packageInfo.version ? ` (v${packageInfo.version})` : "";
|
|
585
|
-
logger.info("%s \u7ED1\u5B9A Tailwind CSS -> %s%s", label, displayPath, version);
|
|
586
|
-
}
|
|
577
|
+
|
|
578
|
+
// src/tailwindcss/targets/record-io.ts
|
|
579
|
+
import { existsSync as existsSync2, readFileSync } from "fs";
|
|
580
|
+
import { mkdir, writeFile } from "fs/promises";
|
|
581
|
+
import path2 from "path";
|
|
582
|
+
import process from "process";
|
|
583
|
+
import { logger } from "@weapp-tailwindcss/logger";
|
|
584
|
+
var loggedInvalidPatchRecords = /* @__PURE__ */ new Set();
|
|
587
585
|
function warnInvalidPatchTargetRecord(baseDir, recordPath, reason) {
|
|
588
|
-
const normalizedPath =
|
|
586
|
+
const normalizedPath = path2.normalize(recordPath);
|
|
589
587
|
if (loggedInvalidPatchRecords.has(normalizedPath)) {
|
|
590
588
|
return;
|
|
591
589
|
}
|
|
592
590
|
loggedInvalidPatchRecords.add(normalizedPath);
|
|
593
591
|
const fileDisplay = formatRelativeToBase(normalizedPath, baseDir);
|
|
594
|
-
const baseDisplay = formatRelativeToBase(
|
|
592
|
+
const baseDisplay = formatRelativeToBase(path2.normalize(baseDir), process.cwd());
|
|
595
593
|
const reasonMessage = reason ? `\uFF1A${reason}` : "";
|
|
596
594
|
logger.warn(
|
|
597
595
|
`\u68C0\u6D4B\u5230\u635F\u574F\u7684 Tailwind CSS \u76EE\u6807\u8BB0\u5F55 ${fileDisplay}${reasonMessage}\u3002\u8BF7\u5728 ${baseDisplay} \u91CD\u65B0\u6267\u884C "weapp-tw patch --record-target" \u6216\u5220\u9664\u8BE5\u6587\u4EF6\u540E\u518D\u8FD0\u884C\u3002`
|
|
@@ -601,9 +599,9 @@ function readPatchTargetRecord(baseDir) {
|
|
|
601
599
|
if (!baseDir) {
|
|
602
600
|
return void 0;
|
|
603
601
|
}
|
|
604
|
-
const normalizedBase =
|
|
602
|
+
const normalizedBase = path2.normalize(baseDir);
|
|
605
603
|
for (const recordPath of getRecordFileCandidates(normalizedBase)) {
|
|
606
|
-
if (!
|
|
604
|
+
if (!existsSync2(recordPath)) {
|
|
607
605
|
continue;
|
|
608
606
|
}
|
|
609
607
|
try {
|
|
@@ -630,22 +628,22 @@ async function saveCliPatchTargetRecord(baseDir, patcher, options) {
|
|
|
630
628
|
if (!baseDir || !patcher?.packageInfo?.rootPath) {
|
|
631
629
|
return void 0;
|
|
632
630
|
}
|
|
633
|
-
const normalizedBase =
|
|
631
|
+
const normalizedBase = path2.normalize(baseDir);
|
|
634
632
|
const location = resolveRecordLocation(normalizedBase);
|
|
635
|
-
const recordPath = options?.recordPath ?
|
|
633
|
+
const recordPath = options?.recordPath ? path2.normalize(options.recordPath) : location.recordPath;
|
|
636
634
|
const record = {
|
|
637
|
-
tailwindPackagePath:
|
|
635
|
+
tailwindPackagePath: path2.normalize(patcher.packageInfo.rootPath),
|
|
638
636
|
packageVersion: patcher.packageInfo.version,
|
|
639
637
|
recordedAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
640
638
|
source: options?.source ?? "cli",
|
|
641
639
|
tailwindcssBasedir: normalizedBase,
|
|
642
|
-
cwd: options?.cwd ?
|
|
640
|
+
cwd: options?.cwd ? path2.normalize(options.cwd) : normalizedBase,
|
|
643
641
|
patchVersion: WEAPP_TW_VERSION,
|
|
644
642
|
packageJsonPath: options?.packageJsonPath ?? location.packageJsonPath,
|
|
645
643
|
recordKey: options?.recordKey ?? location.recordKey
|
|
646
644
|
};
|
|
647
645
|
try {
|
|
648
|
-
await mkdir(
|
|
646
|
+
await mkdir(path2.dirname(recordPath), { recursive: true });
|
|
649
647
|
await writeFile(recordPath, `${JSON.stringify(record, null, 2)}
|
|
650
648
|
`, "utf8");
|
|
651
649
|
return recordPath;
|
|
@@ -660,16 +658,20 @@ async function saveCliPatchTargetRecord(baseDir, patcher, options) {
|
|
|
660
658
|
return void 0;
|
|
661
659
|
}
|
|
662
660
|
}
|
|
661
|
+
|
|
662
|
+
// src/tailwindcss/targets/recorder.ts
|
|
663
|
+
import path3 from "path";
|
|
664
|
+
import process2 from "process";
|
|
663
665
|
function findPatchTargetRecord(baseDir) {
|
|
664
666
|
const visited = /* @__PURE__ */ new Set();
|
|
665
|
-
const fallback = baseDir ??
|
|
666
|
-
let current =
|
|
667
|
+
const fallback = baseDir ?? process2.cwd();
|
|
668
|
+
let current = path3.resolve(fallback);
|
|
667
669
|
while (!visited.has(current)) {
|
|
668
670
|
const record = readPatchTargetRecord(current);
|
|
669
671
|
if (record) {
|
|
670
672
|
return record;
|
|
671
673
|
}
|
|
672
|
-
const parent =
|
|
674
|
+
const parent = path3.dirname(current);
|
|
673
675
|
if (parent === current) {
|
|
674
676
|
break;
|
|
675
677
|
}
|
|
@@ -682,22 +684,22 @@ function createPatchTargetRecorder(baseDir, patcher, options) {
|
|
|
682
684
|
if (!baseDir || !patcher?.packageInfo?.rootPath || options?.recordTarget === false) {
|
|
683
685
|
return void 0;
|
|
684
686
|
}
|
|
685
|
-
const normalizedBase =
|
|
687
|
+
const normalizedBase = path3.normalize(baseDir);
|
|
686
688
|
const recorded = findPatchTargetRecord(normalizedBase);
|
|
687
689
|
const location = resolveRecordLocation(normalizedBase);
|
|
688
|
-
const expectedPath =
|
|
690
|
+
const expectedPath = path3.normalize(patcher.packageInfo.rootPath);
|
|
689
691
|
let reason;
|
|
690
692
|
if (!recorded) {
|
|
691
693
|
reason = "missing";
|
|
692
694
|
} else {
|
|
693
|
-
const normalizedRecorded =
|
|
695
|
+
const normalizedRecorded = path3.normalize(recorded.record.tailwindPackagePath);
|
|
694
696
|
if (normalizedRecorded !== expectedPath) {
|
|
695
697
|
reason = "mismatch";
|
|
696
|
-
} else if (
|
|
698
|
+
} else if (path3.normalize(recorded.path) !== path3.normalize(location.recordPath) || !recorded.record.recordKey || recorded.record.recordKey !== location.recordKey) {
|
|
697
699
|
reason = "migrate";
|
|
698
700
|
} else if (!recorded.record.patchVersion || recorded.record.patchVersion !== WEAPP_TW_VERSION) {
|
|
699
701
|
reason = "stale";
|
|
700
|
-
} else if (options?.cwd && recorded.record.cwd &&
|
|
702
|
+
} else if (options?.cwd && recorded.record.cwd && path3.normalize(recorded.record.cwd) !== path3.normalize(options.cwd)) {
|
|
701
703
|
reason = "metadata";
|
|
702
704
|
} else if (!recorded.record.cwd && options?.cwd) {
|
|
703
705
|
reason = "metadata";
|
|
@@ -737,9 +739,26 @@ function createPatchTargetRecorder(baseDir, patcher, options) {
|
|
|
737
739
|
};
|
|
738
740
|
}
|
|
739
741
|
|
|
742
|
+
// src/tailwindcss/targets.ts
|
|
743
|
+
function logTailwindcssTarget(kind, patcher, baseDir) {
|
|
744
|
+
const packageInfo = patcher?.packageInfo;
|
|
745
|
+
const label = kind === "cli" ? "weapp-tw patch" : "tailwindcss-patcher";
|
|
746
|
+
if (!packageInfo?.rootPath) {
|
|
747
|
+
logger2.warn(
|
|
748
|
+
"%s \u672A\u627E\u5230 Tailwind CSS \u4F9D\u8D56\uFF0C\u8BF7\u68C0\u67E5\u5728 %s \u662F\u5426\u5DF2\u5B89\u88C5 tailwindcss",
|
|
749
|
+
label,
|
|
750
|
+
baseDir ?? process3.cwd()
|
|
751
|
+
);
|
|
752
|
+
return;
|
|
753
|
+
}
|
|
754
|
+
const displayPath = formatRelativeToBase(packageInfo.rootPath, baseDir);
|
|
755
|
+
const version = packageInfo.version ? ` (v${packageInfo.version})` : "";
|
|
756
|
+
logger2.info("%s \u7ED1\u5B9A Tailwind CSS -> %s%s", label, displayPath, version);
|
|
757
|
+
}
|
|
758
|
+
|
|
740
759
|
// src/context/compiler-context-cache.ts
|
|
741
760
|
import { Buffer } from "buffer";
|
|
742
|
-
import { logger as
|
|
761
|
+
import { logger as logger3 } from "@weapp-tailwindcss/logger";
|
|
743
762
|
var globalCacheHolder = globalThis;
|
|
744
763
|
var compilerContextCache = globalCacheHolder.__WEAPP_TW_COMPILER_CONTEXT_CACHE__ ?? (globalCacheHolder.__WEAPP_TW_COMPILER_CONTEXT_CACHE__ = /* @__PURE__ */ new Map());
|
|
745
764
|
function compareNormalizedValues(a, b) {
|
|
@@ -889,7 +908,7 @@ function createCompilerContextCacheKey(opts) {
|
|
|
889
908
|
const serialized = JSON.stringify(normalized);
|
|
890
909
|
return md5(serialized);
|
|
891
910
|
} catch (error) {
|
|
892
|
-
|
|
911
|
+
logger3.debug("skip compiler context cache: %O", error);
|
|
893
912
|
return void 0;
|
|
894
913
|
}
|
|
895
914
|
}
|
|
@@ -924,10 +943,6 @@ function toCustomAttributesEntities(customAttributes) {
|
|
|
924
943
|
// src/context/handlers.ts
|
|
925
944
|
import { createStyleHandler } from "@weapp-tailwindcss/postcss";
|
|
926
945
|
|
|
927
|
-
// src/js/babel.ts
|
|
928
|
-
import { LRUCache as LRUCache2 } from "lru-cache";
|
|
929
|
-
import MagicString from "magic-string";
|
|
930
|
-
|
|
931
946
|
// src/babel/index.ts
|
|
932
947
|
import _babelTraverse from "@babel/traverse";
|
|
933
948
|
import { parse, parseExpression } from "@babel/parser";
|
|
@@ -991,132 +1006,41 @@ function createNameMatcher(list, { exact = false } = {}) {
|
|
|
991
1006
|
};
|
|
992
1007
|
}
|
|
993
1008
|
|
|
994
|
-
// src/js/
|
|
995
|
-
import {
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
return calleePath.isIdentifier({ name: "eval" });
|
|
1000
|
-
}
|
|
1001
|
-
return false;
|
|
1002
|
-
}
|
|
1003
|
-
function createEvalReplacementToken(path2, updated) {
|
|
1004
|
-
const node = path2.node;
|
|
1005
|
-
let offset = 0;
|
|
1006
|
-
let original;
|
|
1007
|
-
if (path2.isStringLiteral()) {
|
|
1008
|
-
offset = 1;
|
|
1009
|
-
original = path2.node.value;
|
|
1010
|
-
} else if (path2.isTemplateElement()) {
|
|
1011
|
-
original = path2.node.value.raw;
|
|
1012
|
-
} else {
|
|
1013
|
-
original = "";
|
|
1014
|
-
}
|
|
1015
|
-
if (typeof node.start !== "number" || typeof node.end !== "number") {
|
|
1016
|
-
return void 0;
|
|
1017
|
-
}
|
|
1018
|
-
const start = node.start + offset;
|
|
1019
|
-
const end = node.end - offset;
|
|
1020
|
-
if (start >= end) {
|
|
1021
|
-
return void 0;
|
|
1022
|
-
}
|
|
1023
|
-
if (original === updated) {
|
|
1024
|
-
return void 0;
|
|
1025
|
-
}
|
|
1026
|
-
const value = path2.isStringLiteral() ? jsStringEscape(updated) : updated;
|
|
1027
|
-
return {
|
|
1028
|
-
start,
|
|
1029
|
-
end,
|
|
1030
|
-
value,
|
|
1031
|
-
path: path2
|
|
1032
|
-
};
|
|
1033
|
-
}
|
|
1034
|
-
function handleEvalStringLiteral(path2, options, updater, handler) {
|
|
1035
|
-
const { code } = handler(path2.node.value, {
|
|
1036
|
-
...options,
|
|
1037
|
-
needEscaped: false,
|
|
1038
|
-
generateMap: false
|
|
1039
|
-
});
|
|
1040
|
-
if (!code) {
|
|
1041
|
-
return;
|
|
1042
|
-
}
|
|
1043
|
-
const token = createEvalReplacementToken(path2, code);
|
|
1044
|
-
if (token) {
|
|
1045
|
-
updater.addToken(token);
|
|
1046
|
-
}
|
|
1047
|
-
}
|
|
1048
|
-
function handleEvalTemplateElement(path2, options, updater, handler) {
|
|
1049
|
-
const { code } = handler(path2.node.value.raw, {
|
|
1050
|
-
...options,
|
|
1051
|
-
generateMap: false
|
|
1052
|
-
});
|
|
1053
|
-
if (!code) {
|
|
1054
|
-
return;
|
|
1009
|
+
// src/js/babel/parse.ts
|
|
1010
|
+
import { LRUCache as LRUCache2 } from "lru-cache";
|
|
1011
|
+
var parseCache = new LRUCache2(
|
|
1012
|
+
{
|
|
1013
|
+
max: 1024
|
|
1055
1014
|
}
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1015
|
+
);
|
|
1016
|
+
function genCacheKey(source, options) {
|
|
1017
|
+
if (typeof options === "string") {
|
|
1018
|
+
return source + options;
|
|
1059
1019
|
}
|
|
1020
|
+
return source + JSON.stringify(options, (_, val) => typeof val === "function" ? val.toString() : val);
|
|
1060
1021
|
}
|
|
1061
|
-
function
|
|
1062
|
-
const
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
handleEvalStringLiteral(innerPath, options, updater, handler);
|
|
1068
|
-
},
|
|
1069
|
-
TemplateElement(innerPath) {
|
|
1070
|
-
handleEvalTemplateElement(innerPath, options, updater, handler);
|
|
1071
|
-
}
|
|
1072
|
-
});
|
|
1073
|
-
} catch (error) {
|
|
1074
|
-
const msg = error?.message ?? "";
|
|
1075
|
-
const scopeError = /pass a scope and parentPath|traversing a Program\/File/i.test(msg);
|
|
1076
|
-
if (!scopeError) {
|
|
1077
|
-
throw error;
|
|
1078
|
-
}
|
|
1079
|
-
}
|
|
1080
|
-
}
|
|
1081
|
-
const getArgs = path2?.get?.("arguments");
|
|
1082
|
-
if (Array.isArray(getArgs)) {
|
|
1083
|
-
for (const arg of getArgs) {
|
|
1084
|
-
if (arg?.isStringLiteral?.()) {
|
|
1085
|
-
handleEvalStringLiteral(arg, options, updater, handler);
|
|
1086
|
-
continue;
|
|
1087
|
-
}
|
|
1088
|
-
if (arg?.isTemplateLiteral?.()) {
|
|
1089
|
-
for (const quasi of arg.get("quasis")) {
|
|
1090
|
-
handleEvalTemplateElement(quasi, options, updater, handler);
|
|
1091
|
-
}
|
|
1092
|
-
}
|
|
1093
|
-
}
|
|
1094
|
-
return;
|
|
1022
|
+
function babelParse(code, opts = {}) {
|
|
1023
|
+
const { cache, cacheKey, ...rest } = opts;
|
|
1024
|
+
const cacheKeyString = genCacheKey(code, cacheKey ?? rest);
|
|
1025
|
+
let result;
|
|
1026
|
+
if (cache) {
|
|
1027
|
+
result = parseCache.get(cacheKeyString);
|
|
1095
1028
|
}
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
node: n,
|
|
1102
|
-
isStringLiteral: () => true
|
|
1103
|
-
};
|
|
1104
|
-
handleEvalStringLiteral(stub, options, updater, handler);
|
|
1105
|
-
} else if (n?.type === "TemplateLiteral" && Array.isArray(n.quasis)) {
|
|
1106
|
-
for (const q of n.quasis) {
|
|
1107
|
-
const stub = {
|
|
1108
|
-
node: q,
|
|
1109
|
-
isTemplateElement: () => true
|
|
1110
|
-
};
|
|
1111
|
-
handleEvalTemplateElement(stub, options, updater, handler);
|
|
1112
|
-
}
|
|
1113
|
-
}
|
|
1029
|
+
if (!result) {
|
|
1030
|
+
const { cache: _cache, cacheKey: _cacheKey, ...parseOptions } = opts;
|
|
1031
|
+
result = parse(code, parseOptions);
|
|
1032
|
+
if (cache) {
|
|
1033
|
+
parseCache.set(cacheKeyString, result);
|
|
1114
1034
|
}
|
|
1115
1035
|
}
|
|
1036
|
+
return result;
|
|
1116
1037
|
}
|
|
1117
1038
|
|
|
1039
|
+
// src/js/babel/process.ts
|
|
1040
|
+
import MagicString from "magic-string";
|
|
1041
|
+
|
|
1118
1042
|
// src/js/handlers.ts
|
|
1119
|
-
import { jsStringEscape
|
|
1043
|
+
import { jsStringEscape } from "@ast-core/escape";
|
|
1120
1044
|
import { escapeStringRegexp as escapeStringRegexp2 } from "@weapp-core/regex";
|
|
1121
1045
|
import { splitCode } from "@weapp-tailwindcss/shared/extractors";
|
|
1122
1046
|
|
|
@@ -1213,15 +1137,15 @@ function shouldTransformClassName(candidate, {
|
|
|
1213
1137
|
}
|
|
1214
1138
|
return !jsPreserveClass?.(candidate);
|
|
1215
1139
|
}
|
|
1216
|
-
function extractLiteralValue(
|
|
1140
|
+
function extractLiteralValue(path4, { unescapeUnicode, arbitraryValues }) {
|
|
1217
1141
|
const allowDoubleQuotes = arbitraryValues?.allowDoubleQuotes;
|
|
1218
1142
|
let offset = 0;
|
|
1219
1143
|
let original;
|
|
1220
|
-
if (
|
|
1144
|
+
if (path4.isStringLiteral()) {
|
|
1221
1145
|
offset = 1;
|
|
1222
|
-
original =
|
|
1223
|
-
} else if (
|
|
1224
|
-
original =
|
|
1146
|
+
original = path4.node.value;
|
|
1147
|
+
} else if (path4.isTemplateElement()) {
|
|
1148
|
+
original = path4.node.value.raw;
|
|
1225
1149
|
} else {
|
|
1226
1150
|
original = "";
|
|
1227
1151
|
}
|
|
@@ -1236,7 +1160,7 @@ function extractLiteralValue(path2, { unescapeUnicode, arbitraryValues }) {
|
|
|
1236
1160
|
original
|
|
1237
1161
|
};
|
|
1238
1162
|
}
|
|
1239
|
-
function replaceHandleValue(
|
|
1163
|
+
function replaceHandleValue(path4, options) {
|
|
1240
1164
|
const {
|
|
1241
1165
|
escapeMap,
|
|
1242
1166
|
needEscaped = false
|
|
@@ -1245,8 +1169,8 @@ function replaceHandleValue(path2, options) {
|
|
|
1245
1169
|
if (!alwaysEscape && (!classNameSet || classNameSet.size === 0)) {
|
|
1246
1170
|
return void 0;
|
|
1247
1171
|
}
|
|
1248
|
-
const { literal, original, allowDoubleQuotes, offset } = extractLiteralValue(
|
|
1249
|
-
if (hasIgnoreComment(
|
|
1172
|
+
const { literal, original, allowDoubleQuotes, offset } = extractLiteralValue(path4, options);
|
|
1173
|
+
if (hasIgnoreComment(path4.node)) {
|
|
1250
1174
|
return void 0;
|
|
1251
1175
|
}
|
|
1252
1176
|
const candidates = splitCode(literal, allowDoubleQuotes);
|
|
@@ -1270,7 +1194,7 @@ function replaceHandleValue(path2, options) {
|
|
|
1270
1194
|
mutated = true;
|
|
1271
1195
|
}
|
|
1272
1196
|
}
|
|
1273
|
-
const node =
|
|
1197
|
+
const node = path4.node;
|
|
1274
1198
|
if (!mutated || typeof node.start !== "number" || typeof node.end !== "number") {
|
|
1275
1199
|
return void 0;
|
|
1276
1200
|
}
|
|
@@ -1279,93 +1203,271 @@ function replaceHandleValue(path2, options) {
|
|
|
1279
1203
|
if (start >= end || transformed === original) {
|
|
1280
1204
|
return void 0;
|
|
1281
1205
|
}
|
|
1282
|
-
const value = needEscaped ?
|
|
1206
|
+
const value = needEscaped ? jsStringEscape(transformed) : transformed;
|
|
1283
1207
|
return {
|
|
1284
1208
|
start,
|
|
1285
1209
|
end,
|
|
1286
1210
|
value,
|
|
1287
|
-
path:
|
|
1211
|
+
path: path4
|
|
1288
1212
|
};
|
|
1289
1213
|
}
|
|
1290
1214
|
|
|
1291
|
-
// src/js/
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1215
|
+
// src/js/sourceAnalysis.ts
|
|
1216
|
+
function createModuleSpecifierReplacementToken(path4, replacement) {
|
|
1217
|
+
const node = path4.node;
|
|
1218
|
+
if (node.value === replacement) {
|
|
1219
|
+
return void 0;
|
|
1295
1220
|
}
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
this.tokens.push(token);
|
|
1299
|
-
}
|
|
1221
|
+
if (typeof node.start !== "number" || typeof node.end !== "number") {
|
|
1222
|
+
return void 0;
|
|
1300
1223
|
}
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1224
|
+
const start = node.start + 1;
|
|
1225
|
+
const end = node.end - 1;
|
|
1226
|
+
if (start >= end) {
|
|
1227
|
+
return void 0;
|
|
1304
1228
|
}
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1229
|
+
return {
|
|
1230
|
+
start,
|
|
1231
|
+
end,
|
|
1232
|
+
value: replacement,
|
|
1233
|
+
path: path4
|
|
1234
|
+
};
|
|
1235
|
+
}
|
|
1236
|
+
function collectModuleSpecifierReplacementTokens(analysis, replacements) {
|
|
1237
|
+
const tokens = [];
|
|
1238
|
+
const applyReplacement = (path4) => {
|
|
1239
|
+
const replacement = replacements[path4.node.value];
|
|
1240
|
+
if (!replacement) {
|
|
1241
|
+
return;
|
|
1242
|
+
}
|
|
1243
|
+
const token = createModuleSpecifierReplacementToken(path4, replacement);
|
|
1244
|
+
if (token) {
|
|
1245
|
+
tokens.push(token);
|
|
1246
|
+
}
|
|
1247
|
+
};
|
|
1248
|
+
for (const importPath of analysis.importDeclarations) {
|
|
1249
|
+
const source = importPath.get("source");
|
|
1250
|
+
if (source.isStringLiteral()) {
|
|
1251
|
+
applyReplacement(source);
|
|
1252
|
+
}
|
|
1310
1253
|
}
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
1254
|
+
for (const exportPath of analysis.exportDeclarations) {
|
|
1255
|
+
if (exportPath.isExportNamedDeclaration() || exportPath.isExportAllDeclaration()) {
|
|
1256
|
+
const source = exportPath.get("source");
|
|
1257
|
+
if (source && !Array.isArray(source) && source.isStringLiteral()) {
|
|
1258
|
+
applyReplacement(source);
|
|
1259
|
+
}
|
|
1260
|
+
}
|
|
1314
1261
|
}
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
return this;
|
|
1262
|
+
for (const literalPath of analysis.requireCallPaths) {
|
|
1263
|
+
applyReplacement(literalPath);
|
|
1318
1264
|
}
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1265
|
+
for (const token of analysis.walker.imports) {
|
|
1266
|
+
const replacement = replacements[token.source];
|
|
1267
|
+
if (replacement) {
|
|
1268
|
+
token.source = replacement;
|
|
1322
1269
|
}
|
|
1323
|
-
return ms;
|
|
1324
1270
|
}
|
|
1325
|
-
|
|
1271
|
+
return tokens;
|
|
1272
|
+
}
|
|
1326
1273
|
|
|
1327
|
-
// src/js/
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
filename: entry.filename,
|
|
1346
|
-
source: entry.source,
|
|
1347
|
-
analysis: entry.analysis
|
|
1348
|
-
});
|
|
1349
|
-
this.queue.push({ filename: entry.filename, depth: 0 });
|
|
1274
|
+
// src/js/babel/process.ts
|
|
1275
|
+
function processUpdatedSource(rawSource, options, analysis) {
|
|
1276
|
+
const { targetPaths, jsTokenUpdater, ignoredPaths } = analysis;
|
|
1277
|
+
const replacementTokens = [];
|
|
1278
|
+
for (const path4 of targetPaths) {
|
|
1279
|
+
if (ignoredPaths.has(path4)) {
|
|
1280
|
+
continue;
|
|
1281
|
+
}
|
|
1282
|
+
const token = replaceHandleValue(
|
|
1283
|
+
path4,
|
|
1284
|
+
{
|
|
1285
|
+
...options,
|
|
1286
|
+
needEscaped: path4.isStringLiteral() ? options.needEscaped ?? true : false
|
|
1287
|
+
}
|
|
1288
|
+
);
|
|
1289
|
+
if (token) {
|
|
1290
|
+
replacementTokens.push(token);
|
|
1291
|
+
}
|
|
1350
1292
|
}
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
|
|
1293
|
+
if (options.moduleSpecifierReplacements) {
|
|
1294
|
+
replacementTokens.push(
|
|
1295
|
+
...collectModuleSpecifierReplacementTokens(analysis, options.moduleSpecifierReplacements)
|
|
1296
|
+
);
|
|
1297
|
+
}
|
|
1298
|
+
if (jsTokenUpdater.length + replacementTokens.length === 0) {
|
|
1299
|
+
return new MagicString(rawSource);
|
|
1300
|
+
}
|
|
1301
|
+
const ms = new MagicString(rawSource);
|
|
1302
|
+
jsTokenUpdater.push(...replacementTokens).filter((token) => !ignoredPaths.has(token.path)).updateMagicString(ms);
|
|
1303
|
+
return ms;
|
|
1304
|
+
}
|
|
1305
|
+
|
|
1306
|
+
// src/js/evalTransforms.ts
|
|
1307
|
+
import { jsStringEscape as jsStringEscape2 } from "@ast-core/escape";
|
|
1308
|
+
function isEvalPath(path4) {
|
|
1309
|
+
if (path4.isCallExpression()) {
|
|
1310
|
+
const calleePath = path4.get("callee");
|
|
1311
|
+
return calleePath.isIdentifier({ name: "eval" });
|
|
1312
|
+
}
|
|
1313
|
+
return false;
|
|
1314
|
+
}
|
|
1315
|
+
function createEvalReplacementToken(path4, updated) {
|
|
1316
|
+
const node = path4.node;
|
|
1317
|
+
let offset = 0;
|
|
1318
|
+
let original;
|
|
1319
|
+
if (path4.isStringLiteral()) {
|
|
1320
|
+
offset = 1;
|
|
1321
|
+
original = path4.node.value;
|
|
1322
|
+
} else if (path4.isTemplateElement()) {
|
|
1323
|
+
original = path4.node.value.raw;
|
|
1324
|
+
} else {
|
|
1325
|
+
original = "";
|
|
1326
|
+
}
|
|
1327
|
+
if (typeof node.start !== "number" || typeof node.end !== "number") {
|
|
1328
|
+
return void 0;
|
|
1329
|
+
}
|
|
1330
|
+
const start = node.start + offset;
|
|
1331
|
+
const end = node.end - offset;
|
|
1332
|
+
if (start >= end) {
|
|
1333
|
+
return void 0;
|
|
1334
|
+
}
|
|
1335
|
+
if (original === updated) {
|
|
1336
|
+
return void 0;
|
|
1337
|
+
}
|
|
1338
|
+
const value = path4.isStringLiteral() ? jsStringEscape2(updated) : updated;
|
|
1339
|
+
return {
|
|
1340
|
+
start,
|
|
1341
|
+
end,
|
|
1342
|
+
value,
|
|
1343
|
+
path: path4
|
|
1344
|
+
};
|
|
1345
|
+
}
|
|
1346
|
+
function handleEvalStringLiteral(path4, options, updater, handler) {
|
|
1347
|
+
const { code } = handler(path4.node.value, {
|
|
1348
|
+
...options,
|
|
1349
|
+
needEscaped: false,
|
|
1350
|
+
generateMap: false
|
|
1351
|
+
});
|
|
1352
|
+
if (!code) {
|
|
1353
|
+
return;
|
|
1354
|
+
}
|
|
1355
|
+
const token = createEvalReplacementToken(path4, code);
|
|
1356
|
+
if (token) {
|
|
1357
|
+
updater.addToken(token);
|
|
1358
|
+
}
|
|
1359
|
+
}
|
|
1360
|
+
function handleEvalTemplateElement(path4, options, updater, handler) {
|
|
1361
|
+
const { code } = handler(path4.node.value.raw, {
|
|
1362
|
+
...options,
|
|
1363
|
+
generateMap: false
|
|
1364
|
+
});
|
|
1365
|
+
if (!code) {
|
|
1366
|
+
return;
|
|
1367
|
+
}
|
|
1368
|
+
const token = createEvalReplacementToken(path4, code);
|
|
1369
|
+
if (token) {
|
|
1370
|
+
updater.addToken(token);
|
|
1371
|
+
}
|
|
1372
|
+
}
|
|
1373
|
+
function walkEvalExpression(path4, options, updater, handler) {
|
|
1374
|
+
const maybeTraverse = path4?.traverse;
|
|
1375
|
+
if (typeof maybeTraverse === "function") {
|
|
1376
|
+
try {
|
|
1377
|
+
return maybeTraverse.call(path4, {
|
|
1378
|
+
StringLiteral(innerPath) {
|
|
1379
|
+
handleEvalStringLiteral(innerPath, options, updater, handler);
|
|
1380
|
+
},
|
|
1381
|
+
TemplateElement(innerPath) {
|
|
1382
|
+
handleEvalTemplateElement(innerPath, options, updater, handler);
|
|
1383
|
+
}
|
|
1384
|
+
});
|
|
1385
|
+
} catch (error) {
|
|
1386
|
+
const msg = error?.message ?? "";
|
|
1387
|
+
const scopeError = /pass a scope and parentPath|traversing a Program\/File/i.test(msg);
|
|
1388
|
+
if (!scopeError) {
|
|
1389
|
+
throw error;
|
|
1390
|
+
}
|
|
1391
|
+
}
|
|
1392
|
+
}
|
|
1393
|
+
const getArgs = path4?.get?.("arguments");
|
|
1394
|
+
if (Array.isArray(getArgs)) {
|
|
1395
|
+
for (const arg of getArgs) {
|
|
1396
|
+
if (arg?.isStringLiteral?.()) {
|
|
1397
|
+
handleEvalStringLiteral(arg, options, updater, handler);
|
|
1356
1398
|
continue;
|
|
1357
1399
|
}
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
const ms = processUpdatedSource(state.source, childOptions, state.analysis);
|
|
1363
|
-
const code = ms.toString();
|
|
1364
|
-
if (code !== state.source) {
|
|
1365
|
-
linked[filename] = { code };
|
|
1400
|
+
if (arg?.isTemplateLiteral?.()) {
|
|
1401
|
+
for (const quasi of arg.get("quasis")) {
|
|
1402
|
+
handleEvalTemplateElement(quasi, options, updater, handler);
|
|
1403
|
+
}
|
|
1366
1404
|
}
|
|
1367
1405
|
}
|
|
1368
|
-
return
|
|
1406
|
+
return;
|
|
1407
|
+
}
|
|
1408
|
+
const nodeArgs = path4?.node?.arguments;
|
|
1409
|
+
if (Array.isArray(nodeArgs)) {
|
|
1410
|
+
for (const n of nodeArgs) {
|
|
1411
|
+
if (n?.type === "StringLiteral") {
|
|
1412
|
+
const stub = {
|
|
1413
|
+
node: n,
|
|
1414
|
+
isStringLiteral: () => true
|
|
1415
|
+
};
|
|
1416
|
+
handleEvalStringLiteral(stub, options, updater, handler);
|
|
1417
|
+
} else if (n?.type === "TemplateLiteral" && Array.isArray(n.quasis)) {
|
|
1418
|
+
for (const q of n.quasis) {
|
|
1419
|
+
const stub = {
|
|
1420
|
+
node: q,
|
|
1421
|
+
isTemplateElement: () => true
|
|
1422
|
+
};
|
|
1423
|
+
handleEvalTemplateElement(stub, options, updater, handler);
|
|
1424
|
+
}
|
|
1425
|
+
}
|
|
1426
|
+
}
|
|
1427
|
+
}
|
|
1428
|
+
}
|
|
1429
|
+
|
|
1430
|
+
// src/js/JsTokenUpdater.ts
|
|
1431
|
+
var JsTokenUpdater = class {
|
|
1432
|
+
constructor({ value } = {}) {
|
|
1433
|
+
this.tokens = value ? [...value] : [];
|
|
1434
|
+
}
|
|
1435
|
+
addToken(token) {
|
|
1436
|
+
if (token) {
|
|
1437
|
+
this.tokens.push(token);
|
|
1438
|
+
}
|
|
1439
|
+
}
|
|
1440
|
+
push(...args) {
|
|
1441
|
+
this.tokens.push(...args);
|
|
1442
|
+
return this;
|
|
1443
|
+
}
|
|
1444
|
+
/**
|
|
1445
|
+
* 待写入的 token 数量。
|
|
1446
|
+
*/
|
|
1447
|
+
get length() {
|
|
1448
|
+
return this.tokens.length;
|
|
1449
|
+
}
|
|
1450
|
+
map(callbackfn) {
|
|
1451
|
+
this.tokens = this.tokens.map(callbackfn);
|
|
1452
|
+
return this;
|
|
1453
|
+
}
|
|
1454
|
+
filter(callbackfn) {
|
|
1455
|
+
this.tokens = this.tokens.filter(callbackfn);
|
|
1456
|
+
return this;
|
|
1457
|
+
}
|
|
1458
|
+
updateMagicString(ms) {
|
|
1459
|
+
for (const { start, end, value } of this.tokens) {
|
|
1460
|
+
ms.update(start, end, value);
|
|
1461
|
+
}
|
|
1462
|
+
return ms;
|
|
1463
|
+
}
|
|
1464
|
+
};
|
|
1465
|
+
|
|
1466
|
+
// src/js/module-graph/ignored-exports.ts
|
|
1467
|
+
var IgnoredExportsTracker = class {
|
|
1468
|
+
constructor(options) {
|
|
1469
|
+
this.options = options;
|
|
1470
|
+
this.ignoredExportNames = /* @__PURE__ */ new Map();
|
|
1369
1471
|
}
|
|
1370
1472
|
addIgnoredExport(filename, exportName) {
|
|
1371
1473
|
if (!exportName) {
|
|
@@ -1380,7 +1482,7 @@ var JsModuleGraph = class {
|
|
|
1380
1482
|
return;
|
|
1381
1483
|
}
|
|
1382
1484
|
pending.add(exportName);
|
|
1383
|
-
const existing = this.modules.get(filename);
|
|
1485
|
+
const existing = this.options.modules.get(filename);
|
|
1384
1486
|
if (existing) {
|
|
1385
1487
|
this.applyIgnoredExportsToAnalysis(filename, existing.analysis);
|
|
1386
1488
|
}
|
|
@@ -1496,7 +1598,7 @@ var JsModuleGraph = class {
|
|
|
1496
1598
|
for (const { specifier, exportName } of propagate) {
|
|
1497
1599
|
let resolved;
|
|
1498
1600
|
try {
|
|
1499
|
-
resolved = this.resolve(specifier, filename);
|
|
1601
|
+
resolved = this.options.resolve(specifier, filename);
|
|
1500
1602
|
} catch {
|
|
1501
1603
|
resolved = void 0;
|
|
1502
1604
|
}
|
|
@@ -1504,7 +1606,7 @@ var JsModuleGraph = class {
|
|
|
1504
1606
|
pending.add(exportName);
|
|
1505
1607
|
continue;
|
|
1506
1608
|
}
|
|
1507
|
-
if (this.filter && !this.filter(resolved, specifier, filename)) {
|
|
1609
|
+
if (this.options.filter && !this.options.filter(resolved, specifier, filename)) {
|
|
1508
1610
|
pending.add(exportName);
|
|
1509
1611
|
continue;
|
|
1510
1612
|
}
|
|
@@ -1514,6 +1616,55 @@ var JsModuleGraph = class {
|
|
|
1514
1616
|
pending.add(name);
|
|
1515
1617
|
}
|
|
1516
1618
|
}
|
|
1619
|
+
};
|
|
1620
|
+
|
|
1621
|
+
// src/js/ModuleGraph.ts
|
|
1622
|
+
var JsModuleGraph = class {
|
|
1623
|
+
constructor(entry, graphOptions) {
|
|
1624
|
+
this.modules = /* @__PURE__ */ new Map();
|
|
1625
|
+
this.queue = [];
|
|
1626
|
+
this.resolve = graphOptions.resolve;
|
|
1627
|
+
this.load = graphOptions.load;
|
|
1628
|
+
this.filter = graphOptions.filter;
|
|
1629
|
+
this.maxDepth = graphOptions.maxDepth ?? Number.POSITIVE_INFINITY;
|
|
1630
|
+
const { moduleGraph: _moduleGraph, filename: _ignoredFilename, ...rest } = entry.handlerOptions;
|
|
1631
|
+
this.baseOptions = {
|
|
1632
|
+
...rest,
|
|
1633
|
+
filename: entry.filename
|
|
1634
|
+
};
|
|
1635
|
+
this.parserOptions = entry.handlerOptions.babelParserOptions;
|
|
1636
|
+
this.rootFilename = entry.filename;
|
|
1637
|
+
this.ignoredExports = new IgnoredExportsTracker({
|
|
1638
|
+
resolve: this.resolve,
|
|
1639
|
+
filter: this.filter,
|
|
1640
|
+
modules: this.modules
|
|
1641
|
+
});
|
|
1642
|
+
this.modules.set(entry.filename, {
|
|
1643
|
+
filename: entry.filename,
|
|
1644
|
+
source: entry.source,
|
|
1645
|
+
analysis: entry.analysis
|
|
1646
|
+
});
|
|
1647
|
+
this.queue.push({ filename: entry.filename, depth: 0 });
|
|
1648
|
+
}
|
|
1649
|
+
build() {
|
|
1650
|
+
this.collectDependencies();
|
|
1651
|
+
const linked = {};
|
|
1652
|
+
for (const [filename, state] of this.modules) {
|
|
1653
|
+
if (filename === this.rootFilename) {
|
|
1654
|
+
continue;
|
|
1655
|
+
}
|
|
1656
|
+
const childOptions = {
|
|
1657
|
+
...this.baseOptions,
|
|
1658
|
+
filename
|
|
1659
|
+
};
|
|
1660
|
+
const ms = processUpdatedSource(state.source, childOptions, state.analysis);
|
|
1661
|
+
const code = ms.toString();
|
|
1662
|
+
if (code !== state.source) {
|
|
1663
|
+
linked[filename] = { code };
|
|
1664
|
+
}
|
|
1665
|
+
}
|
|
1666
|
+
return linked;
|
|
1667
|
+
}
|
|
1517
1668
|
collectDependencies() {
|
|
1518
1669
|
while (this.queue.length > 0) {
|
|
1519
1670
|
const { filename, depth } = this.queue.shift();
|
|
@@ -1553,7 +1704,7 @@ var JsModuleGraph = class {
|
|
|
1553
1704
|
continue;
|
|
1554
1705
|
}
|
|
1555
1706
|
if (tokens.length > 0) {
|
|
1556
|
-
this.registerIgnoredExportsFromTokens(resolved, tokens);
|
|
1707
|
+
this.ignoredExports.registerIgnoredExportsFromTokens(resolved, tokens);
|
|
1557
1708
|
}
|
|
1558
1709
|
if (this.modules.has(resolved)) {
|
|
1559
1710
|
continue;
|
|
@@ -1577,7 +1728,7 @@ var JsModuleGraph = class {
|
|
|
1577
1728
|
...this.baseOptions,
|
|
1578
1729
|
filename: resolved
|
|
1579
1730
|
});
|
|
1580
|
-
this.applyIgnoredExportsToAnalysis(resolved, analysis);
|
|
1731
|
+
this.ignoredExports.applyIgnoredExportsToAnalysis(resolved, analysis);
|
|
1581
1732
|
} catch {
|
|
1582
1733
|
continue;
|
|
1583
1734
|
}
|
|
@@ -1592,6 +1743,93 @@ var JsModuleGraph = class {
|
|
|
1592
1743
|
}
|
|
1593
1744
|
};
|
|
1594
1745
|
|
|
1746
|
+
// src/js/node-path-walker/export-handlers.ts
|
|
1747
|
+
function walkExportDeclaration(ctx, path4) {
|
|
1748
|
+
if (path4.isExportDeclaration()) {
|
|
1749
|
+
if (path4.isExportNamedDeclaration()) {
|
|
1750
|
+
walkExportNamedDeclaration(ctx, path4);
|
|
1751
|
+
} else if (path4.isExportDefaultDeclaration()) {
|
|
1752
|
+
walkExportDefaultDeclaration(ctx, path4);
|
|
1753
|
+
} else if (path4.isExportAllDeclaration()) {
|
|
1754
|
+
walkExportAllDeclaration(ctx, path4);
|
|
1755
|
+
}
|
|
1756
|
+
}
|
|
1757
|
+
}
|
|
1758
|
+
function walkExportNamedDeclaration(ctx, path4) {
|
|
1759
|
+
const declaration = path4.get("declaration");
|
|
1760
|
+
if (declaration.isVariableDeclaration()) {
|
|
1761
|
+
for (const decl of declaration.get("declarations")) {
|
|
1762
|
+
ctx.walkNode(decl);
|
|
1763
|
+
}
|
|
1764
|
+
}
|
|
1765
|
+
const specifiers = path4.get("specifiers");
|
|
1766
|
+
for (const spec of specifiers) {
|
|
1767
|
+
if (spec.isExportSpecifier()) {
|
|
1768
|
+
const local = spec.get("local");
|
|
1769
|
+
if (local.isIdentifier()) {
|
|
1770
|
+
ctx.walkNode(local);
|
|
1771
|
+
}
|
|
1772
|
+
}
|
|
1773
|
+
}
|
|
1774
|
+
}
|
|
1775
|
+
function walkExportDefaultDeclaration(ctx, path4) {
|
|
1776
|
+
const decl = path4.get("declaration");
|
|
1777
|
+
if (decl.isIdentifier()) {
|
|
1778
|
+
ctx.walkNode(decl);
|
|
1779
|
+
} else {
|
|
1780
|
+
ctx.walkNode(decl);
|
|
1781
|
+
}
|
|
1782
|
+
}
|
|
1783
|
+
function walkExportAllDeclaration(ctx, path4) {
|
|
1784
|
+
const source = path4.get("source");
|
|
1785
|
+
if (source.isStringLiteral()) {
|
|
1786
|
+
ctx.imports.add(
|
|
1787
|
+
{
|
|
1788
|
+
declaration: path4,
|
|
1789
|
+
source: source.node.value,
|
|
1790
|
+
type: "ExportAllDeclaration"
|
|
1791
|
+
}
|
|
1792
|
+
);
|
|
1793
|
+
}
|
|
1794
|
+
}
|
|
1795
|
+
|
|
1796
|
+
// src/js/node-path-walker/import-tokens.ts
|
|
1797
|
+
function maybeAddImportToken(imports, arg) {
|
|
1798
|
+
if (!(arg.isImportSpecifier() && arg.node.importKind !== "type" || arg.isImportDefaultSpecifier())) {
|
|
1799
|
+
return false;
|
|
1800
|
+
}
|
|
1801
|
+
const importDeclaration = arg.parentPath;
|
|
1802
|
+
if (!importDeclaration.isImportDeclaration() || importDeclaration.node.importKind === "type") {
|
|
1803
|
+
return false;
|
|
1804
|
+
}
|
|
1805
|
+
if (arg.isImportSpecifier()) {
|
|
1806
|
+
const imported = arg.get("imported");
|
|
1807
|
+
if (imported.isIdentifier()) {
|
|
1808
|
+
imports.add(
|
|
1809
|
+
{
|
|
1810
|
+
declaration: importDeclaration,
|
|
1811
|
+
specifier: arg,
|
|
1812
|
+
imported: imported.node.name,
|
|
1813
|
+
local: arg.node.local.name,
|
|
1814
|
+
source: importDeclaration.node.source.value,
|
|
1815
|
+
type: "ImportSpecifier"
|
|
1816
|
+
}
|
|
1817
|
+
);
|
|
1818
|
+
}
|
|
1819
|
+
return true;
|
|
1820
|
+
}
|
|
1821
|
+
imports.add(
|
|
1822
|
+
{
|
|
1823
|
+
declaration: importDeclaration,
|
|
1824
|
+
specifier: arg,
|
|
1825
|
+
local: arg.node.local.name,
|
|
1826
|
+
source: importDeclaration.node.source.value,
|
|
1827
|
+
type: "ImportDefaultSpecifier"
|
|
1828
|
+
}
|
|
1829
|
+
);
|
|
1830
|
+
return true;
|
|
1831
|
+
}
|
|
1832
|
+
|
|
1595
1833
|
// src/js/NodePathWalker.ts
|
|
1596
1834
|
var NodePathWalker = class {
|
|
1597
1835
|
constructor({ ignoreCallExpressionIdentifiers, callback } = {}) {
|
|
@@ -1602,35 +1840,35 @@ var NodePathWalker = class {
|
|
|
1602
1840
|
this.visited = /* @__PURE__ */ new WeakSet();
|
|
1603
1841
|
this.isIgnoredCallIdentifier = createNameMatcher(this.ignoreCallExpressionIdentifiers, { exact: true });
|
|
1604
1842
|
}
|
|
1605
|
-
walkVariableDeclarator(
|
|
1606
|
-
const init =
|
|
1843
|
+
walkVariableDeclarator(path4) {
|
|
1844
|
+
const init = path4.get("init");
|
|
1607
1845
|
this.walkNode(init);
|
|
1608
1846
|
}
|
|
1609
|
-
walkTemplateLiteral(
|
|
1610
|
-
for (const exp of
|
|
1847
|
+
walkTemplateLiteral(path4) {
|
|
1848
|
+
for (const exp of path4.get("expressions")) {
|
|
1611
1849
|
this.walkNode(exp);
|
|
1612
1850
|
}
|
|
1613
|
-
for (const quasis of
|
|
1851
|
+
for (const quasis of path4.get("quasis")) {
|
|
1614
1852
|
this.callback(quasis);
|
|
1615
1853
|
}
|
|
1616
1854
|
}
|
|
1617
|
-
walkStringLiteral(
|
|
1618
|
-
this.callback(
|
|
1855
|
+
walkStringLiteral(path4) {
|
|
1856
|
+
this.callback(path4);
|
|
1619
1857
|
}
|
|
1620
|
-
walkBinaryExpression(
|
|
1621
|
-
const left =
|
|
1858
|
+
walkBinaryExpression(path4) {
|
|
1859
|
+
const left = path4.get("left");
|
|
1622
1860
|
this.walkNode(left);
|
|
1623
|
-
const right =
|
|
1861
|
+
const right = path4.get("right");
|
|
1624
1862
|
this.walkNode(right);
|
|
1625
1863
|
}
|
|
1626
|
-
walkLogicalExpression(
|
|
1627
|
-
const left =
|
|
1864
|
+
walkLogicalExpression(path4) {
|
|
1865
|
+
const left = path4.get("left");
|
|
1628
1866
|
this.walkNode(left);
|
|
1629
|
-
const right =
|
|
1867
|
+
const right = path4.get("right");
|
|
1630
1868
|
this.walkNode(right);
|
|
1631
1869
|
}
|
|
1632
|
-
walkObjectExpression(
|
|
1633
|
-
const props =
|
|
1870
|
+
walkObjectExpression(path4) {
|
|
1871
|
+
const props = path4.get("properties");
|
|
1634
1872
|
for (const prop of props) {
|
|
1635
1873
|
if (prop.isObjectProperty()) {
|
|
1636
1874
|
const key = prop.get("key");
|
|
@@ -1640,8 +1878,8 @@ var NodePathWalker = class {
|
|
|
1640
1878
|
}
|
|
1641
1879
|
}
|
|
1642
1880
|
}
|
|
1643
|
-
walkArrayExpression(
|
|
1644
|
-
const elements =
|
|
1881
|
+
walkArrayExpression(path4) {
|
|
1882
|
+
const elements = path4.get("elements");
|
|
1645
1883
|
for (const element of elements) {
|
|
1646
1884
|
this.walkNode(element);
|
|
1647
1885
|
}
|
|
@@ -1680,157 +1918,34 @@ var NodePathWalker = class {
|
|
|
1680
1918
|
this.walkArrayExpression(arg);
|
|
1681
1919
|
} else if (arg.isVariableDeclarator()) {
|
|
1682
1920
|
this.walkVariableDeclarator(arg);
|
|
1683
|
-
} else if (
|
|
1684
|
-
const importDeclaration = arg.parentPath;
|
|
1685
|
-
if (importDeclaration.isImportDeclaration() && importDeclaration.node.importKind !== "type") {
|
|
1686
|
-
if (arg.isImportSpecifier()) {
|
|
1687
|
-
const imported = arg.get("imported");
|
|
1688
|
-
if (imported.isIdentifier()) {
|
|
1689
|
-
this.imports.add(
|
|
1690
|
-
{
|
|
1691
|
-
declaration: importDeclaration,
|
|
1692
|
-
specifier: arg,
|
|
1693
|
-
imported: imported.node.name,
|
|
1694
|
-
local: arg.node.local.name,
|
|
1695
|
-
source: importDeclaration.node.source.value,
|
|
1696
|
-
type: "ImportSpecifier"
|
|
1697
|
-
}
|
|
1698
|
-
);
|
|
1699
|
-
}
|
|
1700
|
-
} else if (arg.isImportDefaultSpecifier()) {
|
|
1701
|
-
this.imports.add(
|
|
1702
|
-
{
|
|
1703
|
-
declaration: importDeclaration,
|
|
1704
|
-
specifier: arg,
|
|
1705
|
-
local: arg.node.local.name,
|
|
1706
|
-
source: importDeclaration.node.source.value,
|
|
1707
|
-
type: "ImportDefaultSpecifier"
|
|
1708
|
-
}
|
|
1709
|
-
);
|
|
1710
|
-
}
|
|
1711
|
-
}
|
|
1921
|
+
} else if (maybeAddImportToken(this.imports, arg)) {
|
|
1712
1922
|
}
|
|
1713
1923
|
}
|
|
1714
1924
|
/**
|
|
1715
1925
|
* Walk the arguments of a desired call expression so their bindings can be analysed.
|
|
1716
1926
|
*/
|
|
1717
|
-
walkCallExpression(
|
|
1718
|
-
const calleePath =
|
|
1927
|
+
walkCallExpression(path4) {
|
|
1928
|
+
const calleePath = path4.get("callee");
|
|
1719
1929
|
if (calleePath.isIdentifier() && this.isIgnoredCallIdentifier(calleePath.node.name)) {
|
|
1720
|
-
for (const arg of
|
|
1930
|
+
for (const arg of path4.get("arguments")) {
|
|
1721
1931
|
this.walkNode(arg);
|
|
1722
1932
|
}
|
|
1723
1933
|
}
|
|
1724
1934
|
}
|
|
1725
|
-
walkExportDeclaration(
|
|
1726
|
-
|
|
1727
|
-
if (path2.isExportNamedDeclaration()) {
|
|
1728
|
-
this.walkExportNamedDeclaration(path2);
|
|
1729
|
-
} else if (path2.isExportDefaultDeclaration()) {
|
|
1730
|
-
this.walkExportDefaultDeclaration(path2);
|
|
1731
|
-
} else if (path2.isExportAllDeclaration()) {
|
|
1732
|
-
this.walkExportAllDeclaration(path2);
|
|
1733
|
-
}
|
|
1734
|
-
}
|
|
1935
|
+
walkExportDeclaration(path4) {
|
|
1936
|
+
walkExportDeclaration(this, path4);
|
|
1735
1937
|
}
|
|
1736
|
-
walkExportNamedDeclaration(
|
|
1737
|
-
|
|
1738
|
-
if (declaration.isVariableDeclaration()) {
|
|
1739
|
-
for (const decl of declaration.get("declarations")) {
|
|
1740
|
-
this.walkNode(decl);
|
|
1741
|
-
}
|
|
1742
|
-
}
|
|
1743
|
-
const specifiers = path2.get("specifiers");
|
|
1744
|
-
for (const spec of specifiers) {
|
|
1745
|
-
if (spec.isExportSpecifier()) {
|
|
1746
|
-
const local = spec.get("local");
|
|
1747
|
-
if (local.isIdentifier()) {
|
|
1748
|
-
this.walkNode(local);
|
|
1749
|
-
}
|
|
1750
|
-
}
|
|
1751
|
-
}
|
|
1938
|
+
walkExportNamedDeclaration(path4) {
|
|
1939
|
+
walkExportNamedDeclaration(this, path4);
|
|
1752
1940
|
}
|
|
1753
|
-
walkExportDefaultDeclaration(
|
|
1754
|
-
|
|
1755
|
-
if (decl.isIdentifier()) {
|
|
1756
|
-
this.walkNode(decl);
|
|
1757
|
-
} else {
|
|
1758
|
-
this.walkNode(decl);
|
|
1759
|
-
}
|
|
1941
|
+
walkExportDefaultDeclaration(path4) {
|
|
1942
|
+
walkExportDefaultDeclaration(this, path4);
|
|
1760
1943
|
}
|
|
1761
|
-
walkExportAllDeclaration(
|
|
1762
|
-
|
|
1763
|
-
if (source.isStringLiteral()) {
|
|
1764
|
-
this.imports.add(
|
|
1765
|
-
{
|
|
1766
|
-
declaration: path2,
|
|
1767
|
-
source: source.node.value,
|
|
1768
|
-
type: "ExportAllDeclaration"
|
|
1769
|
-
}
|
|
1770
|
-
);
|
|
1771
|
-
}
|
|
1944
|
+
walkExportAllDeclaration(path4) {
|
|
1945
|
+
walkExportAllDeclaration(this, path4);
|
|
1772
1946
|
}
|
|
1773
1947
|
};
|
|
1774
1948
|
|
|
1775
|
-
// src/js/sourceAnalysis.ts
|
|
1776
|
-
function createModuleSpecifierReplacementToken(path2, replacement) {
|
|
1777
|
-
const node = path2.node;
|
|
1778
|
-
if (node.value === replacement) {
|
|
1779
|
-
return void 0;
|
|
1780
|
-
}
|
|
1781
|
-
if (typeof node.start !== "number" || typeof node.end !== "number") {
|
|
1782
|
-
return void 0;
|
|
1783
|
-
}
|
|
1784
|
-
const start = node.start + 1;
|
|
1785
|
-
const end = node.end - 1;
|
|
1786
|
-
if (start >= end) {
|
|
1787
|
-
return void 0;
|
|
1788
|
-
}
|
|
1789
|
-
return {
|
|
1790
|
-
start,
|
|
1791
|
-
end,
|
|
1792
|
-
value: replacement,
|
|
1793
|
-
path: path2
|
|
1794
|
-
};
|
|
1795
|
-
}
|
|
1796
|
-
function collectModuleSpecifierReplacementTokens(analysis, replacements) {
|
|
1797
|
-
const tokens = [];
|
|
1798
|
-
const applyReplacement = (path2) => {
|
|
1799
|
-
const replacement = replacements[path2.node.value];
|
|
1800
|
-
if (!replacement) {
|
|
1801
|
-
return;
|
|
1802
|
-
}
|
|
1803
|
-
const token = createModuleSpecifierReplacementToken(path2, replacement);
|
|
1804
|
-
if (token) {
|
|
1805
|
-
tokens.push(token);
|
|
1806
|
-
}
|
|
1807
|
-
};
|
|
1808
|
-
for (const importPath of analysis.importDeclarations) {
|
|
1809
|
-
const source = importPath.get("source");
|
|
1810
|
-
if (source.isStringLiteral()) {
|
|
1811
|
-
applyReplacement(source);
|
|
1812
|
-
}
|
|
1813
|
-
}
|
|
1814
|
-
for (const exportPath of analysis.exportDeclarations) {
|
|
1815
|
-
if (exportPath.isExportNamedDeclaration() || exportPath.isExportAllDeclaration()) {
|
|
1816
|
-
const source = exportPath.get("source");
|
|
1817
|
-
if (source && !Array.isArray(source) && source.isStringLiteral()) {
|
|
1818
|
-
applyReplacement(source);
|
|
1819
|
-
}
|
|
1820
|
-
}
|
|
1821
|
-
}
|
|
1822
|
-
for (const literalPath of analysis.requireCallPaths) {
|
|
1823
|
-
applyReplacement(literalPath);
|
|
1824
|
-
}
|
|
1825
|
-
for (const token of analysis.walker.imports) {
|
|
1826
|
-
const replacement = replacements[token.source];
|
|
1827
|
-
if (replacement) {
|
|
1828
|
-
token.source = replacement;
|
|
1829
|
-
}
|
|
1830
|
-
}
|
|
1831
|
-
return tokens;
|
|
1832
|
-
}
|
|
1833
|
-
|
|
1834
1949
|
// src/js/taggedTemplateIgnore.ts
|
|
1835
1950
|
function createTaggedTemplateIgnore({ matcher, names }) {
|
|
1836
1951
|
const bindingIgnoreCache = /* @__PURE__ */ new Map();
|
|
@@ -1852,12 +1967,12 @@ function createTaggedTemplateIgnore({ matcher, names }) {
|
|
|
1852
1967
|
}
|
|
1853
1968
|
return false;
|
|
1854
1969
|
};
|
|
1855
|
-
const resolvesMemberExpressionToIgnore = (
|
|
1856
|
-
const propertyPath =
|
|
1970
|
+
const resolvesMemberExpressionToIgnore = (path4, seen) => {
|
|
1971
|
+
const propertyPath = path4.get("property");
|
|
1857
1972
|
if (propertyMatches(propertyPath)) {
|
|
1858
1973
|
return true;
|
|
1859
1974
|
}
|
|
1860
|
-
const objectPath =
|
|
1975
|
+
const objectPath = path4.get("object");
|
|
1861
1976
|
if (objectPath.isIdentifier()) {
|
|
1862
1977
|
const scope = objectPath?.scope;
|
|
1863
1978
|
const binding = scope?.getBinding?.(objectPath.node.name);
|
|
@@ -1985,42 +2100,15 @@ function createTaggedTemplateIgnore({ matcher, names }) {
|
|
|
1985
2100
|
// src/js/babel.ts
|
|
1986
2101
|
var EXPRESSION_WRAPPER_PREFIX = "(\n";
|
|
1987
2102
|
var EXPRESSION_WRAPPER_SUFFIX = "\n)";
|
|
1988
|
-
var parseCache = new LRUCache2(
|
|
1989
|
-
{
|
|
1990
|
-
max: 1024
|
|
1991
|
-
}
|
|
1992
|
-
);
|
|
1993
|
-
function genCacheKey(source, options) {
|
|
1994
|
-
if (typeof options === "string") {
|
|
1995
|
-
return source + options;
|
|
1996
|
-
}
|
|
1997
|
-
return source + JSON.stringify(options, (_, val) => typeof val === "function" ? val.toString() : val);
|
|
1998
|
-
}
|
|
1999
|
-
function babelParse(code, opts = {}) {
|
|
2000
|
-
const { cache, cacheKey, ...rest } = opts;
|
|
2001
|
-
const cacheKeyString = genCacheKey(code, cacheKey ?? rest);
|
|
2002
|
-
let result;
|
|
2003
|
-
if (cache) {
|
|
2004
|
-
result = parseCache.get(cacheKeyString);
|
|
2005
|
-
}
|
|
2006
|
-
if (!result) {
|
|
2007
|
-
const { cache: _cache, cacheKey: _cacheKey, ...parseOptions } = opts;
|
|
2008
|
-
result = parse(code, parseOptions);
|
|
2009
|
-
if (cache) {
|
|
2010
|
-
parseCache.set(cacheKeyString, result);
|
|
2011
|
-
}
|
|
2012
|
-
}
|
|
2013
|
-
return result;
|
|
2014
|
-
}
|
|
2015
2103
|
function analyzeSource(ast, options, handler) {
|
|
2016
2104
|
const jsTokenUpdater = new JsTokenUpdater();
|
|
2017
2105
|
const ignoredPaths = /* @__PURE__ */ new WeakSet();
|
|
2018
2106
|
const walker = new NodePathWalker(
|
|
2019
2107
|
{
|
|
2020
2108
|
ignoreCallExpressionIdentifiers: options.ignoreCallExpressionIdentifiers,
|
|
2021
|
-
callback(
|
|
2022
|
-
if (
|
|
2023
|
-
ignoredPaths.add(
|
|
2109
|
+
callback(path4) {
|
|
2110
|
+
if (path4.isStringLiteral() || path4.isTemplateElement()) {
|
|
2111
|
+
ignoredPaths.add(path4);
|
|
2024
2112
|
}
|
|
2025
2113
|
}
|
|
2026
2114
|
}
|
|
@@ -2107,36 +2195,6 @@ function analyzeSource(ast, options, handler) {
|
|
|
2107
2195
|
ignoredPaths
|
|
2108
2196
|
};
|
|
2109
2197
|
}
|
|
2110
|
-
function processUpdatedSource(rawSource, options, analysis) {
|
|
2111
|
-
const { targetPaths, jsTokenUpdater, ignoredPaths } = analysis;
|
|
2112
|
-
const replacementTokens = [];
|
|
2113
|
-
for (const path2 of targetPaths) {
|
|
2114
|
-
if (ignoredPaths.has(path2)) {
|
|
2115
|
-
continue;
|
|
2116
|
-
}
|
|
2117
|
-
const token = replaceHandleValue(
|
|
2118
|
-
path2,
|
|
2119
|
-
{
|
|
2120
|
-
...options,
|
|
2121
|
-
needEscaped: path2.isStringLiteral() ? options.needEscaped ?? true : false
|
|
2122
|
-
}
|
|
2123
|
-
);
|
|
2124
|
-
if (token) {
|
|
2125
|
-
replacementTokens.push(token);
|
|
2126
|
-
}
|
|
2127
|
-
}
|
|
2128
|
-
if (options.moduleSpecifierReplacements) {
|
|
2129
|
-
replacementTokens.push(
|
|
2130
|
-
...collectModuleSpecifierReplacementTokens(analysis, options.moduleSpecifierReplacements)
|
|
2131
|
-
);
|
|
2132
|
-
}
|
|
2133
|
-
if (jsTokenUpdater.length + replacementTokens.length === 0) {
|
|
2134
|
-
return new MagicString(rawSource);
|
|
2135
|
-
}
|
|
2136
|
-
const ms = new MagicString(rawSource);
|
|
2137
|
-
jsTokenUpdater.push(...replacementTokens).filter((token) => !ignoredPaths.has(token.path)).updateMagicString(ms);
|
|
2138
|
-
return ms;
|
|
2139
|
-
}
|
|
2140
2198
|
function jsHandler(rawSource, options) {
|
|
2141
2199
|
const shouldWrapExpression = Boolean(options.wrapExpression);
|
|
2142
2200
|
const source = shouldWrapExpression ? `${EXPRESSION_WRAPPER_PREFIX}${rawSource}${EXPRESSION_WRAPPER_SUFFIX}` : rawSource;
|
|
@@ -2227,11 +2285,6 @@ function createJsHandler(options) {
|
|
|
2227
2285
|
return handler;
|
|
2228
2286
|
}
|
|
2229
2287
|
|
|
2230
|
-
// src/wxml/utils.ts
|
|
2231
|
-
import * as t from "@babel/types";
|
|
2232
|
-
import { Parser } from "htmlparser2";
|
|
2233
|
-
import MagicString2 from "magic-string";
|
|
2234
|
-
|
|
2235
2288
|
// src/wxml/custom-attributes.ts
|
|
2236
2289
|
function regTest(reg, str) {
|
|
2237
2290
|
reg.lastIndex = 0;
|
|
@@ -2301,6 +2354,78 @@ function createAttributeMatcher(entities) {
|
|
|
2301
2354
|
};
|
|
2302
2355
|
}
|
|
2303
2356
|
|
|
2357
|
+
// src/wxml/utils/codegen/legacy-rewriter.ts
|
|
2358
|
+
import MagicString2 from "magic-string";
|
|
2359
|
+
|
|
2360
|
+
// src/wxml/utils/codegen/legacy-visitor.ts
|
|
2361
|
+
import * as t from "@babel/types";
|
|
2362
|
+
function shouldSkipLegacyStringLiteral(path4) {
|
|
2363
|
+
if (t.isMemberExpression(path4.parent)) {
|
|
2364
|
+
return true;
|
|
2365
|
+
}
|
|
2366
|
+
return Boolean(
|
|
2367
|
+
t.isBinaryExpression(path4.parent) && (t.isConditionalExpression(path4.parentPath?.parent) || t.isLogicalExpression(path4.parentPath?.parent))
|
|
2368
|
+
);
|
|
2369
|
+
}
|
|
2370
|
+
function createLegacyTraverseOptions(options, jsTokenUpdater) {
|
|
2371
|
+
return {
|
|
2372
|
+
StringLiteral(path4) {
|
|
2373
|
+
if (shouldSkipLegacyStringLiteral(path4)) {
|
|
2374
|
+
return;
|
|
2375
|
+
}
|
|
2376
|
+
jsTokenUpdater.addToken(
|
|
2377
|
+
replaceHandleValue(
|
|
2378
|
+
path4,
|
|
2379
|
+
{
|
|
2380
|
+
escapeMap: options.escapeMap,
|
|
2381
|
+
classNameSet: options.runtimeSet,
|
|
2382
|
+
needEscaped: true,
|
|
2383
|
+
alwaysEscape: true
|
|
2384
|
+
}
|
|
2385
|
+
)
|
|
2386
|
+
);
|
|
2387
|
+
},
|
|
2388
|
+
noScope: true
|
|
2389
|
+
};
|
|
2390
|
+
}
|
|
2391
|
+
|
|
2392
|
+
// src/wxml/utils/codegen/legacy-rewriter.ts
|
|
2393
|
+
function rewriteLegacyExpression(match, options) {
|
|
2394
|
+
const ms = new MagicString2(match);
|
|
2395
|
+
const ast = parseExpression(match);
|
|
2396
|
+
const jsTokenUpdater = new JsTokenUpdater();
|
|
2397
|
+
traverse(ast, createLegacyTraverseOptions(options, jsTokenUpdater));
|
|
2398
|
+
jsTokenUpdater.updateMagicString(ms);
|
|
2399
|
+
return ms.toString();
|
|
2400
|
+
}
|
|
2401
|
+
|
|
2402
|
+
// src/wxml/utils/codegen.ts
|
|
2403
|
+
function generateCode(match, options = {}) {
|
|
2404
|
+
try {
|
|
2405
|
+
const { jsHandler: jsHandler2, runtimeSet } = options;
|
|
2406
|
+
if (jsHandler2 && runtimeSet) {
|
|
2407
|
+
const runHandler = (wrap) => jsHandler2(match, runtimeSet, wrap ? { wrapExpression: true } : void 0);
|
|
2408
|
+
const initial = runHandler(options.wrapExpression);
|
|
2409
|
+
if (!initial.error || options.wrapExpression) {
|
|
2410
|
+
return initial.code;
|
|
2411
|
+
}
|
|
2412
|
+
const fallback = runHandler(true);
|
|
2413
|
+
return fallback.code;
|
|
2414
|
+
} else {
|
|
2415
|
+
return rewriteLegacyExpression(match, options);
|
|
2416
|
+
}
|
|
2417
|
+
} catch {
|
|
2418
|
+
return match;
|
|
2419
|
+
}
|
|
2420
|
+
}
|
|
2421
|
+
|
|
2422
|
+
// src/wxml/utils/custom-template.ts
|
|
2423
|
+
import { Parser } from "htmlparser2";
|
|
2424
|
+
import MagicString4 from "magic-string";
|
|
2425
|
+
|
|
2426
|
+
// src/wxml/utils/template-fragments.ts
|
|
2427
|
+
import MagicString3 from "magic-string";
|
|
2428
|
+
|
|
2304
2429
|
// src/wxml/whitespace.ts
|
|
2305
2430
|
var WHITESPACE_CODES = /* @__PURE__ */ new Set([
|
|
2306
2431
|
9,
|
|
@@ -2452,117 +2577,71 @@ var Tokenizer = class {
|
|
|
2452
2577
|
}
|
|
2453
2578
|
};
|
|
2454
2579
|
|
|
2455
|
-
// src/wxml/utils.ts
|
|
2456
|
-
function
|
|
2457
|
-
|
|
2458
|
-
|
|
2459
|
-
|
|
2460
|
-
|
|
2461
|
-
|
|
2462
|
-
|
|
2463
|
-
|
|
2464
|
-
|
|
2465
|
-
|
|
2466
|
-
|
|
2467
|
-
|
|
2468
|
-
|
|
2469
|
-
|
|
2470
|
-
|
|
2471
|
-
|
|
2472
|
-
StringLiteral(path2) {
|
|
2473
|
-
if (t.isMemberExpression(path2.parent)) {
|
|
2474
|
-
return;
|
|
2475
|
-
}
|
|
2476
|
-
if (t.isBinaryExpression(path2.parent) && (t.isConditionalExpression(path2.parentPath?.parent) || t.isLogicalExpression(path2.parentPath?.parent))) {
|
|
2477
|
-
return;
|
|
2478
|
-
}
|
|
2479
|
-
jsTokenUpdater.addToken(
|
|
2480
|
-
replaceHandleValue(
|
|
2481
|
-
path2,
|
|
2482
|
-
{
|
|
2483
|
-
escapeMap: options.escapeMap,
|
|
2484
|
-
classNameSet: options.runtimeSet,
|
|
2485
|
-
needEscaped: true,
|
|
2486
|
-
alwaysEscape: true
|
|
2487
|
-
}
|
|
2488
|
-
)
|
|
2489
|
-
);
|
|
2490
|
-
},
|
|
2491
|
-
noScope: true
|
|
2492
|
-
});
|
|
2493
|
-
jsTokenUpdater.updateMagicString(ms);
|
|
2494
|
-
return ms.toString();
|
|
2495
|
-
}
|
|
2496
|
-
} catch {
|
|
2497
|
-
return match;
|
|
2498
|
-
}
|
|
2580
|
+
// src/wxml/utils/fragment-helpers.ts
|
|
2581
|
+
function updateWhitespaceGap(ms, start, end, options) {
|
|
2582
|
+
const gap = ms.slice(start, end);
|
|
2583
|
+
if (isAllWhitespace(gap)) {
|
|
2584
|
+
ms.update(start, end, replaceWxml(gap, {
|
|
2585
|
+
keepEOL: false,
|
|
2586
|
+
escapeMap: options.escapeMap,
|
|
2587
|
+
ignoreHead: true
|
|
2588
|
+
}));
|
|
2589
|
+
}
|
|
2590
|
+
}
|
|
2591
|
+
function updateWxmlSegment(ms, start, end, options, keepEOL, ignoreHead) {
|
|
2592
|
+
ms.update(start, end, replaceWxml(ms.slice(start, end), {
|
|
2593
|
+
keepEOL,
|
|
2594
|
+
escapeMap: options.escapeMap,
|
|
2595
|
+
ignoreHead
|
|
2596
|
+
}));
|
|
2499
2597
|
}
|
|
2598
|
+
function updateExpressionSegment(ms, exp, options) {
|
|
2599
|
+
const code = `{{${generateCode(exp.value.slice(2, -2), options)}}}`;
|
|
2600
|
+
ms.update(exp.start, exp.end, code);
|
|
2601
|
+
}
|
|
2602
|
+
|
|
2603
|
+
// src/wxml/utils/fragment-updater.ts
|
|
2500
2604
|
function handleEachClassFragment(ms, tokens, options = {}) {
|
|
2501
2605
|
let previousEnd = 0;
|
|
2502
2606
|
for (const token of tokens) {
|
|
2503
2607
|
if (token.start > previousEnd) {
|
|
2504
|
-
|
|
2505
|
-
if (isAllWhitespace(gap)) {
|
|
2506
|
-
ms.update(previousEnd, token.start, replaceWxml(gap, {
|
|
2507
|
-
keepEOL: false,
|
|
2508
|
-
escapeMap: options.escapeMap,
|
|
2509
|
-
ignoreHead: true
|
|
2510
|
-
}));
|
|
2511
|
-
}
|
|
2608
|
+
updateWhitespaceGap(ms, previousEnd, token.start, options);
|
|
2512
2609
|
}
|
|
2513
2610
|
let p = token.start;
|
|
2514
2611
|
if (token.expressions.length > 0) {
|
|
2515
2612
|
for (const exp of token.expressions) {
|
|
2516
2613
|
if (exp.start > token.start && p < exp.start) {
|
|
2517
|
-
ms
|
|
2518
|
-
keepEOL: true,
|
|
2519
|
-
escapeMap: options.escapeMap,
|
|
2520
|
-
// 首的str才会被转译
|
|
2521
|
-
// example: 2xl:xx 2x{{y}}
|
|
2522
|
-
ignoreHead: p > 0
|
|
2523
|
-
}));
|
|
2614
|
+
updateWxmlSegment(ms, p, exp.start, options, true, p > 0);
|
|
2524
2615
|
}
|
|
2525
|
-
|
|
2526
|
-
ms.update(exp.start, exp.end, code);
|
|
2616
|
+
updateExpressionSegment(ms, exp, options);
|
|
2527
2617
|
p = exp.end;
|
|
2528
2618
|
}
|
|
2529
2619
|
if (token.end > p) {
|
|
2530
|
-
ms
|
|
2531
|
-
keepEOL: false,
|
|
2532
|
-
escapeMap: options.escapeMap,
|
|
2533
|
-
ignoreHead: true
|
|
2534
|
-
}));
|
|
2620
|
+
updateWxmlSegment(ms, p, token.end, options, false, true);
|
|
2535
2621
|
}
|
|
2536
2622
|
} else {
|
|
2537
|
-
ms
|
|
2538
|
-
keepEOL: false,
|
|
2539
|
-
escapeMap: options.escapeMap,
|
|
2540
|
-
ignoreHead: false
|
|
2541
|
-
}));
|
|
2623
|
+
updateWxmlSegment(ms, token.start, token.end, options, false, false);
|
|
2542
2624
|
}
|
|
2543
2625
|
previousEnd = token.end;
|
|
2544
2626
|
}
|
|
2545
2627
|
if (tokens.length > 0) {
|
|
2546
2628
|
const lastToken = tokens[tokens.length - 1];
|
|
2547
2629
|
if (lastToken.end < ms.original.length) {
|
|
2548
|
-
|
|
2549
|
-
if (isAllWhitespace(gap)) {
|
|
2550
|
-
ms.update(lastToken.end, ms.original.length, replaceWxml(gap, {
|
|
2551
|
-
keepEOL: false,
|
|
2552
|
-
escapeMap: options.escapeMap,
|
|
2553
|
-
ignoreHead: true
|
|
2554
|
-
}));
|
|
2555
|
-
}
|
|
2630
|
+
updateWhitespaceGap(ms, lastToken.end, ms.original.length, options);
|
|
2556
2631
|
}
|
|
2557
2632
|
}
|
|
2558
2633
|
}
|
|
2634
|
+
|
|
2635
|
+
// src/wxml/utils/template-fragments.ts
|
|
2559
2636
|
function templateReplacer(original, options = {}) {
|
|
2560
|
-
const ms = new
|
|
2637
|
+
const ms = new MagicString3(original);
|
|
2561
2638
|
const tokenizer = new Tokenizer();
|
|
2562
2639
|
const tokens = tokenizer.run(ms.original);
|
|
2563
2640
|
handleEachClassFragment(ms, tokens, options);
|
|
2564
2641
|
return ms.toString();
|
|
2565
2642
|
}
|
|
2643
|
+
|
|
2644
|
+
// src/wxml/utils/custom-template.ts
|
|
2566
2645
|
async function customTemplateHandler(rawSource, options) {
|
|
2567
2646
|
const {
|
|
2568
2647
|
customAttributesEntities = [],
|
|
@@ -2572,7 +2651,7 @@ async function customTemplateHandler(rawSource, options) {
|
|
|
2572
2651
|
jsHandler: jsHandler2
|
|
2573
2652
|
} = options ?? {};
|
|
2574
2653
|
const matchCustomAttribute = createAttributeMatcher(customAttributesEntities);
|
|
2575
|
-
const s = new
|
|
2654
|
+
const s = new MagicString4(rawSource);
|
|
2576
2655
|
let tag = "";
|
|
2577
2656
|
const wxsArray = [];
|
|
2578
2657
|
const parser = new Parser(
|
|
@@ -2628,6 +2707,8 @@ async function customTemplateHandler(rawSource, options) {
|
|
|
2628
2707
|
}
|
|
2629
2708
|
return s.toString();
|
|
2630
2709
|
}
|
|
2710
|
+
|
|
2711
|
+
// src/wxml/utils.ts
|
|
2631
2712
|
function createTemplateHandler(options = {}) {
|
|
2632
2713
|
return (rawSource, opt = {}) => {
|
|
2633
2714
|
return customTemplateHandler(rawSource, defuOverrideArray(opt, options));
|
|
@@ -2716,7 +2797,7 @@ function createHandlersFromContext(ctx, customAttributesEntities, cssCalcOptions
|
|
|
2716
2797
|
}
|
|
2717
2798
|
|
|
2718
2799
|
// src/context/logger.ts
|
|
2719
|
-
import { logger as
|
|
2800
|
+
import { logger as logger4 } from "@weapp-tailwindcss/logger";
|
|
2720
2801
|
var loggerLevelMap = {
|
|
2721
2802
|
error: 0,
|
|
2722
2803
|
warn: 1,
|
|
@@ -2724,7 +2805,7 @@ var loggerLevelMap = {
|
|
|
2724
2805
|
silent: -999
|
|
2725
2806
|
};
|
|
2726
2807
|
function applyLoggerLevel(logLevel) {
|
|
2727
|
-
|
|
2808
|
+
logger4.level = loggerLevelMap[logLevel ?? "info"] ?? loggerLevelMap.info;
|
|
2728
2809
|
}
|
|
2729
2810
|
|
|
2730
2811
|
// src/context/index.ts
|
|
@@ -2759,7 +2840,7 @@ async function clearTailwindcssPatcherCache(patcher, options) {
|
|
|
2759
2840
|
if (err?.code === "ENOENT") {
|
|
2760
2841
|
continue;
|
|
2761
2842
|
}
|
|
2762
|
-
|
|
2843
|
+
logger5.debug("failed to clear tailwindcss patcher cache: %s %O", cachePath, err);
|
|
2763
2844
|
}
|
|
2764
2845
|
}
|
|
2765
2846
|
}
|
|
@@ -2774,9 +2855,9 @@ function createInternalCompilerContext(opts) {
|
|
|
2774
2855
|
const twPatcher = createTailwindcssPatcherFromContext(ctx);
|
|
2775
2856
|
logTailwindcssTarget("runtime", twPatcher, ctx.tailwindcssBasedir);
|
|
2776
2857
|
if (twPatcher.packageInfo?.version) {
|
|
2777
|
-
|
|
2858
|
+
logger5.success(`\u5F53\u524D\u4F7F\u7528 ${pc.cyanBright("Tailwind CSS")} \u7248\u672C\u4E3A: ${pc.underline(pc.bold(pc.green(twPatcher.packageInfo.version)))}`);
|
|
2778
2859
|
} else {
|
|
2779
|
-
|
|
2860
|
+
logger5.warn(`${pc.cyanBright("Tailwind CSS")} \u672A\u5B89\u88C5\uFF0C\u5DF2\u8DF3\u8FC7\u7248\u672C\u68C0\u6D4B\u4E0E\u8865\u4E01\u5E94\u7528\u3002`);
|
|
2780
2861
|
}
|
|
2781
2862
|
warnMissingCssEntries(ctx, twPatcher);
|
|
2782
2863
|
const cssCalcOptions = applyV4CssCalcDefaults(ctx.cssCalc, twPatcher);
|
|
@@ -2822,8 +2903,8 @@ export {
|
|
|
2822
2903
|
pluginName,
|
|
2823
2904
|
vitePluginName,
|
|
2824
2905
|
WEAPP_TW_REQUIRED_NODE_VERSION,
|
|
2825
|
-
logTailwindcssTarget,
|
|
2826
2906
|
createPatchTargetRecorder,
|
|
2907
|
+
logTailwindcssTarget,
|
|
2827
2908
|
toCustomAttributesEntities,
|
|
2828
2909
|
replaceWxml,
|
|
2829
2910
|
createAttributeMatcher,
|