@plumeria/compiler 5.0.0 → 6.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.
Files changed (2) hide show
  1. package/dist/index.js +249 -70
  2. package/package.json +2 -2
package/dist/index.js CHANGED
@@ -11,8 +11,6 @@ const utils_1 = require("@plumeria/utils");
11
11
  function compileCSS(options) {
12
12
  const { include, exclude, cwd = process.cwd() } = options;
13
13
  const allSheets = new Set();
14
- const dependencies = new Set();
15
- (0, utils_1.scanAll)((p) => dependencies.add(p));
16
14
  const processFile = (filePath) => {
17
15
  const source = fs_1.default.readFileSync(filePath, 'utf-8');
18
16
  const extractedSheets = [];
@@ -28,6 +26,7 @@ function compileCSS(options) {
28
26
  console.warn(`Failed to parse ${filePath}:`, err);
29
27
  return [];
30
28
  }
29
+ const scannedTables = (0, utils_1.scanAll)();
31
30
  const localConsts = (0, utils_1.collectLocalConsts)(ast);
32
31
  const resourcePath = filePath;
33
32
  const importMap = {};
@@ -35,38 +34,86 @@ function compileCSS(options) {
35
34
  ImportDeclaration({ node }) {
36
35
  const sourcePath = node.source.value;
37
36
  const actualPath = (0, utils_1.resolveImportPath)(sourcePath, resourcePath);
38
- if (actualPath && fs_1.default.existsSync(actualPath)) {
39
- if (fs_1.default.existsSync(actualPath)) {
40
- node.specifiers.forEach((specifier) => {
41
- if (specifier.type === 'ImportSpecifier') {
42
- const importedName = specifier.imported
43
- ? specifier.imported.value
44
- : specifier.local.value;
45
- const localName = specifier.local.value;
46
- const uniqueKey = `${actualPath}-${importedName}`;
47
- if (utils_1.tables.staticTable[uniqueKey]) {
48
- importMap[localName] = utils_1.tables.staticTable[uniqueKey];
49
- }
50
- if (utils_1.tables.keyframesHashTable[uniqueKey]) {
51
- importMap[localName] = utils_1.tables.keyframesHashTable[uniqueKey];
52
- }
53
- if (utils_1.tables.viewTransitionHashTable[uniqueKey]) {
54
- importMap[localName] =
55
- utils_1.tables.viewTransitionHashTable[uniqueKey];
56
- }
57
- if (utils_1.tables.themeTable[uniqueKey]) {
58
- importMap[localName] = utils_1.tables.themeTable[uniqueKey];
59
- }
37
+ if (actualPath) {
38
+ node.specifiers.forEach((specifier) => {
39
+ if (specifier.type === 'ImportSpecifier') {
40
+ const importedName = specifier.imported
41
+ ? specifier.imported.value
42
+ : specifier.local.value;
43
+ const localName = specifier.local.value;
44
+ const uniqueKey = `${actualPath}-${importedName}`;
45
+ if (scannedTables.staticTable[uniqueKey]) {
46
+ importMap[localName] = scannedTables.staticTable[uniqueKey];
60
47
  }
61
- });
62
- }
48
+ if (scannedTables.keyframesHashTable[uniqueKey]) {
49
+ importMap[localName] =
50
+ scannedTables.keyframesHashTable[uniqueKey];
51
+ }
52
+ if (scannedTables.viewTransitionHashTable[uniqueKey]) {
53
+ importMap[localName] =
54
+ scannedTables.viewTransitionHashTable[uniqueKey];
55
+ }
56
+ if (scannedTables.themeTable[uniqueKey]) {
57
+ importMap[localName] = scannedTables.themeTable[uniqueKey];
58
+ }
59
+ if (scannedTables.createHashTable[uniqueKey]) {
60
+ importMap[localName] = scannedTables.createHashTable[uniqueKey];
61
+ }
62
+ if (scannedTables.variantsHashTable[uniqueKey]) {
63
+ importMap[localName] =
64
+ scannedTables.variantsHashTable[uniqueKey];
65
+ }
66
+ }
67
+ });
63
68
  }
64
69
  },
65
70
  });
66
- const mergedStaticTable = Object.assign(Object.create(utils_1.tables.staticTable), localConsts, importMap);
67
- const mergedKeyframesTable = Object.assign(Object.create(utils_1.tables.keyframesHashTable), importMap);
68
- const mergedViewTransitionTable = Object.assign(Object.create(utils_1.tables.viewTransitionHashTable), importMap);
69
- const mergedThemeTable = Object.assign(Object.create(utils_1.tables.themeTable), importMap);
71
+ const mergedStaticTable = {};
72
+ for (const key of Object.keys(scannedTables.staticTable)) {
73
+ mergedStaticTable[key] = scannedTables.staticTable[key];
74
+ }
75
+ for (const key of Object.keys(localConsts)) {
76
+ mergedStaticTable[key] = localConsts[key];
77
+ }
78
+ for (const key of Object.keys(importMap)) {
79
+ mergedStaticTable[key] = importMap[key];
80
+ }
81
+ const mergedKeyframesTable = {};
82
+ for (const key of Object.keys(scannedTables.keyframesHashTable)) {
83
+ mergedKeyframesTable[key] = scannedTables.keyframesHashTable[key];
84
+ }
85
+ for (const key of Object.keys(importMap)) {
86
+ mergedKeyframesTable[key] = importMap[key];
87
+ }
88
+ const mergedViewTransitionTable = {};
89
+ for (const key of Object.keys(scannedTables.viewTransitionHashTable)) {
90
+ mergedViewTransitionTable[key] =
91
+ scannedTables.viewTransitionHashTable[key];
92
+ }
93
+ for (const key of Object.keys(importMap)) {
94
+ mergedViewTransitionTable[key] = importMap[key];
95
+ }
96
+ const mergedThemeTable = {};
97
+ for (const key of Object.keys(scannedTables.themeTable)) {
98
+ mergedThemeTable[key] = scannedTables.themeTable[key];
99
+ }
100
+ for (const key of Object.keys(importMap)) {
101
+ mergedThemeTable[key] = importMap[key];
102
+ }
103
+ const mergedCreateTable = {};
104
+ for (const key of Object.keys(scannedTables.createHashTable)) {
105
+ mergedCreateTable[key] = scannedTables.createHashTable[key];
106
+ }
107
+ for (const key of Object.keys(importMap)) {
108
+ mergedCreateTable[key] = importMap[key];
109
+ }
110
+ const mergedVariantsTable = {};
111
+ for (const key of Object.keys(scannedTables.variantsHashTable)) {
112
+ mergedVariantsTable[key] = scannedTables.variantsHashTable[key];
113
+ }
114
+ for (const key of Object.keys(importMap)) {
115
+ mergedVariantsTable[key] = importMap[key];
116
+ }
70
117
  const localCreateStyles = {};
71
118
  (0, utils_1.traverse)(ast, {
72
119
  VariableDeclarator({ node }) {
@@ -75,19 +122,33 @@ function compileCSS(options) {
75
122
  utils_1.t.isCallExpression(node.init) &&
76
123
  utils_1.t.isMemberExpression(node.init.callee) &&
77
124
  utils_1.t.isIdentifier(node.init.callee.object, { name: 'css' }) &&
78
- utils_1.t.isIdentifier(node.init.callee.property, { name: 'create' }) &&
125
+ utils_1.t.isIdentifier(node.init.callee.property) &&
79
126
  node.init.arguments.length === 1 &&
80
127
  utils_1.t.isObjectExpression(node.init.arguments[0].expression)) {
81
- const obj = (0, utils_1.objectExpressionToObject)(node.init.arguments[0].expression, mergedStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedThemeTable);
82
- if (obj) {
83
- localCreateStyles[node.id.value] = obj;
84
- Object.entries(obj).forEach(([key, style]) => {
85
- const records = (0, utils_1.getStyleRecords)(key, style, 1);
86
- (0, utils_1.extractOndemandStyles)(style, extractedSheets);
87
- records.forEach((r) => {
88
- extractedSheets.push(r.sheet);
128
+ const resolveVariable = (name) => {
129
+ if (localCreateStyles[name]) {
130
+ return localCreateStyles[name].obj;
131
+ }
132
+ };
133
+ const propName = node.init.callee.property.value;
134
+ if (propName === 'create') {
135
+ const obj = (0, utils_1.objectExpressionToObject)(node.init.arguments[0].expression, mergedStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedThemeTable, mergedCreateTable, mergedVariantsTable, resolveVariable);
136
+ if (obj) {
137
+ localCreateStyles[node.id.value] = { type: 'create', obj };
138
+ Object.entries(obj).forEach(([key, style]) => {
139
+ const records = (0, utils_1.getStyleRecords)(key, style, 1);
140
+ (0, utils_1.extractOndemandStyles)(style, extractedSheets, scannedTables);
141
+ records.forEach((r) => {
142
+ extractedSheets.push(r.sheet);
143
+ });
89
144
  });
90
- });
145
+ }
146
+ }
147
+ else if (propName === 'variants') {
148
+ const obj = (0, utils_1.objectExpressionToObject)(node.init.arguments[0].expression, mergedStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedThemeTable, mergedCreateTable, mergedVariantsTable, resolveVariable);
149
+ if (obj) {
150
+ localCreateStyles[node.id.value] = { type: 'variant', obj };
151
+ }
91
152
  }
92
153
  }
93
154
  },
@@ -100,7 +161,7 @@ function compileCSS(options) {
100
161
  const extractStylesFromExpression = (expr) => {
101
162
  const results = [];
102
163
  if (utils_1.t.isObjectExpression(expr)) {
103
- const obj = (0, utils_1.objectExpressionToObject)(expr, mergedStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedThemeTable);
164
+ const obj = (0, utils_1.objectExpressionToObject)(expr, mergedStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedThemeTable, mergedCreateTable, mergedVariantsTable);
104
165
  if (obj)
105
166
  results.push(obj);
106
167
  }
@@ -110,23 +171,36 @@ function compileCSS(options) {
110
171
  const varName = expr.object.value;
111
172
  const propName = expr.property.value;
112
173
  const styleSet = localCreateStyles[varName];
113
- if (styleSet && styleSet[propName]) {
114
- results.push(styleSet[propName]);
174
+ if (styleSet && styleSet.obj[propName]) {
175
+ results.push(styleSet.obj[propName]);
176
+ }
177
+ else {
178
+ const hash = mergedCreateTable[varName];
179
+ if (hash) {
180
+ const obj = scannedTables.createObjectTable[hash];
181
+ if (obj && obj[propName]) {
182
+ results.push(obj[propName]);
183
+ }
184
+ }
115
185
  }
116
186
  }
117
187
  else if (utils_1.t.isIdentifier(expr.object) &&
118
188
  expr.property.type === 'Computed') {
119
- const varName = expr.object.value;
120
- const styleSet = localCreateStyles[varName];
121
- if (styleSet) {
122
- Object.values(styleSet).forEach((s) => results.push(s));
123
- }
189
+ return [];
124
190
  }
125
191
  }
126
192
  else if (utils_1.t.isIdentifier(expr)) {
127
193
  const obj = localCreateStyles[expr.value];
128
194
  if (obj) {
129
- results.push(obj);
195
+ results.push(obj.obj);
196
+ }
197
+ else {
198
+ const hash = mergedCreateTable[expr.value];
199
+ if (hash) {
200
+ const obj = scannedTables.createObjectTable[hash];
201
+ if (obj)
202
+ results.push(obj);
203
+ }
130
204
  }
131
205
  }
132
206
  else if (utils_1.t.isConditionalExpression(expr)) {
@@ -143,52 +217,157 @@ function compileCSS(options) {
143
217
  return results;
144
218
  };
145
219
  const processStyle = (style) => {
146
- (0, utils_1.extractOndemandStyles)(style, extractedSheets);
220
+ (0, utils_1.extractOndemandStyles)(style, extractedSheets, scannedTables);
147
221
  const hash = (0, zss_engine_1.genBase36Hash)(style, 1, 8);
148
222
  const records = (0, utils_1.getStyleRecords)(hash, style, 1);
149
223
  records.forEach((r) => extractedSheets.push(r.sheet));
150
224
  };
151
225
  if (callee.property.value === 'props') {
152
- const staticStyles = [];
153
- let isAllStatic = true;
154
- args.forEach((arg) => {
226
+ const conditionals = [];
227
+ let groupIdCounter = 0;
228
+ let baseStyle = {};
229
+ const resolveStyleObject = (expr) => {
230
+ const styles = extractStylesFromExpression(expr);
231
+ if (styles.length === 1)
232
+ return styles[0];
233
+ return null;
234
+ };
235
+ for (const arg of args) {
155
236
  const expr = arg.expression;
237
+ if (utils_1.t.isCallExpression(expr) && utils_1.t.isIdentifier(expr.callee)) {
238
+ const varName = expr.callee.value;
239
+ let variantObj;
240
+ if (localCreateStyles[varName] &&
241
+ localCreateStyles[varName].type === 'variant') {
242
+ variantObj = localCreateStyles[varName].obj;
243
+ }
244
+ else if (mergedVariantsTable[varName]) {
245
+ const hash = mergedVariantsTable[varName];
246
+ if (scannedTables.variantsObjectTable[hash]) {
247
+ variantObj = scannedTables.variantsObjectTable[hash];
248
+ }
249
+ }
250
+ if (variantObj) {
251
+ const callArgs = expr.arguments;
252
+ if (callArgs.length === 1 && !callArgs[0].spread) {
253
+ const arg = callArgs[0].expression;
254
+ if (utils_1.t.isObjectExpression(arg)) {
255
+ for (const prop of arg.properties) {
256
+ let groupName;
257
+ let valExpr;
258
+ if (prop.type === 'KeyValueProperty' &&
259
+ prop.key.type === 'Identifier') {
260
+ groupName = prop.key.value;
261
+ valExpr = prop.value;
262
+ }
263
+ else if (prop.type === 'Identifier') {
264
+ groupName = prop.value;
265
+ valExpr = prop;
266
+ }
267
+ if (groupName && valExpr) {
268
+ const groupVariants = variantObj[groupName];
269
+ if (!groupVariants)
270
+ continue;
271
+ const currentGroupId = ++groupIdCounter;
272
+ if (valExpr.type === 'StringLiteral') {
273
+ if (groupVariants[valExpr.value]) {
274
+ baseStyle = (0, utils_1.deepMerge)(baseStyle, groupVariants[valExpr.value]);
275
+ }
276
+ continue;
277
+ }
278
+ Object.entries(groupVariants).forEach(([_optionName, style]) => {
279
+ conditionals.push({
280
+ test: valExpr,
281
+ truthy: style,
282
+ falsy: {},
283
+ groupId: currentGroupId,
284
+ });
285
+ });
286
+ }
287
+ }
288
+ continue;
289
+ }
290
+ if (utils_1.t.isStringLiteral(arg)) {
291
+ if (variantObj[arg.value]) {
292
+ baseStyle = (0, utils_1.deepMerge)(baseStyle, variantObj[arg.value]);
293
+ }
294
+ continue;
295
+ }
296
+ const currentGroupId = ++groupIdCounter;
297
+ Object.entries(variantObj).forEach(([_key, style]) => {
298
+ conditionals.push({
299
+ test: arg,
300
+ truthy: style,
301
+ falsy: {},
302
+ groupId: currentGroupId,
303
+ });
304
+ });
305
+ continue;
306
+ }
307
+ break;
308
+ }
309
+ }
310
+ const staticStyle = resolveStyleObject(expr);
311
+ if (staticStyle) {
312
+ baseStyle = (0, utils_1.deepMerge)(baseStyle, staticStyle);
313
+ continue;
314
+ }
315
+ else if (expr.type === 'ConditionalExpression') {
316
+ const truthyStyle = resolveStyleObject(expr.consequent);
317
+ const falsyStyle = resolveStyleObject(expr.alternate);
318
+ if (truthyStyle !== null && falsyStyle !== null) {
319
+ conditionals.push({
320
+ test: expr.test,
321
+ truthy: truthyStyle,
322
+ falsy: falsyStyle,
323
+ });
324
+ continue;
325
+ }
326
+ }
156
327
  const styles = extractStylesFromExpression(expr);
157
- if (styles.length === 0) {
158
- isAllStatic = false;
328
+ if (styles.length > 0) {
329
+ styles.forEach(processStyle);
330
+ continue;
159
331
  }
160
- staticStyles.push(...styles);
161
- });
162
- if (isAllStatic && staticStyles.length > 0) {
163
- const merged = staticStyles.reduce((acc, style) => (0, utils_1.deepMerge)(acc, style), {});
164
- processStyle(merged);
165
332
  }
166
- else {
167
- staticStyles.forEach((s) => processStyle(s));
333
+ if (Object.keys(baseStyle).length > 0) {
334
+ processStyle(baseStyle);
335
+ }
336
+ if (conditionals.length > 0) {
337
+ const allStyles = [];
338
+ conditionals.forEach((cond) => {
339
+ if (Object.keys(cond.truthy).length > 0) {
340
+ allStyles.push(cond.truthy);
341
+ }
342
+ if (Object.keys(cond.falsy).length > 0) {
343
+ allStyles.push(cond.falsy);
344
+ }
345
+ });
346
+ allStyles.forEach(processStyle);
168
347
  }
169
348
  }
170
349
  else if (callee.property.value === 'keyframes' &&
171
350
  args.length > 0 &&
172
351
  utils_1.t.isObjectExpression(args[0].expression)) {
173
- const obj = (0, utils_1.objectExpressionToObject)(args[0].expression, mergedStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedThemeTable);
352
+ const obj = (0, utils_1.objectExpressionToObject)(args[0].expression, mergedStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedThemeTable, mergedCreateTable, mergedVariantsTable);
174
353
  const hash = (0, zss_engine_1.genBase36Hash)(obj, 1, 8);
175
- utils_1.tables.keyframesObjectTable[hash] = obj;
354
+ scannedTables.keyframesObjectTable[hash] = obj;
176
355
  }
177
356
  else if (callee.property.value === 'viewTransition' &&
178
357
  args.length > 0 &&
179
358
  utils_1.t.isObjectExpression(args[0].expression)) {
180
- const obj = (0, utils_1.objectExpressionToObject)(args[0].expression, mergedStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedThemeTable);
359
+ const obj = (0, utils_1.objectExpressionToObject)(args[0].expression, mergedStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedThemeTable, mergedCreateTable, mergedVariantsTable);
181
360
  const hash = (0, zss_engine_1.genBase36Hash)(obj, 1, 8);
182
- utils_1.tables.viewTransitionObjectTable[hash] = obj;
183
- (0, utils_1.extractOndemandStyles)(obj, extractedSheets);
184
- (0, utils_1.extractOndemandStyles)({ vt: `vt-${hash}` }, extractedSheets);
361
+ scannedTables.viewTransitionObjectTable[hash] = obj;
362
+ (0, utils_1.extractOndemandStyles)(obj, extractedSheets, scannedTables);
363
+ (0, utils_1.extractOndemandStyles)({ vt: `vt-${hash}` }, extractedSheets, scannedTables);
185
364
  }
186
365
  else if (callee.property.value === 'createTheme' &&
187
366
  args.length > 0 &&
188
367
  utils_1.t.isObjectExpression(args[0].expression)) {
189
- const obj = (0, utils_1.objectExpressionToObject)(args[0].expression, mergedStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedThemeTable);
368
+ const obj = (0, utils_1.objectExpressionToObject)(args[0].expression, mergedStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedThemeTable, mergedCreateTable, mergedVariantsTable);
190
369
  const themeHash = (0, zss_engine_1.genBase36Hash)(obj, 1, 8);
191
- utils_1.tables.createThemeObjectTable[themeHash] = obj;
370
+ scannedTables.createThemeObjectTable[themeHash] = obj;
192
371
  }
193
372
  }
194
373
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@plumeria/compiler",
3
- "version": "5.0.0",
3
+ "version": "6.0.0",
4
4
  "description": "Plumeria swc based compiler for statically extracting css",
5
5
  "author": "Refirst 11",
6
6
  "license": "MIT",
@@ -21,7 +21,7 @@
21
21
  "dist/"
22
22
  ],
23
23
  "dependencies": {
24
- "@plumeria/utils": "^5.0.0"
24
+ "@plumeria/utils": "^6.0.0"
25
25
  },
26
26
  "devDependencies": {
27
27
  "@swc/core": "1.15.8",