@plumeria/utils 2.4.1 → 3.0.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.
@@ -0,0 +1,7 @@
1
+ import { CSSProperties } from 'zss-engine';
2
+ export interface StyleRecord {
3
+ key: string;
4
+ hash: string;
5
+ sheet: string;
6
+ }
7
+ export declare function getStyleRecords(key: string, styleRule: CSSProperties, priority?: number): StyleRecord[];
package/dist/create.js ADDED
@@ -0,0 +1,88 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getStyleRecords = getStyleRecords;
4
+ const zss_engine_1 = require("zss-engine");
5
+ function getStyleRecords(key, styleRule, priority = 1 | 2) {
6
+ const flat = {};
7
+ const nonFlat = {};
8
+ const notNormalize = priority === 1 ? ':not(#\\#)' : ':not(#\\#):not(#\\#)';
9
+ (0, zss_engine_1.splitAtomicAndNested)(styleRule, flat, nonFlat);
10
+ const finalFlat = (0, zss_engine_1.overrideLonghand)(flat);
11
+ const records = [];
12
+ Object.entries(finalFlat).forEach(([prop, value]) => {
13
+ if (prop.startsWith('@media') || prop.startsWith('@container')) {
14
+ Object.entries(value).forEach(([innerProp, innerValue]) => {
15
+ const atomicMap = new Map();
16
+ (0, zss_engine_1.processAtomicProps)({ [innerProp]: innerValue }, atomicMap, prop);
17
+ const querySheets = [];
18
+ const queryHashes = [];
19
+ for (const [hash, sheet] of atomicMap) {
20
+ querySheets.push(sheet.replace(`.${hash}`, `.${hash}${notNormalize}`));
21
+ queryHashes.push(hash);
22
+ }
23
+ if (querySheets.length > 0) {
24
+ records.push({
25
+ key: prop + innerProp,
26
+ hash: queryHashes.join(' '),
27
+ sheet: querySheets.join(''),
28
+ });
29
+ }
30
+ });
31
+ }
32
+ else {
33
+ const atomicMap = new Map();
34
+ (0, zss_engine_1.processAtomicProps)({ [prop]: value }, atomicMap);
35
+ const sheets = [];
36
+ const hashes = [];
37
+ for (const [hash, sheet] of atomicMap) {
38
+ sheets.push(sheet);
39
+ hashes.push(hash);
40
+ }
41
+ if (sheets.length > 0) {
42
+ records.push({
43
+ key: prop,
44
+ hash: hashes.join(' '),
45
+ sheet: sheets.join(''),
46
+ });
47
+ }
48
+ }
49
+ });
50
+ if (Object.keys(nonFlat).length > 0) {
51
+ const nonFlatBase = {};
52
+ const nonFlatQuery = {};
53
+ Object.entries(nonFlat).forEach(([atRule, nestedObj]) => {
54
+ if (atRule.startsWith('@media') || atRule.startsWith('@container')) {
55
+ nonFlatQuery[atRule] = nestedObj;
56
+ }
57
+ else {
58
+ nonFlatBase[atRule] = nestedObj;
59
+ }
60
+ });
61
+ Object.entries(nonFlatBase).forEach(([selector, style], index) => {
62
+ const hashObj = { [key]: { [selector]: style, index } };
63
+ const hash = (0, zss_engine_1.genBase36Hash)(hashObj, 1, 7);
64
+ const transpileObj = { [key]: { [selector]: style } };
65
+ const { styleSheet } = (0, zss_engine_1.transpile)(transpileObj, hash);
66
+ records.push({
67
+ key: selector + index,
68
+ hash: hash,
69
+ sheet: styleSheet,
70
+ });
71
+ });
72
+ Object.entries(nonFlatQuery).forEach(([atRule, nestedStyles]) => {
73
+ Object.entries(nestedStyles).forEach(([selector, style], index) => {
74
+ const hashObj = { [key]: { [atRule]: { [selector]: style, index } } };
75
+ const hash = (0, zss_engine_1.genBase36Hash)(hashObj, 1, 7);
76
+ const transpileObj = { [key]: { [atRule]: { [selector]: style } } };
77
+ const { styleSheet } = (0, zss_engine_1.transpile)(transpileObj, hash);
78
+ const finalSheet = styleSheet.replace(`.${hash}`, `.${hash}${notNormalize}`);
79
+ records.push({
80
+ key: atRule + selector + index,
81
+ hash: hash,
82
+ sheet: finalSheet,
83
+ });
84
+ });
85
+ });
86
+ }
87
+ return records;
88
+ }
@@ -0,0 +1,3 @@
1
+ import { CreateTheme } from 'zss-engine';
2
+ declare const createTheme: <const T extends CreateTheme>(rule: T) => Record<string, Record<string, string | number | Record<string, string | number>>>;
3
+ export { createTheme };
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createTheme = void 0;
4
+ const zss_engine_1 = require("zss-engine");
5
+ const createTheme = (rule) => {
6
+ const styles = {};
7
+ for (const key in rule) {
8
+ const varKey = `--${(0, zss_engine_1.camelToKebabCase)(key)}`;
9
+ const themeMap = rule[key];
10
+ for (const themeKey in themeMap) {
11
+ const value = themeMap[themeKey];
12
+ const isQuery = themeKey.startsWith('@media') || themeKey.startsWith('@container');
13
+ const selector = isQuery || themeKey === 'default'
14
+ ? ':root'
15
+ : `:root[data-theme="${themeKey}"]`;
16
+ const target = (styles[selector] ||= {});
17
+ if (isQuery) {
18
+ const queryObj = (target[themeKey] ||= {});
19
+ queryObj[varKey] = value;
20
+ }
21
+ else {
22
+ target[varKey] = value;
23
+ }
24
+ }
25
+ }
26
+ return styles;
27
+ };
28
+ exports.createTheme = createTheme;
package/dist/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  export type { CSSObject, FileStyles } from './types';
2
- export { createCSS, createVars, createTheme } from './transform';
3
- export { scanForCreateStatic, scanForCreateTheme, scanForKeyframes, scanForViewTransition, objectExpressionToObject, collectLocalConsts, traverse, t, tables, } from './parser';
2
+ export { scanForCreateStatic, scanForCreateTheme, scanForKeyframes, scanForViewTransition, objectExpressionToObject, collectLocalConsts, traverse, t, tables, extractOndemandStyles, } from './parser';
3
+ export { getStyleRecords } from './create';
4
+ export type { StyleRecord } from './create';
package/dist/index.js CHANGED
@@ -1,10 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.tables = exports.t = exports.traverse = exports.collectLocalConsts = exports.objectExpressionToObject = exports.scanForViewTransition = exports.scanForKeyframes = exports.scanForCreateTheme = exports.scanForCreateStatic = exports.createTheme = exports.createVars = exports.createCSS = void 0;
4
- var transform_1 = require("./transform");
5
- Object.defineProperty(exports, "createCSS", { enumerable: true, get: function () { return transform_1.createCSS; } });
6
- Object.defineProperty(exports, "createVars", { enumerable: true, get: function () { return transform_1.createVars; } });
7
- Object.defineProperty(exports, "createTheme", { enumerable: true, get: function () { return transform_1.createTheme; } });
3
+ exports.getStyleRecords = exports.extractOndemandStyles = exports.tables = exports.t = exports.traverse = exports.collectLocalConsts = exports.objectExpressionToObject = exports.scanForViewTransition = exports.scanForKeyframes = exports.scanForCreateTheme = exports.scanForCreateStatic = void 0;
8
4
  var parser_1 = require("./parser");
