@plumeria/vite-plugin 6.3.0 → 6.3.2

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 +86 -42
  2. package/package.json +2 -2
package/dist/index.js CHANGED
@@ -2,7 +2,7 @@ import { createFilter } from 'vite';
2
2
  import { parseSync } from '@swc/core';
3
3
  import path from 'path';
4
4
  import { genBase36Hash } from 'zss-engine';
5
- import { traverse, getStyleRecords, collectLocalConsts, objectExpressionToObject, t, extractOndemandStyles, deepMerge, scanAll, resolveImportPath, } from '@plumeria/utils';
5
+ import { traverse, getStyleRecords, collectLocalConsts, objectExpressionToObject, t, extractOndemandStyles, deepMerge, scanAll, resolveImportPath, optimizer, } from '@plumeria/utils';
6
6
  const TARGET_EXTENSIONS = ['ts', 'tsx', 'js', 'jsx'];
7
7
  const EXTENSION_PATTERN = /\.(ts|tsx|js|jsx)$/;
8
8
  export function plumeria(options = {}) {
@@ -20,6 +20,7 @@ export function plumeria(options = {}) {
20
20
  configResolved(resolvedConfig) {
21
21
  isDev = resolvedConfig.command === 'serve';
22
22
  config = resolvedConfig;
23
+ config.build.cssCodeSplit = false;
23
24
  },
24
25
  configureServer(_server) {
25
26
  if (!isDev)
@@ -49,7 +50,7 @@ export function plumeria(options = {}) {
49
50
  .filter((m) => !!m)
50
51
  .concat(ctx.modules);
51
52
  },
52
- transform(source, id) {
53
+ async transform(source, id) {
53
54
  if (id.includes('node_modules'))
54
55
  return null;
55
56
  if (id.includes('?'))
@@ -268,7 +269,7 @@ export function plumeria(options = {}) {
268
269
  if (obj) {
269
270
  const hashMap = {};
270
271
  Object.entries(obj).forEach(([key, style]) => {
271
- const records = getStyleRecords(key, style, 2);
272
+ const records = getStyleRecords(key, style);
272
273
  extractOndemandStyles(style, extractedSheets, scannedTables);
273
274
  records.forEach((r) => {
274
275
  addSheet(r.sheet);
@@ -416,6 +417,7 @@ export function plumeria(options = {}) {
416
417
  const obj = objectExpressionToObject(args[0].expression, mergedStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedCreateThemeHashTable, scannedTables.createThemeObjectTable, mergedCreateTable, mergedCreateStaticHashTable, scannedTables.createStaticObjectTable, mergedVariantsTable);
417
418
  const hash = genBase36Hash(obj, 1, 8);
418
419
  scannedTables.keyframesObjectTable[hash] = obj;
420
+ extractOndemandStyles({ kf: `kf-${hash}` }, extractedSheets, scannedTables);
419
421
  replacements.push({
420
422
  start: node.span.start - ast.span.start,
421
423
  end: node.span.end - ast.span.start,
@@ -461,7 +463,7 @@ export function plumeria(options = {}) {
461
463
  scannedTables.createObjectTable[hash] = obj;
462
464
  Object.entries(obj).forEach(([key, style]) => {
463
465
  if (typeof style === 'object' && style !== null) {
464
- const records = getStyleRecords(key, style, 2);
466
+ const records = getStyleRecords(key, style);
465
467
  extractOndemandStyles(style, extractedSheets, scannedTables);
466
468
  records.forEach((r) => addSheet(r.sheet));
467
469
  }
@@ -808,7 +810,7 @@ export function plumeria(options = {}) {
808
810
  if (conditionals.length === 0) {
809
811
  extractOndemandStyles(baseStyle, extractedSheets, scannedTables);
810
812
  const hash = genBase36Hash(baseStyle, 1, 8);
811
- const records = getStyleRecords(hash, baseStyle, 2);
813
+ const records = getStyleRecords(hash, baseStyle);
812
814
  records.forEach((r) => addSheet(r.sheet));
813
815
  const className = records
814
816
  .map((r) => r.hash)
@@ -821,55 +823,97 @@ export function plumeria(options = {}) {
821
823
  }
822
824
  else {
823
825
  const table = {};
824
- const combinations = 1 << conditionals.length;
825
- for (let i = 0; i < combinations; i++) {
826
+ const groups = {};
827
+ let strayIdCounter = groupIdCounter + 1;
828
+ conditionals.forEach((c) => {
829
+ const gid = c.groupId !== undefined ? c.groupId : strayIdCounter++;
830
+ if (!groups[gid]) {
831
+ groups[gid] = [];
832
+ }
833
+ groups[gid].push(c);
834
+ });
835
+ const sortedGroupIds = Object.keys(groups)
836
+ .map(Number)
837
+ .sort((a, b) => a - b);
838
+ let totalCombinations = 1;
839
+ const groupMeta = sortedGroupIds.map((gid) => {
840
+ const options = groups[gid];
841
+ const isVariantGroup = options[0].groupId !== undefined;
842
+ const size = isVariantGroup ? options.length : 2;
843
+ const stride = totalCombinations;
844
+ totalCombinations *= size;
845
+ return { gid, options, size, stride, isVariantGroup };
846
+ });
847
+ for (let i = 0; i < totalCombinations; i++) {
826
848
  let currentStyle = { ...baseStyle };
827
- const seenGroups = new Set();
828
- let impossible = false;
829
- for (let j = 0; j < conditionals.length; j++) {
830
- if ((i >> j) & 1) {
831
- if (conditionals[j].groupId !== undefined) {
832
- if (seenGroups.has(conditionals[j].groupId)) {
833
- impossible = true;
834
- break;
835
- }
836
- seenGroups.add(conditionals[j].groupId);
837
- }
838
- currentStyle = deepMerge(currentStyle, conditionals[j].truthy);
849
+ for (const meta of groupMeta) {
850
+ const localIndex = Math.floor(i / meta.stride) % meta.size;
851
+ if (meta.isVariantGroup) {
852
+ currentStyle = deepMerge(currentStyle, meta.options[localIndex].truthy);
839
853
  }
840
854
  else {
841
- currentStyle = deepMerge(currentStyle, conditionals[j].falsy);
855
+ const cond = meta.options[0];
856
+ if (localIndex === 1) {
857
+ currentStyle = deepMerge(currentStyle, cond.truthy);
858
+ }
859
+ else {
860
+ currentStyle = deepMerge(currentStyle, cond.falsy);
861
+ }
842
862
  }
843
863
  }
844
- if (impossible) {
845
- table[i] = '';
846
- continue;
847
- }
848
864
  extractOndemandStyles(currentStyle, extractedSheets, scannedTables);
849
865
  const hash = genBase36Hash(currentStyle, 1, 8);
850
- const records = getStyleRecords(hash, currentStyle, 2);
866
+ const records = getStyleRecords(hash, currentStyle);
851
867
  records.forEach((r) => extractedSheets.push(r.sheet));
852
868
  const className = records
853
869
  .map((r) => r.hash)
854
870
  .join(' ');
855
871
  table[i] = className;
856
872
  }
857
- let indexExpr = '';
858
- if (conditionals.length === 0) {
859
- indexExpr = '0';
860
- }
861
- else {
862
- const parts = conditionals.map((c, idx) => {
863
- if (c.testString) {
864
- return `(!!(${c.testString}) << ${idx})`;
873
+ const indexParts = [];
874
+ for (const meta of groupMeta) {
875
+ if (meta.isVariantGroup) {
876
+ const exprs = meta.options
877
+ .map((opt, idx) => {
878
+ if (idx === 0)
879
+ return null;
880
+ let testStr = opt.testString;
881
+ if (!testStr && opt.test) {
882
+ const start = opt.test.span.start - ast.span.start;
883
+ const end = opt.test.span.end - ast.span.start;
884
+ testStr = source.substring(start, end);
885
+ }
886
+ return `(!!(${testStr}) * ${idx})`;
887
+ })
888
+ .filter(Boolean);
889
+ if (exprs.length > 0) {
890
+ const groupSum = `(${exprs.join(' + ')})`;
891
+ if (meta.stride > 1) {
892
+ indexParts.push(`(${groupSum} * ${meta.stride})`);
893
+ }
894
+ else {
895
+ indexParts.push(groupSum);
896
+ }
865
897
  }
866
- const start = c.test.span.start - ast.span.start;
867
- const end = c.test.span.end - ast.span.start;
868
- const testStr = source.substring(start, end);
869
- return `(!!(${testStr}) << ${idx})`;
870
- });
871
- indexExpr = parts.join(' | ');
898
+ }
899
+ else {
900
+ const cond = meta.options[0];
901
+ let testStr = cond.testString;
902
+ if (!testStr && cond.test) {
903
+ const start = cond.test.span.start - ast.span.start;
904
+ const end = cond.test.span.end - ast.span.start;
905
+ testStr = source.substring(start, end);
906
+ }
907
+ const expr = `(!!(${testStr}))`;
908
+ if (meta.stride > 1) {
909
+ indexParts.push(`(${expr} * ${meta.stride})`);
910
+ }
911
+ else {
912
+ indexParts.push(`(${expr} * 1)`);
913
+ }
914
+ }
872
915
  }
916
+ const indexExpr = indexParts.join(' + ') || '0';
873
917
  const tableStr = JSON.stringify(table);
874
918
  const replacement = `${tableStr}[${indexExpr}]`;
875
919
  replacements.push({
@@ -883,7 +927,7 @@ export function plumeria(options = {}) {
883
927
  },
884
928
  });
885
929
  Object.values(localCreateStyles).forEach((info) => {
886
- if (info.type === 'constant' || info.type === 'variant') {
930
+ if (info.type === 'constant') {
887
931
  return;
888
932
  }
889
933
  if (info.isExported) {
@@ -901,6 +945,7 @@ export function plumeria(options = {}) {
901
945
  });
902
946
  }
903
947
  });
948
+ const optInCSS = await optimizer(extractedSheets.join(''));
904
949
  const buffer = Buffer.from(source);
905
950
  let offset = 0;
906
951
  const parts = [];
@@ -916,14 +961,13 @@ export function plumeria(options = {}) {
916
961
  parts.push(buffer.subarray(offset));
917
962
  const transformedSource = Buffer.concat(parts).toString();
918
963
  if (extractedSheets.length > 0) {
919
- const generatedCSS = extractedSheets.join('');
920
964
  const baseId = id.replace(EXTENSION_PATTERN, '');
921
965
  const cssFilename = `${baseId}.zero.css`;
922
966
  const cssRelativePath = path
923
967
  .relative(config.root, cssFilename)
924
968
  .replace(/\\/g, '/');
925
969
  const cssId = `/${cssRelativePath}`;
926
- cssLookup.set(cssFilename, generatedCSS);
970
+ cssLookup.set(cssFilename, optInCSS);
927
971
  cssFileLookup.set(cssId, cssFilename);
928
972
  const targetIndex = targets.findIndex((t) => t.id === id);
929
973
  if (targetIndex !== -1) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@plumeria/vite-plugin",
3
- "version": "6.3.0",
3
+ "version": "6.3.2",
4
4
  "type": "module",
5
5
  "description": "Plumeria Vite plugin",
6
6
  "author": "Refirst 11",
@@ -22,7 +22,7 @@
22
22
  "dist/"
23
23
  ],
24
24
  "dependencies": {
25
- "@plumeria/utils": "^6.3.0"
25
+ "@plumeria/utils": "^6.3.2"
26
26
  },
27
27
  "devDependencies": {
28
28
  "@swc/core": "1.15.8",