@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.
Files changed (48) hide show
  1. package/README.md +16 -7
  2. package/dist/css-mangler.cjs +4 -4
  3. package/dist/css-mangler.mjs +4 -4
  4. package/dist/index.cjs +23 -1
  5. package/dist/index.d.cts +100 -5
  6. package/dist/index.d.mts +99 -3
  7. package/dist/index.mjs +7 -1
  8. package/dist/next-prebuild.cjs +148 -0
  9. package/dist/next-prebuild.d.cts +66 -0
  10. package/dist/next-prebuild.d.mts +66 -0
  11. package/dist/next-prebuild.mjs +131 -0
  12. package/dist/next-turbo-loader.cjs +210 -0
  13. package/dist/next-turbo-loader.d.cts +68 -0
  14. package/dist/next-turbo-loader.d.mts +66 -0
  15. package/dist/next-turbo-loader.mjs +190 -0
  16. package/dist/next-watcher.cjs +237 -0
  17. package/dist/next-watcher.d.cts +106 -0
  18. package/dist/next-watcher.d.mts +106 -0
  19. package/dist/next-watcher.mjs +219 -0
  20. package/dist/shared/unplugin.8er8o6rn.mjs +276 -0
  21. package/dist/shared/unplugin.B_U4rZvG.cjs +281 -0
  22. package/dist/shared/unplugin.BbtspS8t.mjs +3271 -0
  23. package/dist/shared/unplugin.BceVw1eW.mjs +184 -0
  24. package/dist/shared/unplugin.BtQzlC2C.mjs +567 -0
  25. package/dist/shared/unplugin.CFp386gH.cjs +3321 -0
  26. package/dist/shared/unplugin.CPEWNSA0.d.cts +77 -0
  27. package/dist/shared/unplugin.CPEWNSA0.d.mts +77 -0
  28. package/dist/shared/unplugin.CScQRdTp.d.cts +15 -0
  29. package/dist/shared/unplugin.CScQRdTp.d.mts +15 -0
  30. package/dist/shared/unplugin.CdZxp0x-.d.mts +16 -0
  31. package/dist/shared/unplugin.DLrBgECN.d.cts +282 -0
  32. package/dist/shared/unplugin.DLrBgECN.d.mts +282 -0
  33. package/dist/shared/unplugin.DUxdYaSG.cjs +205 -0
  34. package/dist/shared/unplugin.s62yJbu1.cjs +591 -0
  35. package/dist/shared/unplugin.xeED_qwh.d.cts +16 -0
  36. package/dist/vite.cjs +7 -1
  37. package/dist/vite.d.cts +2 -1
  38. package/dist/vite.d.mts +2 -1
  39. package/dist/vite.mjs +7 -1
  40. package/dist/webpack.cjs +7 -1
  41. package/dist/webpack.d.cts +2 -1
  42. package/dist/webpack.d.mts +2 -1
  43. package/dist/webpack.mjs +7 -1
  44. package/package.json +41 -8
  45. package/dist/shared/unplugin.BNsv2szs.cjs +0 -1753
  46. package/dist/shared/unplugin.DCv0RtVZ.mjs +0 -1722
  47. package/dist/shared/unplugin.DUbr5w-N.d.cts +0 -49
  48. package/dist/shared/unplugin.DUbr5w-N.d.mts +0 -49