9
5
  Object.defineProperty(exports, "scanForCreateStatic", { enumerable: true, get: function () { return parser_1.scanForCreateStatic; } });
10
6
  Object.defineProperty(exports, "scanForCreateTheme", { enumerable: true, get: function () { return parser_1.scanForCreateTheme; } });
@@ -15,3 +11,6 @@ Object.defineProperty(exports, "collectLocalConsts", { enumerable: true, get: fu
15
11
  Object.defineProperty(exports, "traverse", { enumerable: true, get: function () { return parser_1.traverse; } });
16
12
  Object.defineProperty(exports, "t", { enumerable: true, get: function () { return parser_1.t; } });
17
13
  Object.defineProperty(exports, "tables", { enumerable: true, get: function () { return parser_1.tables; } });
14
+ Object.defineProperty(exports, "extractOndemandStyles", { enumerable: true, get: function () { return parser_1.extractOndemandStyles; } });
15
+ var create_1 = require("./create");
16
+ Object.defineProperty(exports, "getStyleRecords", { enumerable: true, get: function () { return create_1.getStyleRecords; } });
package/dist/parser.d.ts CHANGED
@@ -39,5 +39,6 @@ export declare function scanForViewTransition(addDependency: (path: string) => v
39
39
  export declare function scanForCreateStatic(addDependency: (path: string) => void): StaticTable;
40
40
  export declare function scanForCreateTheme(addDependency: (path: string) => void): {
41
41
  themeTableLocal: Record<string, Record<string, any>>;
42
- createThemeObjectTableLocal: Record<string, any>;
42
+ createThemeObjectTableLocal: Record<string, Record<string, any>>;
43
43
  };
44
+ export declare function extractOndemandStyles(obj: any, extractedSheets: string[]): void;
package/dist/parser.js CHANGED
@@ -11,10 +11,13 @@ exports.scanForKeyframes = scanForKeyframes;
11
11
  exports.scanForViewTransition = scanForViewTransition;
12
12
  exports.scanForCreateStatic = scanForCreateStatic;
13
13
  exports.scanForCreateTheme = scanForCreateTheme;
14
+ exports.extractOndemandStyles = extractOndemandStyles;
15
+ const createTheme_1 = require("./createTheme");
14
16
  const core_1 = require("@swc/core");
15
17
  const path_1 = __importDefault(require("path"));
16
18
  const fs_1 = __importDefault(require("fs"));
17
19
  const zss_engine_1 = require("zss-engine");
20
+ const viewTransition_1 = require("./viewTransition");
18
21
  exports.t = {
19
22
  isObjectExpression: (node) => node?.type === 'ObjectExpression',
20
23
  isObjectProperty: (node) => node?.type === 'KeyValueProperty',
@@ -500,8 +503,9 @@ function scanForCreateTheme(addDependency) {
500
503
  exports.t.isObjectExpression(decl.init.arguments[0].expression)) {
501
504
  const varName = decl.id.value;
502
505
  const obj = objectExpressionToObject(decl.init.arguments[0].expression, exports.tables.staticTable, exports.tables.keyframesHashTable, exports.tables.viewTransitionHashTable, themeTableLocal);
506
+ const hash = (0, zss_engine_1.genBase36Hash)(obj, 1, 8);
503
507
  themeTableLocal[varName] = obj;
504
- createThemeObjectTableLocal[varName] = obj;
508
+ createThemeObjectTableLocal[hash] = obj;
505
509
  }
506
510
  }
507
511
  }
@@ -540,3 +544,59 @@ function isCSSDefineFile(filePath, target) {
540
544
  });
541
545
  return found;
542
546
  }
