@plumeria/compiler 5.0.1 → 6.0.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 (2) hide show
  1. package/dist/index.js +290 -71
  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)) {
@@ -140,55 +214,200 @@ function compileCSS(options) {
140
214
  results.push(...extractStylesFromExpression(expr.left));
141
215
  results.push(...extractStylesFromExpression(expr.right));
142
216
  }
217
+ else if (expr.type === 'ParenthesisExpression') {
218
+ results.push(...extractStylesFromExpression(expr.expression));
219
+ }
143
220
  return results;
144
221
  };
145
222
  const processStyle = (style) => {
146
- (0, utils_1.extractOndemandStyles)(style, extractedSheets);
223
+ (0, utils_1.extractOndemandStyles)(style, extractedSheets, scannedTables);
147
224
  const hash = (0, zss_engine_1.genBase36Hash)(style, 1, 8);
148
225
  const records = (0, utils_1.getStyleRecords)(hash, style, 1);
149
226
  records.forEach((r) => extractedSheets.push(r.sheet));
150
227
  };
151
228
  if (callee.property.value === 'props') {
152
- const staticStyles = [];
153
- let isAllStatic = true;
154
- args.forEach((arg) => {
229
+ const conditionals = [];
230
+ let groupIdCounter = 0;
231
+ let baseStyle = {};
232
+ const resolveStyleObject = (expr) => {
233
+ const styles = extractStylesFromExpression(expr);
234
+ if (styles.length === 1)
235
+ return styles[0];
236
+ return null;
237
+ };
238
+ for (const arg of args) {
155
239
  const expr = arg.expression;
240
+ if (utils_1.t.isCallExpression(expr) && utils_1.t.isIdentifier(expr.callee)) {
241
+ const varName = expr.callee.value;
242
+ let variantObj;
243
+ if (localCreateStyles[varName] &&
244
+ localCreateStyles[varName].type === 'variant') {
245
+ variantObj = localCreateStyles[varName].obj;
246
+ }
247
+ else if (mergedVariantsTable[varName]) {
248
+ const hash = mergedVariantsTable[varName];
249
+ if (scannedTables.variantsObjectTable[hash]) {
250
+ variantObj = scannedTables.variantsObjectTable[hash];
251
+ }
252
+ }
253
+ if (variantObj) {
254
+ const callArgs = expr.arguments;
255
+ if (callArgs.length === 1 && !callArgs[0].spread) {
256
+ const arg = callArgs[0].expression;
257
+ if (utils_1.t.isObjectExpression(arg)) {
258
+ for (const prop of arg.properties) {
259
+ let groupName;
260
+ let valExpr;
261
+ if (prop.type === 'KeyValueProperty' &&
262
+ prop.key.type === 'Identifier') {
263
+ groupName = prop.key.value;
264
+ valExpr = prop.value;
265
+ }
266
+ else if (prop.type === 'Identifier') {
267
+ groupName = prop.value;
268
+ valExpr = prop;
269
+ }
270
+ if (groupName && valExpr) {
271
+ const groupVariants = variantObj[groupName];
272
+ if (!groupVariants)
273
+ continue;
274
+ const currentGroupId = ++groupIdCounter;
275
+ if (valExpr.type === 'StringLiteral') {
276
+ if (groupVariants[valExpr.value]) {
277
+ baseStyle = (0, utils_1.deepMerge)(baseStyle, groupVariants[valExpr.value]);
278
+ }
279
+ continue;
280
+ }
281
+ Object.entries(groupVariants).forEach(([_optionName, style]) => {
282
+ conditionals.push({
283
+ test: valExpr,
284
+ truthy: style,
285
+ falsy: {},
286
+ groupId: currentGroupId,
287
+ });
288
+ });
289
+ }
290
+ }
291
+ continue;
292
+ }
293
+ if (utils_1.t.isStringLiteral(arg)) {
294
+ if (variantObj[arg.value]) {
295
+ baseStyle = (0, utils_1.deepMerge)(baseStyle, variantObj[arg.value]);
296
+ }
297
+ continue;
298
+ }
299
+ const currentGroupId = ++groupIdCounter;
300
+ Object.entries(variantObj).forEach(([_key, style]) => {
301
+ conditionals.push({
302
+ test: arg,
303
+ truthy: style,
304
+ falsy: {},
305
+ groupId: currentGroupId,
306
+ });
307
+ });
308
+ continue;
309
+ }
310
+ break;
311
+ }
312
+ }
313
+ if (utils_1.t.isBinaryExpression(expr) && expr.operator === '&&') {
314
+ const truthyStyle = resolveStyleObject(expr.right);
315
+ if (truthyStyle !== null) {
316
+ conditionals.push({
317
+ test: expr.left,
318
+ truthy: truthyStyle,
319
+ falsy: {},
320
+ });
321
+ continue;
322
+ }
323
+ }
324
+ else if (expr.type === 'ParenthesisExpression') {
325
+ const inner = expr.expression;
326
+ const innerStatic = resolveStyleObject(inner);
327
+ if (innerStatic) {
328
+ baseStyle = (0, utils_1.deepMerge)(baseStyle, innerStatic);
329
+ continue;
330
+ }
331
+ }
332
+ const staticStyle = resolveStyleObject(expr);
333
+ if (staticStyle) {
334
+ baseStyle = (0, utils_1.deepMerge)(baseStyle, staticStyle);
335
+ continue;
336
+ }
337
+ else if (expr.type === 'ConditionalExpression') {
338
+ const truthyStyle = resolveStyleObject(expr.consequent);
339
+ const falsyStyle = resolveStyleObject(expr.alternate);
340
+ if (truthyStyle !== null && falsyStyle !== null) {
341
+ conditionals.push({
342
+ test: expr.test,
343
+ truthy: truthyStyle,
344
+ falsy: falsyStyle,
345
+ });
346
+ continue;
347
+ }
348
+ }
156
349
  const styles = extractStylesFromExpression(expr);
157
- if (styles.length === 0) {
158
- isAllStatic = false;
159
- }
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);
350
+ if (styles.length > 0) {
351
+ styles.forEach(processStyle);
352
+ continue;
353
+ }
354
+ }
355
+ if (Object.keys(baseStyle).length > 0 &&
356
+ conditionals.length === 0) {
357
+ processStyle(baseStyle);
165
358
  }
166
- else {
167
- staticStyles.forEach((s) => processStyle(s));
359
+ else if (conditionals.length > 0) {
360
+ const combinations = 1 << conditionals.length;
361
+ for (let i = 0; i < combinations; i++) {
362
+ let currentStyle = { ...baseStyle };
363
+ const seenGroups = new Set();
364
+ let impossible = false;
365
+ for (let j = 0; j < conditionals.length; j++) {
366
+ if ((i >> j) & 1) {
367
+ if (conditionals[j].groupId !== undefined) {
368
+ if (seenGroups.has(conditionals[j].groupId)) {
369
+ impossible = true;
370
+ break;
371
+ }
372
+ seenGroups.add(conditionals[j].groupId);
373
+ }
374
+ currentStyle = (0, utils_1.deepMerge)(currentStyle, conditionals[j].truthy);
375
+ }
376
+ else {
377
+ currentStyle = (0, utils_1.deepMerge)(currentStyle, conditionals[j].falsy);
378
+ }
379
+ }
380
+ if (impossible) {
381
+ continue;
382
+ }
383
+ if (Object.keys(currentStyle).length > 0) {
384
+ processStyle(currentStyle);
385
+ }
386
+ }
168
387
  }
169
388
  }
170
389
  else if (callee.property.value === 'keyframes' &&
171
390
  args.length > 0 &&
172
391
  utils_1.t.isObjectExpression(args[0].expression)) {
173
- const obj = (0, utils_1.objectExpressionToObject)(args[0].expression, mergedStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedThemeTable);
392
+ const obj = (0, utils_1.objectExpressionToObject)(args[0].expression, mergedStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedThemeTable, mergedCreateTable, mergedVariantsTable);
174
393
  const hash = (0, zss_engine_1.genBase36Hash)(obj, 1, 8);
175
- utils_1.tables.keyframesObjectTable[hash] = obj;
394
+ scannedTables.keyframesObjectTable[hash] = obj;
176
395
  }
177
396
  else if (callee.property.value === 'viewTransition' &&
178
397
  args.length > 0 &&
179
398
  utils_1.t.isObjectExpression(args[0].expression)) {
180
- const obj = (0, utils_1.objectExpressionToObject)(args[0].expression, mergedStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedThemeTable);
399
+ const obj = (0, utils_1.objectExpressionToObject)(args[0].expression, mergedStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedThemeTable, mergedCreateTable, mergedVariantsTable);
181
400
  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);
401
+ scannedTables.viewTransitionObjectTable[hash] = obj;
402
+ (0, utils_1.extractOndemandStyles)(obj, extractedSheets, scannedTables);
403
+ (0, utils_1.extractOndemandStyles)({ vt: `vt-${hash}` }, extractedSheets, scannedTables);
185
404
  }
186
405
  else if (callee.property.value === 'createTheme' &&
187
406
  args.length > 0 &&
188
407
  utils_1.t.isObjectExpression(args[0].expression)) {
189
- const obj = (0, utils_1.objectExpressionToObject)(args[0].expression, mergedStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedThemeTable);
408
+ const obj = (0, utils_1.objectExpressionToObject)(args[0].expression, mergedStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedThemeTable, mergedCreateTable, mergedVariantsTable);
190
409
  const themeHash = (0, zss_engine_1.genBase36Hash)(obj, 1, 8);
191
- utils_1.tables.createThemeObjectTable[themeHash] = obj;
410
+ scannedTables.createThemeObjectTable[themeHash] = obj;
192
411
  }
193
412
  }
194
413
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@plumeria/compiler",
3
- "version": "5.0.1",
3
+ "version": "6.0.1",
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.1"
24
+ "@plumeria/utils": "^6.0.1"
25
25
  },
26
26
  "devDependencies": {
27
27
  "@swc/core": "1.15.8",