@csszyx/unplugin 0.8.0 → 0.9.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/README.md +16 -7
- package/dist/css-mangler.cjs +4 -4
- package/dist/css-mangler.mjs +4 -4
- package/dist/index.cjs +23 -1
- package/dist/index.d.cts +100 -5
- package/dist/index.d.mts +99 -3
- package/dist/index.mjs +7 -1
- package/dist/next-prebuild.cjs +148 -0
- package/dist/next-prebuild.d.cts +66 -0
- package/dist/next-prebuild.d.mts +66 -0
- package/dist/next-prebuild.mjs +131 -0
- package/dist/next-turbo-loader.cjs +210 -0
- package/dist/next-turbo-loader.d.cts +68 -0
- package/dist/next-turbo-loader.d.mts +66 -0
- package/dist/next-turbo-loader.mjs +190 -0
- package/dist/next-watcher.cjs +237 -0
- package/dist/next-watcher.d.cts +106 -0
- package/dist/next-watcher.d.mts +106 -0
- package/dist/next-watcher.mjs +219 -0
- package/dist/shared/unplugin.8er8o6rn.mjs +276 -0
- package/dist/shared/unplugin.B_U4rZvG.cjs +281 -0
- package/dist/shared/unplugin.BbtspS8t.mjs +3271 -0
- package/dist/shared/unplugin.BceVw1eW.mjs +184 -0
- package/dist/shared/unplugin.BtQzlC2C.mjs +567 -0
- package/dist/shared/unplugin.CFp386gH.cjs +3321 -0
- package/dist/shared/unplugin.CPEWNSA0.d.cts +77 -0
- package/dist/shared/unplugin.CPEWNSA0.d.mts +77 -0
- package/dist/shared/unplugin.CScQRdTp.d.cts +15 -0
- package/dist/shared/unplugin.CScQRdTp.d.mts +15 -0
- package/dist/shared/unplugin.CdZxp0x-.d.mts +16 -0
- package/dist/shared/unplugin.DLrBgECN.d.cts +282 -0
- package/dist/shared/unplugin.DLrBgECN.d.mts +282 -0
- package/dist/shared/unplugin.DUxdYaSG.cjs +205 -0
- package/dist/shared/unplugin.s62yJbu1.cjs +591 -0
- package/dist/shared/unplugin.xeED_qwh.d.cts +16 -0
- package/dist/vite.cjs +7 -1
- package/dist/vite.d.cts +2 -1
- package/dist/vite.d.mts +2 -1
- package/dist/vite.mjs +7 -1
- package/dist/webpack.cjs +7 -1
- package/dist/webpack.d.cts +2 -1
- package/dist/webpack.d.mts +2 -1
- package/dist/webpack.mjs +7 -1
- package/package.json +41 -8
- package/dist/shared/unplugin.BNsv2szs.cjs +0 -1753
- package/dist/shared/unplugin.DCv0RtVZ.mjs +0 -1722
- package/dist/shared/unplugin.DUbr5w-N.d.cts +0 -49
- package/dist/shared/unplugin.DUbr5w-N.d.mts +0 -49
|
@@ -0,0 +1,282 @@
|
|
|
1
|
+
import { GlobalVarUsageDiagnostic, CssVariableMangleValue, TransformSourceCodeOptions } from '@csszyx/compiler';
|
|
2
|
+
import { PartialCsszyxConfig } from '@csszyx/types';
|
|
3
|
+
import { Plugin } from 'esbuild';
|
|
4
|
+
import { InputPluginOption } from 'rollup';
|
|
5
|
+
import { UnpluginInstance, WebpackPluginInstance } from 'unplugin';
|
|
6
|
+
import { PluginOption } from 'vite';
|
|
7
|
+
|
|
8
|
+
/** Source location for one CSS custom-property occurrence. */
|
|
9
|
+
interface CssVarLocation {
|
|
10
|
+
/** Source file path, when known. */
|
|
11
|
+
filePath: string;
|
|
12
|
+
/** 1-based source line. */
|
|
13
|
+
line: number;
|
|
14
|
+
/** 1-based source column. */
|
|
15
|
+
column: number;
|
|
16
|
+
}
|
|
17
|
+
/** One CSS custom-property definition. */
|
|
18
|
+
interface CssVarDefinition extends CssVarLocation {
|
|
19
|
+
/** Custom-property name including the leading `--`. */
|
|
20
|
+
name: string;
|
|
21
|
+
/** Stable declaration scope key built from ancestor at-rules and selectors. */
|
|
22
|
+
scopeId: string;
|
|
23
|
+
/** Whether the definition lives inside a Tailwind v4 @theme block. */
|
|
24
|
+
tailwindOwned: boolean;
|
|
25
|
+
/** Whether this name is registered by an @property at-rule. */
|
|
26
|
+
registered: boolean;
|
|
27
|
+
}
|
|
28
|
+
/** One var(--token) reference. */
|
|
29
|
+
interface CssVarReference extends CssVarLocation {
|
|
30
|
+
/** Custom-property name including the leading `--`. */
|
|
31
|
+
name: string;
|
|
32
|
+
/** Stable reference scope key built from ancestor at-rules and selectors. */
|
|
33
|
+
scopeId: string;
|
|
34
|
+
/** Declaration property or at-rule params where the reference appears. */
|
|
35
|
+
owner: string;
|
|
36
|
+
/** Whether the reference lives inside a Tailwind v4 @theme block. */
|
|
37
|
+
tailwindOwned: boolean;
|
|
38
|
+
}
|
|
39
|
+
/** CSS custom-property scan output for one CSS source. */
|
|
40
|
+
interface CssVarScanResult {
|
|
41
|
+
/** Source file path, when known. */
|
|
42
|
+
filePath: string;
|
|
43
|
+
/** Custom-property declarations. */
|
|
44
|
+
definitions: CssVarDefinition[];
|
|
45
|
+
/** var(--token) references. */
|
|
46
|
+
references: CssVarReference[];
|
|
47
|
+
/** @property registered custom-property names. */
|
|
48
|
+
registered: string[];
|
|
49
|
+
/** Whether the file path appears to be third-party CSS. */
|
|
50
|
+
thirdParty: boolean;
|
|
51
|
+
}
|
|
52
|
+
/** Cache entry for one CSS variable scan result. */
|
|
53
|
+
interface GlobalVarScanCacheEntry {
|
|
54
|
+
/** Cache key derived from file path, mtime, and content hash. */
|
|
55
|
+
key: string;
|
|
56
|
+
/** Cached scan result. */
|
|
57
|
+
result: CssVarScanResult;
|
|
58
|
+
}
|
|
59
|
+
/** Inputs used to derive a scan cache key. */
|
|
60
|
+
interface GlobalVarScanCacheKeyInput {
|
|
61
|
+
/** Source file path. */
|
|
62
|
+
filePath: string;
|
|
63
|
+
/** CSS source text. */
|
|
64
|
+
css: string;
|
|
65
|
+
/** Source file mtime in milliseconds. */
|
|
66
|
+
mtimeMs: number;
|
|
67
|
+
}
|
|
68
|
+
/** CSS source supplied to the Phase H validation orchestrator. */
|
|
69
|
+
interface GlobalVarCssSource {
|
|
70
|
+
/** Source file path. */
|
|
71
|
+
filePath: string;
|
|
72
|
+
/** CSS source text. */
|
|
73
|
+
css: string;
|
|
74
|
+
/** Source file mtime in milliseconds, used when cacheDir is set. */
|
|
75
|
+
mtimeMs?: number;
|
|
76
|
+
}
|
|
77
|
+
/** JS/TS/JSX/TSX source supplied to the Phase H validation orchestrator. */
|
|
78
|
+
interface GlobalVarCodeSource {
|
|
79
|
+
/** Source file path. */
|
|
80
|
+
filePath: string;
|
|
81
|
+
/** JS/TS/JSX/TSX source text. */
|
|
82
|
+
code: string;
|
|
83
|
+
}
|
|
84
|
+
/** CSS asset supplied by a bundler output hook. */
|
|
85
|
+
interface GlobalVarCssAssetSource {
|
|
86
|
+
/** CSS asset file name, relative to the build output or absolute. */
|
|
87
|
+
fileName: string;
|
|
88
|
+
/** CSS asset source contents. */
|
|
89
|
+
source: string | Uint8Array;
|
|
90
|
+
/** Source file mtime in milliseconds, used when cacheDir is set. */
|
|
91
|
+
mtimeMs?: number;
|
|
92
|
+
}
|
|
93
|
+
/** Options for scanning one CSS source. */
|
|
94
|
+
interface ScanGlobalVarCssOptions {
|
|
95
|
+
/** File path used for diagnostics. */
|
|
96
|
+
filePath?: string;
|
|
97
|
+
}
|
|
98
|
+
/** Planner diagnostic severity. */
|
|
99
|
+
type GlobalVarAliasDiagnosticSeverity = 'error';
|
|
100
|
+
/** Planner diagnostic. */
|
|
101
|
+
interface GlobalVarAliasDiagnostic {
|
|
102
|
+
/** Machine-readable diagnostic code. */
|
|
103
|
+
code: 'missing-definition' | 'tailwind-reserved' | 'tailwind-owned' | 'registered-property' | 'alias-collision';
|
|
104
|
+
/** Diagnostic severity. Phase H M2 is fail-closed. */
|
|
105
|
+
severity: GlobalVarAliasDiagnosticSeverity;
|
|
106
|
+
/** Related custom-property name. */
|
|
107
|
+
name: string;
|
|
108
|
+
/** Human-readable message. */
|
|
109
|
+
message: string;
|
|
110
|
+
/** Source location when available. */
|
|
111
|
+
location?: CssVarLocation;
|
|
112
|
+
}
|
|
113
|
+
/** One planned alias mapping. */
|
|
114
|
+
interface GlobalVarAliasEntry {
|
|
115
|
+
/** Original app-owned custom-property name. */
|
|
116
|
+
original: string;
|
|
117
|
+
/** Deterministic short alias name. */
|
|
118
|
+
alias: string;
|
|
119
|
+
/** Declaration scopes where aliases must be emitted. */
|
|
120
|
+
scopes: string[];
|
|
121
|
+
}
|
|
122
|
+
/** Input to the pure global variable alias planner. */
|
|
123
|
+
interface PlanGlobalVarAliasesInput {
|
|
124
|
+
/** CSS scan results. */
|
|
125
|
+
scans: CssVarScanResult[];
|
|
126
|
+
/** Explicit app-owned custom-property names. */
|
|
127
|
+
tokens?: string[];
|
|
128
|
+
/** Optional app-owned prefix discovery. Empty string disables discovery. */
|
|
129
|
+
autoPrefix?: string;
|
|
130
|
+
/** Prefix for generated aliases. Defaults to `---g`. */
|
|
131
|
+
aliasPrefix?: string;
|
|
132
|
+
/** Additional reserved names or prefixes. Prefixes may end with `*`. */
|
|
133
|
+
reserved?: string[];
|
|
134
|
+
}
|
|
135
|
+
/** Output from the pure global variable alias planner. */
|
|
136
|
+
interface GlobalVarAliasPlan {
|
|
137
|
+
/** Deterministic alias entries. Empty when diagnostics contain errors. */
|
|
138
|
+
entries: GlobalVarAliasEntry[];
|
|
139
|
+
/** Original-to-alias lookup. Empty when diagnostics contain errors. */
|
|
140
|
+
aliases: Map<string, string>;
|
|
141
|
+
/** Planner diagnostics. */
|
|
142
|
+
diagnostics: GlobalVarAliasDiagnostic[];
|
|
143
|
+
}
|
|
144
|
+
/** Input for Phase H scanner/planner/diagnostics integration. */
|
|
145
|
+
interface ValidateGlobalVarAliasInputsOptions {
|
|
146
|
+
/** CSS sources that define or reference custom properties. */
|
|
147
|
+
cssFiles: GlobalVarCssSource[];
|
|
148
|
+
/** JS/TS/JSX/TSX sources to scan for out-of-band usage. */
|
|
149
|
+
sourceFiles?: GlobalVarCodeSource[];
|
|
150
|
+
/** Explicit app-owned custom-property names. */
|
|
151
|
+
tokens?: string[];
|
|
152
|
+
/** Optional app-owned prefix discovery. Empty string disables discovery. */
|
|
153
|
+
autoPrefix?: string;
|
|
154
|
+
/** Prefix for generated aliases. Defaults to `---g`. */
|
|
155
|
+
aliasPrefix?: string;
|
|
156
|
+
/** Additional reserved names or prefixes. Prefixes may end with `*`. */
|
|
157
|
+
reserved?: string[];
|
|
158
|
+
/** Optional global-var scan cache directory. */
|
|
159
|
+
cacheDir?: string;
|
|
160
|
+
}
|
|
161
|
+
/** Input for building validation options from bundler output state. */
|
|
162
|
+
interface CreateGlobalVarAliasValidationOptionsInput {
|
|
163
|
+
/** Project root used to normalize relative asset names. */
|
|
164
|
+
rootDir: string;
|
|
165
|
+
/** CSS assets emitted by the bundler. Non-CSS assets are ignored. */
|
|
166
|
+
cssAssets: GlobalVarCssAssetSource[];
|
|
167
|
+
/** Source files transformed or observed before bundling. */
|
|
168
|
+
sourceFiles?: GlobalVarCodeSource[];
|
|
169
|
+
/** Explicit app-owned custom-property names. */
|
|
170
|
+
tokens?: string[];
|
|
171
|
+
/** Optional app-owned prefix discovery. Empty string disables discovery. */
|
|
172
|
+
autoPrefix?: string;
|
|
173
|
+
/** Prefix for generated aliases. Defaults to `---g`. */
|
|
174
|
+
aliasPrefix?: string;
|
|
175
|
+
/** Additional reserved names or prefixes. Prefixes may end with `*`. */
|
|
176
|
+
reserved?: string[];
|
|
177
|
+
/** Optional global-var scan cache directory. */
|
|
178
|
+
cacheDir?: string;
|
|
179
|
+
}
|
|
180
|
+
/** Output from Phase H scanner/planner/diagnostics integration. */
|
|
181
|
+
interface GlobalVarAliasValidationResult {
|
|
182
|
+
/** CSS scan results. */
|
|
183
|
+
scans: CssVarScanResult[];
|
|
184
|
+
/** Deterministic alias plan. */
|
|
185
|
+
plan: GlobalVarAliasPlan;
|
|
186
|
+
/** JS/JSX out-of-band usage diagnostics for planned candidates. */
|
|
187
|
+
usageDiagnostics: GlobalVarUsageDiagnostic[];
|
|
188
|
+
}
|
|
189
|
+
/** Options for rewriting CSS with a validated global variable alias plan. */
|
|
190
|
+
interface RewriteGlobalVarCssAliasesOptions {
|
|
191
|
+
/** CSS source text. */
|
|
192
|
+
css: string;
|
|
193
|
+
/** Validated alias plan. Diagnostics keep the rewrite as a no-op. */
|
|
194
|
+
plan: GlobalVarAliasPlan;
|
|
195
|
+
/** Source file path used by PostCSS diagnostics/source maps. */
|
|
196
|
+
filePath?: string;
|
|
197
|
+
}
|
|
198
|
+
/** Result of a pure global variable CSS alias rewrite. */
|
|
199
|
+
interface GlobalVarCssAliasRewriteResult {
|
|
200
|
+
/** Rewritten CSS source. */
|
|
201
|
+
css: string;
|
|
202
|
+
/** Number of alias declarations inserted. */
|
|
203
|
+
aliasDeclarations: number;
|
|
204
|
+
/** Number of `var(--token)` references rewritten. */
|
|
205
|
+
rewrittenReferences: number;
|
|
206
|
+
/** Planner diagnostics that prevented rewriting, when any. */
|
|
207
|
+
diagnostics: GlobalVarAliasDiagnostic[];
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* Extracts Phase H global custom-property aliases for manifest/debug tooling.
|
|
212
|
+
*
|
|
213
|
+
* The legacy `varMangleMap` also carries dynamic s/c-tier CSS variables. This
|
|
214
|
+
* helper keeps manifest consumers from guessing tiers by exposing only aliases
|
|
215
|
+
* that use the active generated prefix.
|
|
216
|
+
*
|
|
217
|
+
* @param varMangleMap CSS variable mangle metadata.
|
|
218
|
+
* @param aliasPrefix Active generated alias prefix.
|
|
219
|
+
* @param validationResult Validated CSS alias plan to include CSS-only aliases.
|
|
220
|
+
* @returns Original global variable names mapped to their generated aliases.
|
|
221
|
+
*/
|
|
222
|
+
declare function extractGlobalVarAliasesForManifest(varMangleMap: Record<string, CssVariableMangleValue>, aliasPrefix?: string, validationResult?: GlobalVarAliasValidationResult | null): Record<string, string>;
|
|
223
|
+
/**
|
|
224
|
+
* Serializes the standalone global-var map asset when g-tier aliases exist.
|
|
225
|
+
*
|
|
226
|
+
* @param varMangleMap CSS variable mangle metadata.
|
|
227
|
+
* @param aliasPrefix Active generated alias prefix.
|
|
228
|
+
* @param validationResult Validated CSS alias plan to include CSS-only aliases.
|
|
229
|
+
* @returns JSON asset contents, or null when there are no global aliases.
|
|
230
|
+
*/
|
|
231
|
+
declare function createGlobalVarMapAssetSource(varMangleMap: Record<string, CssVariableMangleValue>, aliasPrefix?: string, validationResult?: GlobalVarAliasValidationResult | null): string | null;
|
|
232
|
+
/**
|
|
233
|
+
* Normalizes compiler global-var aliases for transform-cache identity.
|
|
234
|
+
*
|
|
235
|
+
* @param aliases Compiler option value.
|
|
236
|
+
* @returns Stable original-to-alias entries.
|
|
237
|
+
*/
|
|
238
|
+
declare function normalizeGlobalVarAliasesForCache(aliases: TransformSourceCodeOptions['globalVarAliases']): Array<[string, string]>;
|
|
239
|
+
/**
|
|
240
|
+
* Mangles class strings in bundled code (JS/HTML assets) using the given mangle map.
|
|
241
|
+
*
|
|
242
|
+
* Exported for unit testing; the plugin calls this via the thin private wrapper that
|
|
243
|
+
* supplies state.mangleMap.
|
|
244
|
+
*
|
|
245
|
+
* Pass 1: Direct `className="..."` / `class="..."` static strings
|
|
246
|
+
* Pass 1.5: Template literal quasi (static) segments in `className:\`...\``
|
|
247
|
+
* Pass 2: `className:EXPR` patterns with ternary operators containing quoted strings
|
|
248
|
+
* Pass 3: Quoted string arguments to csszyx runtime helpers (_szMerge, _szIf, etc.)
|
|
249
|
+
*
|
|
250
|
+
* @param code bundled source code
|
|
251
|
+
* @param mangleMap class-name → mangled-token mapping
|
|
252
|
+
* @returns code with mangled class names
|
|
253
|
+
*/
|
|
254
|
+
declare function mangleCodeClassesSync(code: string, mangleMap: Record<string, string>): string;
|
|
255
|
+
declare const unplugin: UnpluginInstance<PartialCsszyxConfig, boolean>;
|
|
256
|
+
/**
|
|
257
|
+
* Creates a Vite plugin array with both pre-transform and post-mangle plugins.
|
|
258
|
+
* @param options - csszyx configuration options
|
|
259
|
+
* @returns array of Vite plugins for pre-transform and post-mangle phases
|
|
260
|
+
*/
|
|
261
|
+
declare const vitePlugin: (options?: PartialCsszyxConfig) => PluginOption[];
|
|
262
|
+
/**
|
|
263
|
+
* Creates a combined Webpack plugin that applies both pre-transform and post-mangle phases.
|
|
264
|
+
* @param options - csszyx configuration options
|
|
265
|
+
* @returns a Webpack plugin instance combining both phases
|
|
266
|
+
*/
|
|
267
|
+
declare const webpackPlugin: (options?: PartialCsszyxConfig) => WebpackPluginInstance;
|
|
268
|
+
/**
|
|
269
|
+
* Creates a Rollup plugin array with both pre-transform and post-mangle plugins.
|
|
270
|
+
* @param options - csszyx configuration options
|
|
271
|
+
* @returns array of Rollup plugins for pre-transform and post-mangle phases
|
|
272
|
+
*/
|
|
273
|
+
declare const rollupPlugin: (options?: PartialCsszyxConfig) => InputPluginOption[];
|
|
274
|
+
/**
|
|
275
|
+
* Creates an esbuild plugin that delegates setup to both pre-transform and post-mangle plugins.
|
|
276
|
+
* @param options - csszyx configuration options
|
|
277
|
+
* @returns an esbuild plugin combining both pre-transform and post-mangle phases
|
|
278
|
+
*/
|
|
279
|
+
declare const esbuildPlugin: (options?: PartialCsszyxConfig) => Plugin;
|
|
280
|
+
|
|
281
|
+
export { createGlobalVarMapAssetSource as o, esbuildPlugin as p, extractGlobalVarAliasesForManifest as q, mangleCodeClassesSync as r, normalizeGlobalVarAliasesForCache as s, rollupPlugin as t, unplugin as u, vitePlugin as v, webpackPlugin as w };
|
|
282
|
+
export type { CssVarScanResult as C, GlobalVarScanCacheKeyInput as G, PlanGlobalVarAliasesInput as P, RewriteGlobalVarCssAliasesOptions as R, ScanGlobalVarCssOptions as S, ValidateGlobalVarAliasInputsOptions as V, GlobalVarAliasPlan as a, GlobalVarCssAliasRewriteResult as b, CreateGlobalVarAliasValidationOptionsInput as c, GlobalVarAliasValidationResult as d, CssVarDefinition as e, CssVarLocation as f, CssVarReference as g, GlobalVarAliasDiagnostic as h, GlobalVarAliasDiagnosticSeverity as i, GlobalVarAliasEntry as j, GlobalVarCodeSource as k, GlobalVarCssAssetSource as l, GlobalVarCssSource as m, GlobalVarScanCacheEntry as n };
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const node_crypto = require('node:crypto');
|
|
4
|
+
const fs = require('node:fs');
|
|
5
|
+
const path = require('node:path');
|
|
6
|
+
|
|
7
|
+
function _interopNamespaceCompat(e) {
|
|
8
|
+
if (e && typeof e === 'object' && 'default' in e) return e;
|
|
9
|
+
const n = Object.create(null);
|
|
10
|
+
if (e) {
|
|
11
|
+
for (const k in e) {
|
|
12
|
+
n[k] = e[k];
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
n.default = e;
|
|
16
|
+
return n;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const fs__namespace = /*#__PURE__*/_interopNamespaceCompat(fs);
|
|
20
|
+
const path__namespace = /*#__PURE__*/_interopNamespaceCompat(path);
|
|
21
|
+
|
|
22
|
+
const CACHE_SCHEMA_VERSION = 6;
|
|
23
|
+
function resolveTransformCacheDir(rootDir, cacheDir) {
|
|
24
|
+
return path__namespace.resolve(rootDir, cacheDir ?? ".csszyx/cache", "transform");
|
|
25
|
+
}
|
|
26
|
+
function createTransformCacheKey(input) {
|
|
27
|
+
const inputSha256 = node_crypto.createHash("sha256").update(input.source).digest("hex");
|
|
28
|
+
const globalVarAliases = normalizeGlobalVarAliasEntries(input.globalVarAliases);
|
|
29
|
+
const keyMaterial = [
|
|
30
|
+
`schema=${CACHE_SCHEMA_VERSION}`,
|
|
31
|
+
`plugin=${input.pluginVersion}`,
|
|
32
|
+
`compiler=${input.compilerVersion}`,
|
|
33
|
+
`parser=${input.parserMode}`,
|
|
34
|
+
`producer=${input.producer}`,
|
|
35
|
+
`astBudget=${input.astBudget ?? "default"}`,
|
|
36
|
+
`mangleVars=${input.mangleVars === true}`,
|
|
37
|
+
`mangleVarHoistMaxDepth=${input.mangleVarHoistMaxDepth ?? "default"}`,
|
|
38
|
+
`globalVarAliases=${JSON.stringify(globalVarAliases)}`,
|
|
39
|
+
`filename=${input.filename}`,
|
|
40
|
+
`source=${inputSha256}`
|
|
41
|
+
].join("\n");
|
|
42
|
+
return {
|
|
43
|
+
key: node_crypto.createHash("sha256").update(keyMaterial).digest("hex").slice(0, 16),
|
|
44
|
+
inputSha256
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
function readTransformCache(cacheRoot, input, precomputedKey) {
|
|
48
|
+
const { key, inputSha256 } = precomputedKey ?? createTransformCacheKey(input);
|
|
49
|
+
const globalVarAliases = normalizeGlobalVarAliasEntries(input.globalVarAliases);
|
|
50
|
+
const file = cacheEntryPath(cacheRoot, key);
|
|
51
|
+
let entry;
|
|
52
|
+
try {
|
|
53
|
+
entry = JSON.parse(fs__namespace.readFileSync(file, "utf8"));
|
|
54
|
+
} catch {
|
|
55
|
+
return null;
|
|
56
|
+
}
|
|
57
|
+
if (entry.version !== CACHE_SCHEMA_VERSION || entry.pluginVersion !== input.pluginVersion || entry.compilerVersion !== input.compilerVersion || entry.parserMode !== input.parserMode || entry.producer !== input.producer || entry.astBudget !== (input.astBudget ?? null) || entry.mangleVars !== (input.mangleVars === true) || entry.mangleVarHoistMaxDepth !== (input.mangleVarHoistMaxDepth ?? null) || !sameGlobalVarAliases(entry.globalVarAliases, globalVarAliases) || entry.filename !== input.filename || entry.inputSha256 !== inputSha256) {
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
60
|
+
return deserializeResult(entry.result);
|
|
61
|
+
}
|
|
62
|
+
function writeTransformCache(cacheRoot, input, result, precomputedKey) {
|
|
63
|
+
const { key, inputSha256 } = precomputedKey ?? createTransformCacheKey(input);
|
|
64
|
+
const globalVarAliases = normalizeGlobalVarAliasEntries(input.globalVarAliases);
|
|
65
|
+
const file = cacheEntryPath(cacheRoot, key);
|
|
66
|
+
const dir = path__namespace.dirname(file);
|
|
67
|
+
const tmp = path__namespace.join(
|
|
68
|
+
dir,
|
|
69
|
+
`.tmp-${process.pid}-${Date.now()}-${Math.random().toString(36).slice(2)}.json`
|
|
70
|
+
);
|
|
71
|
+
const entry = {
|
|
72
|
+
version: CACHE_SCHEMA_VERSION,
|
|
73
|
+
pluginVersion: input.pluginVersion,
|
|
74
|
+
compilerVersion: input.compilerVersion,
|
|
75
|
+
parserMode: input.parserMode,
|
|
76
|
+
producer: input.producer,
|
|
77
|
+
astBudget: input.astBudget ?? null,
|
|
78
|
+
mangleVars: input.mangleVars === true,
|
|
79
|
+
mangleVarHoistMaxDepth: input.mangleVarHoistMaxDepth ?? null,
|
|
80
|
+
globalVarAliases,
|
|
81
|
+
filename: input.filename,
|
|
82
|
+
inputSha256,
|
|
83
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
84
|
+
result: serializeResult(result)
|
|
85
|
+
};
|
|
86
|
+
try {
|
|
87
|
+
fs__namespace.mkdirSync(dir, { recursive: true });
|
|
88
|
+
fs__namespace.writeFileSync(tmp, JSON.stringify(entry), "utf8");
|
|
89
|
+
fs__namespace.renameSync(tmp, file);
|
|
90
|
+
} catch {
|
|
91
|
+
try {
|
|
92
|
+
fs__namespace.rmSync(tmp, { force: true });
|
|
93
|
+
} catch {
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
function evictOldTransformCacheEntries(cacheRoot, options) {
|
|
98
|
+
let deleted = 0;
|
|
99
|
+
const now = options.now ?? Date.now();
|
|
100
|
+
const survivors = [];
|
|
101
|
+
for (const file of listJsonFiles(cacheRoot)) {
|
|
102
|
+
try {
|
|
103
|
+
const entry = JSON.parse(fs__namespace.readFileSync(file, "utf8"));
|
|
104
|
+
const timestamp = typeof entry.timestamp === "string" ? Date.parse(entry.timestamp) : 0;
|
|
105
|
+
if (!Number.isFinite(timestamp) || now - timestamp > options.maxAgeMs) {
|
|
106
|
+
fs__namespace.rmSync(file, { force: true });
|
|
107
|
+
deleted++;
|
|
108
|
+
} else {
|
|
109
|
+
survivors.push({ file, timestamp });
|
|
110
|
+
}
|
|
111
|
+
} catch {
|
|
112
|
+
fs__namespace.rmSync(file, { force: true });
|
|
113
|
+
deleted++;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
const overflow = survivors.length - options.maxEntries ;
|
|
117
|
+
if (overflow > 0) {
|
|
118
|
+
survivors.sort((a, b) => a.timestamp - b.timestamp);
|
|
119
|
+
for (const survivor of survivors.slice(0, overflow)) {
|
|
120
|
+
fs__namespace.rmSync(survivor.file, { force: true });
|
|
121
|
+
deleted++;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
return deleted;
|
|
125
|
+
}
|
|
126
|
+
function cacheEntryPath(cacheRoot, key) {
|
|
127
|
+
return path__namespace.join(cacheRoot, key.slice(0, 2), `${key.slice(2)}.json`);
|
|
128
|
+
}
|
|
129
|
+
function serializeResult(result) {
|
|
130
|
+
return {
|
|
131
|
+
code: result.code,
|
|
132
|
+
transformed: result.transformed,
|
|
133
|
+
usesRuntime: result.usesRuntime,
|
|
134
|
+
usesMerge: result.usesMerge,
|
|
135
|
+
usesColorVar: result.usesColorVar,
|
|
136
|
+
classes: [...result.classes],
|
|
137
|
+
rawClassNames: [...result.rawClassNames],
|
|
138
|
+
diagnostics: [...result.diagnostics],
|
|
139
|
+
recoveryTokens: [...result.recoveryTokens],
|
|
140
|
+
cssVariableMap: [...result.cssVariableMap ?? /* @__PURE__ */ new Map()]
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
function deserializeResult(result) {
|
|
144
|
+
return {
|
|
145
|
+
code: result.code,
|
|
146
|
+
transformed: result.transformed,
|
|
147
|
+
usesRuntime: result.usesRuntime,
|
|
148
|
+
usesMerge: result.usesMerge,
|
|
149
|
+
usesColorVar: result.usesColorVar,
|
|
150
|
+
classes: new Set(result.classes),
|
|
151
|
+
rawClassNames: new Set(result.rawClassNames),
|
|
152
|
+
diagnostics: [...result.diagnostics],
|
|
153
|
+
recoveryTokens: new Map(result.recoveryTokens),
|
|
154
|
+
cssVariableMap: new Map(result.cssVariableMap ?? [])
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
function normalizeGlobalVarAliasEntries(aliases) {
|
|
158
|
+
if (!aliases || aliases.length === 0) {
|
|
159
|
+
return [];
|
|
160
|
+
}
|
|
161
|
+
const normalized = /* @__PURE__ */ new Map();
|
|
162
|
+
for (const [original, alias] of aliases) {
|
|
163
|
+
if (typeof original === "string" && typeof alias === "string") {
|
|
164
|
+
normalized.set(original, alias);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
return [...normalized].sort(([left], [right]) => left.localeCompare(right));
|
|
168
|
+
}
|
|
169
|
+
function sameGlobalVarAliases(left, right) {
|
|
170
|
+
if (!Array.isArray(left) || left.length !== right.length) {
|
|
171
|
+
return false;
|
|
172
|
+
}
|
|
173
|
+
for (let index = 0; index < left.length; index++) {
|
|
174
|
+
const leftEntry = left[index];
|
|
175
|
+
const rightEntry = right[index];
|
|
176
|
+
if (leftEntry?.[0] !== rightEntry?.[0] || leftEntry?.[1] !== rightEntry?.[1]) {
|
|
177
|
+
return false;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
return true;
|
|
181
|
+
}
|
|
182
|
+
function listJsonFiles(dir) {
|
|
183
|
+
let entries;
|
|
184
|
+
try {
|
|
185
|
+
entries = fs__namespace.readdirSync(dir, { withFileTypes: true });
|
|
186
|
+
} catch {
|
|
187
|
+
return [];
|
|
188
|
+
}
|
|
189
|
+
const files = [];
|
|
190
|
+
for (const entry of entries) {
|
|
191
|
+
const fullPath = path__namespace.join(dir, entry.name);
|
|
192
|
+
if (entry.isDirectory()) {
|
|
193
|
+
files.push(...listJsonFiles(fullPath));
|
|
194
|
+
} else if (entry.isFile() && entry.name.endsWith(".json")) {
|
|
195
|
+
files.push(fullPath);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
return files;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
exports.createTransformCacheKey = createTransformCacheKey;
|
|
202
|
+
exports.evictOldTransformCacheEntries = evictOldTransformCacheEntries;
|
|
203
|
+
exports.readTransformCache = readTransformCache;
|
|
204
|
+
exports.resolveTransformCacheDir = resolveTransformCacheDir;
|
|
205
|
+
exports.writeTransformCache = writeTransformCache;
|