547
+ function extractOndemandStyles(obj, extractedSheets) {
548
+ if (!obj || typeof obj !== 'object')
549
+ return;
550
+ const visited = new Set();
551
+ const processedThemes = new Set();
552
+ function walk(n) {
553
+ if (!n || typeof n !== 'object' || visited.has(n))
554
+ return;
555
+ visited.add(n);
556
+ Object.values(n).forEach((val) => {
557
+ if (typeof val === 'string') {
558
+ if (val.startsWith('kf-')) {
559
+ const hash = val.slice(3);
560
+ const definition = exports.tables.keyframesObjectTable[hash];
561
+ if (definition) {
562
+ const { styleSheet } = (0, zss_engine_1.transpile)({ [`@keyframes kf-${hash}`]: definition }, undefined, '--global');
563
+ if (!extractedSheets.includes(styleSheet)) {
564
+ extractedSheets.push(styleSheet);
565
+ }
566
+ }
567
+ }
568
+ else if (val.startsWith('vt-')) {
569
+ const hash = val.slice(3);
570
+ const obj = exports.tables.viewTransitionObjectTable[hash];
571
+ if (obj) {
572
+ const { styleSheet } = (0, zss_engine_1.transpile)((0, viewTransition_1.createViewTransition)(obj, hash), undefined, '--global');
573
+ if (!extractedSheets.includes(styleSheet)) {
574
+ extractedSheets.push(styleSheet);
575
+ }
576
+ }
577
+ }
578
+ else if (val.includes('var(--')) {
579
+ Object.keys(exports.tables.themeTable).forEach((themeVarName) => {
580
+ if (!processedThemes.has(themeVarName)) {
581
+ processedThemes.add(themeVarName);
582
+ const themeObj = exports.tables.themeTable[themeVarName];
583
+ const hash = (0, zss_engine_1.genBase36Hash)(themeObj, 1, 8);
584
+ const definition = exports.tables.createThemeObjectTable[hash];
585
+ if (definition && typeof definition === 'object') {
586
+ const styles = (0, createTheme_1.createTheme)(definition);
587
+ const { styleSheet } = (0, zss_engine_1.transpile)(styles, undefined, '--global');
588
+ if (!extractedSheets.includes(styleSheet)) {
589
+ extractedSheets.push(styleSheet);
590
+ }
591
+ }
592
+ }
593
+ });
594
+ }
595
+ }
596
+ else {
597
+ walk(val);
598
+ }
599
+ });
600
+ }
601
+ walk(obj);
602
+ }
@@ -0,0 +1,4 @@
1
+ import type { CSSProperties, ViewTransition } from 'zss-engine';
2
+ export declare const createViewTransition: (rule: ViewTransition, hash: string) => {
3
+ [x: string]: CSSProperties;
4
+ };
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createViewTransition = void 0;
4
+ const createViewTransition = (rule, hash) => ({
5
+ [`::view-transition-group(vt-${hash})`]: rule.group,
6
+ [`::view-transition-image-pair(vt-${hash})`]: rule.imagePair,
7
+ [`::view-transition-old(vt-${hash})`]: rule.old,
8
+ [`::view-transition-new(vt-${hash})`]: rule.new,
9
+ });
10
+ exports.createViewTransition = createViewTransition;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@plumeria/utils",
3
- "version": "2.4.1",
3
+ "version": "3.0.0",
4
4
  "description": "Plumeria Utils",
