@plumeria/compiler 18.3.17 → 18.4.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 CHANGED
@@ -1,4 +1,5 @@
1
- interface CompilerOptions {
1
+ import type { PropertyPolicyOptions } from '@plumeria/utils';
2
+ interface CompilerOptions extends PropertyPolicyOptions {
2
3
  include: string[];
3
4
  exclude: string[];
4
5
  cwd?: string;
package/dist/index.js CHANGED
@@ -103,6 +103,46 @@ const namedParamsOf = (params, defaults) => {
103
103
  }
104
104
  return named.length > 0 ? named : undefined;
105
105
  };
106
+ const isUnitlessProp = (prop) => zss_engine_1.exceptionCamelCase.includes(prop) || prop.startsWith('--');
107
+ const collectVarProps = (style, cssVar, props) => {
108
+ const reference = `var(${cssVar})`;
109
+ for (const [prop, value] of Object.entries(style)) {
110
+ if (typeof value === 'string' && value.includes(reference))
111
+ props.push(prop);
112
+ else if (value !== null && typeof value === 'object')
113
+ collectVarProps(value, cssVar, props);
114
+ }
115
+ };
116
+ const retargetVar = (style, cssVar, next, unitless) => {
117
+ const reference = `var(${cssVar})`;
118
+ for (const [prop, value] of Object.entries(style)) {
119
+ if (typeof value === 'string' && value.includes(reference)) {
120
+ if (isUnitlessProp(prop) === unitless)
121
+ style[prop] = value
122
+ .split(reference)
123
+ .join(`var(${next})`);
124
+ }
125
+ else if (value !== null && typeof value === 'object') {
126
+ retargetVar(value, cssVar, next, unitless);
127
+ }
128
+ }
129
+ };
130
+ const splitVarByUnit = (style, cssVar) => {
131
+ const props = [];
132
+ collectVarProps(style, cssVar, props);
133
+ if (props.length === 0)
134
+ return [];
135
+ const lead = props[0];
136
+ const split = props.find((prop) => isUnitlessProp(prop) !== isUnitlessProp(lead));
137
+ if (!split)
138
+ return [{ cssVar, prop: lead }];
139
+ const splitVar = `${cssVar}-${(0, zss_engine_1.camelToKebabCase)(split).replace(/^-+/, '')}`;
140
+ retargetVar(style, cssVar, splitVar, isUnitlessProp(split));
141
+ return [
142
+ { cssVar, prop: lead },
143
+ { cssVar: splitVar, prop: split },
144
+ ];
145
+ };
106
146
  const applyVarFallback = (style, cssVar, literal) => {
107
147
  const reference = `var(${cssVar})`;
108
148
  for (const [prop, value] of Object.entries(style)) {
@@ -256,6 +296,7 @@ function extractStylesFromExpression(expression, ctx) {
256
296
  }
257
297
  function compileCSS(options) {
258
298
  const { include, exclude, cwd = process.cwd(), styleProp = utils_1.DEFAULT_STYLE_PROP, } = options;
299
+ const propertyPolicy = (0, utils_1.resolvePropertyPolicy)(options);
259
300
  const allSheets = new Set();
260
301
  const files = rs.globSync(include, {
261
302
  cwd,
@@ -272,6 +313,7 @@ function compileCSS(options) {
272
313
  tsx: true,
273
314
  target: 'es2022',
274
315
  });
316
+ (0, utils_1.assertPropertyPolicy)(ast, propertyPolicy, resourcePath);
275
317
  const leadingLen = (0, utils_2.getLeadingCommentLength)(source);
276
318
  const sourceBuffer = Buffer.from(source, 'utf-8');
277
319
  const leadingBytes = Buffer.byteLength(source.slice(0, leadingLen), 'utf-8');
@@ -700,16 +742,19 @@ function compileCSS(options) {
700
742
  const style = (0, utils_1.objectExpressionToObject)(func.body, tempStaticTable, ctx.mergedKeyframesTable, ctx.mergedViewTransitionTable, ctx.mergedCreateThemeHashTable, ctx.scannedTables.createThemeObjectTable, ctx.mergedCreateTable, ctx.mergedCreateStaticHashTable, ctx.scannedTables.createStaticObjectTable, ctx.mergedVariantsTable);
701
743
  if (!style)
702
744
  return null;
703
- withDefault.forEach(([param, expr]) => {
745
+ const varGroups = new Map();
746
+ varParams.forEach((param) => {
704
747
  const cssVar = cssVars[param];
705
- if (!cssVar)
706
- return;
748
+ if (cssVar)
749
+ varGroups.set(param, splitVarByUnit(style, cssVar));
750
+ });
751
+ withDefault.forEach(([param, expr]) => {
707
752
  const literal = expr.type === 'StringLiteral' || expr.type === 'NumericLiteral'
708
753
  ? expr.value
709
754
  : undefined;
710
755
  if (literal === undefined)
711
756
  return;
712
- applyVarFallback(style, cssVar, literal);
757
+ (varGroups.get(param) ?? []).forEach(({ cssVar }) => applyVarFallback(style, cssVar, literal));
713
758
  });
714
759
  return style;
715
760
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@plumeria/compiler",
3
- "version": "18.3.17",
3
+ "version": "18.4.0",
4
4
  "description": "Plumeria swc based compiler for statically extracting css",
5
5
  "author": "Refirst 11",
6
6
  "license": "MIT",
@@ -21,12 +21,12 @@
21
21
  "dist/"
22
22
  ],
23
23
  "dependencies": {
24
- "@plumeria/utils": "^18.3.17"
24
+ "@plumeria/utils": "^18.4.0"
25
25
  },
26
26
  "devDependencies": {
27
27
  "@rust-gear/glob": "1.1.1",
28
28
  "@swc/core": "1.15.47",
29
- "zss-engine": "2.7.0"
29
+ "zss-engine": "2.8.0"
30
30
  },
31
31
  "publishConfig": {
32
32
  "access": "public",