@plumeria/utils 4.1.3 → 4.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +1 -1
- package/dist/index.js +2 -5
- package/dist/parser.d.ts +3 -15
- package/dist/parser.js +183 -259
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
export type { CSSObject, FileStyles } from './types';
|
|
2
|
-
export {
|
|
2
|
+
export { objectExpressionToObject, collectLocalConsts, traverse, t, tables, extractOndemandStyles, deepMerge, scanAll, } from './parser';
|
|
3
3
|
export { getStyleRecords } from './create';
|
|
4
4
|
export type { StyleRecord } from './create';
|
package/dist/index.js
CHANGED
|
@@ -1,11 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getStyleRecords = exports.deepMerge = exports.extractOndemandStyles = exports.tables = exports.t = exports.traverse = exports.collectLocalConsts = exports.objectExpressionToObject =
|
|
3
|
+
exports.getStyleRecords = exports.scanAll = exports.deepMerge = exports.extractOndemandStyles = exports.tables = exports.t = exports.traverse = exports.collectLocalConsts = exports.objectExpressionToObject = void 0;
|
|
4
4
|
var parser_1 = require("./parser");
|
|
5
|
-
Object.defineProperty(exports, "scanForCreateStatic", { enumerable: true, get: function () { return parser_1.scanForCreateStatic; } });
|
|
6
|
-
Object.defineProperty(exports, "scanForCreateTheme", { enumerable: true, get: function () { return parser_1.scanForCreateTheme; } });
|
|
7
|
-
Object.defineProperty(exports, "scanForKeyframes", { enumerable: true, get: function () { return parser_1.scanForKeyframes; } });
|
|
8
|
-
Object.defineProperty(exports, "scanForViewTransition", { enumerable: true, get: function () { return parser_1.scanForViewTransition; } });
|
|
9
5
|
Object.defineProperty(exports, "objectExpressionToObject", { enumerable: true, get: function () { return parser_1.objectExpressionToObject; } });
|
|
10
6
|
Object.defineProperty(exports, "collectLocalConsts", { enumerable: true, get: function () { return parser_1.collectLocalConsts; } });
|
|
11
7
|
Object.defineProperty(exports, "traverse", { enumerable: true, get: function () { return parser_1.traverse; } });
|
|
@@ -13,5 +9,6 @@ Object.defineProperty(exports, "t", { enumerable: true, get: function () { retur
|
|
|
13
9
|
Object.defineProperty(exports, "tables", { enumerable: true, get: function () { return parser_1.tables; } });
|
|
14
10
|
Object.defineProperty(exports, "extractOndemandStyles", { enumerable: true, get: function () { return parser_1.extractOndemandStyles; } });
|
|
15
11
|
Object.defineProperty(exports, "deepMerge", { enumerable: true, get: function () { return parser_1.deepMerge; } });
|
|
12
|
+
Object.defineProperty(exports, "scanAll", { enumerable: true, get: function () { return parser_1.scanAll; } });
|
|
16
13
|
var create_1 = require("./create");
|
|
17
14
|
Object.defineProperty(exports, "getStyleRecords", { enumerable: true, get: function () { return create_1.getStyleRecords; } });
|
package/dist/parser.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { CSSObject, StaticTable, KeyframesHashTable,
|
|
1
|
+
import type { CSSObject, StaticTable, KeyframesHashTable, ThemeTable, ViewTransitionHashTable, Tables } from './types';
|
|
2
2
|
import { Module, CallExpression, Identifier, KeyValueProperty, StringLiteral, NumericLiteral, BooleanLiteral, ObjectExpression, MemberExpression, TemplateLiteral, BinaryExpression, UnaryExpression, VariableDeclaration, VariableDeclarator, ExportDeclaration, ConditionalExpression } from '@swc/core';
|
|
3
3
|
export declare const t: {
|
|
4
4
|
isObjectExpression: (node: any) => node is ObjectExpression;
|
|
@@ -28,19 +28,7 @@ export declare function traverse(node: Module, visitor: {
|
|
|
28
28
|
}): void;
|
|
29
29
|
export declare const tables: Tables;
|
|
30
30
|
export declare function objectExpressionToObject(node: ObjectExpression, staticTable: StaticTable, keyframesHashTable: KeyframesHashTable, viewTransitionHashTable: ViewTransitionHashTable, themeTable: ThemeTable): CSSObject;
|
|
31
|
-
export declare function collectLocalConsts(ast: Module): Record<string, any>;
|
|
32
|
-
export declare function
|
|
33
|
-
keyframesHashTableLocal: KeyframesHashTable;
|
|
34
|
-
keyframesObjectTableLocal: KeyframesObjectTable;
|
|
35
|
-
};
|
|
36
|
-
export declare function scanForViewTransition(addDependency: (path: string) => void): {
|
|
37
|
-
viewTransitionHashTableLocal: ViewTransitionHashTable;
|
|
38
|
-
viewTransitionObjectTableLocal: ViewTransitionObjectTable;
|
|
39
|
-
};
|
|
40
|
-
export declare function scanForCreateStatic(addDependency: (path: string) => void): StaticTable;
|
|
41
|
-
export declare function scanForCreateTheme(addDependency: (path: string) => void): {
|
|
42
|
-
themeTableLocal: Record<string, Record<string, any>>;
|
|
43
|
-
createThemeObjectTableLocal: Record<string, Record<string, any>>;
|
|
44
|
-
};
|
|
31
|
+
export declare function collectLocalConsts(ast: Module, filePath: string): Record<string, any>;
|
|
32
|
+
export declare function scanAll(addDependency: (path: string) => void): Tables;
|
|
45
33
|
export declare function extractOndemandStyles(obj: any, extractedSheets: string[]): void;
|
|
46
34
|
export declare function deepMerge(target: Record<string, any>, source: Record<string, any>): Record<string, any>;
|
package/dist/parser.js
CHANGED
|
@@ -7,10 +7,7 @@ exports.tables = exports.t = void 0;
|
|
|
7
7
|
exports.traverse = traverse;
|
|
8
8
|
exports.objectExpressionToObject = objectExpressionToObject;
|
|
9
9
|
exports.collectLocalConsts = collectLocalConsts;
|
|
10
|
-
exports.
|
|
11
|
-
exports.scanForViewTransition = scanForViewTransition;
|
|
12
|
-
exports.scanForCreateStatic = scanForCreateStatic;
|
|
13
|
-
exports.scanForCreateTheme = scanForCreateTheme;
|
|
10
|
+
exports.scanAll = scanAll;
|
|
14
11
|
exports.extractOndemandStyles = extractOndemandStyles;
|
|
15
12
|
exports.deepMerge = deepMerge;
|
|
16
13
|
const createTheme_1 = require("./createTheme");
|
|
@@ -87,12 +84,19 @@ exports.tables = {
|
|
|
87
84
|
viewTransitionObjectTable: {},
|
|
88
85
|
createThemeObjectTable: {},
|
|
89
86
|
};
|
|
87
|
+
function genFileId(filePath) {
|
|
88
|
+
const relativePath = path_1.default
|
|
89
|
+
.relative(PROJECT_ROOT, filePath)
|
|
90
|
+
.split(path_1.default.sep)
|
|
91
|
+
.join('/');
|
|
92
|
+
return (0, zss_engine_1.genBase36Hash)(relativePath, 1, 8);
|
|
93
|
+
}
|
|
90
94
|
function objectExpressionToObject(node, staticTable, keyframesHashTable, viewTransitionHashTable, themeTable) {
|
|
91
95
|
const obj = {};
|
|
92
96
|
node.properties.forEach((prop) => {
|
|
93
97
|
if (!exports.t.isObjectProperty(prop))
|
|
94
98
|
return;
|
|
95
|
-
const key = getPropertyKey(prop.key, staticTable);
|
|
99
|
+
const key = getPropertyKey(prop.key, staticTable, keyframesHashTable, viewTransitionHashTable, themeTable);
|
|
96
100
|
if (!key)
|
|
97
101
|
return;
|
|
98
102
|
const val = prop.value;
|
|
@@ -142,8 +146,19 @@ function objectExpressionToObject(node, staticTable, keyframesHashTable, viewTra
|
|
|
142
146
|
});
|
|
143
147
|
return obj;
|
|
144
148
|
}
|
|
145
|
-
function collectLocalConsts(ast) {
|
|
149
|
+
function collectLocalConsts(ast, filePath) {
|
|
146
150
|
const localConsts = {};
|
|
151
|
+
const fileId = genFileId(filePath);
|
|
152
|
+
const createProxy = (table) => {
|
|
153
|
+
return new Proxy(table, {
|
|
154
|
+
get: (target, prop) => {
|
|
155
|
+
if (typeof prop === 'string') {
|
|
156
|
+
return target[`${fileId}-${prop}`];
|
|
157
|
+
}
|
|
158
|
+
return Reflect.get(target, prop);
|
|
159
|
+
},
|
|
160
|
+
});
|
|
161
|
+
};
|
|
147
162
|
traverse(ast, {
|
|
148
163
|
VariableDeclarator({ node }) {
|
|
149
164
|
if (exports.t.isIdentifier(node.id) && node.init) {
|
|
@@ -151,14 +166,14 @@ function collectLocalConsts(ast) {
|
|
|
151
166
|
localConsts[node.id.value] = node.init.value;
|
|
152
167
|
}
|
|
153
168
|
else if (exports.t.isObjectExpression(node.init)) {
|
|
154
|
-
localConsts[node.id.value] = objectExpressionToObject(node.init, localConsts, exports.tables.keyframesHashTable, exports.tables.viewTransitionHashTable, exports.tables.themeTable);
|
|
169
|
+
localConsts[node.id.value] = objectExpressionToObject(node.init, localConsts, createProxy(exports.tables.keyframesHashTable), createProxy(exports.tables.viewTransitionHashTable), createProxy(exports.tables.themeTable));
|
|
155
170
|
}
|
|
156
171
|
}
|
|
157
172
|
},
|
|
158
173
|
});
|
|
159
174
|
return localConsts;
|
|
160
175
|
}
|
|
161
|
-
function getPropertyKey(node, staticTable) {
|
|
176
|
+
function getPropertyKey(node, staticTable, keyframesHashTable, viewTransitionHashTable, themeTable) {
|
|
162
177
|
if (exports.t.isIdentifier(node)) {
|
|
163
178
|
if (staticTable[node.value] &&
|
|
164
179
|
typeof staticTable[node.value] === 'string') {
|
|
@@ -186,42 +201,42 @@ function getPropertyKey(node, staticTable) {
|
|
|
186
201
|
return result;
|
|
187
202
|
}
|
|
188
203
|
if (exports.t.isTemplateLiteral(expr)) {
|
|
189
|
-
return evaluateTemplateLiteral(expr, staticTable);
|
|
204
|
+
return evaluateTemplateLiteral(expr, staticTable, keyframesHashTable, viewTransitionHashTable, themeTable);
|
|
190
205
|
}
|
|
191
206
|
if (exports.t.isBinaryExpression(expr)) {
|
|
192
|
-
return evaluateBinaryExpression(expr, staticTable);
|
|
207
|
+
return evaluateBinaryExpression(expr, staticTable, keyframesHashTable, viewTransitionHashTable, themeTable);
|
|
193
208
|
}
|
|
194
209
|
return '';
|
|
195
210
|
}
|
|
196
211
|
if (exports.t.isTemplateLiteral(node)) {
|
|
197
|
-
return evaluateTemplateLiteral(node, staticTable);
|
|
212
|
+
return evaluateTemplateLiteral(node, staticTable, keyframesHashTable, viewTransitionHashTable, themeTable);
|
|
198
213
|
}
|
|
199
214
|
if (exports.t.isBinaryExpression(node)) {
|
|
200
|
-
return evaluateBinaryExpression(node, staticTable);
|
|
215
|
+
return evaluateBinaryExpression(node, staticTable, keyframesHashTable, viewTransitionHashTable, themeTable);
|
|
201
216
|
}
|
|
202
217
|
return '';
|
|
203
218
|
}
|
|
204
|
-
function evaluateTemplateLiteral(node, staticTable) {
|
|
219
|
+
function evaluateTemplateLiteral(node, staticTable, keyframesHashTable, viewTransitionHashTable, themeTable) {
|
|
205
220
|
let result = '';
|
|
206
221
|
for (let i = 0; i < node.quasis.length; i++) {
|
|
207
222
|
result += node.quasis[i].cooked || node.quasis[i].raw;
|
|
208
223
|
if (i < node.expressions.length) {
|
|
209
224
|
const expr = node.expressions[i];
|
|
210
|
-
const evaluatedExpr = evaluateExpression(expr, staticTable);
|
|
225
|
+
const evaluatedExpr = evaluateExpression(expr, staticTable, keyframesHashTable, viewTransitionHashTable, themeTable);
|
|
211
226
|
result += String(evaluatedExpr);
|
|
212
227
|
}
|
|
213
228
|
}
|
|
214
229
|
return result;
|
|
215
230
|
}
|
|
216
|
-
function evaluateBinaryExpression(node, staticTable) {
|
|
217
|
-
const left = evaluateExpression(node.left, staticTable);
|
|
218
|
-
const right = evaluateExpression(node.right, staticTable);
|
|
231
|
+
function evaluateBinaryExpression(node, staticTable, keyframesHashTable, viewTransitionHashTable, themeTable) {
|
|
232
|
+
const left = evaluateExpression(node.left, staticTable, keyframesHashTable, viewTransitionHashTable, themeTable);
|
|
233
|
+
const right = evaluateExpression(node.right, staticTable, keyframesHashTable, viewTransitionHashTable, themeTable);
|
|
219
234
|
if (node.operator === '+') {
|
|
220
235
|
return String(left) + String(right);
|
|
221
236
|
}
|
|
222
237
|
throw new Error(`Unsupported binary operator: ${node.operator}`);
|
|
223
238
|
}
|
|
224
|
-
function evaluateExpression(node, staticTable) {
|
|
239
|
+
function evaluateExpression(node, staticTable, keyframesHashTable, viewTransitionHashTable, themeTable) {
|
|
225
240
|
if (exports.t.isStringLiteral(node)) {
|
|
226
241
|
return node.value;
|
|
227
242
|
}
|
|
@@ -238,11 +253,11 @@ function evaluateExpression(node, staticTable) {
|
|
|
238
253
|
if (staticTable[node.value] !== undefined) {
|
|
239
254
|
return staticTable[node.value];
|
|
240
255
|
}
|
|
241
|
-
if (
|
|
242
|
-
return
|
|
256
|
+
if (keyframesHashTable[node.value] !== undefined) {
|
|
257
|
+
return keyframesHashTable[node.value];
|
|
243
258
|
}
|
|
244
|
-
if (
|
|
245
|
-
return
|
|
259
|
+
if (viewTransitionHashTable[node.value] !== undefined) {
|
|
260
|
+
return viewTransitionHashTable[node.value];
|
|
246
261
|
}
|
|
247
262
|
return `[unresolved: ${node.value}]`;
|
|
248
263
|
}
|
|
@@ -251,17 +266,17 @@ function evaluateExpression(node, staticTable) {
|
|
|
251
266
|
if (resolved !== undefined) {
|
|
252
267
|
return resolved;
|
|
253
268
|
}
|
|
254
|
-
const resolvedTheme = resolveThemeTableMemberExpressionByNode(node,
|
|
269
|
+
const resolvedTheme = resolveThemeTableMemberExpressionByNode(node, themeTable);
|
|
255
270
|
if (resolvedTheme !== undefined) {
|
|
256
271
|
return resolvedTheme;
|
|
257
272
|
}
|
|
258
273
|
return `[unresolved member expression]`;
|
|
259
274
|
}
|
|
260
275
|
if (exports.t.isBinaryExpression(node)) {
|
|
261
|
-
return evaluateBinaryExpression(node, staticTable);
|
|
276
|
+
return evaluateBinaryExpression(node, staticTable, keyframesHashTable, viewTransitionHashTable, themeTable);
|
|
262
277
|
}
|
|
263
278
|
if (exports.t.isTemplateLiteral(node)) {
|
|
264
|
-
return evaluateTemplateLiteral(node, staticTable);
|
|
279
|
+
return evaluateTemplateLiteral(node, staticTable, keyframesHashTable, viewTransitionHashTable, themeTable);
|
|
265
280
|
}
|
|
266
281
|
if (exports.t.isUnaryExpression(node)) {
|
|
267
282
|
return evaluateUnaryExpression(node);
|
|
@@ -333,224 +348,124 @@ function resolveThemeTableMemberExpressionByNode(node, themeTable) {
|
|
|
333
348
|
}
|
|
334
349
|
return undefined;
|
|
335
350
|
}
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
const
|
|
339
|
-
|
|
340
|
-
for (const filePath of files) {
|
|
341
|
-
if (!isCSSDefineFile(filePath, 'keyframes'))
|
|
342
|
-
continue;
|
|
343
|
-
addDependency(filePath);
|
|
344
|
-
const source = fs_1.default.readFileSync(filePath, 'utf8');
|
|
345
|
-
const ast = (0, core_1.parseSync)(source, {
|
|
346
|
-
syntax: 'typescript',
|
|
347
|
-
tsx: true,
|
|
348
|
-
target: 'es2022',
|
|
349
|
-
});
|
|
350
|
-
for (const node of ast.body) {
|
|
351
|
-
let declarations = [];
|
|
352
|
-
if (exports.t.isVariableDeclaration(node)) {
|
|
353
|
-
declarations = node.declarations;
|
|
354
|
-
}
|
|
355
|
-
else if (exports.t.isExportDeclaration(node) &&
|
|
356
|
-
exports.t.isVariableDeclaration(node.declaration)) {
|
|
357
|
-
declarations = node.declaration.declarations;
|
|
358
|
-
}
|
|
359
|
-
for (const decl of declarations) {
|
|
360
|
-
if (exports.t.isVariableDeclarator(decl) &&
|
|
361
|
-
exports.t.isIdentifier(decl.id) &&
|
|
362
|
-
decl.init &&
|
|
363
|
-
exports.t.isCallExpression(decl.init) &&
|
|
364
|
-
exports.t.isMemberExpression(decl.init.callee) &&
|
|
365
|
-
exports.t.isIdentifier(decl.init.callee.object, { name: 'css' }) &&
|
|
366
|
-
exports.t.isIdentifier(decl.init.callee.property, { name: 'keyframes' }) &&
|
|
367
|
-
decl.init.arguments.length > 0 &&
|
|
368
|
-
exports.t.isObjectExpression(decl.init.arguments[0].expression)) {
|
|
369
|
-
const varName = decl.id.value;
|
|
370
|
-
const obj = objectExpressionToObject(decl.init.arguments[0].expression, exports.tables.staticTable, keyframesHashTableLocal, exports.tables.viewTransitionHashTable, exports.tables.themeTable);
|
|
371
|
-
const hash = (0, zss_engine_1.genBase36Hash)(obj, 1, 8);
|
|
372
|
-
keyframesHashTableLocal[varName] = hash;
|
|
373
|
-
keyframesObjectTableLocal[hash] = obj;
|
|
374
|
-
}
|
|
375
|
-
}
|
|
376
|
-
}
|
|
351
|
+
const fileCache = {};
|
|
352
|
+
function scanAll(addDependency) {
|
|
353
|
+
for (const key in exports.tables) {
|
|
354
|
+
exports.tables[key] = {};
|
|
377
355
|
}
|
|
378
|
-
return {
|
|
379
|
-
keyframesHashTableLocal,
|
|
380
|
-
keyframesObjectTableLocal,
|
|
381
|
-
};
|
|
382
|
-
}
|
|
383
|
-
function scanForViewTransition(addDependency) {
|
|
384
|
-
const viewTransitionHashTableLocal = {};
|
|
385
|
-
const viewTransitionObjectTableLocal = {};
|
|
386
356
|
const files = fs_1.default.globSync(PATTERN_PATH, GLOB_OPTIONS);
|
|
387
357
|
for (const filePath of files) {
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
if (exports.t.isVariableDeclaration(node)) {
|
|
400
|
-
declarations = node.declarations;
|
|
401
|
-
}
|
|
402
|
-
else if (exports.t.isExportDeclaration(node) &&
|
|
403
|
-
exports.t.isVariableDeclaration(node.declaration)) {
|
|
404
|
-
declarations = node.declaration.declarations;
|
|
405
|
-
}
|
|
406
|
-
for (const decl of declarations) {
|
|
407
|
-
if (exports.t.isVariableDeclarator(decl) &&
|
|
408
|
-
exports.t.isIdentifier(decl.id) &&
|
|
409
|
-
decl.init &&
|
|
410
|
-
exports.t.isCallExpression(decl.init) &&
|
|
411
|
-
exports.t.isMemberExpression(decl.init.callee) &&
|
|
412
|
-
exports.t.isIdentifier(decl.init.callee.object, { name: 'css' }) &&
|
|
413
|
-
exports.t.isIdentifier(decl.init.callee.property, {
|
|
414
|
-
name: 'viewTransition',
|
|
415
|
-
}) &&
|
|
416
|
-
decl.init.arguments.length > 0 &&
|
|
417
|
-
exports.t.isObjectExpression(decl.init.arguments[0].expression)) {
|
|
418
|
-
const varName = decl.id.value;
|
|
419
|
-
const obj = objectExpressionToObject(decl.init.arguments[0].expression, exports.tables.staticTable, exports.tables.keyframesHashTable, viewTransitionHashTableLocal, exports.tables.themeTable);
|
|
420
|
-
const hash = (0, zss_engine_1.genBase36Hash)(obj, 1, 8);
|
|
421
|
-
viewTransitionHashTableLocal[varName] = hash;
|
|
422
|
-
viewTransitionObjectTableLocal[hash] = obj;
|
|
358
|
+
try {
|
|
359
|
+
const stats = fs_1.default.statSync(filePath);
|
|
360
|
+
const cached = fileCache[filePath];
|
|
361
|
+
if (cached && cached.mtimeMs === stats.mtimeMs) {
|
|
362
|
+
if (cached.hasCssUsage) {
|
|
363
|
+
addDependency(filePath);
|
|
364
|
+
Object.assign(exports.tables.staticTable, cached.staticTable);
|
|
365
|
+
Object.assign(exports.tables.keyframesHashTable, cached.keyframesHashTable);
|
|
366
|
+
Object.assign(exports.tables.viewTransitionHashTable, cached.viewTransitionHashTable);
|
|
367
|
+
Object.assign(exports.tables.themeTable, cached.themeTable);
|
|
368
|
+
Object.assign(exports.tables.createThemeObjectTable, cached.createThemeObjectTable);
|
|
423
369
|
}
|
|
370
|
+
continue;
|
|
424
371
|
}
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
}
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
continue;
|
|
438
|
-
addDependency(filePath);
|
|
439
|
-
const source = fs_1.default.readFileSync(filePath, 'utf8');
|
|
440
|
-
const ast = (0, core_1.parseSync)(source, {
|
|
441
|
-
syntax: 'typescript',
|
|
442
|
-
tsx: true,
|
|
443
|
-
target: 'es2022',
|
|
444
|
-
});
|
|
445
|
-
for (const node of ast.body) {
|
|
446
|
-
let declarations = [];
|
|
447
|
-
if (exports.t.isVariableDeclaration(node)) {
|
|
448
|
-
declarations = node.declarations;
|
|
449
|
-
}
|
|
450
|
-
else if (exports.t.isExportDeclaration(node) &&
|
|
451
|
-
exports.t.isVariableDeclaration(node.declaration)) {
|
|
452
|
-
declarations = node.declaration.declarations;
|
|
372
|
+
const source = fs_1.default.readFileSync(filePath, 'utf8');
|
|
373
|
+
if (!source.includes('css.')) {
|
|
374
|
+
fileCache[filePath] = {
|
|
375
|
+
mtimeMs: stats.mtimeMs,
|
|
376
|
+
staticTable: {},
|
|
377
|
+
keyframesHashTable: {},
|
|
378
|
+
viewTransitionHashTable: {},
|
|
379
|
+
themeTable: {},
|
|
380
|
+
createThemeObjectTable: {},
|
|
381
|
+
hasCssUsage: false,
|
|
382
|
+
};
|
|
383
|
+
continue;
|
|
453
384
|
}
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
385
|
+
const ast = (0, core_1.parseSync)(source, {
|
|
386
|
+
syntax: 'typescript',
|
|
387
|
+
tsx: true,
|
|
388
|
+
target: 'es2022',
|
|
389
|
+
});
|
|
390
|
+
addDependency(filePath);
|
|
391
|
+
const fileId = genFileId(filePath);
|
|
392
|
+
const localStaticTable = {};
|
|
393
|
+
const localKeyframesHashTable = {};
|
|
394
|
+
const localViewTransitionHashTable = {};
|
|
395
|
+
const localThemeTable = {};
|
|
396
|
+
const localCreateThemeObjectTable = {};
|
|
397
|
+
for (const node of ast.body) {
|
|
398
|
+
let declarations = [];
|
|
399
|
+
if (exports.t.isVariableDeclaration(node)) {
|
|
400
|
+
declarations = node.declarations;
|
|
467
401
|
}
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
402
|
+
else if (exports.t.isExportDeclaration(node) &&
|
|
403
|
+
exports.t.isVariableDeclaration(node.declaration)) {
|
|
404
|
+
declarations = node.declaration.declarations;
|
|
405
|
+
}
|
|
406
|
+
for (const decl of declarations) {
|
|
407
|
+
if (exports.t.isVariableDeclarator(decl) &&
|
|
408
|
+
exports.t.isIdentifier(decl.id) &&
|
|
409
|
+
decl.init &&
|
|
410
|
+
exports.t.isCallExpression(decl.init) &&
|
|
411
|
+
exports.t.isMemberExpression(decl.init.callee) &&
|
|
412
|
+
exports.t.isIdentifier(decl.init.callee.object, { name: 'css' }) &&
|
|
413
|
+
exports.t.isIdentifier(decl.init.callee.property) &&
|
|
414
|
+
decl.init.arguments.length > 0 &&
|
|
415
|
+
exports.t.isObjectExpression(decl.init.arguments[0].expression)) {
|
|
416
|
+
const method = decl.init.callee.property.value;
|
|
417
|
+
const name = decl.id.value;
|
|
418
|
+
const init = decl.init;
|
|
419
|
+
const obj = objectExpressionToObject(init.arguments[0].expression, localStaticTable, localKeyframesHashTable, localViewTransitionHashTable, localThemeTable);
|
|
420
|
+
const uniqueKey = `${fileId}-${name}`;
|
|
421
|
+
if (method === 'createStatic') {
|
|
422
|
+
localStaticTable[name] = obj;
|
|
423
|
+
exports.tables.staticTable[uniqueKey] = obj;
|
|
424
|
+
}
|
|
425
|
+
else if (method === 'keyframes') {
|
|
426
|
+
const hash = (0, zss_engine_1.genBase36Hash)(obj, 1, 8);
|
|
427
|
+
localKeyframesHashTable[name] = hash;
|
|
428
|
+
exports.tables.keyframesHashTable[uniqueKey] = hash;
|
|
429
|
+
exports.tables.keyframesObjectTable[hash] = obj;
|
|
430
|
+
}
|
|
431
|
+
else if (method === 'viewTransition') {
|
|
432
|
+
const hash = (0, zss_engine_1.genBase36Hash)(obj, 1, 8);
|
|
433
|
+
localViewTransitionHashTable[name] = hash;
|
|
434
|
+
exports.tables.viewTransitionHashTable[uniqueKey] = hash;
|
|
435
|
+
exports.tables.viewTransitionObjectTable[hash] = obj;
|
|
436
|
+
}
|
|
437
|
+
else if (method === 'createTheme') {
|
|
438
|
+
const hash = (0, zss_engine_1.genBase36Hash)(obj, 1, 8);
|
|
439
|
+
localThemeTable[name] = obj;
|
|
440
|
+
exports.tables.themeTable[uniqueKey] = obj;
|
|
441
|
+
exports.tables.createThemeObjectTable[hash] = obj;
|
|
442
|
+
localCreateThemeObjectTable[hash] = obj;
|
|
443
|
+
}
|
|
444
|
+
}
|
|
511
445
|
}
|
|
512
446
|
}
|
|
447
|
+
fileCache[filePath] = {
|
|
448
|
+
mtimeMs: stats.mtimeMs,
|
|
449
|
+
staticTable: localStaticTable,
|
|
450
|
+
keyframesHashTable: localKeyframesHashTable,
|
|
451
|
+
viewTransitionHashTable: localViewTransitionHashTable,
|
|
452
|
+
themeTable: localThemeTable,
|
|
453
|
+
createThemeObjectTable: localCreateThemeObjectTable,
|
|
454
|
+
hasCssUsage: true,
|
|
455
|
+
};
|
|
456
|
+
}
|
|
457
|
+
catch (e) {
|
|
513
458
|
}
|
|
514
459
|
}
|
|
515
|
-
return
|
|
516
|
-
}
|
|
517
|
-
function isCSSDefineFile(filePath, target) {
|
|
518
|
-
if (fs_1.default.statSync(filePath).isDirectory())
|
|
519
|
-
return false;
|
|
520
|
-
const code = fs_1.default.readFileSync(filePath, 'utf8');
|
|
521
|
-
let ast;
|
|
522
|
-
try {
|
|
523
|
-
ast = (0, core_1.parseSync)(code, {
|
|
524
|
-
syntax: 'typescript',
|
|
525
|
-
tsx: true,
|
|
526
|
-
target: 'es2022',
|
|
527
|
-
});
|
|
528
|
-
}
|
|
529
|
-
catch (err) {
|
|
530
|
-
console.log(err);
|
|
531
|
-
return false;
|
|
532
|
-
}
|
|
533
|
-
let found = false;
|
|
534
|
-
traverse(ast, {
|
|
535
|
-
CallExpression({ node, stop }) {
|
|
536
|
-
const callee = node.callee;
|
|
537
|
-
if (callee.type === 'MemberExpression' &&
|
|
538
|
-
callee.object.type === 'Identifier' &&
|
|
539
|
-
callee.object.value === 'css' &&
|
|
540
|
-
callee.property.type === 'Identifier' &&
|
|
541
|
-
callee.property.value === target) {
|
|
542
|
-
found = true;
|
|
543
|
-
stop();
|
|
544
|
-
}
|
|
545
|
-
},
|
|
546
|
-
});
|
|
547
|
-
return found;
|
|
460
|
+
return exports.tables;
|
|
548
461
|
}
|
|
549
462
|
function extractOndemandStyles(obj, extractedSheets) {
|
|
550
463
|
if (!obj || typeof obj !== 'object')
|
|
551
464
|
return;
|
|
552
465
|
const visited = new Set();
|
|
553
|
-
const
|
|
466
|
+
const keyframesHashes = new Set();
|
|
467
|
+
const viewTransitionHashes = new Set();
|
|
468
|
+
let needsTheme = false;
|
|
554
469
|
function walk(n) {
|
|
555
470
|
if (!n || typeof n !== 'object' || visited.has(n))
|
|
556
471
|
return;
|
|
@@ -558,41 +473,13 @@ function extractOndemandStyles(obj, extractedSheets) {
|
|
|
558
473
|
Object.values(n).forEach((val) => {
|
|
559
474
|
if (typeof val === 'string') {
|
|
560
475
|
if (val.startsWith('kf-')) {
|
|
561
|
-
|
|
562
|
-
const definition = exports.tables.keyframesObjectTable[hash];
|
|
563
|
-
if (definition) {
|
|
564
|
-
const { styleSheet } = (0, zss_engine_1.transpile)({ [`@keyframes kf-${hash}`]: definition }, undefined, '--global');
|
|
565
|
-
if (!extractedSheets.includes(styleSheet)) {
|
|
566
|
-
extractedSheets.push(styleSheet);
|
|
567
|
-
}
|
|
568
|
-
}
|
|
476
|
+
keyframesHashes.add(val.slice(3));
|
|
569
477
|
}
|
|
570
478
|
else if (val.startsWith('vt-')) {
|
|
571
|
-
|
|
572
|
-
const obj = exports.tables.viewTransitionObjectTable[hash];
|
|
573
|
-
if (obj) {
|
|
574
|
-
const { styleSheet } = (0, zss_engine_1.transpile)((0, viewTransition_1.createViewTransition)(obj, hash), undefined, '--global');
|
|
575
|
-
if (!extractedSheets.includes(styleSheet)) {
|
|
576
|
-
extractedSheets.push(styleSheet);
|
|
577
|
-
}
|
|
578
|
-
}
|
|
479
|
+
viewTransitionHashes.add(val.slice(3));
|
|
579
480
|
}
|
|
580
|
-
else if (val.includes('var(--')) {
|
|
581
|
-
|
|
582
|
-
if (!processedThemes.has(themeVarName)) {
|
|
583
|
-
processedThemes.add(themeVarName);
|
|
584
|
-
const themeObj = exports.tables.themeTable[themeVarName];
|
|
585
|
-
const hash = (0, zss_engine_1.genBase36Hash)(themeObj, 1, 8);
|
|
586
|
-
const definition = exports.tables.createThemeObjectTable[hash];
|
|
587
|
-
if (definition && typeof definition === 'object') {
|
|
588
|
-
const styles = (0, createTheme_1.createTheme)(definition);
|
|
589
|
-
const { styleSheet } = (0, zss_engine_1.transpile)(styles, undefined, '--global');
|
|
590
|
-
if (!extractedSheets.includes(styleSheet)) {
|
|
591
|
-
extractedSheets.push(styleSheet);
|
|
592
|
-
}
|
|
593
|
-
}
|
|
594
|
-
}
|
|
595
|
-
});
|
|
481
|
+
else if (!needsTheme && val.includes('var(--')) {
|
|
482
|
+
needsTheme = true;
|
|
596
483
|
}
|
|
597
484
|
}
|
|
598
485
|
else {
|
|
@@ -601,6 +488,43 @@ function extractOndemandStyles(obj, extractedSheets) {
|
|
|
601
488
|
});
|
|
602
489
|
}
|
|
603
490
|
walk(obj);
|
|
491
|
+
const existingSheets = new Set(extractedSheets);
|
|
492
|
+
const addSheet = (sheet) => {
|
|
493
|
+
if (!existingSheets.has(sheet)) {
|
|
494
|
+
existingSheets.add(sheet);
|
|
495
|
+
extractedSheets.push(sheet);
|
|
496
|
+
}
|
|
497
|
+
};
|
|
498
|
+
if (keyframesHashes.size > 0) {
|
|
499
|
+
for (const hash of keyframesHashes) {
|
|
500
|
+
const definition = exports.tables.keyframesObjectTable[hash];
|
|
501
|
+
if (definition) {
|
|
502
|
+
const { styleSheet } = (0, zss_engine_1.transpile)({ [`@keyframes kf-${hash}`]: definition }, undefined, '--global');
|
|
503
|
+
addSheet(styleSheet);
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
if (viewTransitionHashes.size > 0) {
|
|
508
|
+
for (const hash of viewTransitionHashes) {
|
|
509
|
+
const obj = exports.tables.viewTransitionObjectTable[hash];
|
|
510
|
+
if (obj) {
|
|
511
|
+
const { styleSheet } = (0, zss_engine_1.transpile)((0, viewTransition_1.createViewTransition)(obj, hash), undefined, '--global');
|
|
512
|
+
addSheet(styleSheet);
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
if (needsTheme) {
|
|
517
|
+
for (const themeVarName in exports.tables.themeTable) {
|
|
518
|
+
const themeObj = exports.tables.themeTable[themeVarName];
|
|
519
|
+
const hash = (0, zss_engine_1.genBase36Hash)(themeObj, 1, 8);
|
|
520
|
+
const definition = exports.tables.createThemeObjectTable[hash];
|
|
521
|
+
if (definition && typeof definition === 'object') {
|
|
522
|
+
const styles = (0, createTheme_1.createTheme)(definition);
|
|
523
|
+
const { styleSheet } = (0, zss_engine_1.transpile)(styles, undefined, '--global');
|
|
524
|
+
addSheet(styleSheet);
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
}
|
|
604
528
|
}
|
|
605
529
|
function deepMerge(target, source) {
|
|
606
530
|
const result = { ...target };
|