5
5
  "author": "Refirst 11",
6
6
  "license": "MIT",
@@ -21,10 +21,8 @@
21
21
  "dist/"
22
22
  ],
23
23
  "dependencies": {
24
- "@swc/core": "1.15.2"
25
- },
26
- "devDependencies": {
27
- "zss-engine": "2.1.0"
24
+ "@swc/core": "1.15.2",
25
+ "zss-engine": "2.1.2"
28
26
  },
29
27
  "publishConfig": {
30
28
  "access": "public",
@@ -1,5 +0,0 @@
1
- import type { CSSProperties, CreateStyleType, CreateTheme, CreateValues } from 'zss-engine';
2
- declare function createCSS<T extends Record<string, CSSProperties>>(rule: CreateStyleType<T>): string;
3
- declare const createVars: <const T extends CreateValues>(rule: T) => Record<string, CreateValues>;
4
- declare const createTheme: <const T extends CreateTheme>(rule: T) => Record<string, Record<string, string | number | Record<string, string | number>>>;
5
- export { createCSS, createVars, createTheme };
package/dist/transform.js DELETED
@@ -1,142 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.createTheme = exports.createVars = void 0;
4
- exports.createCSS = createCSS;
5
- const zss_engine_1 = require("zss-engine");
6
- function compileToSingleCSS(rule) {
7
- const baseSheets = [];
8
- const querySheets = [];
9
- const processedHashes = new Set();
10
- Object.entries(rule).forEach(([key, styleRule]) => {
11
- const flat = {};
12
- const nonFlat = {};
13
- (0, zss_engine_1.splitAtomicAndNested)(styleRule, flat, nonFlat);
14
- const finalFlat = (0, zss_engine_1.overrideLonghand)(flat);
15
- const records = [];
16
- Object.entries(finalFlat).forEach(([prop, value]) => {
17
- if (prop.startsWith('@media') || prop.startsWith('@container')) {
18
- Object.entries(value).forEach(([innerProp, innerValue]) => {
19
- const atomicMap = new Map();
20
- (0, zss_engine_1.processAtomicProps)({ [innerProp]: innerValue }, atomicMap, prop);
21
- const querySheets = [];
22
- const queryHashes = [];
23
- for (const [hash, sheet] of atomicMap) {
24
- querySheets.push(sheet.replace(`.${hash}`, `.${hash}:not(#\\#):not(#\\#)`));
25
- queryHashes.push(hash);
26
- }
27
- if (querySheets.length > 0) {
28
- records.push({
29
- key: prop + innerProp,
30
- hash: queryHashes.join(' '),
31
- sheet: querySheets.join(''),
32
- });
33
- }
34
- });
35
- }
36
- else {
37
- const atomicMap = new Map();
38
- (0, zss_engine_1.processAtomicProps)({ [prop]: value }, atomicMap);
39
- const sheets = [];
40
- const hashes = [];
41
- for (const [hash, sheet] of atomicMap) {
42
- sheets.push(sheet);
43
- hashes.push(hash);
44
- }
45
- if (sheets.length > 0) {
46
- records.push({
47
- key: prop,
48
- hash: hashes.join(' '),
49
- sheet: sheets.join(''),
50
- });
51
- }
52
- }
53
- });
54
- if (Object.keys(nonFlat).length > 0) {
55
- const nonFlatBase = {};
56
- const nonFlatQuery = {};
57
- Object.entries(nonFlat).forEach(([atRule, nestedObj]) => {
58
- if (atRule.startsWith('@media') || atRule.startsWith('@container')) {
59
- nonFlatQuery[atRule] = nestedObj;
60
- }
61
- else {
62
- nonFlatBase[atRule] = nestedObj;
63
- }
64
- });
65
- Object.entries(nonFlatBase).forEach(([selector, style], index) => {
66
- const hashObj = { [key]: { [selector]: style, index } };
67
- const hash = (0, zss_engine_1.genBase36Hash)(hashObj, 1, 7);
68
- const transpileObj = { [key]: { [selector]: style } };
69
- const { styleSheet } = (0, zss_engine_1.transpile)(transpileObj, hash);
70
- records.push({
71
- key: selector + index,
72
- hash: hash,
73
- sheet: styleSheet,
74
- });
75
- });
76
- Object.entries(nonFlatQuery).forEach(([atRule, nestedStyles]) => {
77
- Object.entries(nestedStyles).forEach(([selector, style], index) => {
78
- const hashObj = { [key]: { [atRule]: { [selector]: style, index } } };
79
- const hash = (0, zss_engine_1.genBase36Hash)(hashObj, 1, 7);
80
- const transpileObj = { [key]: { [atRule]: { [selector]: style } } };
81
- const { styleSheet } = (0, zss_engine_1.transpile)(transpileObj, hash);
82
- const finalSheet = styleSheet.replace(`.${hash}`, `.${hash}:not(#\\#):not(#\\#)`);
83
- records.push({
84
- key: atRule + selector + index,
85
- hash: hash,
86
- sheet: finalSheet,
87
- });
88
- });
89
- });
90
- }
91
- records.reverse().forEach(({ hash, sheet }) => {
92
- if (!processedHashes.has(hash)) {
93
- processedHashes.add(hash);
94
- if (sheet.includes('@media') || sheet.includes('@container')) {
95
- querySheets.push(sheet);
96
- }
97
- else {
98
- baseSheets.push(sheet);
99
- }
100
- }
101
- });
102
- });
103
- return [...baseSheets, ...querySheets].filter(Boolean).join('');
104
- }
105
- function createCSS(rule) {
106
- const compiledCSS = compileToSingleCSS(rule);
107
- return compiledCSS;
108
- }
109
- const createVars = (rule) => {
110
- const styles = {
111
- ':root': {},
112
- };
113
- Object.entries(rule).forEach(([key, value]) => {
114
- styles[':root'][`--${key}`] = value;
115
- });
116
- return styles;
117
- };
118
- exports.createVars = createVars;
119
- const createTheme = (rule) => {
120
- const styles = {};
121
- for (const key in rule) {
122
- const varKey = `--${(0, zss_engine_1.camelToKebabCase)(key)}`;
123
- const themeMap = rule[key];
124
- for (const themeKey in themeMap) {
125
- const value = themeMap[themeKey];
126
- const isQuery = themeKey.startsWith('@media') || themeKey.startsWith('@container');
127
- const selector = isQuery || themeKey === 'default'
128
- ? ':root'
129
- : `:root[data-theme="${themeKey}"]`;
130
- const target = (styles[selector] ||= {});
131
- if (isQuery) {
132
- const queryObj = (target[themeKey] ||= {});
133
- queryObj[varKey] = value;
134
- }
135
- else {
136
- target[varKey] = value;
137
- }
138
- }
139
- }
140
- return styles;
141
- };
142
- exports.createTheme = createTheme;