weapp-tailwindcss 4.12.0 → 5.0.0-next.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/babel/index.d.ts +3 -0
- package/dist/bundlers/gulp/index.d.ts +7 -0
- package/dist/bundlers/shared/cache.d.ts +16 -0
- package/dist/bundlers/shared/css-cleanup.d.ts +4 -0
- package/dist/bundlers/shared/css-imports.d.ts +8 -0
- package/dist/bundlers/shared/generator-candidates.d.ts +5 -0
- package/dist/bundlers/shared/generator-css.d.ts +41 -0
- package/dist/bundlers/shared/module-graph.d.ts +5 -0
- package/dist/bundlers/shared/run-tasks.d.ts +2 -0
- package/dist/bundlers/vite/bundle-entries.d.ts +14 -0
- package/dist/bundlers/vite/bundle-state.d.ts +43 -0
- package/dist/bundlers/vite/css-finalizer.d.ts +19 -0
- package/dist/bundlers/vite/generate-bundle.d.ts +20 -0
- package/dist/bundlers/vite/incremental-runtime-class-set.d.ts +21 -0
- package/dist/bundlers/vite/index.d.ts +3 -0
- package/dist/bundlers/vite/js-precheck.d.ts +1 -0
- package/dist/bundlers/vite/query.d.ts +15 -0
- package/dist/bundlers/vite/resolve-app-type.d.ts +2 -0
- package/dist/bundlers/vite/rewrite-css-imports.d.ts +11 -0
- package/dist/bundlers/vite/runtime-affecting-signature.d.ts +2 -0
- package/dist/bundlers/vite/utils.d.ts +9 -0
- package/dist/bundlers/webpack/BaseUnifiedPlugin/shared.d.ts +20 -0
- package/dist/bundlers/webpack/BaseUnifiedPlugin/v4-assets.d.ts +14 -0
- package/dist/bundlers/webpack/BaseUnifiedPlugin/v4-loaders.d.ts +15 -0
- package/dist/bundlers/webpack/BaseUnifiedPlugin/v4.d.ts +9 -0
- package/dist/bundlers/webpack/BaseUnifiedPlugin/v5-assets.d.ts +17 -0
- package/dist/bundlers/webpack/BaseUnifiedPlugin/v5-loaders.d.ts +19 -0
- package/dist/bundlers/webpack/BaseUnifiedPlugin/v5.d.ts +9 -0
- package/dist/bundlers/webpack/index.d.ts +1 -0
- package/dist/bundlers/webpack/loaders/weapp-tw-css-import-rewrite-loader.d.ts +12 -0
- package/dist/bundlers/webpack/loaders/weapp-tw-runtime-classset-loader.d.ts +11 -0
- package/dist/bundlers/webpack/shared/css-imports.d.ts +6 -0
- package/dist/bundlers/webpack/shared/loader-anchors.d.ts +8 -0
- package/dist/cache/index.d.ts +38 -0
- package/dist/cache/md5.d.ts +1 -0
- package/dist/chunk-8l464Juk.js +28 -0
- package/dist/cli/config.d.ts +5 -0
- package/dist/cli/context.d.ts +16 -0
- package/dist/cli/doctor/constants.d.ts +7 -0
- package/dist/cli/doctor/types.d.ts +31 -0
- package/dist/cli/doctor.d.ts +4 -0
- package/dist/cli/helpers/options/format.d.ts +2 -0
- package/dist/cli/helpers/options/parse.d.ts +3 -0
- package/dist/cli/helpers/options/resolve.d.ts +1 -0
- package/dist/cli/helpers/options.d.ts +3 -0
- package/dist/cli/helpers/patch-cwd.d.ts +1 -0
- package/dist/cli/helpers.d.ts +5 -0
- package/dist/cli/mount-options/patch-status.d.ts +2 -0
- package/dist/cli/mount-options.d.ts +2 -0
- package/dist/cli/patch-options.d.ts +6 -0
- package/dist/cli/tokens.d.ts +4 -0
- package/dist/cli/types.d.ts +17 -0
- package/dist/cli/vscode-entry.d.ts +14 -0
- package/dist/cli/workspace/package-dirs.d.ts +3 -0
- package/dist/cli/workspace/patch-package.d.ts +3 -0
- package/dist/cli/workspace/patch-utils.d.ts +3 -0
- package/dist/cli/workspace/types.d.ts +11 -0
- package/dist/cli/workspace/workspace-globs.d.ts +2 -0
- package/dist/cli/workspace/workspace-io.d.ts +1 -0
- package/dist/cli/workspace/workspace-lock.d.ts +1 -0
- package/dist/cli/workspace.d.ts +2 -0
- package/dist/cli.d.ts +1 -2
- package/dist/cli.js +2425 -3524
- package/dist/cli.mjs +2402 -3505
- package/dist/constants-B-_T5UnW.mjs +44 -0
- package/dist/constants-p1dyh1x1.js +73 -0
- package/dist/constants.d.ts +13 -0
- package/dist/context/compiler-context-cache.d.ts +3 -0
- package/dist/context/custom-attributes.d.ts +2 -0
- package/dist/context/handlers.d.ts +6 -0
- package/dist/context/index.d.ts +7 -0
- package/dist/context/logger.d.ts +4 -0
- package/dist/context/tailwindcss/basedir.d.ts +1 -0
- package/dist/context/tailwindcss/rax.d.ts +2 -0
- package/dist/context/tailwindcss.d.ts +4 -0
- package/dist/context/workspace.d.ts +3 -0
- package/dist/core.d.ts +5 -21
- package/dist/core.js +138 -180
- package/dist/core.mjs +135 -180
- package/dist/css-imports-BbrbluP9.js +177 -0
- package/dist/css-imports-CSdPq_Sc.mjs +128 -0
- package/dist/css-macro/constants.d.ts +14 -0
- package/dist/css-macro/index.d.ts +15 -0
- package/dist/css-macro/postcss.d.ts +3 -7
- package/dist/css-macro/postcss.js +44 -58
- package/dist/css-macro/postcss.mjs +44 -56
- package/dist/css-macro.d.ts +1 -20
- package/dist/css-macro.js +37 -50
- package/dist/css-macro.mjs +33 -47
- package/dist/debug/index.d.ts +5 -0
- package/dist/defaults.d.ts +2 -11
- package/dist/defaults.js +132 -8
- package/dist/defaults.mjs +128 -7
- package/dist/escape.js +31 -54
- package/dist/escape.mjs +18 -25
- package/dist/experimental/index.d.ts +2 -0
- package/dist/experimental/oxc/ast-utils.d.ts +30 -0
- package/dist/experimental/oxc/index.d.ts +2 -0
- package/dist/experimental/oxc/module-specifiers.d.ts +2 -0
- package/dist/experimental/shared/cache.d.ts +3 -0
- package/dist/experimental/shared/transform.d.ts +3 -0
- package/dist/experimental/shared.d.ts +8 -0
- package/dist/experimental/swc/ast-utils.d.ts +30 -0
- package/dist/experimental/swc/index.d.ts +2 -0
- package/dist/experimental/swc/module-specifiers.d.ts +2 -0
- package/dist/generator/index.d.ts +11 -0
- package/dist/generator/options.d.ts +15 -0
- package/dist/generator/types.d.ts +19 -0
- package/dist/generator-CZ-JXw6T.js +492 -0
- package/dist/generator-Dwxgra97.mjs +399 -0
- package/dist/generator-css-CnYjiMrD.js +1084 -0
- package/dist/generator-css-DhPFjSzK.mjs +1057 -0
- package/dist/generator.d.ts +1 -0
- package/dist/generator.js +19 -0
- package/dist/generator.mjs +2 -0
- package/dist/gulp.d.ts +4 -24
- package/dist/gulp.js +271 -13
- package/dist/gulp.mjs +263 -13
- package/dist/index.d.ts +8 -15
- package/dist/index.js +12 -24
- package/dist/index.mjs +6 -24
- package/dist/js/JsTokenUpdater.d.ts +14 -0
- package/dist/js/ModuleGraph.d.ts +18 -0
- package/dist/js/NodePathWalker.d.ts +33 -0
- package/dist/js/babel/parse.d.ts +9 -0
- package/dist/js/babel/process.d.ts +4 -0
- package/dist/js/babel.d.ts +13 -0
- package/dist/js/class-context.d.ts +3 -0
- package/dist/js/evalTransforms.d.ts +7 -0
- package/dist/js/handlers.d.ts +5 -0
- package/dist/js/index.d.ts +4 -0
- package/dist/js/module-graph/ignored-exports.d.ts +18 -0
- package/dist/js/module-graph/types.d.ts +17 -0
- package/dist/js/node-path-walker/export-handlers.d.ts +12 -0
- package/dist/js/node-path-walker/import-tokens.d.ts +24 -0
- package/dist/js/precheck.d.ts +2 -0
- package/dist/js/sourceAnalysis.d.ts +17 -0
- package/dist/js/syntax.d.ts +10 -0
- package/dist/js/taggedTemplateIgnore.d.ts +13 -0
- package/dist/js/types.d.ts +11 -0
- package/dist/lightningcss/index.d.ts +8 -0
- package/dist/lightningcss/style-handler/options.d.ts +3 -0
- package/dist/lightningcss/style-handler/selector-transform.d.ts +10 -0
- package/dist/lightningcss/style-handler/selector-utils.d.ts +10 -0
- package/dist/lightningcss/style-handler.d.ts +17 -0
- package/dist/loader-anchors-DvwgIYdA.mjs +205 -0
- package/dist/loader-anchors-cprm4Klq.js +273 -0
- package/dist/logger/index.d.ts +2 -0
- package/dist/logger-BZ45DZJT.js +1003 -0
- package/dist/logger-BoVx1Dbt.mjs +935 -0
- package/dist/patcher-options-6gJN2EXy.js +115 -0
- package/dist/patcher-options-DQfR5xxT.mjs +92 -0
- package/dist/postcss-html-transform.d.ts +3 -3
- package/dist/postcss-html-transform.js +7 -10
- package/dist/postcss-html-transform.mjs +3 -6
- package/dist/postcss.d.ts +15 -0
- package/dist/postcss.js +278 -0
- package/dist/postcss.mjs +268 -0
- package/dist/presets/hbuilderx.d.ts +4 -0
- package/dist/presets/index.d.ts +3 -0
- package/dist/presets/shared.d.ts +10 -0
- package/dist/presets/taro.d.ts +4 -0
- package/dist/presets/uni-app-x.d.ts +16 -0
- package/dist/presets/uni-app.d.ts +4 -0
- package/dist/presets.d.ts +1 -76
- package/dist/presets.js +115 -163
- package/dist/presets.mjs +107 -163
- package/dist/recorder-B_XyZ576.mjs +2763 -0
- package/dist/recorder-rn_2v_nd.js +2878 -0
- package/dist/reset/index.d.ts +2 -0
- package/dist/reset.d.ts +1 -4
- package/dist/reset.js +19 -8
- package/dist/reset.mjs +2 -8
- package/dist/shared/classname-transform.d.ts +14 -0
- package/dist/shared/mpx.d.ts +7 -0
- package/dist/shared/tailwindcss-css-redirect.d.ts +1 -0
- package/dist/tailwindcss/index.d.ts +11 -0
- package/dist/tailwindcss/miniprogram.d.ts +1 -0
- package/dist/tailwindcss/patcher-options.d.ts +56 -0
- package/dist/tailwindcss/patcher-resolve.d.ts +4 -0
- package/dist/tailwindcss/patcher.d.ts +13 -0
- package/dist/tailwindcss/recorder.d.ts +13 -0
- package/dist/tailwindcss/remove-unsupported-css.d.ts +2 -0
- package/dist/tailwindcss/runtime/cache.d.ts +11 -0
- package/dist/tailwindcss/runtime-logs.d.ts +3 -0
- package/dist/tailwindcss/runtime.d.ts +29 -0
- package/dist/tailwindcss/targets/paths.d.ts +13 -0
- package/dist/tailwindcss/targets/record-io.d.ts +5 -0
- package/dist/tailwindcss/targets/recorder.d.ts +3 -0
- package/dist/tailwindcss/targets/types.d.ts +35 -0
- package/dist/tailwindcss/targets.d.ts +6 -0
- package/dist/tailwindcss/v3-engine/generator.d.ts +2 -0
- package/dist/tailwindcss/v3-engine/index.d.ts +4 -0
- package/dist/tailwindcss/v3-engine/miniprogram.d.ts +4 -0
- package/dist/tailwindcss/v3-engine/source.d.ts +5 -0
- package/dist/tailwindcss/v3-engine/types.d.ts +55 -0
- package/dist/tailwindcss/v4/config.d.ts +5 -0
- package/dist/tailwindcss/v4/css-entries.d.ts +7 -0
- package/dist/tailwindcss/v4/index.d.ts +2 -0
- package/dist/tailwindcss/v4/multi-patcher.d.ts +2 -0
- package/dist/tailwindcss/v4/patcher-options.d.ts +24 -0
- package/dist/tailwindcss/v4/patcher.d.ts +14 -0
- package/dist/tailwindcss/v4-engine/design-system.d.ts +1 -0
- package/dist/tailwindcss/v4-engine/generator.d.ts +2 -0
- package/dist/tailwindcss/v4-engine/index.d.ts +5 -0
- package/dist/tailwindcss/v4-engine/miniprogram.d.ts +4 -0
- package/dist/tailwindcss/v4-engine/source.d.ts +7 -0
- package/dist/tailwindcss/v4-engine/types.d.ts +17 -0
- package/dist/typedoc.export.d.ts +5 -0
- package/dist/types/base.d.ts +1 -0
- package/dist/types/disabled-options.d.ts +4 -0
- package/dist/types/index.d.ts +104 -0
- package/dist/types/shared.d.ts +7 -0
- package/dist/types/user-defined-options/general.d.ts +31 -0
- package/dist/types/user-defined-options/important.d.ts +37 -0
- package/dist/types/user-defined-options/index.d.ts +11 -0
- package/dist/types/user-defined-options/lifecycle.d.ts +6 -0
- package/dist/types/user-defined-options/matcher.d.ts +9 -0
- package/dist/types.d.ts +1 -150
- package/dist/types.js +0 -1
- package/dist/types.mjs +1 -1
- package/dist/uni-app-x/component-local-style.d.ts +19 -0
- package/dist/uni-app-x/index.d.ts +2 -0
- package/dist/uni-app-x/options.d.ts +12 -0
- package/dist/uni-app-x/style-isolation.d.ts +2 -0
- package/dist/uni-app-x/transform.d.ts +9 -0
- package/dist/uni-app-x/vite.d.ts +36 -0
- package/dist/utils/decode.d.ts +2 -0
- package/dist/utils/disabled.d.ts +6 -0
- package/dist/utils/hbuilderx.d.ts +5 -0
- package/dist/utils/index.d.ts +7 -0
- package/dist/utils/nameMatcher.d.ts +4 -0
- package/dist/utils/resolve-package.d.ts +1 -0
- package/dist/utils/uni-platform.d.ts +11 -0
- package/dist/utils-7DUGTFED.mjs +48 -0
- package/dist/utils-DmC9_In3.js +61 -0
- package/dist/vite-BHpAqldo.js +1952 -0
- package/dist/vite-C8JlHiyR.mjs +1940 -0
- package/dist/vite.d.ts +4 -17
- package/dist/vite.js +5 -14
- package/dist/vite.mjs +2 -14
- package/dist/weapp-tw-css-import-rewrite-loader.js +59 -92
- package/dist/weapp-tw-runtime-classset-loader.js +31 -50
- package/dist/webpack-CABjKGGQ.mjs +441 -0
- package/dist/webpack-DNIJ0ysE.js +456 -0
- package/dist/webpack.d.ts +4 -25
- package/dist/webpack.js +6 -17
- package/dist/webpack.mjs +2 -17
- package/dist/webpack4.d.ts +4 -26
- package/dist/webpack4.js +375 -481
- package/dist/webpack4.mjs +366 -482
- package/dist/wxml/Tokenizer.d.ts +15 -0
- package/dist/wxml/custom-attributes.d.ts +4 -0
- package/dist/wxml/index.d.ts +2 -0
- package/dist/wxml/shared.d.ts +2 -0
- package/dist/wxml/tokenizer/types.d.ts +18 -0
- package/dist/wxml/utils/codegen/legacy-rewriter.d.ts +2 -0
- package/dist/wxml/utils/codegen/legacy-visitor.d.ts +8 -0
- package/dist/wxml/utils/codegen.d.ts +2 -0
- package/dist/wxml/utils/custom-template.d.ts +3 -0
- package/dist/wxml/utils/fragment-helpers.d.ts +6 -0
- package/dist/wxml/utils/fragment-updater.d.ts +4 -0
- package/dist/wxml/utils/template-fragments.d.ts +3 -0
- package/dist/wxml/utils.d.ts +8 -0
- package/dist/wxml/whitespace.d.ts +2 -0
- package/generator-placeholder.css +1 -0
- package/package.json +19 -5
- package/dist/chunk-24AGZQVR.js +0 -183
- package/dist/chunk-3VQKDHGP.mjs +0 -179
- package/dist/chunk-4AFQP74Z.js +0 -24
- package/dist/chunk-57SOQCAU.mjs +0 -582
- package/dist/chunk-5ZYHNDEK.mjs +0 -2439
- package/dist/chunk-76S2EME4.mjs +0 -34
- package/dist/chunk-A5PB4KZT.js +0 -138
- package/dist/chunk-DUHYLR2R.js +0 -276
- package/dist/chunk-DYLQ6UOI.js +0 -71
- package/dist/chunk-E7I5TW5K.js +0 -52
- package/dist/chunk-FS2NOOEB.js +0 -292
- package/dist/chunk-HVNGIKLS.js +0 -3930
- package/dist/chunk-JZQBZHN5.js +0 -579
- package/dist/chunk-KGTVD4EP.mjs +0 -3930
- package/dist/chunk-NNOQDMUP.mjs +0 -10
- package/dist/chunk-OF6MFURR.js +0 -34
- package/dist/chunk-OFB2KBRP.js +0 -2442
- package/dist/chunk-OOHJLO5M.mjs +0 -71
- package/dist/chunk-PCDYXXSK.mjs +0 -1515
- package/dist/chunk-RKISS72P.js +0 -7
- package/dist/chunk-RRQZL7FQ.mjs +0 -292
- package/dist/chunk-XAKAD2CR.mjs +0 -138
- package/dist/chunk-XZP3MREK.mjs +0 -276
- package/dist/chunk-ZAA5ZG3D.js +0 -1518
- package/dist/chunk-ZCH4YINE.mjs +0 -52
- package/dist/cli.d.mts +0 -2
- package/dist/core.d.mts +0 -26
- package/dist/css-macro/postcss.d.mts +0 -7
- package/dist/css-macro.d.mts +0 -18
- package/dist/defaults.d.mts +0 -11
- package/dist/gulp.d.mts +0 -24
- package/dist/index-BXrmQelt.d.mts +0 -672
- package/dist/index-BXrmQelt.d.ts +0 -672
- package/dist/index.d.mts +0 -15
- package/dist/postcss-html-transform.d.mts +0 -2
- package/dist/presets.d.mts +0 -76
- package/dist/reset.d.mts +0 -1
- package/dist/types.d.mts +0 -150
- package/dist/vite.d.mts +0 -17
- package/dist/webpack.d.mts +0 -25
- package/dist/webpack4.d.mts +0 -26
|
@@ -0,0 +1,2878 @@
|
|
|
1
|
+
const require_chunk = require("./chunk-8l464Juk.js");
|
|
2
|
+
require("./utils-DmC9_In3.js");
|
|
3
|
+
const require_defaults = require("./defaults.js");
|
|
4
|
+
const require_logger = require("./logger-BZ45DZJT.js");
|
|
5
|
+
let _weapp_tailwindcss_postcss = require("@weapp-tailwindcss/postcss");
|
|
6
|
+
let node_path = require("node:path");
|
|
7
|
+
node_path = require_chunk.__toESM(node_path);
|
|
8
|
+
let node_process = require("node:process");
|
|
9
|
+
node_process = require_chunk.__toESM(node_process);
|
|
10
|
+
let node_buffer = require("node:buffer");
|
|
11
|
+
let node_fs = require("node:fs");
|
|
12
|
+
let node_fs_promises = require("node:fs/promises");
|
|
13
|
+
let _weapp_tailwindcss_logger = require("@weapp-tailwindcss/logger");
|
|
14
|
+
let lru_cache = require("lru-cache");
|
|
15
|
+
let _weapp_tailwindcss_shared_node = require("@weapp-tailwindcss/shared/node");
|
|
16
|
+
let _weapp_core_escape = require("@weapp-core/escape");
|
|
17
|
+
let debug = require("debug");
|
|
18
|
+
debug = require_chunk.__toESM(debug);
|
|
19
|
+
let _babel_traverse = require("@babel/traverse");
|
|
20
|
+
_babel_traverse = require_chunk.__toESM(_babel_traverse);
|
|
21
|
+
let _babel_parser = require("@babel/parser");
|
|
22
|
+
let _weapp_core_regex = require("@weapp-core/regex");
|
|
23
|
+
let magic_string = require("magic-string");
|
|
24
|
+
magic_string = require_chunk.__toESM(magic_string);
|
|
25
|
+
let _ast_core_escape = require("@ast-core/escape");
|
|
26
|
+
let _weapp_tailwindcss_shared_extractors = require("@weapp-tailwindcss/shared/extractors");
|
|
27
|
+
let _babel_types = require("@babel/types");
|
|
28
|
+
_babel_types = require_chunk.__toESM(_babel_types);
|
|
29
|
+
let htmlparser2 = require("htmlparser2");
|
|
30
|
+
let _weapp_tailwindcss_shared = require("@weapp-tailwindcss/shared");
|
|
31
|
+
//#region src/cache/index.ts
|
|
32
|
+
function isProcessResult(value) {
|
|
33
|
+
return typeof value === "object" && value !== null && "result" in value;
|
|
34
|
+
}
|
|
35
|
+
function createCache(options) {
|
|
36
|
+
const disabled = options === false;
|
|
37
|
+
const hashMap = /* @__PURE__ */ new Map();
|
|
38
|
+
const instance = new lru_cache.LRUCache({
|
|
39
|
+
max: 1024,
|
|
40
|
+
ttl: 0,
|
|
41
|
+
ttlAutopurge: false
|
|
42
|
+
});
|
|
43
|
+
const cache = {
|
|
44
|
+
hashMap,
|
|
45
|
+
instance,
|
|
46
|
+
hasHashKey(key) {
|
|
47
|
+
return hashMap.has(key);
|
|
48
|
+
},
|
|
49
|
+
getHashValue(key) {
|
|
50
|
+
return hashMap.get(key);
|
|
51
|
+
},
|
|
52
|
+
setHashValue(key, value) {
|
|
53
|
+
return hashMap.set(key, value);
|
|
54
|
+
},
|
|
55
|
+
get(key) {
|
|
56
|
+
return instance.get(key);
|
|
57
|
+
},
|
|
58
|
+
set(key, value) {
|
|
59
|
+
return instance.set(key, value);
|
|
60
|
+
},
|
|
61
|
+
computeHash(message) {
|
|
62
|
+
return (0, _weapp_tailwindcss_shared_node.md5)(message);
|
|
63
|
+
},
|
|
64
|
+
calcHashValueChanged(key, hash) {
|
|
65
|
+
const hit = hashMap.get(key);
|
|
66
|
+
if (hit) hashMap.set(key, {
|
|
67
|
+
changed: hash !== hit.hash,
|
|
68
|
+
hash
|
|
69
|
+
});
|
|
70
|
+
else hashMap.set(key, {
|
|
71
|
+
changed: true,
|
|
72
|
+
hash
|
|
73
|
+
});
|
|
74
|
+
return cache;
|
|
75
|
+
},
|
|
76
|
+
has(key) {
|
|
77
|
+
return instance.has(key);
|
|
78
|
+
},
|
|
79
|
+
async process({ key, hashKey, rawSource, hash, resolveCache, transform, onCacheHit }) {
|
|
80
|
+
if (disabled) {
|
|
81
|
+
const value = await transform();
|
|
82
|
+
return isProcessResult(value) ? value.result : value;
|
|
83
|
+
}
|
|
84
|
+
const cacheHashKey = hashKey ?? key;
|
|
85
|
+
let hasChanged = true;
|
|
86
|
+
if (hash != null || rawSource != null) {
|
|
87
|
+
const nextHash = hash ?? cache.computeHash(rawSource);
|
|
88
|
+
cache.calcHashValueChanged(cacheHashKey, nextHash);
|
|
89
|
+
hasChanged = cache.getHashValue(cacheHashKey)?.changed ?? true;
|
|
90
|
+
}
|
|
91
|
+
const readCache = resolveCache ?? (() => cache.get(key));
|
|
92
|
+
if (!hasChanged) {
|
|
93
|
+
const cached = readCache();
|
|
94
|
+
if (cached !== void 0) {
|
|
95
|
+
await onCacheHit?.(cached);
|
|
96
|
+
return cached;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
const value = await transform();
|
|
100
|
+
const normalized = isProcessResult(value) ? value : { result: value };
|
|
101
|
+
const stored = normalized.cacheValue ?? normalized.result;
|
|
102
|
+
cache.set(key, stored);
|
|
103
|
+
return normalized.result;
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
return cache;
|
|
107
|
+
}
|
|
108
|
+
function initializeCache(cacheConfig) {
|
|
109
|
+
if (typeof cacheConfig === "boolean" || cacheConfig === void 0) return createCache(cacheConfig);
|
|
110
|
+
return cacheConfig;
|
|
111
|
+
}
|
|
112
|
+
//#endregion
|
|
113
|
+
//#region src/debug/index.ts
|
|
114
|
+
const _debug = (0, debug.default)("weapp-tw");
|
|
115
|
+
function createDebug(prefix) {
|
|
116
|
+
const debug$3 = ((formatter, ...args) => {
|
|
117
|
+
return _debug((prefix ?? "") + formatter, ...args);
|
|
118
|
+
});
|
|
119
|
+
Object.defineProperty(debug$3, "enabled", {
|
|
120
|
+
enumerable: false,
|
|
121
|
+
configurable: false,
|
|
122
|
+
get() {
|
|
123
|
+
return _debug.enabled;
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
return debug$3;
|
|
127
|
+
}
|
|
128
|
+
//#endregion
|
|
129
|
+
//#region src/tailwindcss/runtime.ts
|
|
130
|
+
const debug$2 = createDebug("[tailwindcss:runtime] ");
|
|
131
|
+
const refreshTailwindcssPatcherSymbol = Symbol.for("weapp-tailwindcss.refreshTailwindcssPatcher");
|
|
132
|
+
function createTailwindPatchPromise(twPatcher, onPatched) {
|
|
133
|
+
return Promise.resolve(twPatcher.patch()).then(async () => {
|
|
134
|
+
require_logger.invalidateRuntimeClassSet(twPatcher);
|
|
135
|
+
if (onPatched) try {
|
|
136
|
+
await onPatched();
|
|
137
|
+
} catch (error) {
|
|
138
|
+
debug$2("failed to persist patch target after patch(): %O", error);
|
|
139
|
+
}
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
const runtimeClassSetStateCache = /* @__PURE__ */ new WeakMap();
|
|
143
|
+
function getRuntimeClassSetStateEntry(state) {
|
|
144
|
+
let entry = runtimeClassSetStateCache.get(state);
|
|
145
|
+
if (!entry) {
|
|
146
|
+
entry = {};
|
|
147
|
+
runtimeClassSetStateCache.set(state, entry);
|
|
148
|
+
}
|
|
149
|
+
return entry;
|
|
150
|
+
}
|
|
151
|
+
async function refreshTailwindRuntimeState(state, forceOrOptions) {
|
|
152
|
+
const normalizedOptions = typeof forceOrOptions === "boolean" ? { force: forceOrOptions } : forceOrOptions;
|
|
153
|
+
const force = normalizedOptions.force;
|
|
154
|
+
const clearCache = normalizedOptions.clearCache === true;
|
|
155
|
+
if (!force) return false;
|
|
156
|
+
debug$2("refresh runtime state start, clearCache=%s major=%s", clearCache, state.twPatcher.majorVersion ?? "unknown");
|
|
157
|
+
await state.patchPromise;
|
|
158
|
+
let refreshed = false;
|
|
159
|
+
if (typeof state.refreshTailwindcssPatcher === "function") {
|
|
160
|
+
const next = await state.refreshTailwindcssPatcher({ clearCache });
|
|
161
|
+
if (next !== state.twPatcher) state.twPatcher = next;
|
|
162
|
+
refreshed = true;
|
|
163
|
+
}
|
|
164
|
+
if (refreshed) state.patchPromise = createTailwindPatchPromise(state.twPatcher, state.onPatchCompleted);
|
|
165
|
+
debug$2("refresh runtime state end, refreshed=%s major=%s", refreshed, state.twPatcher.majorVersion ?? "unknown");
|
|
166
|
+
return refreshed;
|
|
167
|
+
}
|
|
168
|
+
async function ensureRuntimeClassSet(state, options = {}) {
|
|
169
|
+
const forceRefresh = options.forceRefresh === true;
|
|
170
|
+
const forceCollect = options.forceCollect === true;
|
|
171
|
+
const clearCache = options.clearCache === true;
|
|
172
|
+
const allowEmpty = options.allowEmpty === true;
|
|
173
|
+
if (forceRefresh) await refreshTailwindRuntimeState(state, {
|
|
174
|
+
force: true,
|
|
175
|
+
clearCache
|
|
176
|
+
});
|
|
177
|
+
await state.patchPromise;
|
|
178
|
+
const entry = getRuntimeClassSetStateEntry(state);
|
|
179
|
+
const signature = require_logger.getRuntimeClassSetSignature(state.twPatcher);
|
|
180
|
+
const signatureChanged = entry.signature !== signature;
|
|
181
|
+
const shouldForceCollect = forceCollect || forceRefresh || signatureChanged;
|
|
182
|
+
if (!shouldForceCollect) {
|
|
183
|
+
if (entry.value && (allowEmpty || entry.value.size > 0)) return entry.value;
|
|
184
|
+
if (entry.promise) return entry.promise;
|
|
185
|
+
}
|
|
186
|
+
const task = (async () => {
|
|
187
|
+
const collected = await collectRuntimeClassSet(state.twPatcher, {
|
|
188
|
+
force: shouldForceCollect,
|
|
189
|
+
skipRefresh: true,
|
|
190
|
+
clearCache
|
|
191
|
+
});
|
|
192
|
+
if (allowEmpty || collected.size > 0) return collected;
|
|
193
|
+
await refreshTailwindRuntimeState(state, {
|
|
194
|
+
force: true,
|
|
195
|
+
clearCache: true
|
|
196
|
+
});
|
|
197
|
+
await state.patchPromise;
|
|
198
|
+
return collectRuntimeClassSet(state.twPatcher, {
|
|
199
|
+
force: true,
|
|
200
|
+
skipRefresh: true,
|
|
201
|
+
clearCache: true
|
|
202
|
+
});
|
|
203
|
+
})();
|
|
204
|
+
entry.promise = task;
|
|
205
|
+
try {
|
|
206
|
+
const runtimeSet = await task;
|
|
207
|
+
entry.value = runtimeSet;
|
|
208
|
+
entry.signature = require_logger.getRuntimeClassSetSignature(state.twPatcher);
|
|
209
|
+
return runtimeSet;
|
|
210
|
+
} finally {
|
|
211
|
+
if (entry.promise === task) entry.promise = void 0;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
function shouldPreferSync(majorVersion) {
|
|
215
|
+
if (majorVersion == null) return true;
|
|
216
|
+
if (majorVersion === 3) return true;
|
|
217
|
+
if (majorVersion === 4) return true;
|
|
218
|
+
return false;
|
|
219
|
+
}
|
|
220
|
+
function tryGetRuntimeClassSetSync(twPatcher) {
|
|
221
|
+
if (typeof twPatcher.getClassSetSync !== "function") return;
|
|
222
|
+
if (!shouldPreferSync(twPatcher.majorVersion)) return;
|
|
223
|
+
try {
|
|
224
|
+
const set = twPatcher.getClassSetSync();
|
|
225
|
+
if (set && set.size === 0) return;
|
|
226
|
+
return set;
|
|
227
|
+
} catch (error) {
|
|
228
|
+
if (twPatcher.majorVersion === 4) debug$2("getClassSetSync() unavailable for tailwindcss v4, fallback to async getClassSet(): %O", error);
|
|
229
|
+
else debug$2("getClassSetSync() failed, fallback to async getClassSet(): %O", error);
|
|
230
|
+
return;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
async function collectRuntimeClassSet(twPatcher, options = {}) {
|
|
234
|
+
let activePatcher = twPatcher;
|
|
235
|
+
if (options.force && !options.skipRefresh) {
|
|
236
|
+
const refresh = activePatcher[refreshTailwindcssPatcherSymbol];
|
|
237
|
+
if (typeof refresh === "function") try {
|
|
238
|
+
const refreshed = await refresh({ clearCache: options.clearCache === true });
|
|
239
|
+
if (refreshed) activePatcher = refreshed;
|
|
240
|
+
} catch (error) {
|
|
241
|
+
debug$2("refreshTailwindcssPatcher failed, continuing with existing patcher: %O", error);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
const entry = require_logger.getRuntimeClassSetCacheEntry(activePatcher);
|
|
245
|
+
const signature = require_logger.getRuntimeClassSetSignature(activePatcher);
|
|
246
|
+
if (!options.force) {
|
|
247
|
+
if (entry.value && entry.signature === signature) return entry.value;
|
|
248
|
+
if (entry.promise) return entry.promise;
|
|
249
|
+
} else entry.value = void 0;
|
|
250
|
+
const task = (async () => {
|
|
251
|
+
const preExtractSyncSet = options.force ? tryGetRuntimeClassSetSync(activePatcher) : void 0;
|
|
252
|
+
if (preExtractSyncSet) debug$2("runtime class set snapshot via getClassSetSync() before extract(), size=%d", preExtractSyncSet.size);
|
|
253
|
+
const preferExtract = options.force === true;
|
|
254
|
+
try {
|
|
255
|
+
const result = await activePatcher.extract({ write: false });
|
|
256
|
+
if (result?.classSet) {
|
|
257
|
+
if (result.classSet.size > 0) {
|
|
258
|
+
debug$2("runtime class set resolved via extract(), size=%d", result.classSet.size);
|
|
259
|
+
return result.classSet;
|
|
260
|
+
}
|
|
261
|
+
if (preferExtract) debug$2("runtime class set from extract() is empty on force collect, fallback to sync/async class set");
|
|
262
|
+
else debug$2("runtime class set from extract() is empty, fallback to sync/async class set");
|
|
263
|
+
}
|
|
264
|
+
} catch (error) {
|
|
265
|
+
debug$2("extract() failed, fallback to getClassSet(): %O", error);
|
|
266
|
+
}
|
|
267
|
+
if (preExtractSyncSet) {
|
|
268
|
+
debug$2("runtime class set fallback to pre-extract sync snapshot, size=%d", preExtractSyncSet.size);
|
|
269
|
+
return preExtractSyncSet;
|
|
270
|
+
}
|
|
271
|
+
const syncSet = tryGetRuntimeClassSetSync(activePatcher);
|
|
272
|
+
if (syncSet) {
|
|
273
|
+
debug$2("runtime class set resolved via getClassSetSync(), size=%d", syncSet.size);
|
|
274
|
+
return syncSet;
|
|
275
|
+
}
|
|
276
|
+
try {
|
|
277
|
+
const fallbackSet = await Promise.resolve(activePatcher.getClassSet());
|
|
278
|
+
if (fallbackSet) {
|
|
279
|
+
debug$2("runtime class set resolved via getClassSet(), size=%d", fallbackSet.size);
|
|
280
|
+
return fallbackSet;
|
|
281
|
+
}
|
|
282
|
+
} catch (error) {
|
|
283
|
+
debug$2("getClassSet() failed, returning empty set: %O", error);
|
|
284
|
+
}
|
|
285
|
+
return /* @__PURE__ */ new Set();
|
|
286
|
+
})();
|
|
287
|
+
entry.promise = task;
|
|
288
|
+
entry.signature = signature;
|
|
289
|
+
try {
|
|
290
|
+
const resolved = await task;
|
|
291
|
+
entry.value = resolved;
|
|
292
|
+
entry.promise = void 0;
|
|
293
|
+
entry.signature = signature;
|
|
294
|
+
return resolved;
|
|
295
|
+
} catch (error) {
|
|
296
|
+
entry.promise = void 0;
|
|
297
|
+
throw error;
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
//#endregion
|
|
301
|
+
//#region src/tailwindcss/runtime-logs.ts
|
|
302
|
+
const runtimeLogDedupeHolder = globalThis;
|
|
303
|
+
const runtimeLogDedupe = runtimeLogDedupeHolder.__WEAPP_TW_RUNTIME_LOG_DEDUPE__ ?? (runtimeLogDedupeHolder.__WEAPP_TW_RUNTIME_LOG_DEDUPE__ = /* @__PURE__ */ new Set());
|
|
304
|
+
function createRuntimeLogKey(category, baseDir, rootPath, version) {
|
|
305
|
+
return JSON.stringify([
|
|
306
|
+
category,
|
|
307
|
+
baseDir ?? node_process.default.cwd(),
|
|
308
|
+
rootPath ?? "",
|
|
309
|
+
version ?? ""
|
|
310
|
+
]);
|
|
311
|
+
}
|
|
312
|
+
function markRuntimeLog(category, baseDir, rootPath, version) {
|
|
313
|
+
const key = createRuntimeLogKey(category, baseDir, rootPath, version);
|
|
314
|
+
if (runtimeLogDedupe.has(key)) return false;
|
|
315
|
+
runtimeLogDedupe.add(key);
|
|
316
|
+
return true;
|
|
317
|
+
}
|
|
318
|
+
function logRuntimeTailwindcssTarget(baseDir, rootPath, version) {
|
|
319
|
+
if (!markRuntimeLog("target", baseDir, rootPath, version)) return;
|
|
320
|
+
const versionText = version ? ` (v${version})` : "";
|
|
321
|
+
_weapp_tailwindcss_logger.logger.info("%s 使用 Tailwind CSS%s", "Weapp-tailwindcss", versionText);
|
|
322
|
+
}
|
|
323
|
+
function logRuntimeTailwindcssVersion(baseDir, rootPath, version) {
|
|
324
|
+
if (version) {
|
|
325
|
+
if (!markRuntimeLog("version", baseDir, rootPath, version)) return;
|
|
326
|
+
_weapp_tailwindcss_logger.logger.success(`当前使用 ${_weapp_tailwindcss_logger.pc.cyanBright("Tailwind CSS")} 版本为: ${_weapp_tailwindcss_logger.pc.underline(_weapp_tailwindcss_logger.pc.bold(_weapp_tailwindcss_logger.pc.green(version)))}`);
|
|
327
|
+
return;
|
|
328
|
+
}
|
|
329
|
+
if (!markRuntimeLog("missing", baseDir, rootPath, version)) return;
|
|
330
|
+
_weapp_tailwindcss_logger.logger.warn(`${_weapp_tailwindcss_logger.pc.cyanBright("Tailwind CSS")} 未安装,已跳过版本检测与补丁应用。`);
|
|
331
|
+
}
|
|
332
|
+
//#endregion
|
|
333
|
+
//#region src/tailwindcss/targets/paths.ts
|
|
334
|
+
const PATCH_INFO_FILENAME = "tailwindcss-target.json";
|
|
335
|
+
const PATCH_INFO_CACHE_RELATIVE_PATH = node_path.default.join("node_modules", ".cache", "weapp-tailwindcss", PATCH_INFO_FILENAME);
|
|
336
|
+
const PATCH_INFO_LEGACY_RELATIVE_PATH = node_path.default.join(".tw-patch", PATCH_INFO_FILENAME);
|
|
337
|
+
function toDisplayPath(value) {
|
|
338
|
+
return node_path.default.normalize(value).replace(/\\/g, "/");
|
|
339
|
+
}
|
|
340
|
+
function formatRelativeToBase(targetPath, baseDir) {
|
|
341
|
+
if (!baseDir) return toDisplayPath(targetPath);
|
|
342
|
+
const relative = node_path.default.relative(baseDir, targetPath);
|
|
343
|
+
if (!relative || relative === ".") return ".";
|
|
344
|
+
if (relative.startsWith("..")) return toDisplayPath(targetPath);
|
|
345
|
+
return toDisplayPath(node_path.default.join(".", relative));
|
|
346
|
+
}
|
|
347
|
+
function resolveRecordLocation(baseDir) {
|
|
348
|
+
const normalizedBase = node_path.default.normalize(baseDir);
|
|
349
|
+
const packageRoot = require_logger.findNearestPackageRoot(normalizedBase) ?? normalizedBase;
|
|
350
|
+
const packageJsonPath = node_path.default.join(packageRoot, "package.json");
|
|
351
|
+
const hasPackageJson = (0, node_fs.existsSync)(packageJsonPath);
|
|
352
|
+
const recordKeySource = hasPackageJson ? packageJsonPath : normalizedBase;
|
|
353
|
+
const recordKey = (0, _weapp_tailwindcss_shared_node.md5)(node_path.default.normalize(recordKeySource));
|
|
354
|
+
const recordDir = node_path.default.join(packageRoot, "node_modules", ".cache", "weapp-tailwindcss", recordKey);
|
|
355
|
+
return {
|
|
356
|
+
normalizedBase,
|
|
357
|
+
packageRoot,
|
|
358
|
+
recordDir,
|
|
359
|
+
recordKey,
|
|
360
|
+
recordPath: node_path.default.join(recordDir, PATCH_INFO_FILENAME),
|
|
361
|
+
packageJsonPath: hasPackageJson ? packageJsonPath : void 0
|
|
362
|
+
};
|
|
363
|
+
}
|
|
364
|
+
function getRecordFileCandidates(baseDir) {
|
|
365
|
+
const { normalizedBase, packageRoot, recordPath } = resolveRecordLocation(baseDir);
|
|
366
|
+
return [...new Set([
|
|
367
|
+
recordPath,
|
|
368
|
+
node_path.default.join(packageRoot, PATCH_INFO_CACHE_RELATIVE_PATH),
|
|
369
|
+
node_path.default.join(normalizedBase, PATCH_INFO_CACHE_RELATIVE_PATH),
|
|
370
|
+
node_path.default.join(normalizedBase, PATCH_INFO_LEGACY_RELATIVE_PATH)
|
|
371
|
+
])];
|
|
372
|
+
}
|
|
373
|
+
//#endregion
|
|
374
|
+
//#region package.json
|
|
375
|
+
var version = "5.0.0-next.1";
|
|
376
|
+
//#endregion
|
|
377
|
+
//#region src/constants.ts
|
|
378
|
+
const pluginName = "weapp-tailwindcss-webpack-plugin";
|
|
379
|
+
const vitePluginName = "weapp-tailwindcss:adaptor";
|
|
380
|
+
const WEAPP_TW_VERSION = version;
|
|
381
|
+
const DEFAULT_RUNTIME_PACKAGE_REPLACEMENTS = {
|
|
382
|
+
"tailwind-merge": "@weapp-tailwindcss/merge",
|
|
383
|
+
"class-variance-authority": "@weapp-tailwindcss/cva",
|
|
384
|
+
"tailwind-variants": "@weapp-tailwindcss/variants"
|
|
385
|
+
};
|
|
386
|
+
//#endregion
|
|
387
|
+
//#region src/tailwindcss/targets/record-io.ts
|
|
388
|
+
const loggedInvalidPatchRecords = /* @__PURE__ */ new Set();
|
|
389
|
+
function warnInvalidPatchTargetRecord(baseDir, recordPath, reason) {
|
|
390
|
+
const normalizedPath = node_path.default.normalize(recordPath);
|
|
391
|
+
if (loggedInvalidPatchRecords.has(normalizedPath)) return;
|
|
392
|
+
loggedInvalidPatchRecords.add(normalizedPath);
|
|
393
|
+
const fileDisplay = formatRelativeToBase(normalizedPath, baseDir);
|
|
394
|
+
const baseDisplay = formatRelativeToBase(node_path.default.normalize(baseDir), node_process.default.cwd());
|
|
395
|
+
const reasonMessage = reason ? `:${reason}` : "";
|
|
396
|
+
_weapp_tailwindcss_logger.logger.warn(`检测到损坏的 Tailwind CSS 目标记录 ${fileDisplay}${reasonMessage}。请在 ${baseDisplay} 重新执行 "weapp-tw patch --record-target" 或删除该文件后再运行。`);
|
|
397
|
+
}
|
|
398
|
+
function readPatchTargetRecord(baseDir) {
|
|
399
|
+
if (!baseDir) return;
|
|
400
|
+
const normalizedBase = node_path.default.normalize(baseDir);
|
|
401
|
+
for (const recordPath of getRecordFileCandidates(normalizedBase)) {
|
|
402
|
+
if (!(0, node_fs.existsSync)(recordPath)) continue;
|
|
403
|
+
try {
|
|
404
|
+
const content = (0, node_fs.readFileSync)(recordPath, "utf8");
|
|
405
|
+
const parsed = JSON.parse(content);
|
|
406
|
+
if (!parsed || typeof parsed.tailwindPackagePath !== "string") {
|
|
407
|
+
warnInvalidPatchTargetRecord(normalizedBase, recordPath, "缺少 tailwindPackagePath 字段");
|
|
408
|
+
continue;
|
|
409
|
+
}
|
|
410
|
+
return {
|
|
411
|
+
baseDir: normalizedBase,
|
|
412
|
+
path: recordPath,
|
|
413
|
+
record: parsed
|
|
414
|
+
};
|
|
415
|
+
} catch (error) {
|
|
416
|
+
warnInvalidPatchTargetRecord(normalizedBase, recordPath, error instanceof Error ? error.message : String(error));
|
|
417
|
+
continue;
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
async function saveCliPatchTargetRecord(baseDir, patcher, options) {
|
|
422
|
+
if (!baseDir || !patcher?.packageInfo?.rootPath) return;
|
|
423
|
+
const normalizedBase = node_path.default.normalize(baseDir);
|
|
424
|
+
const location = resolveRecordLocation(normalizedBase);
|
|
425
|
+
const recordPath = options?.recordPath ? node_path.default.normalize(options.recordPath) : location.recordPath;
|
|
426
|
+
const record = {
|
|
427
|
+
tailwindPackagePath: node_path.default.normalize(patcher.packageInfo.rootPath),
|
|
428
|
+
packageVersion: patcher.packageInfo.version,
|
|
429
|
+
recordedAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
430
|
+
source: options?.source ?? "cli",
|
|
431
|
+
tailwindcssBasedir: normalizedBase,
|
|
432
|
+
cwd: options?.cwd ? node_path.default.normalize(options.cwd) : normalizedBase,
|
|
433
|
+
patchVersion: WEAPP_TW_VERSION,
|
|
434
|
+
packageJsonPath: options?.packageJsonPath ?? location.packageJsonPath,
|
|
435
|
+
recordKey: options?.recordKey ?? location.recordKey
|
|
436
|
+
};
|
|
437
|
+
try {
|
|
438
|
+
await (0, node_fs_promises.mkdir)(node_path.default.dirname(recordPath), { recursive: true });
|
|
439
|
+
await (0, node_fs_promises.writeFile)(recordPath, `${JSON.stringify(record, null, 2)}\n`, "utf8");
|
|
440
|
+
return recordPath;
|
|
441
|
+
} catch (error) {
|
|
442
|
+
const baseDisplay = formatRelativeToBase(normalizedBase, node_process.default.cwd());
|
|
443
|
+
_weapp_tailwindcss_logger.logger.warn("自动更新 Tailwind CSS 补丁记录失败,请在 %s 运行 \"weapp-tw patch --cwd %s\"。", baseDisplay, normalizedBase);
|
|
444
|
+
_weapp_tailwindcss_logger.logger.debug("failed to persist patch target record %s: %O", recordPath, error);
|
|
445
|
+
return;
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
//#endregion
|
|
449
|
+
//#region src/tailwindcss/targets/recorder.ts
|
|
450
|
+
function findPatchTargetRecord(baseDir) {
|
|
451
|
+
const visited = /* @__PURE__ */ new Set();
|
|
452
|
+
const fallback = baseDir ?? node_process.default.cwd();
|
|
453
|
+
let current = node_path.default.resolve(fallback);
|
|
454
|
+
while (!visited.has(current)) {
|
|
455
|
+
const record = readPatchTargetRecord(current);
|
|
456
|
+
if (record) return record;
|
|
457
|
+
const parent = node_path.default.dirname(current);
|
|
458
|
+
if (parent === current) break;
|
|
459
|
+
visited.add(current);
|
|
460
|
+
current = parent;
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
function createPatchTargetRecorder(baseDir, patcher, options) {
|
|
464
|
+
if (!baseDir || !patcher?.packageInfo?.rootPath || options?.recordTarget === false) return;
|
|
465
|
+
const normalizedBase = node_path.default.normalize(baseDir);
|
|
466
|
+
const recorded = findPatchTargetRecord(normalizedBase);
|
|
467
|
+
const location = resolveRecordLocation(normalizedBase);
|
|
468
|
+
const expectedPath = node_path.default.normalize(patcher.packageInfo.rootPath);
|
|
469
|
+
let reason;
|
|
470
|
+
if (!recorded) reason = "missing";
|
|
471
|
+
else if (node_path.default.normalize(recorded.record.tailwindPackagePath) !== expectedPath) reason = "mismatch";
|
|
472
|
+
else if (node_path.default.normalize(recorded.path) !== node_path.default.normalize(location.recordPath) || !recorded.record.recordKey || recorded.record.recordKey !== location.recordKey) reason = "migrate";
|
|
473
|
+
else if (!recorded.record.patchVersion || recorded.record.patchVersion !== WEAPP_TW_VERSION) reason = "stale";
|
|
474
|
+
else if (options?.cwd && recorded.record.cwd && node_path.default.normalize(recorded.record.cwd) !== node_path.default.normalize(options.cwd)) reason = "metadata";
|
|
475
|
+
else if (!recorded.record.cwd && options?.cwd) reason = "metadata";
|
|
476
|
+
if (!(options?.alwaysRecord || !recorded || Boolean(reason))) return;
|
|
477
|
+
let message;
|
|
478
|
+
switch (reason) {
|
|
479
|
+
case "mismatch":
|
|
480
|
+
message = "检测到 Tailwind CSS 目标记录与当前解析结果不一致,正在自动重新 patch 并刷新缓存。";
|
|
481
|
+
break;
|
|
482
|
+
case "migrate":
|
|
483
|
+
case "stale":
|
|
484
|
+
message = "正在刷新当前子包的 Tailwind CSS 补丁记录,确保缓存隔离。";
|
|
485
|
+
break;
|
|
486
|
+
case "missing":
|
|
487
|
+
message = "未找到当前子包的 Tailwind CSS 目标记录,正在生成。";
|
|
488
|
+
break;
|
|
489
|
+
default: break;
|
|
490
|
+
}
|
|
491
|
+
const onPatched = async () => saveCliPatchTargetRecord(normalizedBase, patcher, {
|
|
492
|
+
cwd: options?.cwd ?? normalizedBase,
|
|
493
|
+
source: options?.source ?? "cli",
|
|
494
|
+
recordPath: location.recordPath,
|
|
495
|
+
recordKey: location.recordKey,
|
|
496
|
+
packageJsonPath: location.packageJsonPath
|
|
497
|
+
});
|
|
498
|
+
return {
|
|
499
|
+
recordPath: location.recordPath,
|
|
500
|
+
message,
|
|
501
|
+
reason,
|
|
502
|
+
onPatched
|
|
503
|
+
};
|
|
504
|
+
}
|
|
505
|
+
//#endregion
|
|
506
|
+
//#region src/tailwindcss/targets.ts
|
|
507
|
+
function logTailwindcssTarget(kind, patcher, baseDir) {
|
|
508
|
+
const packageInfo = patcher?.packageInfo;
|
|
509
|
+
const label = kind === "cli" ? "weapp-tw patch" : "Weapp-tailwindcss";
|
|
510
|
+
if (!packageInfo?.rootPath) {
|
|
511
|
+
_weapp_tailwindcss_logger.logger.warn("%s 未找到 Tailwind CSS 依赖,请检查在 %s 是否已安装 tailwindcss", label, baseDir ?? node_process.default.cwd());
|
|
512
|
+
return;
|
|
513
|
+
}
|
|
514
|
+
const displayPath = formatRelativeToBase(packageInfo.rootPath, baseDir);
|
|
515
|
+
const version = packageInfo.version ? ` (v${packageInfo.version})` : "";
|
|
516
|
+
if (kind === "runtime") {
|
|
517
|
+
logRuntimeTailwindcssTarget(baseDir, packageInfo.rootPath, packageInfo.version);
|
|
518
|
+
return;
|
|
519
|
+
}
|
|
520
|
+
_weapp_tailwindcss_logger.logger.info("%s 绑定 Tailwind CSS -> %s%s", label, displayPath, version);
|
|
521
|
+
}
|
|
522
|
+
//#endregion
|
|
523
|
+
//#region src/context/compiler-context-cache.ts
|
|
524
|
+
const PAREN_CONTENT_RE = /\(([^)]+)\)/u;
|
|
525
|
+
const AT_LOCATION_RE = /at\s+(\S.*)$/u;
|
|
526
|
+
const TRAILING_LINE_COL_RE = /:\d+(?::\d+)?$/u;
|
|
527
|
+
const globalCacheHolder = globalThis;
|
|
528
|
+
const compilerContextCache = globalCacheHolder.__WEAPP_TW_COMPILER_CONTEXT_CACHE__ ?? (globalCacheHolder.__WEAPP_TW_COMPILER_CONTEXT_CACHE__ = /* @__PURE__ */ new Map());
|
|
529
|
+
const compilerContextKeyCacheByOptions = /* @__PURE__ */ new WeakMap();
|
|
530
|
+
const compilerContextKeyCacheWithoutOptions = /* @__PURE__ */ new Map();
|
|
531
|
+
function withCircularGuard(value, stack, factory) {
|
|
532
|
+
if (stack.has(value)) throw new TypeError("Cannot serialize circular structure in compiler context options");
|
|
533
|
+
stack.add(value);
|
|
534
|
+
try {
|
|
535
|
+
return factory();
|
|
536
|
+
} finally {
|
|
537
|
+
stack.delete(value);
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
function encodeTaggedValue(type, value) {
|
|
541
|
+
const record = { __type: type };
|
|
542
|
+
if (value !== void 0) record.value = value;
|
|
543
|
+
return record;
|
|
544
|
+
}
|
|
545
|
+
function hasExplicitOptionBasedir(opts) {
|
|
546
|
+
return typeof opts?.tailwindcssBasedir === "string" && opts.tailwindcssBasedir.length > 0;
|
|
547
|
+
}
|
|
548
|
+
function shouldProbeCallerLocation(opts) {
|
|
549
|
+
if (hasExplicitOptionBasedir(opts)) return false;
|
|
550
|
+
return !(node_process.default.env.WEAPP_TAILWINDCSS_BASEDIR || node_process.default.env.WEAPP_TAILWINDCSS_BASE_DIR || node_process.default.env.TAILWINDCSS_BASEDIR || node_process.default.env.TAILWINDCSS_BASE_DIR);
|
|
551
|
+
}
|
|
552
|
+
function detectCallerLocation() {
|
|
553
|
+
const stack = (/* @__PURE__ */ new Error("compiler-context-cache stack probe")).stack;
|
|
554
|
+
if (!stack) return;
|
|
555
|
+
const lines = stack.split("\n");
|
|
556
|
+
for (const line of lines) {
|
|
557
|
+
const location = (line.match(PAREN_CONTENT_RE) ?? line.match(AT_LOCATION_RE))?.[1];
|
|
558
|
+
if (!location) continue;
|
|
559
|
+
const candidatePath = location.replace(TRAILING_LINE_COL_RE, "");
|
|
560
|
+
if (!candidatePath || !node_path.default.isAbsolute(candidatePath)) continue;
|
|
561
|
+
if (candidatePath.includes(`${node_path.default.sep}weapp-tailwindcss${node_path.default.sep}src${node_path.default.sep}`) || candidatePath.includes(`${node_path.default.sep}weapp-tailwindcss${node_path.default.sep}dist${node_path.default.sep}`) || candidatePath.includes(`${node_path.default.sep}node_modules${node_path.default.sep}weapp-tailwindcss${node_path.default.sep}`)) continue;
|
|
562
|
+
return candidatePath;
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
function getRuntimeCacheScope(opts) {
|
|
566
|
+
if (hasExplicitOptionBasedir(opts)) return { caller: void 0 };
|
|
567
|
+
const runtimeScope = {
|
|
568
|
+
caller: void 0,
|
|
569
|
+
cwd: node_process.default.cwd(),
|
|
570
|
+
init_cwd: node_process.default.env.INIT_CWD,
|
|
571
|
+
npm_config_local_prefix: node_process.default.env.npm_config_local_prefix,
|
|
572
|
+
npm_package_json: node_process.default.env.npm_package_json,
|
|
573
|
+
pnpm_package_name: node_process.default.env.PNPM_PACKAGE_NAME,
|
|
574
|
+
pwd: node_process.default.env.PWD,
|
|
575
|
+
tailwindcss_base_dir: node_process.default.env.TAILWINDCSS_BASE_DIR,
|
|
576
|
+
tailwindcss_basedir: node_process.default.env.TAILWINDCSS_BASEDIR,
|
|
577
|
+
uni_app_input_dir: node_process.default.env.UNI_APP_INPUT_DIR,
|
|
578
|
+
uni_cli_root: node_process.default.env.UNI_CLI_ROOT,
|
|
579
|
+
uni_input_dir: node_process.default.env.UNI_INPUT_DIR,
|
|
580
|
+
uni_input_root: node_process.default.env.UNI_INPUT_ROOT,
|
|
581
|
+
weapp_tailwindcss_base_dir: node_process.default.env.WEAPP_TAILWINDCSS_BASE_DIR,
|
|
582
|
+
weapp_tailwindcss_basedir: node_process.default.env.WEAPP_TAILWINDCSS_BASEDIR
|
|
583
|
+
};
|
|
584
|
+
if (shouldProbeCallerLocation(opts)) runtimeScope.caller = detectCallerLocation();
|
|
585
|
+
return runtimeScope;
|
|
586
|
+
}
|
|
587
|
+
function serializeNormalizedValue(value) {
|
|
588
|
+
return JSON.stringify(value);
|
|
589
|
+
}
|
|
590
|
+
function createRuntimeCacheScopeKey(opts) {
|
|
591
|
+
return serializeNormalizedValue(normalizeOptionsValue(getRuntimeCacheScope(opts)));
|
|
592
|
+
}
|
|
593
|
+
function getCompilerContextKeyCacheStore(opts) {
|
|
594
|
+
if (!opts) return compilerContextKeyCacheWithoutOptions;
|
|
595
|
+
let store = compilerContextKeyCacheByOptions.get(opts);
|
|
596
|
+
if (!store) {
|
|
597
|
+
store = /* @__PURE__ */ new Map();
|
|
598
|
+
compilerContextKeyCacheByOptions.set(opts, store);
|
|
599
|
+
}
|
|
600
|
+
return store;
|
|
601
|
+
}
|
|
602
|
+
function createComparableNormalizedValue(rawValue, stack) {
|
|
603
|
+
const normalized = normalizeOptionsValue(rawValue, stack);
|
|
604
|
+
return {
|
|
605
|
+
normalized,
|
|
606
|
+
sortKey: serializeNormalizedValue(normalized)
|
|
607
|
+
};
|
|
608
|
+
}
|
|
609
|
+
function getRuntimeCacheScopeValue(opts) {
|
|
610
|
+
return {
|
|
611
|
+
options: opts ?? {},
|
|
612
|
+
runtime: getRuntimeCacheScope(opts)
|
|
613
|
+
};
|
|
614
|
+
}
|
|
615
|
+
function normalizeOptionsValue(rawValue, stack = /* @__PURE__ */ new WeakSet()) {
|
|
616
|
+
if (rawValue === null) return null;
|
|
617
|
+
if (rawValue === void 0) return encodeTaggedValue("Undefined");
|
|
618
|
+
const type = typeof rawValue;
|
|
619
|
+
if (type === "string") return rawValue;
|
|
620
|
+
if (type === "boolean") return rawValue;
|
|
621
|
+
if (type === "number") {
|
|
622
|
+
const numericValue = rawValue;
|
|
623
|
+
if (Number.isNaN(numericValue)) return encodeTaggedValue("Number", "NaN");
|
|
624
|
+
if (!Number.isFinite(numericValue)) return encodeTaggedValue("Number", numericValue > 0 ? "Infinity" : "-Infinity");
|
|
625
|
+
if (Object.is(numericValue, -0)) return encodeTaggedValue("Number", "-0");
|
|
626
|
+
return numericValue;
|
|
627
|
+
}
|
|
628
|
+
if (type === "bigint") return encodeTaggedValue("BigInt", rawValue.toString());
|
|
629
|
+
if (type === "symbol") {
|
|
630
|
+
const symbolValue = rawValue;
|
|
631
|
+
return encodeTaggedValue("Symbol", symbolValue.description ?? String(symbolValue));
|
|
632
|
+
}
|
|
633
|
+
if (type === "function") return encodeTaggedValue("Function", rawValue.toString());
|
|
634
|
+
if (Array.isArray(rawValue)) return withCircularGuard(rawValue, stack, () => rawValue.map((item) => normalizeOptionsValue(item, stack)));
|
|
635
|
+
if (rawValue instanceof Date) return encodeTaggedValue("Date", rawValue.toISOString());
|
|
636
|
+
if (rawValue instanceof RegExp) return {
|
|
637
|
+
__type: "RegExp",
|
|
638
|
+
source: rawValue.source,
|
|
639
|
+
flags: rawValue.flags
|
|
640
|
+
};
|
|
641
|
+
if (typeof node_buffer.Buffer !== "undefined" && node_buffer.Buffer.isBuffer(rawValue)) return encodeTaggedValue("Buffer", rawValue.toString("base64"));
|
|
642
|
+
if (ArrayBuffer.isView(rawValue)) {
|
|
643
|
+
const view = rawValue;
|
|
644
|
+
const buffer = node_buffer.Buffer.from(view.buffer, view.byteOffset, view.byteLength);
|
|
645
|
+
return encodeTaggedValue(view.constructor?.name ?? "ArrayBufferView", buffer.toString("base64"));
|
|
646
|
+
}
|
|
647
|
+
if (rawValue instanceof ArrayBuffer) return encodeTaggedValue("ArrayBuffer", node_buffer.Buffer.from(rawValue).toString("base64"));
|
|
648
|
+
if (rawValue instanceof Set) return withCircularGuard(rawValue, stack, () => {
|
|
649
|
+
const normalizedEntries = Array.from(rawValue, (element) => createComparableNormalizedValue(element, stack));
|
|
650
|
+
normalizedEntries.sort((a, b) => a.sortKey.localeCompare(b.sortKey));
|
|
651
|
+
return {
|
|
652
|
+
__type: "Set",
|
|
653
|
+
value: normalizedEntries.map((entry) => entry.normalized)
|
|
654
|
+
};
|
|
655
|
+
});
|
|
656
|
+
if (rawValue instanceof Map) return withCircularGuard(rawValue, stack, () => {
|
|
657
|
+
const normalizedEntries = Array.from(rawValue.entries(), ([key, entryValue]) => {
|
|
658
|
+
const normalizedKey = createComparableNormalizedValue(key, stack);
|
|
659
|
+
return {
|
|
660
|
+
key: normalizedKey.normalized,
|
|
661
|
+
sortKey: normalizedKey.sortKey,
|
|
662
|
+
value: normalizeOptionsValue(entryValue, stack)
|
|
663
|
+
};
|
|
664
|
+
});
|
|
665
|
+
normalizedEntries.sort((a, b) => a.sortKey.localeCompare(b.sortKey));
|
|
666
|
+
return {
|
|
667
|
+
__type: "Map",
|
|
668
|
+
value: normalizedEntries.map((entry) => [entry.key, entry.value])
|
|
669
|
+
};
|
|
670
|
+
});
|
|
671
|
+
if (typeof URL !== "undefined" && rawValue instanceof URL) return encodeTaggedValue("URL", rawValue.toString());
|
|
672
|
+
if (rawValue instanceof Error) {
|
|
673
|
+
const errorValue = rawValue;
|
|
674
|
+
return {
|
|
675
|
+
__type: "Error",
|
|
676
|
+
name: errorValue.name,
|
|
677
|
+
message: errorValue.message,
|
|
678
|
+
stack: errorValue.stack ?? ""
|
|
679
|
+
};
|
|
680
|
+
}
|
|
681
|
+
if (rawValue instanceof Promise) return encodeTaggedValue("Promise");
|
|
682
|
+
if (rawValue instanceof WeakMap) return encodeTaggedValue("WeakMap");
|
|
683
|
+
if (rawValue instanceof WeakSet) return encodeTaggedValue("WeakSet");
|
|
684
|
+
if (rawValue && typeof rawValue === "object") return withCircularGuard(rawValue, stack, () => {
|
|
685
|
+
const result = {};
|
|
686
|
+
const entries = Object.entries(rawValue);
|
|
687
|
+
entries.sort(([a], [b]) => a.localeCompare(b));
|
|
688
|
+
for (const [key, entryValue] of entries) result[key] = normalizeOptionsValue(entryValue, stack);
|
|
689
|
+
return result;
|
|
690
|
+
});
|
|
691
|
+
return encodeTaggedValue(typeof rawValue, String(rawValue));
|
|
692
|
+
}
|
|
693
|
+
function createCompilerContextCacheKey(opts) {
|
|
694
|
+
try {
|
|
695
|
+
const runtimeCacheScopeKey = createRuntimeCacheScopeKey(opts);
|
|
696
|
+
const keyStore = getCompilerContextKeyCacheStore(opts);
|
|
697
|
+
const cached = keyStore.get(runtimeCacheScopeKey);
|
|
698
|
+
if (cached !== void 0) return cached;
|
|
699
|
+
const cacheKey = (0, _weapp_tailwindcss_shared_node.md5)(serializeNormalizedValue(normalizeOptionsValue(getRuntimeCacheScopeValue(opts))));
|
|
700
|
+
keyStore.set(runtimeCacheScopeKey, cacheKey);
|
|
701
|
+
return cacheKey;
|
|
702
|
+
} catch (error) {
|
|
703
|
+
_weapp_tailwindcss_logger.logger.debug("skip compiler context cache: %O", error);
|
|
704
|
+
return;
|
|
705
|
+
}
|
|
706
|
+
}
|
|
707
|
+
function withCompilerContextCache(opts, factory) {
|
|
708
|
+
const cacheKey = createCompilerContextCacheKey(opts);
|
|
709
|
+
if (cacheKey) {
|
|
710
|
+
const cached = compilerContextCache.get(cacheKey);
|
|
711
|
+
if (cached) return cached;
|
|
712
|
+
}
|
|
713
|
+
const ctx = factory();
|
|
714
|
+
if (cacheKey) compilerContextCache.set(cacheKey, ctx);
|
|
715
|
+
return ctx;
|
|
716
|
+
}
|
|
717
|
+
//#endregion
|
|
718
|
+
//#region src/context/custom-attributes.ts
|
|
719
|
+
function toCustomAttributesEntities(customAttributes) {
|
|
720
|
+
if (!customAttributes) return [];
|
|
721
|
+
if ((0, _weapp_tailwindcss_shared.isMap)(customAttributes)) return [...customAttributes.entries()];
|
|
722
|
+
return Object.entries(customAttributes);
|
|
723
|
+
}
|
|
724
|
+
//#endregion
|
|
725
|
+
//#region src/babel/index.ts
|
|
726
|
+
function _interopDefaultCompat(e) {
|
|
727
|
+
return e && typeof e === "object" && "default" in e ? e.default : e;
|
|
728
|
+
}
|
|
729
|
+
const traverse = _interopDefaultCompat(_babel_traverse.default);
|
|
730
|
+
//#endregion
|
|
731
|
+
//#region src/utils/nameMatcher.ts
|
|
732
|
+
const NEVER_MATCH_NAME$1 = () => false;
|
|
733
|
+
const GLOBAL_FLAG_REGEXP = /g/g;
|
|
734
|
+
function buildFuzzyMatcher(fuzzyStrings) {
|
|
735
|
+
if (fuzzyStrings.length === 0) return;
|
|
736
|
+
if (fuzzyStrings.length === 1) {
|
|
737
|
+
const [needle] = fuzzyStrings;
|
|
738
|
+
return (value) => value.includes(needle);
|
|
739
|
+
}
|
|
740
|
+
const unique = [...new Set(fuzzyStrings)];
|
|
741
|
+
const pattern = new RegExp(unique.map(_weapp_core_regex.escapeStringRegexp).join("|"));
|
|
742
|
+
return (value) => pattern.test(value);
|
|
743
|
+
}
|
|
744
|
+
function normaliseRegex(regex) {
|
|
745
|
+
const { source, flags } = regex;
|
|
746
|
+
if (!flags.includes("g")) return regex;
|
|
747
|
+
return new RegExp(source, flags.replace(GLOBAL_FLAG_REGEXP, ""));
|
|
748
|
+
}
|
|
749
|
+
function createNameMatcher(list, { exact = false } = {}) {
|
|
750
|
+
if (!list || list.length === 0) return NEVER_MATCH_NAME$1;
|
|
751
|
+
const exactStrings = exact ? /* @__PURE__ */ new Set() : void 0;
|
|
752
|
+
const fuzzyStrings = [];
|
|
753
|
+
const regexList = [];
|
|
754
|
+
for (const item of list) if (typeof item === "string") if (exact) exactStrings.add(item);
|
|
755
|
+
else fuzzyStrings.push(item);
|
|
756
|
+
else regexList.push(normaliseRegex(item));
|
|
757
|
+
if (exact) {
|
|
758
|
+
const exactStringCount = exactStrings?.size ?? 0;
|
|
759
|
+
if (exactStringCount === 1 && regexList.length === 0) {
|
|
760
|
+
const [needle] = exactStrings;
|
|
761
|
+
return (value) => value === needle;
|
|
762
|
+
}
|
|
763
|
+
if (regexList.length === 0) return (value) => exactStrings.has(value);
|
|
764
|
+
if (exactStringCount === 0 && regexList.length === 1) {
|
|
765
|
+
const [regex] = regexList;
|
|
766
|
+
return (value) => regex.test(value);
|
|
767
|
+
}
|
|
768
|
+
return (value) => {
|
|
769
|
+
if (exactStrings?.has(value)) return true;
|
|
770
|
+
return regexList.some((regex) => regex.test(value));
|
|
771
|
+
};
|
|
772
|
+
}
|
|
773
|
+
const fuzzyMatcher = exact ? void 0 : buildFuzzyMatcher(fuzzyStrings);
|
|
774
|
+
const hasRegex = regexList.length > 0;
|
|
775
|
+
if (fuzzyMatcher && !hasRegex) return fuzzyMatcher;
|
|
776
|
+
if (!fuzzyMatcher && regexList.length === 1) {
|
|
777
|
+
const [regex] = regexList;
|
|
778
|
+
return (value) => regex.test(value);
|
|
779
|
+
}
|
|
780
|
+
return (value) => {
|
|
781
|
+
if (fuzzyMatcher?.(value)) return true;
|
|
782
|
+
if (!hasRegex) return false;
|
|
783
|
+
return regexList.some((regex) => regex.test(value));
|
|
784
|
+
};
|
|
785
|
+
}
|
|
786
|
+
//#endregion
|
|
787
|
+
//#region src/js/babel/parse.ts
|
|
788
|
+
const parseCache = new lru_cache.LRUCache({ max: 1024 });
|
|
789
|
+
function genCacheKey(source, options) {
|
|
790
|
+
if (typeof options === "string") return source + options;
|
|
791
|
+
return source + JSON.stringify(options, (_, val) => typeof val === "function" ? val.toString() : val);
|
|
792
|
+
}
|
|
793
|
+
function babelParse(code, opts = {}) {
|
|
794
|
+
const { cache, cacheKey, ...rest } = opts;
|
|
795
|
+
const cacheKeyString = genCacheKey(code, cacheKey ?? rest);
|
|
796
|
+
let result;
|
|
797
|
+
if (cache) result = parseCache.get(cacheKeyString);
|
|
798
|
+
if (!result) {
|
|
799
|
+
const { cache: _cache, cacheKey: _cacheKey, ...parseOptions } = opts;
|
|
800
|
+
result = (0, _babel_parser.parse)(code, parseOptions);
|
|
801
|
+
if (cache) parseCache.set(cacheKeyString, result);
|
|
802
|
+
}
|
|
803
|
+
return result;
|
|
804
|
+
}
|
|
805
|
+
//#endregion
|
|
806
|
+
//#region src/wxml/shared.ts
|
|
807
|
+
const NEWLINE_RE = /[\n\r]+/g;
|
|
808
|
+
function replaceWxml(original, options = {
|
|
809
|
+
keepEOL: false,
|
|
810
|
+
escapeMap: _weapp_core_escape.MappingChars2String
|
|
811
|
+
}) {
|
|
812
|
+
const { keepEOL, escapeMap, ignoreHead } = options;
|
|
813
|
+
let res = original;
|
|
814
|
+
if (!keepEOL) res = res.replaceAll(NEWLINE_RE, "");
|
|
815
|
+
res = (0, _weapp_core_escape.escape)(res, {
|
|
816
|
+
map: escapeMap,
|
|
817
|
+
ignoreHead
|
|
818
|
+
});
|
|
819
|
+
return res;
|
|
820
|
+
}
|
|
821
|
+
//#endregion
|
|
822
|
+
//#region src/shared/classname-transform.ts
|
|
823
|
+
const escapedCandidateCacheByEscapeMap = /* @__PURE__ */ new WeakMap();
|
|
824
|
+
const defaultEscapedCandidateCache = /* @__PURE__ */ new Map();
|
|
825
|
+
let lastEscapedCandidateEscapeMap;
|
|
826
|
+
let lastEscapedCandidateCacheStore;
|
|
827
|
+
function isUrlLikeCandidate(candidate) {
|
|
828
|
+
return candidate.startsWith("//") || candidate.startsWith("http://") || candidate.startsWith("https://");
|
|
829
|
+
}
|
|
830
|
+
function isArbitraryValueCandidate(candidate) {
|
|
831
|
+
let hasOpenBracket = false;
|
|
832
|
+
let hasCloseBracket = false;
|
|
833
|
+
for (let i = 0; i < candidate.length; i++) {
|
|
834
|
+
const char = candidate[i];
|
|
835
|
+
if (char === "[") hasOpenBracket = true;
|
|
836
|
+
else if (char === "]") hasCloseBracket = true;
|
|
837
|
+
if (hasOpenBracket && hasCloseBracket) break;
|
|
838
|
+
}
|
|
839
|
+
if (!hasOpenBracket || !hasCloseBracket) return false;
|
|
840
|
+
if (isUrlLikeCandidate(candidate.trim())) return false;
|
|
841
|
+
return true;
|
|
842
|
+
}
|
|
843
|
+
function shouldEnableArbitraryValueFallbackByInputs(classNameSet, jsArbitraryValueFallback, tailwindcssMajorVersion) {
|
|
844
|
+
if (jsArbitraryValueFallback === true) return true;
|
|
845
|
+
if (jsArbitraryValueFallback === false) return false;
|
|
846
|
+
return tailwindcssMajorVersion === 4 && (!classNameSet || classNameSet.size === 0);
|
|
847
|
+
}
|
|
848
|
+
function shouldEnableArbitraryValueFallback({ classNameSet, jsArbitraryValueFallback, tailwindcssMajorVersion }) {
|
|
849
|
+
return shouldEnableArbitraryValueFallbackByInputs(classNameSet, jsArbitraryValueFallback, tailwindcssMajorVersion);
|
|
850
|
+
}
|
|
851
|
+
const SKIP_RESULT = { decision: "skip" };
|
|
852
|
+
const DIRECT_RESULT = { decision: "direct" };
|
|
853
|
+
const FALLBACK_RESULT = { decision: "fallback" };
|
|
854
|
+
function getEscapedCandidateCacheStore(escapeMap) {
|
|
855
|
+
if (!escapeMap) return defaultEscapedCandidateCache;
|
|
856
|
+
if (escapeMap === lastEscapedCandidateEscapeMap && lastEscapedCandidateCacheStore) return lastEscapedCandidateCacheStore;
|
|
857
|
+
let store = escapedCandidateCacheByEscapeMap.get(escapeMap);
|
|
858
|
+
if (!store) {
|
|
859
|
+
store = /* @__PURE__ */ new Map();
|
|
860
|
+
escapedCandidateCacheByEscapeMap.set(escapeMap, store);
|
|
861
|
+
}
|
|
862
|
+
lastEscapedCandidateEscapeMap = escapeMap;
|
|
863
|
+
lastEscapedCandidateCacheStore = store;
|
|
864
|
+
return store;
|
|
865
|
+
}
|
|
866
|
+
function getEscapedCandidate(candidate, escapeMap, store = getEscapedCandidateCacheStore(escapeMap)) {
|
|
867
|
+
let cached = store.get(candidate);
|
|
868
|
+
if (cached === void 0) {
|
|
869
|
+
cached = replaceWxml(candidate, { escapeMap });
|
|
870
|
+
store.set(candidate, cached);
|
|
871
|
+
}
|
|
872
|
+
return cached;
|
|
873
|
+
}
|
|
874
|
+
/**
|
|
875
|
+
* JS 转译严格遵循 runtime class set:
|
|
876
|
+
* 1. 直接命中 classNameSet 原始值;
|
|
877
|
+
* 2. 兼容命中 classNameSet 中已转义值;
|
|
878
|
+
* 3. 仅在受控条件下允许 class 语义兜底。
|
|
879
|
+
*
|
|
880
|
+
* 返回结构化结果,附带已计算的 escapedValue 以避免下游重复 escape。
|
|
881
|
+
*/
|
|
882
|
+
function resolveClassNameTransformWithResult(candidate, { alwaysEscape, classNameSet, escapeMap, jsArbitraryValueFallback, jsPreserveClass, tailwindcssMajorVersion, classContext }) {
|
|
883
|
+
if (alwaysEscape) return DIRECT_RESULT;
|
|
884
|
+
if (jsPreserveClass?.(candidate)) return SKIP_RESULT;
|
|
885
|
+
if (classNameSet?.has(candidate)) return DIRECT_RESULT;
|
|
886
|
+
if (classNameSet && classNameSet.size > 0) {
|
|
887
|
+
const escapedCandidate = getEscapedCandidate(candidate, escapeMap);
|
|
888
|
+
if (escapedCandidate !== candidate && classNameSet.has(escapedCandidate)) return {
|
|
889
|
+
decision: "escaped",
|
|
890
|
+
escapedValue: escapedCandidate
|
|
891
|
+
};
|
|
892
|
+
}
|
|
893
|
+
if (classContext && shouldEnableArbitraryValueFallbackByInputs(classNameSet, jsArbitraryValueFallback, tailwindcssMajorVersion) && isArbitraryValueCandidate(candidate)) return FALLBACK_RESULT;
|
|
894
|
+
return SKIP_RESULT;
|
|
895
|
+
}
|
|
896
|
+
//#endregion
|
|
897
|
+
//#region src/utils/decode.ts
|
|
898
|
+
const unicodeEscapeRE = /\\u([\dA-Fa-f]{4})/g;
|
|
899
|
+
const unicodeEscapeTestRE = /\\u[\dA-Fa-f]{4}/;
|
|
900
|
+
function decodeUnicode(value) {
|
|
901
|
+
if (!unicodeEscapeTestRE.test(value)) return value;
|
|
902
|
+
return value.replace(unicodeEscapeRE, (_match, hex) => {
|
|
903
|
+
const codePoint = Number.parseInt(hex, 16);
|
|
904
|
+
return Number.isNaN(codePoint) ? _match : String.fromCharCode(codePoint);
|
|
905
|
+
});
|
|
906
|
+
}
|
|
907
|
+
function decodeUnicode2(input) {
|
|
908
|
+
if (!unicodeEscapeTestRE.test(input)) return input;
|
|
909
|
+
try {
|
|
910
|
+
return JSON.parse(`"${input}"`);
|
|
911
|
+
} catch (_error) {
|
|
912
|
+
return decodeUnicode(input);
|
|
913
|
+
}
|
|
914
|
+
}
|
|
915
|
+
//#endregion
|
|
916
|
+
//#region src/js/class-context.ts
|
|
917
|
+
const CLASS_LIKE_KEYWORDS = new Set([
|
|
918
|
+
"class",
|
|
919
|
+
"classname",
|
|
920
|
+
"hoverclass",
|
|
921
|
+
"virtualhostclass",
|
|
922
|
+
"rootclass"
|
|
923
|
+
]);
|
|
924
|
+
const CLASS_HELPER_IDENTIFIERS = new Set([
|
|
925
|
+
"cn",
|
|
926
|
+
"clsx",
|
|
927
|
+
"classnames",
|
|
928
|
+
"twmerge",
|
|
929
|
+
"cva",
|
|
930
|
+
"tv",
|
|
931
|
+
"cx",
|
|
932
|
+
"r"
|
|
933
|
+
]);
|
|
934
|
+
const DASH_CODE = 45;
|
|
935
|
+
const COLON_CODE = 58;
|
|
936
|
+
const UPPERCASE_A_CODE = 65;
|
|
937
|
+
const UPPERCASE_Z_CODE = 90;
|
|
938
|
+
const UNDERSCORE_CODE = 95;
|
|
939
|
+
const ASCII_MAX_CODE = 127;
|
|
940
|
+
const NORMALIZE_KEYWORD_REGEXP = /[-_:]/g;
|
|
941
|
+
function normalizeKeyword(name) {
|
|
942
|
+
const length = name.length;
|
|
943
|
+
let firstNormalizedIndex = -1;
|
|
944
|
+
for (let i = 0; i < length; i++) {
|
|
945
|
+
const code = name.charCodeAt(i);
|
|
946
|
+
if (code === DASH_CODE || code === UNDERSCORE_CODE || code === COLON_CODE || code >= UPPERCASE_A_CODE && code <= UPPERCASE_Z_CODE) {
|
|
947
|
+
firstNormalizedIndex = i;
|
|
948
|
+
break;
|
|
949
|
+
}
|
|
950
|
+
if (code > ASCII_MAX_CODE) return name.replace(NORMALIZE_KEYWORD_REGEXP, "").toLowerCase();
|
|
951
|
+
}
|
|
952
|
+
if (firstNormalizedIndex === -1) return name;
|
|
953
|
+
let normalized = name.slice(0, firstNormalizedIndex);
|
|
954
|
+
for (let i = firstNormalizedIndex; i < length; i++) {
|
|
955
|
+
const code = name.charCodeAt(i);
|
|
956
|
+
if (code === DASH_CODE || code === UNDERSCORE_CODE || code === COLON_CODE) continue;
|
|
957
|
+
if (code >= UPPERCASE_A_CODE && code <= UPPERCASE_Z_CODE) {
|
|
958
|
+
normalized += String.fromCharCode(code + 32);
|
|
959
|
+
continue;
|
|
960
|
+
}
|
|
961
|
+
if (code > ASCII_MAX_CODE) return name.replace(NORMALIZE_KEYWORD_REGEXP, "").toLowerCase();
|
|
962
|
+
normalized += name[i];
|
|
963
|
+
}
|
|
964
|
+
return normalized;
|
|
965
|
+
}
|
|
966
|
+
function readObjectKeyName(path) {
|
|
967
|
+
if (path.isIdentifier()) return path.node.name;
|
|
968
|
+
if (path.isStringLiteral()) return path.node.value;
|
|
969
|
+
if (path.isTemplateLiteral() && path.node.expressions.length === 0) return path.node.quasis[0]?.value.cooked ?? path.node.quasis[0]?.value.raw;
|
|
970
|
+
}
|
|
971
|
+
function isClassLikeObjectProperty(path, valuePath) {
|
|
972
|
+
if (!path.isObjectProperty()) return false;
|
|
973
|
+
if (path.get("value") !== valuePath) return false;
|
|
974
|
+
const keyName = readObjectKeyName(path.get("key"));
|
|
975
|
+
if (!keyName) return false;
|
|
976
|
+
return CLASS_LIKE_KEYWORDS.has(normalizeKeyword(keyName));
|
|
977
|
+
}
|
|
978
|
+
function isClassLikeJsxAttribute(path) {
|
|
979
|
+
if (!path.isJSXAttribute()) return false;
|
|
980
|
+
const namePath = path.get("name");
|
|
981
|
+
if (!namePath.isJSXIdentifier()) return false;
|
|
982
|
+
return CLASS_LIKE_KEYWORDS.has(normalizeKeyword(namePath.node.name));
|
|
983
|
+
}
|
|
984
|
+
function readCallHelperName(calleePath) {
|
|
985
|
+
if (calleePath.isIdentifier()) return calleePath.node.name;
|
|
986
|
+
if (calleePath.isMemberExpression()) {
|
|
987
|
+
const propertyPath = calleePath.get("property");
|
|
988
|
+
if (propertyPath.isIdentifier()) return propertyPath.node.name;
|
|
989
|
+
if (propertyPath.isStringLiteral()) return propertyPath.node.value;
|
|
990
|
+
}
|
|
991
|
+
}
|
|
992
|
+
function isClassLikeCallExpression(path, valuePath) {
|
|
993
|
+
if (!path.isCallExpression()) return false;
|
|
994
|
+
const helperName = readCallHelperName(path.get("callee"));
|
|
995
|
+
if (!helperName || !CLASS_HELPER_IDENTIFIERS.has(normalizeKeyword(helperName))) return false;
|
|
996
|
+
return path.get("arguments").some((argumentPath) => argumentPath.node === valuePath.node);
|
|
997
|
+
}
|
|
998
|
+
/**
|
|
999
|
+
* 判断字符串字面量是否处于 class 语义上下文。
|
|
1000
|
+
* 仅用于受控兜底场景,避免将普通业务文本误判为 class。
|
|
1001
|
+
*/
|
|
1002
|
+
function isClassContextLiteralPath(path) {
|
|
1003
|
+
let current = path;
|
|
1004
|
+
while (current.parentPath) {
|
|
1005
|
+
const parent = current.parentPath;
|
|
1006
|
+
if (isClassLikeObjectProperty(parent, current)) return true;
|
|
1007
|
+
if (isClassLikeJsxAttribute(parent)) return true;
|
|
1008
|
+
if (isClassLikeCallExpression(parent, current)) return true;
|
|
1009
|
+
current = parent;
|
|
1010
|
+
}
|
|
1011
|
+
return false;
|
|
1012
|
+
}
|
|
1013
|
+
//#endregion
|
|
1014
|
+
//#region src/js/handlers.ts
|
|
1015
|
+
const debug$1 = createDebug("[js:handlers] ");
|
|
1016
|
+
const replacementCacheByEscapeMap = /* @__PURE__ */ new WeakMap();
|
|
1017
|
+
const defaultReplacementCache = /* @__PURE__ */ new Map();
|
|
1018
|
+
const WEAPP_TW_IGNORE_MARKER = "weapp-tw";
|
|
1019
|
+
const IGNORE_MARKER = "ignore";
|
|
1020
|
+
function getReplacementCacheStore(escapeMap) {
|
|
1021
|
+
if (!escapeMap) return defaultReplacementCache;
|
|
1022
|
+
let store = replacementCacheByEscapeMap.get(escapeMap);
|
|
1023
|
+
if (!store) {
|
|
1024
|
+
store = /* @__PURE__ */ new Map();
|
|
1025
|
+
replacementCacheByEscapeMap.set(escapeMap, store);
|
|
1026
|
+
}
|
|
1027
|
+
return store;
|
|
1028
|
+
}
|
|
1029
|
+
function getReplacement(candidate, escapeMap, store = getReplacementCacheStore(escapeMap)) {
|
|
1030
|
+
let cached = store.get(candidate);
|
|
1031
|
+
if (cached === void 0) {
|
|
1032
|
+
cached = replaceWxml(candidate, { escapeMap });
|
|
1033
|
+
store.set(candidate, cached);
|
|
1034
|
+
}
|
|
1035
|
+
return cached;
|
|
1036
|
+
}
|
|
1037
|
+
function hasIgnoreComment(node) {
|
|
1038
|
+
const { leadingComments } = node;
|
|
1039
|
+
if (!Array.isArray(leadingComments) || leadingComments.length === 0) return false;
|
|
1040
|
+
for (const comment of leadingComments) {
|
|
1041
|
+
const { value } = comment;
|
|
1042
|
+
if (value.includes(WEAPP_TW_IGNORE_MARKER) && value.includes(IGNORE_MARKER)) return true;
|
|
1043
|
+
}
|
|
1044
|
+
return false;
|
|
1045
|
+
}
|
|
1046
|
+
function extractLiteralValue(path, { unescapeUnicode, arbitraryValues }) {
|
|
1047
|
+
const allowDoubleQuotes = arbitraryValues?.allowDoubleQuotes;
|
|
1048
|
+
const { node } = path;
|
|
1049
|
+
let offset = 0;
|
|
1050
|
+
let original;
|
|
1051
|
+
if (node.type === "StringLiteral") {
|
|
1052
|
+
offset = 1;
|
|
1053
|
+
original = node.value;
|
|
1054
|
+
} else if (node.type === "TemplateElement") original = node.value.raw;
|
|
1055
|
+
else original = "";
|
|
1056
|
+
let literal = original;
|
|
1057
|
+
if (unescapeUnicode && original.includes("\\u")) literal = decodeUnicode2(original);
|
|
1058
|
+
return {
|
|
1059
|
+
allowDoubleQuotes,
|
|
1060
|
+
literal,
|
|
1061
|
+
offset,
|
|
1062
|
+
original
|
|
1063
|
+
};
|
|
1064
|
+
}
|
|
1065
|
+
function createCandidatePlanResolver(options, classContext) {
|
|
1066
|
+
const { escapeMap } = options;
|
|
1067
|
+
const replacementCache = getReplacementCacheStore(escapeMap);
|
|
1068
|
+
const transformOptions = classContext ? {
|
|
1069
|
+
...options,
|
|
1070
|
+
classContext
|
|
1071
|
+
} : options;
|
|
1072
|
+
let firstCandidate = "";
|
|
1073
|
+
let firstPlan;
|
|
1074
|
+
let cache;
|
|
1075
|
+
const buildCandidatePlan = (candidate) => {
|
|
1076
|
+
const result = resolveClassNameTransformWithResult(candidate, transformOptions);
|
|
1077
|
+
if (result.decision === "skip") return { result };
|
|
1078
|
+
let replacement;
|
|
1079
|
+
if (result.decision === "escaped" && result.escapedValue) {
|
|
1080
|
+
replacement = result.escapedValue;
|
|
1081
|
+
replacementCache.set(candidate, replacement);
|
|
1082
|
+
} else replacement = getReplacement(candidate, escapeMap, replacementCache);
|
|
1083
|
+
return {
|
|
1084
|
+
result,
|
|
1085
|
+
replacement
|
|
1086
|
+
};
|
|
1087
|
+
};
|
|
1088
|
+
return (candidate) => {
|
|
1089
|
+
if (cache) {
|
|
1090
|
+
const cached = cache.get(candidate);
|
|
1091
|
+
if (cached) return cached;
|
|
1092
|
+
} else if (firstPlan && candidate === firstCandidate) return firstPlan;
|
|
1093
|
+
const plan = buildCandidatePlan(candidate);
|
|
1094
|
+
if (!firstPlan) {
|
|
1095
|
+
firstCandidate = candidate;
|
|
1096
|
+
firstPlan = plan;
|
|
1097
|
+
return plan;
|
|
1098
|
+
}
|
|
1099
|
+
if (!cache) {
|
|
1100
|
+
cache = /* @__PURE__ */ new Map();
|
|
1101
|
+
cache.set(firstCandidate, firstPlan);
|
|
1102
|
+
}
|
|
1103
|
+
cache.set(candidate, plan);
|
|
1104
|
+
return plan;
|
|
1105
|
+
};
|
|
1106
|
+
}
|
|
1107
|
+
function replaceHandleValue(path, options) {
|
|
1108
|
+
const { needEscaped = false } = options;
|
|
1109
|
+
const { classNameSet, alwaysEscape } = options;
|
|
1110
|
+
const fallbackEnabled = shouldEnableArbitraryValueFallback(options);
|
|
1111
|
+
if (!alwaysEscape && !fallbackEnabled && (!classNameSet || classNameSet.size === 0)) return;
|
|
1112
|
+
if (hasIgnoreComment(path.node)) return;
|
|
1113
|
+
const { literal, original, allowDoubleQuotes, offset } = extractLiteralValue(path, options);
|
|
1114
|
+
const candidates = (0, _weapp_tailwindcss_shared_extractors.splitCode)(literal, allowDoubleQuotes);
|
|
1115
|
+
if (candidates.length === 0) return;
|
|
1116
|
+
const debugEnabled = debug$1.enabled;
|
|
1117
|
+
const classContext = options.wrapExpression || isClassContextLiteralPath(path);
|
|
1118
|
+
let transformed = literal;
|
|
1119
|
+
let mutated = false;
|
|
1120
|
+
let matchedCandidateCount = 0;
|
|
1121
|
+
let escapedDecisionCount = 0;
|
|
1122
|
+
let fallbackDecisionCount = 0;
|
|
1123
|
+
let escapedSamples;
|
|
1124
|
+
let skippedSamples;
|
|
1125
|
+
const resolveCandidatePlan = createCandidatePlanResolver(options, classContext);
|
|
1126
|
+
for (const candidate of candidates) {
|
|
1127
|
+
const plan = resolveCandidatePlan(candidate);
|
|
1128
|
+
if (plan.result.decision === "skip") {
|
|
1129
|
+
if (debugEnabled) {
|
|
1130
|
+
if (!skippedSamples) skippedSamples = [];
|
|
1131
|
+
if (skippedSamples.length < 6) skippedSamples.push(candidate);
|
|
1132
|
+
}
|
|
1133
|
+
continue;
|
|
1134
|
+
}
|
|
1135
|
+
if (debugEnabled) {
|
|
1136
|
+
matchedCandidateCount += 1;
|
|
1137
|
+
if (plan.result.decision === "escaped") {
|
|
1138
|
+
escapedDecisionCount += 1;
|
|
1139
|
+
if (!escapedSamples) escapedSamples = [];
|
|
1140
|
+
if (escapedSamples.length < 6) escapedSamples.push(candidate);
|
|
1141
|
+
}
|
|
1142
|
+
if (plan.result.decision === "fallback") fallbackDecisionCount += 1;
|
|
1143
|
+
}
|
|
1144
|
+
const replaced = transformed.replace(candidate, plan.replacement);
|
|
1145
|
+
if (replaced !== transformed) {
|
|
1146
|
+
transformed = replaced;
|
|
1147
|
+
mutated = true;
|
|
1148
|
+
}
|
|
1149
|
+
}
|
|
1150
|
+
const node = path.node;
|
|
1151
|
+
if (!mutated || typeof node.start !== "number" || typeof node.end !== "number") return;
|
|
1152
|
+
if (debugEnabled) debug$1("runtimeSet size=%d fallbackTriggered=%s candidates=%d matched=%d escapedHits=%d skipped=%d file=%s escapedSamples=%s skippedSamples=%s", classNameSet?.size ?? 0, fallbackDecisionCount > 0, candidates.length, matchedCandidateCount, escapedDecisionCount, skippedSamples?.length ?? 0, options.filename ?? "unknown", escapedSamples?.join(",") || "-", skippedSamples?.join(",") || "-");
|
|
1153
|
+
const start = node.start + offset;
|
|
1154
|
+
const end = node.end - offset;
|
|
1155
|
+
if (start >= end || transformed === original) return;
|
|
1156
|
+
return {
|
|
1157
|
+
start,
|
|
1158
|
+
end,
|
|
1159
|
+
value: needEscaped ? (0, _ast_core_escape.jsStringEscape)(transformed) : transformed,
|
|
1160
|
+
path
|
|
1161
|
+
};
|
|
1162
|
+
}
|
|
1163
|
+
//#endregion
|
|
1164
|
+
//#region src/js/sourceAnalysis.ts
|
|
1165
|
+
function hasReplacementEntries(replacements) {
|
|
1166
|
+
for (const key in replacements) if (Object.hasOwn(replacements, key)) return true;
|
|
1167
|
+
return false;
|
|
1168
|
+
}
|
|
1169
|
+
function createModuleSpecifierReplacementToken(path, replacement) {
|
|
1170
|
+
const node = path.node;
|
|
1171
|
+
if (node.value === replacement) return;
|
|
1172
|
+
if (typeof node.start !== "number" || typeof node.end !== "number") return;
|
|
1173
|
+
const start = node.start + 1;
|
|
1174
|
+
const end = node.end - 1;
|
|
1175
|
+
if (start >= end) return;
|
|
1176
|
+
return {
|
|
1177
|
+
start,
|
|
1178
|
+
end,
|
|
1179
|
+
value: replacement,
|
|
1180
|
+
path
|
|
1181
|
+
};
|
|
1182
|
+
}
|
|
1183
|
+
function collectModuleSpecifierReplacementTokens(analysis, replacements) {
|
|
1184
|
+
if (!hasReplacementEntries(replacements)) return [];
|
|
1185
|
+
if (analysis.importDeclarations.size === 0 && analysis.exportDeclarations.size === 0 && analysis.requireCallPaths.length === 0 && analysis.walker.imports.size === 0) return [];
|
|
1186
|
+
const tokens = [];
|
|
1187
|
+
const applyReplacement = (path) => {
|
|
1188
|
+
const replacement = replacements[path.node.value];
|
|
1189
|
+
if (!replacement) return;
|
|
1190
|
+
const token = createModuleSpecifierReplacementToken(path, replacement);
|
|
1191
|
+
if (token) tokens.push(token);
|
|
1192
|
+
};
|
|
1193
|
+
for (const importPath of analysis.importDeclarations) {
|
|
1194
|
+
const source = importPath.get("source");
|
|
1195
|
+
if (source.isStringLiteral()) applyReplacement(source);
|
|
1196
|
+
}
|
|
1197
|
+
for (const exportPath of analysis.exportDeclarations) if (exportPath.isExportNamedDeclaration() || exportPath.isExportAllDeclaration()) {
|
|
1198
|
+
const source = exportPath.get("source");
|
|
1199
|
+
if (source && !Array.isArray(source) && source.isStringLiteral()) applyReplacement(source);
|
|
1200
|
+
}
|
|
1201
|
+
for (const literalPath of analysis.requireCallPaths) applyReplacement(literalPath);
|
|
1202
|
+
for (const token of analysis.walker.imports) {
|
|
1203
|
+
const replacement = replacements[token.source];
|
|
1204
|
+
if (replacement) token.source = replacement;
|
|
1205
|
+
}
|
|
1206
|
+
return tokens;
|
|
1207
|
+
}
|
|
1208
|
+
//#endregion
|
|
1209
|
+
//#region src/js/babel/process.ts
|
|
1210
|
+
const optionVariantsCache = /* @__PURE__ */ new WeakMap();
|
|
1211
|
+
function getNeedEscapedOptions(options, needEscaped) {
|
|
1212
|
+
if (options.needEscaped === needEscaped) return options;
|
|
1213
|
+
let cached = optionVariantsCache.get(options);
|
|
1214
|
+
if (!cached) {
|
|
1215
|
+
cached = {};
|
|
1216
|
+
optionVariantsCache.set(options, cached);
|
|
1217
|
+
}
|
|
1218
|
+
if (needEscaped) {
|
|
1219
|
+
if (!cached.stringLiteralOptions) cached.stringLiteralOptions = {
|
|
1220
|
+
...options,
|
|
1221
|
+
needEscaped: true
|
|
1222
|
+
};
|
|
1223
|
+
return cached.stringLiteralOptions;
|
|
1224
|
+
}
|
|
1225
|
+
if (!cached.templateLiteralOptions) cached.templateLiteralOptions = {
|
|
1226
|
+
...options,
|
|
1227
|
+
needEscaped: false
|
|
1228
|
+
};
|
|
1229
|
+
return cached.templateLiteralOptions;
|
|
1230
|
+
}
|
|
1231
|
+
function processUpdatedSource(rawSource, options, analysis) {
|
|
1232
|
+
const { targetPaths, jsTokenUpdater, ignoredPaths } = analysis;
|
|
1233
|
+
if (targetPaths.length === 0 && !options.moduleSpecifierReplacements && jsTokenUpdater.length === 0) return new magic_string.default(rawSource);
|
|
1234
|
+
const replacementTokens = [];
|
|
1235
|
+
for (const path of targetPaths) {
|
|
1236
|
+
if (ignoredPaths.has(path)) continue;
|
|
1237
|
+
const token = replaceHandleValue(path, path.isStringLiteral() ? getNeedEscapedOptions(options, true) : getNeedEscapedOptions(options, false));
|
|
1238
|
+
if (token) replacementTokens.push(token);
|
|
1239
|
+
}
|
|
1240
|
+
if (options.moduleSpecifierReplacements) replacementTokens.push(...collectModuleSpecifierReplacementTokens(analysis, options.moduleSpecifierReplacements));
|
|
1241
|
+
if (jsTokenUpdater.length + replacementTokens.length === 0) return new magic_string.default(rawSource);
|
|
1242
|
+
const ms = new magic_string.default(rawSource);
|
|
1243
|
+
jsTokenUpdater.push(...replacementTokens).filter((token) => !ignoredPaths.has(token.path)).updateMagicString(ms);
|
|
1244
|
+
return ms;
|
|
1245
|
+
}
|
|
1246
|
+
//#endregion
|
|
1247
|
+
//#region src/js/evalTransforms.ts
|
|
1248
|
+
const evalHandlerOptionsCache = /* @__PURE__ */ new WeakMap();
|
|
1249
|
+
const EVAL_SCOPE_ERROR_REGEXP = /pass a scope and parentPath|traversing a Program\/File/i;
|
|
1250
|
+
function isEvalPath(path) {
|
|
1251
|
+
if (path.isCallExpression()) return path.get("callee").isIdentifier({ name: "eval" });
|
|
1252
|
+
return false;
|
|
1253
|
+
}
|
|
1254
|
+
function createEvalReplacementToken(path, updated) {
|
|
1255
|
+
const node = path.node;
|
|
1256
|
+
let offset = 0;
|
|
1257
|
+
let original;
|
|
1258
|
+
if (path.isStringLiteral()) {
|
|
1259
|
+
offset = 1;
|
|
1260
|
+
original = path.node.value;
|
|
1261
|
+
} else if (path.isTemplateElement()) original = path.node.value.raw;
|
|
1262
|
+
else original = "";
|
|
1263
|
+
if (typeof node.start !== "number" || typeof node.end !== "number") return;
|
|
1264
|
+
const start = node.start + offset;
|
|
1265
|
+
const end = node.end - offset;
|
|
1266
|
+
if (start >= end) return;
|
|
1267
|
+
if (original === updated) return;
|
|
1268
|
+
return {
|
|
1269
|
+
start,
|
|
1270
|
+
end,
|
|
1271
|
+
value: path.isStringLiteral() ? (0, _ast_core_escape.jsStringEscape)(updated) : updated,
|
|
1272
|
+
path
|
|
1273
|
+
};
|
|
1274
|
+
}
|
|
1275
|
+
function handleEvalStringLiteral(path, handlerOptions, updater, handler) {
|
|
1276
|
+
const { code } = handler(path.node.value, handlerOptions);
|
|
1277
|
+
if (!code) return;
|
|
1278
|
+
const token = createEvalReplacementToken(path, code);
|
|
1279
|
+
if (token) updater.addToken(token);
|
|
1280
|
+
}
|
|
1281
|
+
function handleEvalTemplateElement(path, handlerOptions, updater, handler) {
|
|
1282
|
+
const { code } = handler(path.node.value.raw, handlerOptions);
|
|
1283
|
+
if (!code) return;
|
|
1284
|
+
const token = createEvalReplacementToken(path, code);
|
|
1285
|
+
if (token) updater.addToken(token);
|
|
1286
|
+
}
|
|
1287
|
+
function getEvalStringHandlerOptions(options) {
|
|
1288
|
+
if (options.needEscaped === false && options.generateMap === false) return options;
|
|
1289
|
+
let cached = evalHandlerOptionsCache.get(options);
|
|
1290
|
+
if (!cached) {
|
|
1291
|
+
cached = {};
|
|
1292
|
+
evalHandlerOptionsCache.set(options, cached);
|
|
1293
|
+
}
|
|
1294
|
+
if (!cached.stringLiteralOptions) cached.stringLiteralOptions = {
|
|
1295
|
+
...options,
|
|
1296
|
+
needEscaped: false,
|
|
1297
|
+
generateMap: false
|
|
1298
|
+
};
|
|
1299
|
+
return cached.stringLiteralOptions;
|
|
1300
|
+
}
|
|
1301
|
+
function getEvalTemplateHandlerOptions(options) {
|
|
1302
|
+
if (options.generateMap === false) return options;
|
|
1303
|
+
let cached = evalHandlerOptionsCache.get(options);
|
|
1304
|
+
if (!cached) {
|
|
1305
|
+
cached = {};
|
|
1306
|
+
evalHandlerOptionsCache.set(options, cached);
|
|
1307
|
+
}
|
|
1308
|
+
if (!cached.templateLiteralOptions) cached.templateLiteralOptions = {
|
|
1309
|
+
...options,
|
|
1310
|
+
generateMap: false
|
|
1311
|
+
};
|
|
1312
|
+
return cached.templateLiteralOptions;
|
|
1313
|
+
}
|
|
1314
|
+
function walkEvalExpression(path, options, updater, handler) {
|
|
1315
|
+
const stringHandlerOptions = getEvalStringHandlerOptions(options);
|
|
1316
|
+
const templateHandlerOptions = getEvalTemplateHandlerOptions(options);
|
|
1317
|
+
const maybeTraverse = path?.traverse;
|
|
1318
|
+
if (typeof maybeTraverse === "function") try {
|
|
1319
|
+
return maybeTraverse.call(path, {
|
|
1320
|
+
StringLiteral(innerPath) {
|
|
1321
|
+
handleEvalStringLiteral(innerPath, stringHandlerOptions, updater, handler);
|
|
1322
|
+
},
|
|
1323
|
+
TemplateElement(innerPath) {
|
|
1324
|
+
handleEvalTemplateElement(innerPath, templateHandlerOptions, updater, handler);
|
|
1325
|
+
}
|
|
1326
|
+
});
|
|
1327
|
+
} catch (error) {
|
|
1328
|
+
const msg = error?.message ?? "";
|
|
1329
|
+
if (!EVAL_SCOPE_ERROR_REGEXP.test(msg)) throw error;
|
|
1330
|
+
}
|
|
1331
|
+
const getArgs = path?.get?.("arguments");
|
|
1332
|
+
if (Array.isArray(getArgs)) {
|
|
1333
|
+
for (const arg of getArgs) {
|
|
1334
|
+
if (arg?.isStringLiteral?.()) {
|
|
1335
|
+
handleEvalStringLiteral(arg, stringHandlerOptions, updater, handler);
|
|
1336
|
+
continue;
|
|
1337
|
+
}
|
|
1338
|
+
if (arg?.isTemplateLiteral?.()) for (const quasi of arg.get("quasis")) handleEvalTemplateElement(quasi, templateHandlerOptions, updater, handler);
|
|
1339
|
+
}
|
|
1340
|
+
return;
|
|
1341
|
+
}
|
|
1342
|
+
const nodeArgs = path?.node?.arguments;
|
|
1343
|
+
if (Array.isArray(nodeArgs)) {
|
|
1344
|
+
for (const n of nodeArgs) if (n?.type === "StringLiteral") handleEvalStringLiteral({
|
|
1345
|
+
node: n,
|
|
1346
|
+
isStringLiteral: () => true
|
|
1347
|
+
}, stringHandlerOptions, updater, handler);
|
|
1348
|
+
else if (n?.type === "TemplateLiteral" && Array.isArray(n.quasis)) for (const q of n.quasis) handleEvalTemplateElement({
|
|
1349
|
+
node: q,
|
|
1350
|
+
isStringLiteral: () => false,
|
|
1351
|
+
isTemplateElement: () => true
|
|
1352
|
+
}, templateHandlerOptions, updater, handler);
|
|
1353
|
+
}
|
|
1354
|
+
}
|
|
1355
|
+
//#endregion
|
|
1356
|
+
//#region src/js/JsTokenUpdater.ts
|
|
1357
|
+
/**
|
|
1358
|
+
* Lightweight helper that batches updates to {@link MagicString}.
|
|
1359
|
+
* It keeps the transformation logic out of the traversal code and makes
|
|
1360
|
+
* it easier to reason about the order in which tokens are written back.
|
|
1361
|
+
*/
|
|
1362
|
+
var JsTokenUpdater = class {
|
|
1363
|
+
constructor({ value } = {}) {
|
|
1364
|
+
this.tokens = value ? [...value] : [];
|
|
1365
|
+
}
|
|
1366
|
+
addToken(token) {
|
|
1367
|
+
if (token) this.tokens.push(token);
|
|
1368
|
+
}
|
|
1369
|
+
push(...args) {
|
|
1370
|
+
this.tokens.push(...args);
|
|
1371
|
+
return this;
|
|
1372
|
+
}
|
|
1373
|
+
/**
|
|
1374
|
+
* 待写入的 token 数量。
|
|
1375
|
+
*/
|
|
1376
|
+
get length() {
|
|
1377
|
+
return this.tokens.length;
|
|
1378
|
+
}
|
|
1379
|
+
map(callbackfn) {
|
|
1380
|
+
this.tokens = this.tokens.map(callbackfn);
|
|
1381
|
+
return this;
|
|
1382
|
+
}
|
|
1383
|
+
filter(callbackfn) {
|
|
1384
|
+
this.tokens = this.tokens.filter(callbackfn);
|
|
1385
|
+
return this;
|
|
1386
|
+
}
|
|
1387
|
+
updateMagicString(ms) {
|
|
1388
|
+
for (const { start, end, value } of this.tokens) ms.update(start, end, value);
|
|
1389
|
+
return ms;
|
|
1390
|
+
}
|
|
1391
|
+
};
|
|
1392
|
+
//#endregion
|
|
1393
|
+
//#region src/js/module-graph/ignored-exports.ts
|
|
1394
|
+
var IgnoredExportsTracker = class {
|
|
1395
|
+
constructor(options) {
|
|
1396
|
+
this.options = options;
|
|
1397
|
+
this.ignoredExportNames = /* @__PURE__ */ new Map();
|
|
1398
|
+
}
|
|
1399
|
+
addIgnoredExport(filename, exportName) {
|
|
1400
|
+
if (!exportName) return;
|
|
1401
|
+
let pending = this.ignoredExportNames.get(filename);
|
|
1402
|
+
if (!pending) {
|
|
1403
|
+
pending = /* @__PURE__ */ new Set();
|
|
1404
|
+
this.ignoredExportNames.set(filename, pending);
|
|
1405
|
+
}
|
|
1406
|
+
if (pending.has(exportName)) return;
|
|
1407
|
+
pending.add(exportName);
|
|
1408
|
+
const existing = this.options.modules.get(filename);
|
|
1409
|
+
if (existing) this.applyIgnoredExportsToAnalysis(filename, existing.analysis);
|
|
1410
|
+
}
|
|
1411
|
+
registerIgnoredExportsFromTokens(resolved, tokens) {
|
|
1412
|
+
for (const token of tokens) if (token.type === "ImportSpecifier") this.addIgnoredExport(resolved, token.imported);
|
|
1413
|
+
else if (token.type === "ImportDefaultSpecifier") this.addIgnoredExport(resolved, "default");
|
|
1414
|
+
}
|
|
1415
|
+
applyIgnoredExportsToAnalysis(filename, analysis) {
|
|
1416
|
+
const pending = this.ignoredExportNames.get(filename);
|
|
1417
|
+
if (!pending || pending.size === 0) return;
|
|
1418
|
+
const names = new Set(pending);
|
|
1419
|
+
pending.clear();
|
|
1420
|
+
const propagate = [];
|
|
1421
|
+
for (const exportPath of analysis.exportDeclarations) {
|
|
1422
|
+
if (names.size === 0) break;
|
|
1423
|
+
if (exportPath.isExportDefaultDeclaration()) {
|
|
1424
|
+
if (names.has("default")) {
|
|
1425
|
+
analysis.walker.walkExportDefaultDeclaration(exportPath);
|
|
1426
|
+
names.delete("default");
|
|
1427
|
+
}
|
|
1428
|
+
continue;
|
|
1429
|
+
}
|
|
1430
|
+
if (exportPath.isExportNamedDeclaration()) {
|
|
1431
|
+
const source = exportPath.node.source?.value;
|
|
1432
|
+
if (typeof source === "string") {
|
|
1433
|
+
for (const spec of exportPath.get("specifiers")) {
|
|
1434
|
+
if (!spec.isExportSpecifier()) continue;
|
|
1435
|
+
const exported = spec.get("exported");
|
|
1436
|
+
let exportedName;
|
|
1437
|
+
if (exported.isIdentifier()) exportedName = exported.node.name;
|
|
1438
|
+
else if (exported.isStringLiteral()) exportedName = exported.node.value;
|
|
1439
|
+
if (!exportedName || !names.has(exportedName)) continue;
|
|
1440
|
+
const local = spec.get("local");
|
|
1441
|
+
if (local.isIdentifier()) {
|
|
1442
|
+
propagate.push({
|
|
1443
|
+
specifier: source,
|
|
1444
|
+
exportName: local.node.name
|
|
1445
|
+
});
|
|
1446
|
+
names.delete(exportedName);
|
|
1447
|
+
} else if (local.isStringLiteral()) {
|
|
1448
|
+
propagate.push({
|
|
1449
|
+
specifier: source,
|
|
1450
|
+
exportName: local.node.value
|
|
1451
|
+
});
|
|
1452
|
+
names.delete(exportedName);
|
|
1453
|
+
}
|
|
1454
|
+
}
|
|
1455
|
+
continue;
|
|
1456
|
+
}
|
|
1457
|
+
const declaration = exportPath.get("declaration");
|
|
1458
|
+
if (declaration.isVariableDeclaration()) for (const decl of declaration.get("declarations")) {
|
|
1459
|
+
const id = decl.get("id");
|
|
1460
|
+
if (id.isIdentifier()) {
|
|
1461
|
+
const exportName = id.node.name;
|
|
1462
|
+
if (names.has(exportName)) {
|
|
1463
|
+
analysis.walker.walkVariableDeclarator(decl);
|
|
1464
|
+
names.delete(exportName);
|
|
1465
|
+
}
|
|
1466
|
+
}
|
|
1467
|
+
}
|
|
1468
|
+
for (const spec of exportPath.get("specifiers")) {
|
|
1469
|
+
if (!spec.isExportSpecifier()) continue;
|
|
1470
|
+
const exported = spec.get("exported");
|
|
1471
|
+
let exportedName;
|
|
1472
|
+
if (exported.isIdentifier()) exportedName = exported.node.name;
|
|
1473
|
+
else if (exported.isStringLiteral()) exportedName = exported.node.value;
|
|
1474
|
+
if (!exportedName || !names.has(exportedName)) continue;
|
|
1475
|
+
const local = spec.get("local");
|
|
1476
|
+
analysis.walker.walkNode(local);
|
|
1477
|
+
names.delete(exportedName);
|
|
1478
|
+
}
|
|
1479
|
+
continue;
|
|
1480
|
+
}
|
|
1481
|
+
if (exportPath.isExportAllDeclaration()) {
|
|
1482
|
+
const source = exportPath.node.source?.value;
|
|
1483
|
+
if (typeof source === "string") {
|
|
1484
|
+
for (const exportName of names) propagate.push({
|
|
1485
|
+
specifier: source,
|
|
1486
|
+
exportName
|
|
1487
|
+
});
|
|
1488
|
+
names.clear();
|
|
1489
|
+
}
|
|
1490
|
+
}
|
|
1491
|
+
}
|
|
1492
|
+
for (const { specifier, exportName } of propagate) {
|
|
1493
|
+
let resolved;
|
|
1494
|
+
try {
|
|
1495
|
+
resolved = this.options.resolve(specifier, filename);
|
|
1496
|
+
} catch {
|
|
1497
|
+
resolved = void 0;
|
|
1498
|
+
}
|
|
1499
|
+
if (!resolved) {
|
|
1500
|
+
pending.add(exportName);
|
|
1501
|
+
continue;
|
|
1502
|
+
}
|
|
1503
|
+
if (this.options.filter && !this.options.filter(resolved, specifier, filename)) {
|
|
1504
|
+
pending.add(exportName);
|
|
1505
|
+
continue;
|
|
1506
|
+
}
|
|
1507
|
+
this.addIgnoredExport(resolved, exportName);
|
|
1508
|
+
}
|
|
1509
|
+
for (const name of names) pending.add(name);
|
|
1510
|
+
}
|
|
1511
|
+
};
|
|
1512
|
+
//#endregion
|
|
1513
|
+
//#region src/js/ModuleGraph.ts
|
|
1514
|
+
var JsModuleGraph = class {
|
|
1515
|
+
constructor(entry, graphOptions) {
|
|
1516
|
+
this.modules = /* @__PURE__ */ new Map();
|
|
1517
|
+
this.queue = [];
|
|
1518
|
+
this.resolve = graphOptions.resolve;
|
|
1519
|
+
this.load = graphOptions.load;
|
|
1520
|
+
this.filter = graphOptions.filter;
|
|
1521
|
+
this.maxDepth = graphOptions.maxDepth ?? Number.POSITIVE_INFINITY;
|
|
1522
|
+
const { moduleGraph: _moduleGraph, filename: _ignoredFilename, ...rest } = entry.handlerOptions;
|
|
1523
|
+
this.baseOptions = {
|
|
1524
|
+
...rest,
|
|
1525
|
+
filename: entry.filename
|
|
1526
|
+
};
|
|
1527
|
+
this.parserOptions = entry.handlerOptions.babelParserOptions;
|
|
1528
|
+
this.rootFilename = entry.filename;
|
|
1529
|
+
this.ignoredExports = new IgnoredExportsTracker({
|
|
1530
|
+
resolve: this.resolve,
|
|
1531
|
+
filter: this.filter,
|
|
1532
|
+
modules: this.modules
|
|
1533
|
+
});
|
|
1534
|
+
this.modules.set(entry.filename, {
|
|
1535
|
+
filename: entry.filename,
|
|
1536
|
+
source: entry.source,
|
|
1537
|
+
analysis: entry.analysis
|
|
1538
|
+
});
|
|
1539
|
+
this.queue.push({
|
|
1540
|
+
filename: entry.filename,
|
|
1541
|
+
depth: 0
|
|
1542
|
+
});
|
|
1543
|
+
}
|
|
1544
|
+
build() {
|
|
1545
|
+
this.collectDependencies();
|
|
1546
|
+
let linked;
|
|
1547
|
+
for (const [filename, state] of this.modules) {
|
|
1548
|
+
if (filename === this.rootFilename) continue;
|
|
1549
|
+
const childOptions = {
|
|
1550
|
+
...this.baseOptions,
|
|
1551
|
+
filename
|
|
1552
|
+
};
|
|
1553
|
+
const code = processUpdatedSource(state.source, childOptions, state.analysis).toString();
|
|
1554
|
+
if (code !== state.source) {
|
|
1555
|
+
if (!linked) linked = {};
|
|
1556
|
+
linked[filename] = { code };
|
|
1557
|
+
}
|
|
1558
|
+
}
|
|
1559
|
+
return linked;
|
|
1560
|
+
}
|
|
1561
|
+
collectDependencies() {
|
|
1562
|
+
while (this.queue.length > 0) {
|
|
1563
|
+
const { filename, depth } = this.queue.shift();
|
|
1564
|
+
if (depth >= this.maxDepth) continue;
|
|
1565
|
+
const state = this.modules.get(filename);
|
|
1566
|
+
if (!state) continue;
|
|
1567
|
+
const dependencySpecifiers = /* @__PURE__ */ new Map();
|
|
1568
|
+
for (const token of state.analysis.walker.imports) {
|
|
1569
|
+
if (!dependencySpecifiers.has(token.source)) dependencySpecifiers.set(token.source, []);
|
|
1570
|
+
dependencySpecifiers.get(token.source).push(token);
|
|
1571
|
+
}
|
|
1572
|
+
for (const exportPath of state.analysis.exportDeclarations) if (exportPath.isExportAllDeclaration() || exportPath.isExportNamedDeclaration()) {
|
|
1573
|
+
const source = exportPath.node.source?.value;
|
|
1574
|
+
if (typeof source === "string" && !dependencySpecifiers.has(source)) dependencySpecifiers.set(source, []);
|
|
1575
|
+
}
|
|
1576
|
+
for (const [specifier, tokens] of dependencySpecifiers) {
|
|
1577
|
+
let resolved;
|
|
1578
|
+
try {
|
|
1579
|
+
resolved = this.resolve(specifier, filename);
|
|
1580
|
+
} catch {
|
|
1581
|
+
continue;
|
|
1582
|
+
}
|
|
1583
|
+
if (!resolved) continue;
|
|
1584
|
+
if (this.filter && !this.filter(resolved, specifier, filename)) continue;
|
|
1585
|
+
if (tokens.length > 0) this.ignoredExports.registerIgnoredExportsFromTokens(resolved, tokens);
|
|
1586
|
+
if (this.modules.has(resolved)) continue;
|
|
1587
|
+
let source;
|
|
1588
|
+
try {
|
|
1589
|
+
source = this.load(resolved);
|
|
1590
|
+
} catch {
|
|
1591
|
+
continue;
|
|
1592
|
+
}
|
|
1593
|
+
if (typeof source !== "string") continue;
|
|
1594
|
+
let analysis;
|
|
1595
|
+
try {
|
|
1596
|
+
analysis = analyzeSource(babelParse(source, {
|
|
1597
|
+
...this.parserOptions,
|
|
1598
|
+
sourceFilename: resolved
|
|
1599
|
+
}), {
|
|
1600
|
+
...this.baseOptions,
|
|
1601
|
+
filename: resolved
|
|
1602
|
+
});
|
|
1603
|
+
this.ignoredExports.applyIgnoredExportsToAnalysis(resolved, analysis);
|
|
1604
|
+
} catch {
|
|
1605
|
+
continue;
|
|
1606
|
+
}
|
|
1607
|
+
this.modules.set(resolved, {
|
|
1608
|
+
filename: resolved,
|
|
1609
|
+
source,
|
|
1610
|
+
analysis
|
|
1611
|
+
});
|
|
1612
|
+
this.queue.push({
|
|
1613
|
+
filename: resolved,
|
|
1614
|
+
depth: depth + 1
|
|
1615
|
+
});
|
|
1616
|
+
}
|
|
1617
|
+
}
|
|
1618
|
+
}
|
|
1619
|
+
};
|
|
1620
|
+
//#endregion
|
|
1621
|
+
//#region src/js/node-path-walker/export-handlers.ts
|
|
1622
|
+
function walkExportDeclaration(ctx, path) {
|
|
1623
|
+
if (path.isExportDeclaration()) {
|
|
1624
|
+
if (path.isExportNamedDeclaration()) walkExportNamedDeclaration(ctx, path);
|
|
1625
|
+
else if (path.isExportDefaultDeclaration()) walkExportDefaultDeclaration(ctx, path);
|
|
1626
|
+
else if (path.isExportAllDeclaration()) walkExportAllDeclaration(ctx, path);
|
|
1627
|
+
}
|
|
1628
|
+
}
|
|
1629
|
+
function walkExportNamedDeclaration(ctx, path) {
|
|
1630
|
+
const declaration = path.get("declaration");
|
|
1631
|
+
if (declaration.isVariableDeclaration()) for (const decl of declaration.get("declarations")) ctx.walkNode(decl);
|
|
1632
|
+
const specifiers = path.get("specifiers");
|
|
1633
|
+
for (const spec of specifiers) if (spec.isExportSpecifier()) {
|
|
1634
|
+
const local = spec.get("local");
|
|
1635
|
+
if (local.isIdentifier()) ctx.walkNode(local);
|
|
1636
|
+
}
|
|
1637
|
+
}
|
|
1638
|
+
function walkExportDefaultDeclaration(ctx, path) {
|
|
1639
|
+
const decl = path.get("declaration");
|
|
1640
|
+
if (decl.isIdentifier()) ctx.walkNode(decl);
|
|
1641
|
+
else ctx.walkNode(decl);
|
|
1642
|
+
}
|
|
1643
|
+
function walkExportAllDeclaration(ctx, path) {
|
|
1644
|
+
const source = path.get("source");
|
|
1645
|
+
if (source.isStringLiteral()) ctx.addImportToken({
|
|
1646
|
+
declaration: path,
|
|
1647
|
+
source: source.node.value,
|
|
1648
|
+
type: "ExportAllDeclaration"
|
|
1649
|
+
});
|
|
1650
|
+
}
|
|
1651
|
+
//#endregion
|
|
1652
|
+
//#region src/js/node-path-walker/import-tokens.ts
|
|
1653
|
+
function maybeAddImportToken(imports, arg) {
|
|
1654
|
+
if (!(arg.isImportSpecifier() && arg.node.importKind !== "type" || arg.isImportDefaultSpecifier())) return false;
|
|
1655
|
+
const importDeclaration = arg.parentPath;
|
|
1656
|
+
if (!importDeclaration.isImportDeclaration() || importDeclaration.node.importKind === "type") return false;
|
|
1657
|
+
if (arg.isImportSpecifier()) {
|
|
1658
|
+
const imported = arg.get("imported");
|
|
1659
|
+
if (imported.isIdentifier()) imports.add({
|
|
1660
|
+
declaration: importDeclaration,
|
|
1661
|
+
specifier: arg,
|
|
1662
|
+
imported: imported.node.name,
|
|
1663
|
+
local: arg.node.local.name,
|
|
1664
|
+
source: importDeclaration.node.source.value,
|
|
1665
|
+
type: "ImportSpecifier"
|
|
1666
|
+
});
|
|
1667
|
+
return true;
|
|
1668
|
+
}
|
|
1669
|
+
imports.add({
|
|
1670
|
+
declaration: importDeclaration,
|
|
1671
|
+
specifier: arg,
|
|
1672
|
+
local: arg.node.local.name,
|
|
1673
|
+
source: importDeclaration.node.source.value,
|
|
1674
|
+
type: "ImportDefaultSpecifier"
|
|
1675
|
+
});
|
|
1676
|
+
return true;
|
|
1677
|
+
}
|
|
1678
|
+
//#endregion
|
|
1679
|
+
//#region src/js/NodePathWalker.ts
|
|
1680
|
+
const EMPTY_IGNORE_CALL_EXPRESSION_IDENTIFIERS = [];
|
|
1681
|
+
const EMPTY_IMPORT_TOKENS = /* @__PURE__ */ new Set();
|
|
1682
|
+
function NOOP_STRING_PATH_CALLBACK() {}
|
|
1683
|
+
const NEVER_MATCH_NAME = () => false;
|
|
1684
|
+
/**
|
|
1685
|
+
* 遍历我们关注的调用表达式所关联的绑定,收集后续需要转换的字符串节点。
|
|
1686
|
+
*/
|
|
1687
|
+
var NodePathWalker = class {
|
|
1688
|
+
constructor({ ignoreCallExpressionIdentifiers, callback } = {}) {
|
|
1689
|
+
this.hasIgnoredCallIdentifiers = Boolean(ignoreCallExpressionIdentifiers && ignoreCallExpressionIdentifiers.length > 0);
|
|
1690
|
+
this.ignoreCallExpressionIdentifiers = ignoreCallExpressionIdentifiers ?? EMPTY_IGNORE_CALL_EXPRESSION_IDENTIFIERS;
|
|
1691
|
+
this.callback = callback ?? NOOP_STRING_PATH_CALLBACK;
|
|
1692
|
+
this.isIgnoredCallIdentifier = this.hasIgnoredCallIdentifiers ? createNameMatcher(this.ignoreCallExpressionIdentifiers, { exact: true }) : NEVER_MATCH_NAME;
|
|
1693
|
+
}
|
|
1694
|
+
get imports() {
|
|
1695
|
+
return this.importsStore ?? EMPTY_IMPORT_TOKENS;
|
|
1696
|
+
}
|
|
1697
|
+
getWritableImports() {
|
|
1698
|
+
if (!this.importsStore) this.importsStore = /* @__PURE__ */ new Set();
|
|
1699
|
+
return this.importsStore;
|
|
1700
|
+
}
|
|
1701
|
+
addImportToken(token) {
|
|
1702
|
+
this.getWritableImports().add(token);
|
|
1703
|
+
}
|
|
1704
|
+
getVisited() {
|
|
1705
|
+
if (!this.visitedStore) this.visitedStore = /* @__PURE__ */ new WeakSet();
|
|
1706
|
+
return this.visitedStore;
|
|
1707
|
+
}
|
|
1708
|
+
walkVariableDeclarator(path) {
|
|
1709
|
+
const init = path.get("init");
|
|
1710
|
+
this.walkNode(init);
|
|
1711
|
+
}
|
|
1712
|
+
walkTemplateLiteral(path) {
|
|
1713
|
+
for (const exp of path.get("expressions")) this.walkNode(exp);
|
|
1714
|
+
for (const quasis of path.get("quasis")) this.callback(quasis);
|
|
1715
|
+
}
|
|
1716
|
+
walkStringLiteral(path) {
|
|
1717
|
+
this.callback(path);
|
|
1718
|
+
}
|
|
1719
|
+
walkBinaryExpression(path) {
|
|
1720
|
+
const left = path.get("left");
|
|
1721
|
+
this.walkNode(left);
|
|
1722
|
+
const right = path.get("right");
|
|
1723
|
+
this.walkNode(right);
|
|
1724
|
+
}
|
|
1725
|
+
walkLogicalExpression(path) {
|
|
1726
|
+
const left = path.get("left");
|
|
1727
|
+
this.walkNode(left);
|
|
1728
|
+
const right = path.get("right");
|
|
1729
|
+
this.walkNode(right);
|
|
1730
|
+
}
|
|
1731
|
+
walkObjectExpression(path) {
|
|
1732
|
+
const props = path.get("properties");
|
|
1733
|
+
for (const prop of props) if (prop.isObjectProperty()) {
|
|
1734
|
+
const key = prop.get("key");
|
|
1735
|
+
this.walkNode(key);
|
|
1736
|
+
const value = prop.get("value");
|
|
1737
|
+
this.walkNode(value);
|
|
1738
|
+
}
|
|
1739
|
+
}
|
|
1740
|
+
walkArrayExpression(path) {
|
|
1741
|
+
const elements = path.get("elements");
|
|
1742
|
+
for (const element of elements) this.walkNode(element);
|
|
1743
|
+
}
|
|
1744
|
+
walkNode(arg) {
|
|
1745
|
+
const visited = this.getVisited();
|
|
1746
|
+
if (visited.has(arg)) return;
|
|
1747
|
+
visited.add(arg);
|
|
1748
|
+
if (arg.isIdentifier()) {
|
|
1749
|
+
const binding = arg?.scope?.getBinding?.(arg.node.name);
|
|
1750
|
+
if (binding) this.walkNode(binding.path);
|
|
1751
|
+
} else if (arg.isMemberExpression()) {
|
|
1752
|
+
const objectPath = arg.get("object");
|
|
1753
|
+
if (objectPath.isIdentifier()) {
|
|
1754
|
+
const binding = arg?.scope?.getBinding?.(objectPath.node.name);
|
|
1755
|
+
if (binding) {
|
|
1756
|
+
if (binding.path.isVariableDeclarator()) this.walkVariableDeclarator(binding.path);
|
|
1757
|
+
}
|
|
1758
|
+
}
|
|
1759
|
+
} else if (arg.isTemplateLiteral()) this.walkTemplateLiteral(arg);
|
|
1760
|
+
else if (arg.isStringLiteral()) this.walkStringLiteral(arg);
|
|
1761
|
+
else if (arg.isBinaryExpression()) this.walkBinaryExpression(arg);
|
|
1762
|
+
else if (arg.isLogicalExpression()) this.walkLogicalExpression(arg);
|
|
1763
|
+
else if (arg.isObjectExpression()) this.walkObjectExpression(arg);
|
|
1764
|
+
else if (arg.isArrayExpression()) this.walkArrayExpression(arg);
|
|
1765
|
+
else if (arg.isVariableDeclarator()) this.walkVariableDeclarator(arg);
|
|
1766
|
+
else if (maybeAddImportToken(this.getWritableImports(), arg)) {}
|
|
1767
|
+
}
|
|
1768
|
+
/**
|
|
1769
|
+
* Walk the arguments of a desired call expression so their bindings can be analysed.
|
|
1770
|
+
*/
|
|
1771
|
+
walkCallExpression(path) {
|
|
1772
|
+
if (!this.hasIgnoredCallIdentifiers) return;
|
|
1773
|
+
const calleePath = path.get("callee");
|
|
1774
|
+
if (calleePath.isIdentifier() && this.isIgnoredCallIdentifier(calleePath.node.name)) for (const arg of path.get("arguments")) this.walkNode(arg);
|
|
1775
|
+
}
|
|
1776
|
+
walkExportDeclaration(path) {
|
|
1777
|
+
walkExportDeclaration(this, path);
|
|
1778
|
+
}
|
|
1779
|
+
walkExportNamedDeclaration(path) {
|
|
1780
|
+
walkExportNamedDeclaration(this, path);
|
|
1781
|
+
}
|
|
1782
|
+
walkExportDefaultDeclaration(path) {
|
|
1783
|
+
walkExportDefaultDeclaration(this, path);
|
|
1784
|
+
}
|
|
1785
|
+
walkExportAllDeclaration(path) {
|
|
1786
|
+
walkExportAllDeclaration(this, path);
|
|
1787
|
+
}
|
|
1788
|
+
};
|
|
1789
|
+
//#endregion
|
|
1790
|
+
//#region src/js/taggedTemplateIgnore.ts
|
|
1791
|
+
function createTaggedTemplateIgnore({ matcher, names }) {
|
|
1792
|
+
const bindingIgnoreCache = /* @__PURE__ */ new Map();
|
|
1793
|
+
const taggedTemplateIgnoreCache = /* @__PURE__ */ new WeakMap();
|
|
1794
|
+
const seenBindings = /* @__PURE__ */ new Set();
|
|
1795
|
+
let singleCanonicalIgnoreName;
|
|
1796
|
+
let canonicalIgnoreNames;
|
|
1797
|
+
for (const item of names ?? []) {
|
|
1798
|
+
if (typeof item !== "string") continue;
|
|
1799
|
+
if (singleCanonicalIgnoreName === void 0) {
|
|
1800
|
+
singleCanonicalIgnoreName = item;
|
|
1801
|
+
continue;
|
|
1802
|
+
}
|
|
1803
|
+
if (item === singleCanonicalIgnoreName) continue;
|
|
1804
|
+
if (!canonicalIgnoreNames) {
|
|
1805
|
+
canonicalIgnoreNames = new Set([singleCanonicalIgnoreName, item]);
|
|
1806
|
+
continue;
|
|
1807
|
+
}
|
|
1808
|
+
canonicalIgnoreNames.add(item);
|
|
1809
|
+
}
|
|
1810
|
+
const hasCanonicalIgnoreNames = singleCanonicalIgnoreName !== void 0;
|
|
1811
|
+
const matchesIgnoreName = (value) => {
|
|
1812
|
+
if (hasCanonicalIgnoreNames) {
|
|
1813
|
+
if (canonicalIgnoreNames) {
|
|
1814
|
+
if (canonicalIgnoreNames.has(value)) return true;
|
|
1815
|
+
} else if (value === singleCanonicalIgnoreName) return true;
|
|
1816
|
+
}
|
|
1817
|
+
return matcher(value);
|
|
1818
|
+
};
|
|
1819
|
+
const propertyMatches = (propertyPath) => {
|
|
1820
|
+
if (!propertyPath) return false;
|
|
1821
|
+
if (propertyPath.isIdentifier()) return matchesIgnoreName(propertyPath.node.name);
|
|
1822
|
+
if (propertyPath.isStringLiteral()) return matchesIgnoreName(propertyPath.node.value);
|
|
1823
|
+
return false;
|
|
1824
|
+
};
|
|
1825
|
+
const resolvesMemberExpressionToIgnore = (path, seen) => {
|
|
1826
|
+
if (propertyMatches(path.get("property"))) return true;
|
|
1827
|
+
const objectPath = path.get("object");
|
|
1828
|
+
if (objectPath.isIdentifier()) {
|
|
1829
|
+
const binding = (objectPath?.scope)?.getBinding?.(objectPath.node.name);
|
|
1830
|
+
if (binding) return resolvesToWeappTwIgnore(binding, seen);
|
|
1831
|
+
}
|
|
1832
|
+
return false;
|
|
1833
|
+
};
|
|
1834
|
+
const resolvesToWeappTwIgnore = (binding, seen) => {
|
|
1835
|
+
const cached = bindingIgnoreCache.get(binding);
|
|
1836
|
+
if (cached !== void 0) return cached;
|
|
1837
|
+
if (seen.has(binding)) return false;
|
|
1838
|
+
seen.add(binding);
|
|
1839
|
+
let result = false;
|
|
1840
|
+
const bindingPath = binding.path;
|
|
1841
|
+
if (bindingPath.isImportSpecifier()) {
|
|
1842
|
+
const imported = bindingPath.node.imported;
|
|
1843
|
+
if (imported.type === "Identifier" && matchesIgnoreName(imported.name)) result = true;
|
|
1844
|
+
else if (imported.type === "StringLiteral" && matchesIgnoreName(imported.value)) result = true;
|
|
1845
|
+
} else if (bindingPath.isVariableDeclarator()) {
|
|
1846
|
+
const init = bindingPath.get("init");
|
|
1847
|
+
if (init && init.node) {
|
|
1848
|
+
if (init.isIdentifier()) {
|
|
1849
|
+
const target = binding?.scope?.getBinding?.(init.node.name);
|
|
1850
|
+
if (target) result = resolvesToWeappTwIgnore(target, seen);
|
|
1851
|
+
} else if (init.isMemberExpression()) result = resolvesMemberExpressionToIgnore(init, seen);
|
|
1852
|
+
}
|
|
1853
|
+
}
|
|
1854
|
+
bindingIgnoreCache.set(binding, result);
|
|
1855
|
+
seen.delete(binding);
|
|
1856
|
+
return result;
|
|
1857
|
+
};
|
|
1858
|
+
const getEffectiveTagPath = (tagPath) => {
|
|
1859
|
+
let current = tagPath;
|
|
1860
|
+
while (true) {
|
|
1861
|
+
if (current.isParenthesizedExpression?.() || current.node.type === "ParenthesizedExpression") {
|
|
1862
|
+
current = current.get("expression");
|
|
1863
|
+
continue;
|
|
1864
|
+
}
|
|
1865
|
+
if (current.isTSAsExpression() || current.isTSTypeAssertion()) {
|
|
1866
|
+
current = current.get("expression");
|
|
1867
|
+
continue;
|
|
1868
|
+
}
|
|
1869
|
+
if (current.isTSNonNullExpression()) {
|
|
1870
|
+
current = current.get("expression");
|
|
1871
|
+
continue;
|
|
1872
|
+
}
|
|
1873
|
+
if (current.isTypeCastExpression?.()) {
|
|
1874
|
+
current = current.get("expression");
|
|
1875
|
+
continue;
|
|
1876
|
+
}
|
|
1877
|
+
if (current.isSequenceExpression()) {
|
|
1878
|
+
const last = current.get("expressions").at(-1);
|
|
1879
|
+
if (last) {
|
|
1880
|
+
current = last;
|
|
1881
|
+
continue;
|
|
1882
|
+
}
|
|
1883
|
+
}
|
|
1884
|
+
if (current.isCallExpression?.() || current.node.type === "CallExpression") {
|
|
1885
|
+
current = current.get("callee");
|
|
1886
|
+
continue;
|
|
1887
|
+
}
|
|
1888
|
+
break;
|
|
1889
|
+
}
|
|
1890
|
+
return current;
|
|
1891
|
+
};
|
|
1892
|
+
const evaluateTagPath = (tagPath, seen) => {
|
|
1893
|
+
if (tagPath.isCallExpression?.() || tagPath.node.type === "CallExpression") return evaluateTagPath(tagPath.get("callee"), seen);
|
|
1894
|
+
if (tagPath.isIdentifier()) {
|
|
1895
|
+
if (matchesIgnoreName(tagPath.node.name)) return true;
|
|
1896
|
+
const binding = tagPath?.scope?.getBinding?.(tagPath.node.name);
|
|
1897
|
+
if (binding) return resolvesToWeappTwIgnore(binding, seen);
|
|
1898
|
+
return false;
|
|
1899
|
+
}
|
|
1900
|
+
if (tagPath.isMemberExpression()) return resolvesMemberExpressionToIgnore(tagPath, seen);
|
|
1901
|
+
return false;
|
|
1902
|
+
};
|
|
1903
|
+
const computeIgnore = (tagPath) => {
|
|
1904
|
+
const cached = taggedTemplateIgnoreCache.get(tagPath.node);
|
|
1905
|
+
if (cached !== void 0) return cached;
|
|
1906
|
+
const effectiveTagPath = getEffectiveTagPath(tagPath);
|
|
1907
|
+
const effectiveCached = taggedTemplateIgnoreCache.get(effectiveTagPath.node);
|
|
1908
|
+
if (effectiveCached !== void 0) {
|
|
1909
|
+
taggedTemplateIgnoreCache.set(tagPath.node, effectiveCached);
|
|
1910
|
+
return effectiveCached;
|
|
1911
|
+
}
|
|
1912
|
+
seenBindings.clear();
|
|
1913
|
+
const result = evaluateTagPath(effectiveTagPath, seenBindings);
|
|
1914
|
+
taggedTemplateIgnoreCache.set(effectiveTagPath.node, result);
|
|
1915
|
+
taggedTemplateIgnoreCache.set(tagPath.node, result);
|
|
1916
|
+
return result;
|
|
1917
|
+
};
|
|
1918
|
+
return {
|
|
1919
|
+
shouldIgnore(tagPath) {
|
|
1920
|
+
return computeIgnore(tagPath);
|
|
1921
|
+
},
|
|
1922
|
+
getEffectiveTagPath
|
|
1923
|
+
};
|
|
1924
|
+
}
|
|
1925
|
+
//#endregion
|
|
1926
|
+
//#region src/js/babel.ts
|
|
1927
|
+
const EXPRESSION_WRAPPER_PREFIX = "(\n";
|
|
1928
|
+
const EXPRESSION_WRAPPER_SUFFIX = "\n)";
|
|
1929
|
+
const EMPTY_IGNORED_PATHS = /* @__PURE__ */ new WeakSet();
|
|
1930
|
+
const EMPTY_IMPORT_DECLARATIONS = /* @__PURE__ */ new Set();
|
|
1931
|
+
const EMPTY_EXPORT_DECLARATIONS = /* @__PURE__ */ new Set();
|
|
1932
|
+
const EMPTY_REQUIRE_CALL_PATHS = [];
|
|
1933
|
+
const ignoredTaggedTemplateMatcherCache = /* @__PURE__ */ new WeakMap();
|
|
1934
|
+
let defaultEvalHandler;
|
|
1935
|
+
function getIgnoredTaggedTemplateMatcher(options) {
|
|
1936
|
+
const cached = ignoredTaggedTemplateMatcherCache.get(options);
|
|
1937
|
+
if (cached) return cached;
|
|
1938
|
+
const created = createNameMatcher(options.ignoreTaggedTemplateExpressionIdentifiers, { exact: true });
|
|
1939
|
+
ignoredTaggedTemplateMatcherCache.set(options, created);
|
|
1940
|
+
return created;
|
|
1941
|
+
}
|
|
1942
|
+
function getDefaultEvalHandler() {
|
|
1943
|
+
if (!defaultEvalHandler) throw new Error("Default JS eval handler is not initialized.");
|
|
1944
|
+
return defaultEvalHandler;
|
|
1945
|
+
}
|
|
1946
|
+
function analyzeSource(ast, options, handler, collectModuleMetadata = true) {
|
|
1947
|
+
const jsTokenUpdater = new JsTokenUpdater();
|
|
1948
|
+
const needScope = Boolean(options.ignoreCallExpressionIdentifiers && options.ignoreCallExpressionIdentifiers.length > 0);
|
|
1949
|
+
const ignoredPaths = needScope ? /* @__PURE__ */ new WeakSet() : EMPTY_IGNORED_PATHS;
|
|
1950
|
+
const walker = needScope ? new NodePathWalker({
|
|
1951
|
+
ignoreCallExpressionIdentifiers: options.ignoreCallExpressionIdentifiers,
|
|
1952
|
+
callback(path) {
|
|
1953
|
+
ignoredPaths.add(path);
|
|
1954
|
+
}
|
|
1955
|
+
}) : new NodePathWalker();
|
|
1956
|
+
let taggedTemplateIgnore;
|
|
1957
|
+
const hasTaggedTemplateIgnoreIdentifiers = Boolean(options.ignoreTaggedTemplateExpressionIdentifiers && options.ignoreTaggedTemplateExpressionIdentifiers.length > 0);
|
|
1958
|
+
function getTaggedTemplateIgnore() {
|
|
1959
|
+
if (!taggedTemplateIgnore) taggedTemplateIgnore = createTaggedTemplateIgnore({
|
|
1960
|
+
matcher: getIgnoredTaggedTemplateMatcher(options),
|
|
1961
|
+
names: options.ignoreTaggedTemplateExpressionIdentifiers
|
|
1962
|
+
});
|
|
1963
|
+
return taggedTemplateIgnore;
|
|
1964
|
+
}
|
|
1965
|
+
const targetPaths = [];
|
|
1966
|
+
const importDeclarations = collectModuleMetadata ? /* @__PURE__ */ new Set() : EMPTY_IMPORT_DECLARATIONS;
|
|
1967
|
+
const exportDeclarations = collectModuleMetadata ? /* @__PURE__ */ new Set() : EMPTY_EXPORT_DECLARATIONS;
|
|
1968
|
+
const requireCallPaths = collectModuleMetadata ? [] : EMPTY_REQUIRE_CALL_PATHS;
|
|
1969
|
+
const evalHandler = handler ?? getDefaultEvalHandler();
|
|
1970
|
+
traverse(ast, {
|
|
1971
|
+
StringLiteral: { enter(p) {
|
|
1972
|
+
if (isEvalPath(p.parentPath)) return;
|
|
1973
|
+
targetPaths.push(p);
|
|
1974
|
+
} },
|
|
1975
|
+
TemplateElement: { enter: hasTaggedTemplateIgnoreIdentifiers ? (p) => {
|
|
1976
|
+
const pp = p.parentPath;
|
|
1977
|
+
if (pp.isTemplateLiteral()) {
|
|
1978
|
+
const ppp = pp.parentPath;
|
|
1979
|
+
if (isEvalPath(ppp)) return;
|
|
1980
|
+
if (ppp.isTaggedTemplateExpression()) {
|
|
1981
|
+
const tagPath = ppp.get("tag");
|
|
1982
|
+
if (getTaggedTemplateIgnore().shouldIgnore(tagPath)) return;
|
|
1983
|
+
}
|
|
1984
|
+
}
|
|
1985
|
+
targetPaths.push(p);
|
|
1986
|
+
} : (p) => {
|
|
1987
|
+
const pp = p.parentPath;
|
|
1988
|
+
if (pp.isTemplateLiteral()) {
|
|
1989
|
+
const ppp = pp.parentPath;
|
|
1990
|
+
if (isEvalPath(ppp)) return;
|
|
1991
|
+
}
|
|
1992
|
+
targetPaths.push(p);
|
|
1993
|
+
} },
|
|
1994
|
+
CallExpression: { enter: !collectModuleMetadata && !needScope ? (p) => {
|
|
1995
|
+
if (isEvalPath(p)) walkEvalExpression(p, options, jsTokenUpdater, evalHandler);
|
|
1996
|
+
} : (p) => {
|
|
1997
|
+
if (isEvalPath(p)) {
|
|
1998
|
+
walkEvalExpression(p, options, jsTokenUpdater, evalHandler);
|
|
1999
|
+
return;
|
|
2000
|
+
}
|
|
2001
|
+
const calleePath = p.get("callee");
|
|
2002
|
+
if (collectModuleMetadata && calleePath.isIdentifier({ name: "require" }) && !p?.scope?.hasBinding?.("require")) {
|
|
2003
|
+
const args = p.get("arguments");
|
|
2004
|
+
if (Array.isArray(args) && args.length > 0) {
|
|
2005
|
+
const first = args[0];
|
|
2006
|
+
if (first?.isStringLiteral()) requireCallPaths.push(first);
|
|
2007
|
+
}
|
|
2008
|
+
}
|
|
2009
|
+
if (needScope) walker.walkCallExpression(p);
|
|
2010
|
+
} },
|
|
2011
|
+
...collectModuleMetadata ? {
|
|
2012
|
+
ImportDeclaration: { enter(p) {
|
|
2013
|
+
importDeclarations.add(p);
|
|
2014
|
+
} },
|
|
2015
|
+
ExportDeclaration: { enter(p) {
|
|
2016
|
+
exportDeclarations.add(p);
|
|
2017
|
+
} }
|
|
2018
|
+
} : {},
|
|
2019
|
+
noScope: !needScope
|
|
2020
|
+
});
|
|
2021
|
+
return {
|
|
2022
|
+
walker,
|
|
2023
|
+
jsTokenUpdater,
|
|
2024
|
+
ast,
|
|
2025
|
+
targetPaths,
|
|
2026
|
+
importDeclarations,
|
|
2027
|
+
exportDeclarations,
|
|
2028
|
+
requireCallPaths,
|
|
2029
|
+
ignoredPaths
|
|
2030
|
+
};
|
|
2031
|
+
}
|
|
2032
|
+
function jsHandler(rawSource, options) {
|
|
2033
|
+
const shouldWrapExpression = Boolean(options.wrapExpression);
|
|
2034
|
+
const source = shouldWrapExpression ? `${EXPRESSION_WRAPPER_PREFIX}${rawSource}${EXPRESSION_WRAPPER_SUFFIX}` : rawSource;
|
|
2035
|
+
let ast;
|
|
2036
|
+
try {
|
|
2037
|
+
ast = babelParse(source, options.babelParserOptions);
|
|
2038
|
+
} catch (error) {
|
|
2039
|
+
return {
|
|
2040
|
+
code: rawSource,
|
|
2041
|
+
error
|
|
2042
|
+
};
|
|
2043
|
+
}
|
|
2044
|
+
const needsModuleMetadata = Boolean(options.moduleSpecifierReplacements || options.moduleGraph && options.filename);
|
|
2045
|
+
const analysis = analyzeSource(ast, options, jsHandler, needsModuleMetadata);
|
|
2046
|
+
const ms = processUpdatedSource(source, options, analysis);
|
|
2047
|
+
if (shouldWrapExpression) {
|
|
2048
|
+
const start = 0;
|
|
2049
|
+
const end = source.length;
|
|
2050
|
+
const prefixLength = 2;
|
|
2051
|
+
const suffixLength = 2;
|
|
2052
|
+
ms.remove(start, start + prefixLength);
|
|
2053
|
+
ms.remove(end - suffixLength, end);
|
|
2054
|
+
}
|
|
2055
|
+
const result = { code: ms.toString() };
|
|
2056
|
+
if (options.generateMap) Object.defineProperty(result, "map", {
|
|
2057
|
+
configurable: true,
|
|
2058
|
+
enumerable: true,
|
|
2059
|
+
get() {
|
|
2060
|
+
return ms.generateMap();
|
|
2061
|
+
}
|
|
2062
|
+
});
|
|
2063
|
+
if (options.moduleGraph && options.filename) {
|
|
2064
|
+
const linked = new JsModuleGraph({
|
|
2065
|
+
filename: options.filename,
|
|
2066
|
+
source: rawSource,
|
|
2067
|
+
analysis,
|
|
2068
|
+
handlerOptions: options
|
|
2069
|
+
}, options.moduleGraph).build();
|
|
2070
|
+
if (linked) result.linked = linked;
|
|
2071
|
+
}
|
|
2072
|
+
return result;
|
|
2073
|
+
}
|
|
2074
|
+
defaultEvalHandler = jsHandler;
|
|
2075
|
+
//#endregion
|
|
2076
|
+
//#region src/js/index.ts
|
|
2077
|
+
/** 默认 LRU 缓存最大条目数 */
|
|
2078
|
+
const RESULT_CACHE_MAX = 512;
|
|
2079
|
+
/** 仅对短片段做内层结果缓存,避免 bundler 热路径重复 hash 大块 JS。 */
|
|
2080
|
+
const CACHEABLE_SOURCE_MAX_LENGTH = 512;
|
|
2081
|
+
/** 为每个 ClassNameSet 实例分配递增 ID */
|
|
2082
|
+
const classNameSetIds = /* @__PURE__ */ new WeakMap();
|
|
2083
|
+
let nextClassNameSetId = 0;
|
|
2084
|
+
/**
|
|
2085
|
+
* 获取 ClassNameSet 的唯一身份 ID。
|
|
2086
|
+
* 每个 Set 引用分配一个递增整数,用于指纹计算。
|
|
2087
|
+
*/
|
|
2088
|
+
function getClassNameSetId(set) {
|
|
2089
|
+
if (!set) return "none";
|
|
2090
|
+
const existing = classNameSetIds.get(set);
|
|
2091
|
+
if (existing !== void 0) return String(existing);
|
|
2092
|
+
const id = nextClassNameSetId++;
|
|
2093
|
+
classNameSetIds.set(set, id);
|
|
2094
|
+
return String(id);
|
|
2095
|
+
}
|
|
2096
|
+
/** 缓存 IJsHandlerOptions -> fingerprint 的映射 */
|
|
2097
|
+
const fingerprintCache = /* @__PURE__ */ new WeakMap();
|
|
2098
|
+
/**
|
|
2099
|
+
* 计算选项指纹,包含所有影响转译结果的字段。
|
|
2100
|
+
* 不包含 filename、moduleGraph、jsPreserveClass。
|
|
2101
|
+
*/
|
|
2102
|
+
function getOptionsFingerprint(options) {
|
|
2103
|
+
const cached = fingerprintCache.get(options);
|
|
2104
|
+
if (cached) return cached;
|
|
2105
|
+
const fingerprint = [
|
|
2106
|
+
getClassNameSetId(options.classNameSet),
|
|
2107
|
+
JSON.stringify(options.escapeMap ?? null),
|
|
2108
|
+
options.needEscaped ? "1" : "0",
|
|
2109
|
+
options.alwaysEscape ? "1" : "0",
|
|
2110
|
+
options.unescapeUnicode ? "1" : "0",
|
|
2111
|
+
options.generateMap ? "1" : "0",
|
|
2112
|
+
options.uniAppX ? "1" : "0",
|
|
2113
|
+
options.wrapExpression ? "1" : "0",
|
|
2114
|
+
String(options.tailwindcssMajorVersion ?? ""),
|
|
2115
|
+
String(options.staleClassNameFallback ?? ""),
|
|
2116
|
+
String(options.jsArbitraryValueFallback ?? ""),
|
|
2117
|
+
JSON.stringify(options.arbitraryValues ?? null),
|
|
2118
|
+
JSON.stringify(options.ignoreCallExpressionIdentifiers ?? null),
|
|
2119
|
+
JSON.stringify(options.ignoreTaggedTemplateExpressionIdentifiers?.map((v) => v instanceof RegExp ? v.source : v) ?? null),
|
|
2120
|
+
JSON.stringify(options.moduleSpecifierReplacements ?? null),
|
|
2121
|
+
JSON.stringify(options.babelParserOptions ?? null)
|
|
2122
|
+
].join("|");
|
|
2123
|
+
fingerprintCache.set(options, fingerprint);
|
|
2124
|
+
return fingerprint;
|
|
2125
|
+
}
|
|
2126
|
+
function hasDefinedOverrides(options) {
|
|
2127
|
+
if (!options) return false;
|
|
2128
|
+
for (const key in options) if (options[key] !== void 0) return true;
|
|
2129
|
+
return false;
|
|
2130
|
+
}
|
|
2131
|
+
function shouldCacheJsResult(rawSource, options) {
|
|
2132
|
+
if (rawSource.length === 0 || rawSource.length > CACHEABLE_SOURCE_MAX_LENGTH) return false;
|
|
2133
|
+
if (options.moduleGraph || options.filename) return false;
|
|
2134
|
+
return true;
|
|
2135
|
+
}
|
|
2136
|
+
function createJsHandler(options) {
|
|
2137
|
+
const defaults = {
|
|
2138
|
+
escapeMap: options.escapeMap,
|
|
2139
|
+
staleClassNameFallback: options.staleClassNameFallback,
|
|
2140
|
+
jsArbitraryValueFallback: options.jsArbitraryValueFallback,
|
|
2141
|
+
tailwindcssMajorVersion: options.tailwindcssMajorVersion,
|
|
2142
|
+
arbitraryValues: options.arbitraryValues,
|
|
2143
|
+
jsPreserveClass: options.jsPreserveClass,
|
|
2144
|
+
generateMap: options.generateMap,
|
|
2145
|
+
needEscaped: options.needEscaped,
|
|
2146
|
+
alwaysEscape: options.alwaysEscape,
|
|
2147
|
+
unescapeUnicode: options.unescapeUnicode,
|
|
2148
|
+
babelParserOptions: options.babelParserOptions,
|
|
2149
|
+
ignoreCallExpressionIdentifiers: options.ignoreCallExpressionIdentifiers,
|
|
2150
|
+
ignoreTaggedTemplateExpressionIdentifiers: options.ignoreTaggedTemplateExpressionIdentifiers,
|
|
2151
|
+
uniAppX: options.uniAppX,
|
|
2152
|
+
moduleSpecifierReplacements: options.moduleSpecifierReplacements
|
|
2153
|
+
};
|
|
2154
|
+
/** 层1: 无 override 时,classNameSet -> resolvedOptions */
|
|
2155
|
+
const defaultOptionsCache = /* @__PURE__ */ new WeakMap();
|
|
2156
|
+
let resolvedOptionsWithoutClassNameSet;
|
|
2157
|
+
/** 层2: 有 override 时,overrideOptions -> { bySet, noSet } */
|
|
2158
|
+
const overrideOptionsCache = /* @__PURE__ */ new WeakMap();
|
|
2159
|
+
const resultCache = new lru_cache.LRUCache({ max: RESULT_CACHE_MAX });
|
|
2160
|
+
function resolveDefaultOptions(classNameSet) {
|
|
2161
|
+
if (!classNameSet) {
|
|
2162
|
+
if (!resolvedOptionsWithoutClassNameSet) resolvedOptionsWithoutClassNameSet = {
|
|
2163
|
+
...defaults,
|
|
2164
|
+
classNameSet
|
|
2165
|
+
};
|
|
2166
|
+
return resolvedOptionsWithoutClassNameSet;
|
|
2167
|
+
}
|
|
2168
|
+
const cached = defaultOptionsCache.get(classNameSet);
|
|
2169
|
+
if (cached) return cached;
|
|
2170
|
+
const created = {
|
|
2171
|
+
...defaults,
|
|
2172
|
+
classNameSet
|
|
2173
|
+
};
|
|
2174
|
+
defaultOptionsCache.set(classNameSet, created);
|
|
2175
|
+
return created;
|
|
2176
|
+
}
|
|
2177
|
+
function getCachedJsResult(rawSource, resolvedOptions) {
|
|
2178
|
+
if (!shouldCacheJsResult(rawSource, resolvedOptions)) return;
|
|
2179
|
+
const key = `${getOptionsFingerprint(resolvedOptions)}:${(0, _weapp_tailwindcss_shared_node.md5)(rawSource)}`;
|
|
2180
|
+
return resultCache.get(key);
|
|
2181
|
+
}
|
|
2182
|
+
function setCachedJsResult(rawSource, resolvedOptions, result) {
|
|
2183
|
+
if (!shouldCacheJsResult(rawSource, resolvedOptions) || result.error || result.linked) return result;
|
|
2184
|
+
const key = `${getOptionsFingerprint(resolvedOptions)}:${(0, _weapp_tailwindcss_shared_node.md5)(rawSource)}`;
|
|
2185
|
+
resultCache.set(key, result);
|
|
2186
|
+
return result;
|
|
2187
|
+
}
|
|
2188
|
+
function resolveOptions(classNameSet, overrideOptions) {
|
|
2189
|
+
if (!hasDefinedOverrides(overrideOptions)) return resolveDefaultOptions(classNameSet);
|
|
2190
|
+
let entry = overrideOptionsCache.get(overrideOptions);
|
|
2191
|
+
if (!entry) {
|
|
2192
|
+
entry = { bySet: /* @__PURE__ */ new WeakMap() };
|
|
2193
|
+
overrideOptionsCache.set(overrideOptions, entry);
|
|
2194
|
+
}
|
|
2195
|
+
if (!classNameSet) {
|
|
2196
|
+
if (entry.noSet) return entry.noSet;
|
|
2197
|
+
const created = (0, _weapp_tailwindcss_shared.defuOverrideArray)({
|
|
2198
|
+
...overrideOptions,
|
|
2199
|
+
classNameSet
|
|
2200
|
+
}, defaults);
|
|
2201
|
+
entry.noSet = created;
|
|
2202
|
+
return created;
|
|
2203
|
+
}
|
|
2204
|
+
const cached = entry.bySet.get(classNameSet);
|
|
2205
|
+
if (cached) return cached;
|
|
2206
|
+
const created = (0, _weapp_tailwindcss_shared.defuOverrideArray)({
|
|
2207
|
+
...overrideOptions,
|
|
2208
|
+
classNameSet
|
|
2209
|
+
}, defaults);
|
|
2210
|
+
entry.bySet.set(classNameSet, created);
|
|
2211
|
+
return created;
|
|
2212
|
+
}
|
|
2213
|
+
function handler(rawSource, classNameSet, options) {
|
|
2214
|
+
const resolvedOptions = resolveOptions(classNameSet, options);
|
|
2215
|
+
const cached = getCachedJsResult(rawSource, resolvedOptions);
|
|
2216
|
+
if (cached) return cached;
|
|
2217
|
+
return setCachedJsResult(rawSource, resolvedOptions, jsHandler(rawSource, resolvedOptions));
|
|
2218
|
+
}
|
|
2219
|
+
return handler;
|
|
2220
|
+
}
|
|
2221
|
+
//#endregion
|
|
2222
|
+
//#region src/wxml/custom-attributes.ts
|
|
2223
|
+
function regTest(reg, str) {
|
|
2224
|
+
reg.lastIndex = 0;
|
|
2225
|
+
return reg.test(str);
|
|
2226
|
+
}
|
|
2227
|
+
function isPropsMatch(props, attr) {
|
|
2228
|
+
if (Array.isArray(props)) {
|
|
2229
|
+
let lowerAttr;
|
|
2230
|
+
for (const prop of props) if (typeof prop === "string") {
|
|
2231
|
+
lowerAttr ?? (lowerAttr = attr.toLowerCase());
|
|
2232
|
+
if (prop.toLowerCase() === lowerAttr) return true;
|
|
2233
|
+
} else if (regTest(prop, attr)) return true;
|
|
2234
|
+
return false;
|
|
2235
|
+
} else if (typeof props === "string") return props === attr;
|
|
2236
|
+
else return regTest(props, attr);
|
|
2237
|
+
}
|
|
2238
|
+
function createAttributeMatcher(entities) {
|
|
2239
|
+
if (!entities || entities.length === 0) return;
|
|
2240
|
+
const wildcardAttributeRules = [];
|
|
2241
|
+
const tagAttributeRuleMap = /* @__PURE__ */ new Map();
|
|
2242
|
+
const regexpAttributeRules = [];
|
|
2243
|
+
for (const [selector, props] of entities) if (selector === "*") wildcardAttributeRules.push(props);
|
|
2244
|
+
else if (typeof selector === "string") {
|
|
2245
|
+
const list = tagAttributeRuleMap.get(selector);
|
|
2246
|
+
if (list) list.push(props);
|
|
2247
|
+
else tagAttributeRuleMap.set(selector, [props]);
|
|
2248
|
+
} else regexpAttributeRules.push([selector, props]);
|
|
2249
|
+
return (tag, attr) => {
|
|
2250
|
+
for (const props of wildcardAttributeRules) if (isPropsMatch(props, attr)) return true;
|
|
2251
|
+
const tagRules = tagAttributeRuleMap.get(tag);
|
|
2252
|
+
if (tagRules) {
|
|
2253
|
+
for (const props of tagRules) if (isPropsMatch(props, attr)) return true;
|
|
2254
|
+
}
|
|
2255
|
+
for (const [selector, props] of regexpAttributeRules) if (regTest(selector, tag) && isPropsMatch(props, attr)) return true;
|
|
2256
|
+
return false;
|
|
2257
|
+
};
|
|
2258
|
+
}
|
|
2259
|
+
//#endregion
|
|
2260
|
+
//#region src/wxml/utils/codegen/legacy-visitor.ts
|
|
2261
|
+
function shouldSkipLegacyStringLiteral(path) {
|
|
2262
|
+
if (_babel_types.isMemberExpression(path.parent)) return true;
|
|
2263
|
+
return Boolean(_babel_types.isBinaryExpression(path.parent) && (_babel_types.isConditionalExpression(path.parentPath?.parent) || _babel_types.isLogicalExpression(path.parentPath?.parent)));
|
|
2264
|
+
}
|
|
2265
|
+
function createLegacyTraverseOptions(options, jsTokenUpdater) {
|
|
2266
|
+
const legacyReplaceOptions = {
|
|
2267
|
+
escapeMap: options.escapeMap,
|
|
2268
|
+
classNameSet: options.runtimeSet,
|
|
2269
|
+
needEscaped: true,
|
|
2270
|
+
alwaysEscape: true
|
|
2271
|
+
};
|
|
2272
|
+
return {
|
|
2273
|
+
StringLiteral(path) {
|
|
2274
|
+
if (shouldSkipLegacyStringLiteral(path)) return;
|
|
2275
|
+
jsTokenUpdater.addToken(replaceHandleValue(path, legacyReplaceOptions));
|
|
2276
|
+
},
|
|
2277
|
+
noScope: true
|
|
2278
|
+
};
|
|
2279
|
+
}
|
|
2280
|
+
//#endregion
|
|
2281
|
+
//#region src/wxml/utils/codegen/legacy-rewriter.ts
|
|
2282
|
+
function rewriteLegacyExpression(match, options) {
|
|
2283
|
+
const ast = (0, _babel_parser.parseExpression)(match);
|
|
2284
|
+
const jsTokenUpdater = new JsTokenUpdater();
|
|
2285
|
+
traverse(ast, createLegacyTraverseOptions(options, jsTokenUpdater));
|
|
2286
|
+
if (jsTokenUpdater.length === 0) return match;
|
|
2287
|
+
const ms = new magic_string.default(match);
|
|
2288
|
+
jsTokenUpdater.updateMagicString(ms);
|
|
2289
|
+
return ms.toString();
|
|
2290
|
+
}
|
|
2291
|
+
//#endregion
|
|
2292
|
+
//#region src/wxml/utils/codegen.ts
|
|
2293
|
+
const WRAP_EXPRESSION_HANDLER_OPTIONS = Object.freeze({ wrapExpression: true });
|
|
2294
|
+
function generateCode(match, options = {}) {
|
|
2295
|
+
try {
|
|
2296
|
+
const { jsHandler, runtimeSet, wrapExpression } = options;
|
|
2297
|
+
if (jsHandler && runtimeSet) {
|
|
2298
|
+
const initial = jsHandler(match, runtimeSet, wrapExpression ? WRAP_EXPRESSION_HANDLER_OPTIONS : void 0);
|
|
2299
|
+
if (!initial.error || wrapExpression) return initial.code;
|
|
2300
|
+
return jsHandler(match, runtimeSet, WRAP_EXPRESSION_HANDLER_OPTIONS).code;
|
|
2301
|
+
} else
|
|
2302
|
+
/**
|
|
2303
|
+
* @deprecated
|
|
2304
|
+
*/
|
|
2305
|
+
return rewriteLegacyExpression(match, options);
|
|
2306
|
+
} catch {
|
|
2307
|
+
return match;
|
|
2308
|
+
}
|
|
2309
|
+
}
|
|
2310
|
+
//#endregion
|
|
2311
|
+
//#region src/wxml/whitespace.ts
|
|
2312
|
+
const WHITESPACE_CODES = new Set([
|
|
2313
|
+
9,
|
|
2314
|
+
10,
|
|
2315
|
+
11,
|
|
2316
|
+
12,
|
|
2317
|
+
13,
|
|
2318
|
+
32,
|
|
2319
|
+
160,
|
|
2320
|
+
65279
|
|
2321
|
+
]);
|
|
2322
|
+
function isWhitespace(char) {
|
|
2323
|
+
if (char.length === 0) return false;
|
|
2324
|
+
return WHITESPACE_CODES.has(char.charCodeAt(0));
|
|
2325
|
+
}
|
|
2326
|
+
function isAllWhitespace(value) {
|
|
2327
|
+
for (let i = 0; i < value.length; i++) if (!WHITESPACE_CODES.has(value.charCodeAt(i))) return false;
|
|
2328
|
+
return true;
|
|
2329
|
+
}
|
|
2330
|
+
//#endregion
|
|
2331
|
+
//#region src/wxml/Tokenizer.ts
|
|
2332
|
+
var Tokenizer = class {
|
|
2333
|
+
constructor() {
|
|
2334
|
+
this.reset();
|
|
2335
|
+
}
|
|
2336
|
+
processChar(char, index) {
|
|
2337
|
+
switch (this.state) {
|
|
2338
|
+
case 0:
|
|
2339
|
+
if (isWhitespace(char)) {} else if (char === "{") {
|
|
2340
|
+
this.state = 2;
|
|
2341
|
+
this.bufferStartIndex = index;
|
|
2342
|
+
this.buffer += char;
|
|
2343
|
+
this.expressionBuffer = char;
|
|
2344
|
+
this.expressionStartIndex = index;
|
|
2345
|
+
} else {
|
|
2346
|
+
this.state = 1;
|
|
2347
|
+
this.bufferStartIndex = index;
|
|
2348
|
+
this.buffer += char;
|
|
2349
|
+
}
|
|
2350
|
+
break;
|
|
2351
|
+
case 1:
|
|
2352
|
+
if (isWhitespace(char)) {
|
|
2353
|
+
this.tokens.push({
|
|
2354
|
+
start: this.bufferStartIndex,
|
|
2355
|
+
end: index,
|
|
2356
|
+
value: this.buffer,
|
|
2357
|
+
expressions: this.expressions
|
|
2358
|
+
});
|
|
2359
|
+
this.buffer = "";
|
|
2360
|
+
this.expressions = [];
|
|
2361
|
+
this.state = 0;
|
|
2362
|
+
} else if (char === "{") {
|
|
2363
|
+
this.buffer += char;
|
|
2364
|
+
this.expressionBuffer = char;
|
|
2365
|
+
this.expressionStartIndex = index;
|
|
2366
|
+
this.state = 2;
|
|
2367
|
+
} else this.buffer += char;
|
|
2368
|
+
break;
|
|
2369
|
+
case 2:
|
|
2370
|
+
if (char === "}") {
|
|
2371
|
+
this.buffer += char;
|
|
2372
|
+
this.expressionBuffer += char;
|
|
2373
|
+
this.state = 3;
|
|
2374
|
+
} else {
|
|
2375
|
+
this.buffer += char;
|
|
2376
|
+
this.expressionBuffer += char;
|
|
2377
|
+
}
|
|
2378
|
+
break;
|
|
2379
|
+
case 3:
|
|
2380
|
+
if (char === "}") {
|
|
2381
|
+
this.buffer += char;
|
|
2382
|
+
this.expressionBuffer += char;
|
|
2383
|
+
this.expressions.push({
|
|
2384
|
+
start: this.expressionStartIndex,
|
|
2385
|
+
end: index + 1,
|
|
2386
|
+
value: this.expressionBuffer
|
|
2387
|
+
});
|
|
2388
|
+
this.expressionBuffer = "";
|
|
2389
|
+
this.state = 4;
|
|
2390
|
+
} else {
|
|
2391
|
+
this.buffer += char;
|
|
2392
|
+
this.expressionBuffer += char;
|
|
2393
|
+
this.state = 2;
|
|
2394
|
+
}
|
|
2395
|
+
break;
|
|
2396
|
+
case 4:
|
|
2397
|
+
if (isWhitespace(char)) {
|
|
2398
|
+
this.tokens.push({
|
|
2399
|
+
start: this.bufferStartIndex,
|
|
2400
|
+
end: index,
|
|
2401
|
+
value: this.buffer,
|
|
2402
|
+
expressions: this.expressions
|
|
2403
|
+
});
|
|
2404
|
+
this.buffer = "";
|
|
2405
|
+
this.expressions = [];
|
|
2406
|
+
this.state = 0;
|
|
2407
|
+
} else if (char === "{") {
|
|
2408
|
+
this.expressionStartIndex = index;
|
|
2409
|
+
this.expressionBuffer = char;
|
|
2410
|
+
this.buffer += char;
|
|
2411
|
+
this.state = 2;
|
|
2412
|
+
} else {
|
|
2413
|
+
this.buffer += char;
|
|
2414
|
+
this.state = 1;
|
|
2415
|
+
}
|
|
2416
|
+
break;
|
|
2417
|
+
default: throw new Error("Unexpected state");
|
|
2418
|
+
}
|
|
2419
|
+
}
|
|
2420
|
+
run(input) {
|
|
2421
|
+
this.reset();
|
|
2422
|
+
for (let i = 0; i < input.length; i++) {
|
|
2423
|
+
const char = input[i];
|
|
2424
|
+
this.processChar(char, i);
|
|
2425
|
+
}
|
|
2426
|
+
if (this.buffer.length > 0) this.tokens.push({
|
|
2427
|
+
start: this.bufferStartIndex,
|
|
2428
|
+
end: input.length,
|
|
2429
|
+
value: this.buffer,
|
|
2430
|
+
expressions: this.expressions
|
|
2431
|
+
});
|
|
2432
|
+
const tokens = this.tokens;
|
|
2433
|
+
this.reset();
|
|
2434
|
+
return tokens;
|
|
2435
|
+
}
|
|
2436
|
+
reset() {
|
|
2437
|
+
this.state = 0;
|
|
2438
|
+
this.buffer = "";
|
|
2439
|
+
this.tokens = [];
|
|
2440
|
+
this.bufferStartIndex = 0;
|
|
2441
|
+
this.expressionBuffer = "";
|
|
2442
|
+
this.expressionStartIndex = 0;
|
|
2443
|
+
this.expressions = [];
|
|
2444
|
+
}
|
|
2445
|
+
};
|
|
2446
|
+
//#endregion
|
|
2447
|
+
//#region src/wxml/utils/fragment-helpers.ts
|
|
2448
|
+
function updateWhitespaceGap(ms, start, end, options) {
|
|
2449
|
+
const gap = ms.slice(start, end);
|
|
2450
|
+
if (isAllWhitespace(gap)) ms.update(start, end, replaceWxml(gap, {
|
|
2451
|
+
keepEOL: false,
|
|
2452
|
+
escapeMap: options.escapeMap,
|
|
2453
|
+
ignoreHead: true
|
|
2454
|
+
}));
|
|
2455
|
+
}
|
|
2456
|
+
function updateWxmlSegment(ms, start, end, options, keepEOL, ignoreHead) {
|
|
2457
|
+
ms.update(start, end, replaceWxml(ms.slice(start, end), {
|
|
2458
|
+
keepEOL,
|
|
2459
|
+
escapeMap: options.escapeMap,
|
|
2460
|
+
ignoreHead
|
|
2461
|
+
}));
|
|
2462
|
+
}
|
|
2463
|
+
function updateExpressionSegment(ms, exp, options) {
|
|
2464
|
+
const code = `{{${generateCode(exp.value.slice(2, -2), options)}}}`;
|
|
2465
|
+
ms.update(exp.start, exp.end, code);
|
|
2466
|
+
}
|
|
2467
|
+
//#endregion
|
|
2468
|
+
//#region src/wxml/utils/fragment-updater.ts
|
|
2469
|
+
function handleEachClassFragment(ms, tokens, options = {}) {
|
|
2470
|
+
let previousEnd = 0;
|
|
2471
|
+
for (const token of tokens) {
|
|
2472
|
+
if (token.start > previousEnd) updateWhitespaceGap(ms, previousEnd, token.start, options);
|
|
2473
|
+
let p = token.start;
|
|
2474
|
+
if (token.expressions.length > 0) {
|
|
2475
|
+
for (const exp of token.expressions) {
|
|
2476
|
+
if (exp.start > token.start && p < exp.start) updateWxmlSegment(ms, p, exp.start, options, true, p > 0);
|
|
2477
|
+
updateExpressionSegment(ms, exp, options);
|
|
2478
|
+
p = exp.end;
|
|
2479
|
+
}
|
|
2480
|
+
if (token.end > p) updateWxmlSegment(ms, p, token.end, options, false, true);
|
|
2481
|
+
} else updateWxmlSegment(ms, token.start, token.end, options, false, false);
|
|
2482
|
+
previousEnd = token.end;
|
|
2483
|
+
}
|
|
2484
|
+
if (tokens.length > 0) {
|
|
2485
|
+
const lastToken = tokens[tokens.length - 1];
|
|
2486
|
+
if (lastToken.end < ms.original.length) updateWhitespaceGap(ms, lastToken.end, ms.original.length, options);
|
|
2487
|
+
}
|
|
2488
|
+
}
|
|
2489
|
+
//#endregion
|
|
2490
|
+
//#region src/wxml/utils/template-fragments.ts
|
|
2491
|
+
/**
|
|
2492
|
+
* 模块级共享 Tokenizer 实例,避免每次调用都重新创建。
|
|
2493
|
+
* Tokenizer.run() 末尾已调用 reset(),天然支持复用。
|
|
2494
|
+
*/
|
|
2495
|
+
const sharedTokenizer = new Tokenizer();
|
|
2496
|
+
function templateReplacer(original, options = {}, tokenizer) {
|
|
2497
|
+
const ms = new magic_string.default(original);
|
|
2498
|
+
handleEachClassFragment(ms, (tokenizer ?? sharedTokenizer).run(ms.original), options);
|
|
2499
|
+
return ms.toString();
|
|
2500
|
+
}
|
|
2501
|
+
//#endregion
|
|
2502
|
+
//#region src/wxml/utils/custom-template.ts
|
|
2503
|
+
async function customTemplateHandler(rawSource, options, cachedMatcher) {
|
|
2504
|
+
const { customAttributesEntities = [], disabledDefaultTemplateHandler, inlineWxs, runtimeSet, jsHandler } = options ?? {};
|
|
2505
|
+
const matchCustomAttribute = cachedMatcher ?? createAttributeMatcher(customAttributesEntities);
|
|
2506
|
+
const defaultTemplateHandlerEnabled = !disabledDefaultTemplateHandler;
|
|
2507
|
+
let replaceOptions;
|
|
2508
|
+
let cachedQuote;
|
|
2509
|
+
let s;
|
|
2510
|
+
let tag = "";
|
|
2511
|
+
let wxsArray;
|
|
2512
|
+
function getMagicString() {
|
|
2513
|
+
if (!s) s = new magic_string.default(rawSource);
|
|
2514
|
+
return s;
|
|
2515
|
+
}
|
|
2516
|
+
function getReplaceOptions(quote) {
|
|
2517
|
+
if (!replaceOptions) {
|
|
2518
|
+
replaceOptions = {
|
|
2519
|
+
...options,
|
|
2520
|
+
quote
|
|
2521
|
+
};
|
|
2522
|
+
cachedQuote = quote;
|
|
2523
|
+
return replaceOptions;
|
|
2524
|
+
}
|
|
2525
|
+
if (cachedQuote !== quote) {
|
|
2526
|
+
replaceOptions.quote = quote;
|
|
2527
|
+
cachedQuote = quote;
|
|
2528
|
+
}
|
|
2529
|
+
return replaceOptions;
|
|
2530
|
+
}
|
|
2531
|
+
function isDefaultTemplateAttribute(name) {
|
|
2532
|
+
if (name === "class" || name === "hover-class" || name === "virtualhostclass") return true;
|
|
2533
|
+
const lowerName = name.toLowerCase();
|
|
2534
|
+
return lowerName === "class" || lowerName === "hover-class" || lowerName === "virtualhostclass";
|
|
2535
|
+
}
|
|
2536
|
+
const parser = new htmlparser2.Parser({
|
|
2537
|
+
onopentagname(name) {
|
|
2538
|
+
tag = name;
|
|
2539
|
+
},
|
|
2540
|
+
onattribute(name, value, quote) {
|
|
2541
|
+
if (!value) return;
|
|
2542
|
+
const shouldHandleDefault = defaultTemplateHandlerEnabled && isDefaultTemplateAttribute(name);
|
|
2543
|
+
const shouldHandleCustom = matchCustomAttribute?.(tag, name) ?? false;
|
|
2544
|
+
if (!shouldHandleDefault && !shouldHandleCustom) return;
|
|
2545
|
+
getMagicString().update(parser.startIndex + name.length + 2, parser.endIndex - 1, templateReplacer(value, getReplaceOptions(quote)));
|
|
2546
|
+
},
|
|
2547
|
+
ontext(data) {
|
|
2548
|
+
if (inlineWxs && tag === "wxs") (wxsArray ?? (wxsArray = [])).push({
|
|
2549
|
+
data,
|
|
2550
|
+
endIndex: parser.endIndex + 1,
|
|
2551
|
+
startIndex: parser.startIndex
|
|
2552
|
+
});
|
|
2553
|
+
},
|
|
2554
|
+
onclosetag() {
|
|
2555
|
+
tag = "";
|
|
2556
|
+
}
|
|
2557
|
+
}, { xmlMode: true });
|
|
2558
|
+
parser.write(rawSource);
|
|
2559
|
+
parser.end();
|
|
2560
|
+
for (const { data, endIndex, startIndex } of wxsArray ?? []) {
|
|
2561
|
+
const { code } = await jsHandler(data, runtimeSet);
|
|
2562
|
+
if (code !== data) getMagicString().update(startIndex, endIndex, code);
|
|
2563
|
+
}
|
|
2564
|
+
return s?.toString() ?? rawSource;
|
|
2565
|
+
}
|
|
2566
|
+
//#endregion
|
|
2567
|
+
//#region src/wxml/utils.ts
|
|
2568
|
+
function createTemplateHandler(options = {}) {
|
|
2569
|
+
const cachedMatcher = createAttributeMatcher(options.customAttributesEntities);
|
|
2570
|
+
const defaultOptions = options;
|
|
2571
|
+
let cachedRuntimeSet;
|
|
2572
|
+
let cachedOptionsWithRuntimeSet;
|
|
2573
|
+
return (rawSource, opt) => {
|
|
2574
|
+
const runtimeSet = opt?.runtimeSet;
|
|
2575
|
+
if (runtimeSet === void 0) return customTemplateHandler(rawSource, defaultOptions, cachedMatcher);
|
|
2576
|
+
if (cachedRuntimeSet !== runtimeSet || !cachedOptionsWithRuntimeSet) {
|
|
2577
|
+
cachedRuntimeSet = runtimeSet;
|
|
2578
|
+
cachedOptionsWithRuntimeSet = {
|
|
2579
|
+
...defaultOptions,
|
|
2580
|
+
runtimeSet
|
|
2581
|
+
};
|
|
2582
|
+
}
|
|
2583
|
+
return customTemplateHandler(rawSource, cachedOptionsWithRuntimeSet, cachedMatcher);
|
|
2584
|
+
};
|
|
2585
|
+
}
|
|
2586
|
+
//#endregion
|
|
2587
|
+
//#region src/context/handlers.ts
|
|
2588
|
+
function resolveRuntimePackageReplacements(option) {
|
|
2589
|
+
if (!option) return;
|
|
2590
|
+
const mapping = option === true ? DEFAULT_RUNTIME_PACKAGE_REPLACEMENTS : option;
|
|
2591
|
+
const normalized = {};
|
|
2592
|
+
for (const [from, to] of Object.entries(mapping)) {
|
|
2593
|
+
if (!from || typeof to !== "string" || to.length === 0) continue;
|
|
2594
|
+
normalized[from] = to;
|
|
2595
|
+
}
|
|
2596
|
+
return Object.keys(normalized).length > 0 ? normalized : void 0;
|
|
2597
|
+
}
|
|
2598
|
+
function createHandlersFromContext(ctx, customAttributesEntities, cssCalcOptions, tailwindcssMajorVersion) {
|
|
2599
|
+
const { cssPreflight, cssPreflightRange, escapeMap, cssChildCombinatorReplaceValue, injectAdditionalCssVarScope, cssSelectorReplacement, rem2rpx, postcssOptions, cssRemoveProperty, cssRemoveHoverPseudoClass, cssPresetEnv, autoprefixer, uniAppX, px2rpx, unitsToPx, arbitraryValues, jsPreserveClass, staleClassNameFallback, jsArbitraryValueFallback, babelParserOptions, ignoreCallExpressionIdentifiers, ignoreTaggedTemplateExpressionIdentifiers, inlineWxs, disabledDefaultTemplateHandler, replaceRuntimePackages } = ctx;
|
|
2600
|
+
const uniAppXEnabled = require_logger.isUniAppXEnabled(uniAppX);
|
|
2601
|
+
const resolvedUniAppXOptions = require_logger.resolveUniAppXOptions(uniAppX);
|
|
2602
|
+
const moduleSpecifierReplacements = resolveRuntimePackageReplacements(replaceRuntimePackages);
|
|
2603
|
+
const styleHandler = (0, _weapp_tailwindcss_postcss.createStyleHandler)({
|
|
2604
|
+
cssPreflight,
|
|
2605
|
+
cssPreflightRange,
|
|
2606
|
+
escapeMap,
|
|
2607
|
+
cssChildCombinatorReplaceValue,
|
|
2608
|
+
injectAdditionalCssVarScope,
|
|
2609
|
+
cssSelectorReplacement,
|
|
2610
|
+
rem2rpx,
|
|
2611
|
+
postcssOptions,
|
|
2612
|
+
cssRemoveProperty,
|
|
2613
|
+
cssRemoveHoverPseudoClass,
|
|
2614
|
+
cssPresetEnv,
|
|
2615
|
+
autoprefixer,
|
|
2616
|
+
uniAppX: uniAppXEnabled,
|
|
2617
|
+
uniAppXUnsupported: resolvedUniAppXOptions.uvueUnsupported,
|
|
2618
|
+
cssCalc: cssCalcOptions,
|
|
2619
|
+
px2rpx,
|
|
2620
|
+
unitsToPx,
|
|
2621
|
+
majorVersion: tailwindcssMajorVersion
|
|
2622
|
+
});
|
|
2623
|
+
const jsHandler = createJsHandler({
|
|
2624
|
+
escapeMap,
|
|
2625
|
+
arbitraryValues,
|
|
2626
|
+
jsPreserveClass,
|
|
2627
|
+
staleClassNameFallback,
|
|
2628
|
+
jsArbitraryValueFallback: jsArbitraryValueFallback ?? "auto",
|
|
2629
|
+
tailwindcssMajorVersion,
|
|
2630
|
+
generateMap: true,
|
|
2631
|
+
babelParserOptions,
|
|
2632
|
+
ignoreCallExpressionIdentifiers,
|
|
2633
|
+
ignoreTaggedTemplateExpressionIdentifiers,
|
|
2634
|
+
uniAppX: uniAppXEnabled,
|
|
2635
|
+
moduleSpecifierReplacements
|
|
2636
|
+
});
|
|
2637
|
+
return {
|
|
2638
|
+
styleHandler,
|
|
2639
|
+
jsHandler,
|
|
2640
|
+
templateHandler: createTemplateHandler({
|
|
2641
|
+
customAttributesEntities,
|
|
2642
|
+
escapeMap,
|
|
2643
|
+
inlineWxs,
|
|
2644
|
+
jsHandler,
|
|
2645
|
+
disabledDefaultTemplateHandler
|
|
2646
|
+
})
|
|
2647
|
+
};
|
|
2648
|
+
}
|
|
2649
|
+
//#endregion
|
|
2650
|
+
//#region src/context/logger.ts
|
|
2651
|
+
const loggerLevelMap = {
|
|
2652
|
+
error: 0,
|
|
2653
|
+
warn: 1,
|
|
2654
|
+
info: 3,
|
|
2655
|
+
silent: -999
|
|
2656
|
+
};
|
|
2657
|
+
function applyLoggerLevel(logLevel) {
|
|
2658
|
+
_weapp_tailwindcss_logger.logger.level = loggerLevelMap[logLevel ?? "info"] ?? loggerLevelMap.info;
|
|
2659
|
+
}
|
|
2660
|
+
//#endregion
|
|
2661
|
+
//#region src/context/index.ts
|
|
2662
|
+
async function clearTailwindcssPatcherCache(patcher, options) {
|
|
2663
|
+
if (!patcher) return;
|
|
2664
|
+
const cacheOptions = patcher.options?.cache;
|
|
2665
|
+
if (cacheOptions == null || typeof cacheOptions === "object" && cacheOptions.enabled === false) return;
|
|
2666
|
+
if (typeof patcher.clearCache === "function") try {
|
|
2667
|
+
await patcher.clearCache({ scope: "all" });
|
|
2668
|
+
} catch (error) {
|
|
2669
|
+
_weapp_tailwindcss_logger.logger.debug("failed to clear tailwindcss patcher cache via clearCache(): %O", error);
|
|
2670
|
+
}
|
|
2671
|
+
if (!options?.removeDirectory) return;
|
|
2672
|
+
const cachePaths = /* @__PURE__ */ new Map();
|
|
2673
|
+
const normalizedCacheOptions = typeof cacheOptions === "object" ? cacheOptions : void 0;
|
|
2674
|
+
if (normalizedCacheOptions?.path) cachePaths.set(normalizedCacheOptions.path, false);
|
|
2675
|
+
const privateCachePath = patcher?.cacheStore?.options?.path;
|
|
2676
|
+
if (privateCachePath) cachePaths.set(privateCachePath, false);
|
|
2677
|
+
if (options?.removeDirectory && normalizedCacheOptions?.dir) cachePaths.set(normalizedCacheOptions.dir, true);
|
|
2678
|
+
if (!cachePaths.size) return;
|
|
2679
|
+
for (const [cachePath, recursive] of cachePaths.entries()) try {
|
|
2680
|
+
await (0, node_fs_promises.rm)(cachePath, {
|
|
2681
|
+
force: true,
|
|
2682
|
+
recursive
|
|
2683
|
+
});
|
|
2684
|
+
} catch (error) {
|
|
2685
|
+
const err = error;
|
|
2686
|
+
if (err?.code === "ENOENT") continue;
|
|
2687
|
+
_weapp_tailwindcss_logger.logger.debug("failed to clear tailwindcss patcher cache: %s %O", cachePath, err);
|
|
2688
|
+
}
|
|
2689
|
+
}
|
|
2690
|
+
function createInternalCompilerContext(opts) {
|
|
2691
|
+
const ctx = (0, _weapp_tailwindcss_shared.defuOverrideArray)(opts, require_defaults.getDefaultOptions(), {});
|
|
2692
|
+
ctx.escapeMap = ctx.customReplaceDictionary;
|
|
2693
|
+
applyLoggerLevel(ctx.logLevel);
|
|
2694
|
+
const twPatcher = require_logger.createTailwindcssPatcherFromContext(ctx);
|
|
2695
|
+
logTailwindcssTarget("runtime", twPatcher, ctx.tailwindcssBasedir);
|
|
2696
|
+
logRuntimeTailwindcssVersion(ctx.tailwindcssBasedir, twPatcher.packageInfo?.rootPath, twPatcher.packageInfo?.version);
|
|
2697
|
+
require_logger.warnMissingCssEntries(ctx, twPatcher);
|
|
2698
|
+
const cssCalcOptions = require_logger.applyV4CssCalcDefaults(ctx.cssCalc, twPatcher);
|
|
2699
|
+
ctx.cssCalc = cssCalcOptions;
|
|
2700
|
+
const { styleHandler, jsHandler, templateHandler } = createHandlersFromContext(ctx, toCustomAttributesEntities(ctx.customAttributes), cssCalcOptions, twPatcher.majorVersion);
|
|
2701
|
+
ctx.styleHandler = styleHandler;
|
|
2702
|
+
ctx.jsHandler = jsHandler;
|
|
2703
|
+
ctx.templateHandler = templateHandler;
|
|
2704
|
+
ctx.cache = initializeCache(ctx.cache);
|
|
2705
|
+
ctx.twPatcher = twPatcher;
|
|
2706
|
+
const refreshTailwindcssPatcher = async (options) => {
|
|
2707
|
+
const previousPatcher = ctx.twPatcher;
|
|
2708
|
+
if (options?.clearCache !== false) await clearTailwindcssPatcherCache(previousPatcher);
|
|
2709
|
+
require_logger.invalidateRuntimeClassSet(previousPatcher);
|
|
2710
|
+
const nextPatcher = require_logger.createTailwindcssPatcherFromContext(ctx);
|
|
2711
|
+
Object.assign(previousPatcher, nextPatcher);
|
|
2712
|
+
ctx.twPatcher = previousPatcher;
|
|
2713
|
+
return previousPatcher;
|
|
2714
|
+
};
|
|
2715
|
+
ctx.refreshTailwindcssPatcher = refreshTailwindcssPatcher;
|
|
2716
|
+
Object.defineProperty(ctx.twPatcher, refreshTailwindcssPatcherSymbol, {
|
|
2717
|
+
value: refreshTailwindcssPatcher,
|
|
2718
|
+
configurable: true
|
|
2719
|
+
});
|
|
2720
|
+
return ctx;
|
|
2721
|
+
}
|
|
2722
|
+
/**
|
|
2723
|
+
* 获取用户定义选项的内部表示,并初始化相关的处理程序和补丁。
|
|
2724
|
+
* @param opts - 用户定义的选项,可选。
|
|
2725
|
+
* @returns 返回一个包含内部用户定义选项的对象,包括样式、JS和模板处理程序,以及Tailwind CSS补丁。
|
|
2726
|
+
*/
|
|
2727
|
+
function getCompilerContext(opts) {
|
|
2728
|
+
return withCompilerContextCache(opts, () => createInternalCompilerContext(opts));
|
|
2729
|
+
}
|
|
2730
|
+
//#endregion
|
|
2731
|
+
//#region src/js/precheck.ts
|
|
2732
|
+
/** 用于检测源码中是否包含类名相关模式的正则表达式 */
|
|
2733
|
+
const FAST_JS_TRANSFORM_HINT_RE = /className\b|class\s*=|classList\.|\b(?:twMerge|clsx|classnames|cn|cva)\b|\[["'`]class["'`]\]|text-\[|bg-\[|\b(?:[whpm]|px|py|mx|my|rounded|flex|grid|gap)-/;
|
|
2734
|
+
/** 用于检测源码中是否包含 import/export/require 语句的正则表达式 */
|
|
2735
|
+
const DEPENDENCY_HINT_RE = /\bimport\s*[("'`{*]|\brequire\s*\(|\bexport\s+\*\s+from\s+["'`]|\bexport\s*\{[^}]*\}\s*from\s+["'`]/;
|
|
2736
|
+
/**
|
|
2737
|
+
* 判断是否可以跳过 JS 转换。
|
|
2738
|
+
* 通过正则快速检测源码内容,避免不必要的 Babel AST 解析。
|
|
2739
|
+
*
|
|
2740
|
+
* @param rawSource - 原始 JS 源码字符串
|
|
2741
|
+
* @param options - 可选的 JS 处理器配置选项
|
|
2742
|
+
* @returns 如果可以跳过转换返回 `true`,否则返回 `false`
|
|
2743
|
+
*/
|
|
2744
|
+
function shouldSkipJsTransform(rawSource, options) {
|
|
2745
|
+
if (node_process.default.env.WEAPP_TW_DISABLE_JS_PRECHECK === "1") return false;
|
|
2746
|
+
if (!rawSource) return true;
|
|
2747
|
+
if (options?.alwaysEscape) return false;
|
|
2748
|
+
if (options?.moduleSpecifierReplacements && Object.keys(options.moduleSpecifierReplacements).length > 0) return false;
|
|
2749
|
+
if (options?.wrapExpression) return false;
|
|
2750
|
+
if (DEPENDENCY_HINT_RE.test(rawSource)) return false;
|
|
2751
|
+
return !FAST_JS_TRANSFORM_HINT_RE.test(rawSource);
|
|
2752
|
+
}
|
|
2753
|
+
//#endregion
|
|
2754
|
+
//#region src/tailwindcss/recorder.ts
|
|
2755
|
+
function setupPatchRecorder(patcher, baseDir, options) {
|
|
2756
|
+
const recorder = createPatchTargetRecorder(baseDir, patcher, options);
|
|
2757
|
+
if (recorder?.message && options?.logMessage !== false) {
|
|
2758
|
+
const prefix = options?.messagePrefix ? `${options.messagePrefix} ` : "";
|
|
2759
|
+
_weapp_tailwindcss_logger.logger.info("%s%s", prefix, recorder.message);
|
|
2760
|
+
}
|
|
2761
|
+
const onPatchCompleted = recorder?.onPatched ? async () => {
|
|
2762
|
+
await recorder.onPatched();
|
|
2763
|
+
} : void 0;
|
|
2764
|
+
return {
|
|
2765
|
+
recorder,
|
|
2766
|
+
patchPromise: patcher ? createTailwindPatchPromise(patcher, onPatchCompleted) : Promise.resolve(),
|
|
2767
|
+
onPatchCompleted
|
|
2768
|
+
};
|
|
2769
|
+
}
|
|
2770
|
+
//#endregion
|
|
2771
|
+
Object.defineProperty(exports, "JsTokenUpdater", {
|
|
2772
|
+
enumerable: true,
|
|
2773
|
+
get: function() {
|
|
2774
|
+
return JsTokenUpdater;
|
|
2775
|
+
}
|
|
2776
|
+
});
|
|
2777
|
+
Object.defineProperty(exports, "analyzeSource", {
|
|
2778
|
+
enumerable: true,
|
|
2779
|
+
get: function() {
|
|
2780
|
+
return analyzeSource;
|
|
2781
|
+
}
|
|
2782
|
+
});
|
|
2783
|
+
Object.defineProperty(exports, "babelParse", {
|
|
2784
|
+
enumerable: true,
|
|
2785
|
+
get: function() {
|
|
2786
|
+
return babelParse;
|
|
2787
|
+
}
|
|
2788
|
+
});
|
|
2789
|
+
Object.defineProperty(exports, "collectRuntimeClassSet", {
|
|
2790
|
+
enumerable: true,
|
|
2791
|
+
get: function() {
|
|
2792
|
+
return collectRuntimeClassSet;
|
|
2793
|
+
}
|
|
2794
|
+
});
|
|
2795
|
+
Object.defineProperty(exports, "createAttributeMatcher", {
|
|
2796
|
+
enumerable: true,
|
|
2797
|
+
get: function() {
|
|
2798
|
+
return createAttributeMatcher;
|
|
2799
|
+
}
|
|
2800
|
+
});
|
|
2801
|
+
Object.defineProperty(exports, "createDebug", {
|
|
2802
|
+
enumerable: true,
|
|
2803
|
+
get: function() {
|
|
2804
|
+
return createDebug;
|
|
2805
|
+
}
|
|
2806
|
+
});
|
|
2807
|
+
Object.defineProperty(exports, "ensureRuntimeClassSet", {
|
|
2808
|
+
enumerable: true,
|
|
2809
|
+
get: function() {
|
|
2810
|
+
return ensureRuntimeClassSet;
|
|
2811
|
+
}
|
|
2812
|
+
});
|
|
2813
|
+
Object.defineProperty(exports, "generateCode", {
|
|
2814
|
+
enumerable: true,
|
|
2815
|
+
get: function() {
|
|
2816
|
+
return generateCode;
|
|
2817
|
+
}
|
|
2818
|
+
});
|
|
2819
|
+
Object.defineProperty(exports, "getCompilerContext", {
|
|
2820
|
+
enumerable: true,
|
|
2821
|
+
get: function() {
|
|
2822
|
+
return getCompilerContext;
|
|
2823
|
+
}
|
|
2824
|
+
});
|
|
2825
|
+
Object.defineProperty(exports, "isClassContextLiteralPath", {
|
|
2826
|
+
enumerable: true,
|
|
2827
|
+
get: function() {
|
|
2828
|
+
return isClassContextLiteralPath;
|
|
2829
|
+
}
|
|
2830
|
+
});
|
|
2831
|
+
Object.defineProperty(exports, "pluginName", {
|
|
2832
|
+
enumerable: true,
|
|
2833
|
+
get: function() {
|
|
2834
|
+
return pluginName;
|
|
2835
|
+
}
|
|
2836
|
+
});
|
|
2837
|
+
Object.defineProperty(exports, "refreshTailwindRuntimeState", {
|
|
2838
|
+
enumerable: true,
|
|
2839
|
+
get: function() {
|
|
2840
|
+
return refreshTailwindRuntimeState;
|
|
2841
|
+
}
|
|
2842
|
+
});
|
|
2843
|
+
Object.defineProperty(exports, "replaceWxml", {
|
|
2844
|
+
enumerable: true,
|
|
2845
|
+
get: function() {
|
|
2846
|
+
return replaceWxml;
|
|
2847
|
+
}
|
|
2848
|
+
});
|
|
2849
|
+
Object.defineProperty(exports, "setupPatchRecorder", {
|
|
2850
|
+
enumerable: true,
|
|
2851
|
+
get: function() {
|
|
2852
|
+
return setupPatchRecorder;
|
|
2853
|
+
}
|
|
2854
|
+
});
|
|
2855
|
+
Object.defineProperty(exports, "shouldSkipJsTransform", {
|
|
2856
|
+
enumerable: true,
|
|
2857
|
+
get: function() {
|
|
2858
|
+
return shouldSkipJsTransform;
|
|
2859
|
+
}
|
|
2860
|
+
});
|
|
2861
|
+
Object.defineProperty(exports, "toCustomAttributesEntities", {
|
|
2862
|
+
enumerable: true,
|
|
2863
|
+
get: function() {
|
|
2864
|
+
return toCustomAttributesEntities;
|
|
2865
|
+
}
|
|
2866
|
+
});
|
|
2867
|
+
Object.defineProperty(exports, "traverse", {
|
|
2868
|
+
enumerable: true,
|
|
2869
|
+
get: function() {
|
|
2870
|
+
return traverse;
|
|
2871
|
+
}
|
|
2872
|
+
});
|
|
2873
|
+
Object.defineProperty(exports, "vitePluginName", {
|
|
2874
|
+
enumerable: true,
|
|
2875
|
+
get: function() {
|
|
2876
|
+
return vitePluginName;
|
|
2877
|
+
}
|
|
2878
|
+
});
|