@@ -0,0 +1,281 @@
1
+ 'use strict';
2
+
3
+ const fs = require('node:fs');
4
+ const compiler = require('@csszyx/compiler');
5
+ const transformCache = require('./unplugin.DUxdYaSG.cjs');
6
+ const node_crypto = require('node:crypto');
7
+
8
+ function readPackageVersion(relativePackageJson, fromUrl) {
9
+ try {
10
+ const packageJson = JSON.parse(
11
+ fs.readFileSync(new URL(relativePackageJson, fromUrl), "utf8")
12
+ );
13
+ return typeof packageJson.version === "string" ? packageJson.version : "0.0.0";
14
+ } catch {
15
+ return "0.0.0";
16
+ }
17
+ }
18
+
19
+ function transformNextSource(input) {
20
+ const filename = normalizeSourceFilename(input.filename);
21
+ const producer = input.parserMode;
22
+ const cacheInput = createNextSourceTransformCacheInput(input, filename, producer);
23
+ if (input.parserMode === "rust") {
24
+ compiler.ensureRustTransformAvailable();
25
+ }
26
+ const cacheKey = input.cacheRoot ? transformCache.createTransformCacheKey(cacheInput) : null;
27
+ if (input.cacheRoot && cacheKey) {
28
+ const cached = transformCache.readTransformCache(input.cacheRoot, cacheInput, cacheKey);
29
+ if (cached) {
30
+ return { result: cached, cacheStatus: "hit", producer };
31
+ }
32
+ }
33
+ const result = runNextSourceTransform(input, filename);
34
+ assertNoUnsafePassThrough(input.source, result.result, filename);
35
+ if (input.cacheRoot && cacheKey && result.producer === producer) {
36
+ transformCache.writeTransformCache(input.cacheRoot, cacheInput, result.result, cacheKey);
37
+ return { ...result, cacheStatus: "write" };
38
+ }
39
+ return { ...result, cacheStatus: input.cacheRoot ? "miss" : "disabled" };
40
+ }
41
+ function assertNoUnsafePassThrough(source, result, filename) {
42
+ if (!hasSzSyntax(source)) {
43
+ return;
44
+ }
45
+ if (!result.transformed && result.code === source) {
46
+ throw new Error(
47
+ `[csszyx] Next source transform failed closed for ${filename}: source still contains csszyx sz syntax.`
48
+ );
49
+ }
50
+ }
51
+ function hasSzSyntax(source) {
52
+ const cleaned = stripJsCommentsAndStrings(source);
53
+ return /\bsz\s*=/.test(cleaned) || /\bsz\s*:\s*[{"']/.test(cleaned);
54
+ }
55
+ function stripJsCommentsAndStrings(source) {
56
+ let out = "";
57
+ let index = 0;
58
+ const length = source.length;
59
+ while (index < length) {
60
+ const char = source[index];
61
+ const peek = index + 1 < length ? source[index + 1] : "";
62
+ if (char === "/" && peek === "/") {
63
+ index += 2;
64
+ while (index < length && source[index] !== "\n") {
65
+ index++;
66
+ }
67
+ continue;
68
+ }
69
+ if (char === "/" && peek === "*") {
70
+ index += 2;
71
+ while (index + 1 < length && !(source[index] === "*" && source[index + 1] === "/")) {
72
+ index++;
73
+ }
74
+ index = Math.min(length, index + 2);
75
+ continue;
76
+ }
77
+ if (char === "/" && isRegexLiteralStart(out)) {
78
+ index++;
79
+ let inCharClass = false;
80
+ while (index < length) {
81
+ const current = source[index];
82
+ if (current === "\\" && index + 1 < length) {
83
+ index += 2;
84
+ continue;
85
+ }
86
+ if (current === "[") {
87
+ inCharClass = true;
88
+ index++;
89
+ continue;
90
+ }
91
+ if (current === "]") {
92
+ inCharClass = false;
93
+ index++;
94
+ continue;
95
+ }
96
+ if (current === "/" && !inCharClass) {
97
+ index++;
98
+ while (/[a-z]/i.test(source[index] ?? "")) {
99
+ index++;
100
+ }
101
+ break;
102
+ }
103
+ index++;
104
+ }
105
+ continue;
106
+ }
107
+ if (char === '"' || char === "'" || char === "`") {
108
+ const quote = char;
109
+ index++;
110
+ while (index < length && source[index] !== quote) {
111
+ if (source[index] === "\\" && index + 1 < length) {
112
+ index += 2;
113
+ } else {
114
+ index++;
115
+ }
116
+ }
117
+ index++;
118
+ continue;
119
+ }
120
+ out += char;
121
+ index++;
122
+ }
123
+ return out;
124
+ }
125
+ function isRegexLiteralStart(emitted) {
126
+ const trimmed = emitted.trimEnd();
127
+ const previous = trimmed.length > 0 ? trimmed.charAt(trimmed.length - 1) : void 0;
128
+ return previous === void 0 || /[({[=:;,!&|?+\-*%^~<>]/.test(previous);
129
+ }
130
+ function runNextSourceTransform(input, filename) {
131
+ const compilerOptions = input.compilerOptions;
132
+ if (input.parserMode === "babel") {
133
+ return {
134
+ result: compiler.transformSourceCode(input.source, filename, compilerOptions),
135
+ producer: "babel"
136
+ };
137
+ }
138
+ if (input.parserMode === "rust") {
139
+ return {
140
+ result: compiler.transformRust(input.source, filename, compilerOptions),
141
+ producer: "rust"
142
+ };
143
+ }
144
+ try {
145
+ return {
146
+ result: compiler.transformOxc(input.source, filename, compilerOptions),
147
+ producer: "oxc"
148
+ };
149
+ } catch (error) {
150
+ if (input.allowBabelFallback === false) {
151
+ throw error;
152
+ }
153
+ const result = compiler.transformSourceCode(input.source, filename, compilerOptions);
154
+ const reason = error instanceof Error ? error.message : String(error);
155
+ result.diagnostics.push(
156
+ `[csszyx] oxc parser fell back to Babel for ${filename}: ${reason}`
157
+ );
158
+ return { result, producer: "babel-fallback" };
159
+ }
160
+ }
161
+ function createNextSourceTransformCacheInput(input, filename, producer) {
162
+ return {
163
+ pluginVersion: input.pluginVersion,
164
+ compilerVersion: input.compilerVersion,
165
+ parserMode: input.parserMode,
166
+ producer,
167
+ astBudget: input.astBudget ?? input.compilerOptions?.astBudget,
168
+ mangleVars: input.compilerOptions?.mangleVars,
169
+ mangleVarHoistMaxDepth: input.compilerOptions?.mangleVarHoistMaxDepth,
170
+ globalVarAliases: normalizeGlobalVarAliasesForCache(
171
+ input.compilerOptions?.globalVarAliases
172
+ ),
173
+ filename,
174
+ source: input.source
175
+ };
176
+ }
177
+ function normalizeSourceFilename(filename) {
178
+ return filename.replace(/\\/g, "/");
179
+ }
180
+ function normalizeGlobalVarAliasesForCache(aliases) {
181
+ if (!aliases) {
182
+ return [];
183
+ }
184
+ const entries = aliases instanceof Map ? aliases.entries() : Array.isArray(aliases) ? aliases : Object.entries(aliases);
185
+ const normalized = /* @__PURE__ */ new Map();
186
+ for (const [original, alias] of entries) {
187
+ if (original.startsWith("--") && alias.startsWith("--")) {
188
+ normalized.set(original, alias);
189
+ }
190
+ }
191
+ return [...normalized].sort(([left], [right]) => left.localeCompare(right));
192
+ }
193
+
194
+ function collectNextTransformMetadata(result, source, sourcePath) {
195
+ const classes = new Set(result.classes);
196
+ collectRuntimeStaticClasses(result, classes);
197
+ return {
198
+ sourcePath,
199
+ sourceHash: node_crypto.createHash("sha256").update(source).digest("hex"),
200
+ classes: [...classes].sort(),
201
+ rawClassNames: [...result.rawClassNames].sort(),
202
+ recoveryTokenCount: result.recoveryTokens.size,
203
+ cssVariableCount: result.cssVariableMap.size
204
+ };
205
+ }
206
+ function createNextSafelistShardFromMetadata(metadata, cacheKey) {
207
+ return {
208
+ cacheKey,
209
+ sourcePath: metadata.sourcePath,
210
+ sourceHash: metadata.sourceHash,
211
+ classes: metadata.classes
212
+ };
213
+ }
214
+ function collectRuntimeStaticClasses(result, discoveredClasses) {
215
+ if (!result.usesRuntime) {
216
+ return;
217
+ }
218
+ const szCallRe = /_sz\(\s*\{/g;
219
+ for (const szMatch of result.code.matchAll(szCallRe)) {
220
+ let depth = 1;
221
+ let index = (szMatch.index ?? 0) + szMatch[0].length;
222
+ while (index < result.code.length && depth > 0) {
223
+ if (result.code[index] === "{") {
224
+ depth++;
225
+ } else if (result.code[index] === "}") {
226
+ depth--;
227
+ }
228
+ index++;
229
+ }
230
+ const objectSource = result.code.slice((szMatch.index ?? 0) + szMatch[0].length, index - 1);
231
+ collectRuntimeStringClasses(objectSource, discoveredClasses);
232
+ collectRuntimeNumberClasses(objectSource, discoveredClasses);
233
+ collectRuntimeBooleanClasses(objectSource, discoveredClasses);
234
+ }
235
+ }
236
+ function collectRuntimeStringClasses(objectSource, discoveredClasses) {
237
+ const stringKeyValue = /(\w+)\s*:\s*(?:"([^"]*)"|'([^']*)')/g;
238
+ for (const match of objectSource.matchAll(stringKeyValue)) {
239
+ try {
240
+ collectTransformClasses(
241
+ compiler.transform({ [match[1]]: match[2] ?? match[3] }),
242
+ discoveredClasses
243
+ );
244
+ } catch {
245
+ }
246
+ }
247
+ }
248
+ function collectRuntimeNumberClasses(objectSource, discoveredClasses) {
249
+ const numberKeyValue = /(\w+)\s*:\s*(-?\d+(?:\.\d+)?)\s*(?=[,}\n])/g;
250
+ for (const match of objectSource.matchAll(numberKeyValue)) {
251
+ try {
252
+ collectTransformClasses(
253
+ compiler.transform({ [match[1]]: Number.parseFloat(match[2]) }),
254
+ discoveredClasses
255
+ );
256
+ } catch {
257
+ }
258
+ }
259
+ }
260
+ function collectRuntimeBooleanClasses(objectSource, discoveredClasses) {
261
+ const booleanKeyValue = /(\w+)\s*:\s*(true|false)\s*(?=[,}\n])/g;
262
+ for (const match of objectSource.matchAll(booleanKeyValue)) {
263
+ try {
264
+ collectTransformClasses(
265
+ compiler.transform({ [match[1]]: match[2] === "true" }),
266
+ discoveredClasses
267
+ );
268
+ } catch {
269
+ }
270
+ }
271
+ }
272
+ function collectTransformClasses(result, discoveredClasses) {
273
+ for (const className of result.className.split(/\s+/).filter(Boolean)) {
274
+ discoveredClasses.add(className);
275
+ }
276
+ }
277
+
278
+ exports.collectNextTransformMetadata = collectNextTransformMetadata;
279
+ exports.createNextSafelistShardFromMetadata = createNextSafelistShardFromMetadata;
280
+ exports.readPackageVersion = readPackageVersion;
281
+ exports.transformNextSource = transformNextSource;