tokenami 0.0.90 → 0.0.91
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/cli.js +315 -243
- package/package.json +4 -4
package/dist/cli.js
CHANGED
|
@@ -665,7 +665,6 @@ function debug(str) {
|
|
|
665
665
|
var UNUSED_LAYERS_REGEX = /[\n\s]*@layer[^;{]+;/g;
|
|
666
666
|
var DEFAULT_SELECTOR = "[style]";
|
|
667
667
|
var CUSTOM_PROP_PREFIX = "--_";
|
|
668
|
-
var SELECTOR_TAG = "<selector>";
|
|
669
668
|
var LAYERS = {
|
|
670
669
|
BASE: "tk",
|
|
671
670
|
LOGICAL: "tkl",
|
|
@@ -692,114 +691,257 @@ function generate(params) {
|
|
|
692
691
|
}
|
|
693
692
|
function createSheet(params) {
|
|
694
693
|
if (!params.tokens.properties.length) return "";
|
|
695
|
-
const
|
|
696
|
-
const tokenValues = params.tokens.values;
|
|
694
|
+
const sheet = new Sheet(params.tokens.values, params.config);
|
|
697
695
|
const composeBlocks = parseComposeBlocks(params.tokens.composeBlocks, params.config);
|
|
698
|
-
const propertyConfigsByCSSProperty = getPropertyConfigs(
|
|
699
|
-
|
|
700
|
-
const styles = {
|
|
701
|
-
reset: /* @__PURE__ */ new Set(),
|
|
702
|
-
atomic: {},
|
|
703
|
-
selectors: {},
|
|
704
|
-
components: {},
|
|
705
|
-
toggles: {}
|
|
706
|
-
};
|
|
707
|
-
for (const [cssProperty, configs] of propertyConfigsByCSSProperty) {
|
|
696
|
+
const propertyConfigsByCSSProperty = getPropertyConfigs(params.tokens.properties, params.config);
|
|
697
|
+
for (const [cssProperty, propertyConfigs] of propertyConfigsByCSSProperty) {
|
|
708
698
|
const isInheritable = inheritedProperties.has(cssProperty);
|
|
709
|
-
const elementConfigs =
|
|
710
|
-
const selectorConfig =
|
|
699
|
+
const elementConfigs = propertyConfigs.filter((c) => {
|
|
700
|
+
const selectorConfig = getSelectorFromConfig(c.selector, params.config);
|
|
711
701
|
return !selectorConfig.some(isPseudoElementSelector);
|
|
712
702
|
});
|
|
713
|
-
for (const prop of
|
|
714
|
-
const
|
|
715
|
-
const
|
|
716
|
-
const
|
|
717
|
-
const
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
703
|
+
for (const prop of propertyConfigs) {
|
|
704
|
+
const baseProperty = prop.isCustom ? CUSTOM_PROP_PREFIX + cssProperty : cssProperty;
|
|
705
|
+
const gridProperty3 = hashVariantProperty("grid", prop.tokenProperty);
|
|
706
|
+
const gridToggleValue = createGridToggleValue(prop.tokenProperty);
|
|
707
|
+
const selectorConfig = getSelectorFromConfig(prop.selector, params.config);
|
|
708
|
+
const parsedSelectors = getPropertySelectors(composeBlocks, prop, selectorConfig);
|
|
709
|
+
const configs = selectorConfig.some(isPseudoElementSelector) ? propertyConfigs.filter((c) => c.selector === prop.selector) : elementConfigs;
|
|
710
|
+
sheet.addThemeTokenSelectors(parsedSelectors.elements);
|
|
711
|
+
if (!isInheritable && !selectorConfig.some(isChildSelector)) {
|
|
712
|
+
sheet.addReset(prop.tokenProperty);
|
|
713
|
+
}
|
|
714
|
+
if (prop.variant) {
|
|
715
|
+
const responsiveConfig = getResponsiveSelectorFromConfig(prop.responsive, params.config);
|
|
725
716
|
const hashedProperty = hashVariantProperty(prop.variant, cssProperty);
|
|
726
|
-
const basePropertyValue = getBasePropertyValue(prop.tokenProperty, prop, false);
|
|
727
717
|
const toggleProperty = Tokenami2.parsedTokenProperty(prop.variant);
|
|
728
|
-
const
|
|
729
|
-
const
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
const
|
|
733
|
-
|
|
734
|
-
styles.reset.add(`${hashedProperty}: initial;`);
|
|
735
|
-
if (!isInheritable && !hasChildSelector) {
|
|
736
|
-
styles.reset.add(`${prop.tokenProperty}: initial;`);
|
|
737
|
-
}
|
|
738
|
-
styles.selectors[template] ??= /* @__PURE__ */ new Set();
|
|
739
|
-
styles.selectors[toggleTemplate] ??= /* @__PURE__ */ new Set();
|
|
740
|
-
styles.selectors[gridToggleTemplate] ??= /* @__PURE__ */ new Set();
|
|
741
|
-
for (const selector of selectors.base) {
|
|
742
|
-
const elemSelector = removePseudoElementSelector(selector);
|
|
743
|
-
styles.selectors[template].add(prop.isCustom ? elemSelector : selector);
|
|
744
|
-
styles.selectors[toggleTemplate].add(elemSelector);
|
|
745
|
-
styles.selectors[gridToggleTemplate].add(elemSelector);
|
|
718
|
+
const variantValue = createVariantValue(cssProperty, prop, configs);
|
|
719
|
+
const variantToggleValue = prop.isGrid ? createGridVariantToggleValue(toggleProperty, prop.tokenProperty) : createVariantToggleValue(toggleProperty, prop.tokenProperty);
|
|
720
|
+
sheet.addReset(toggleProperty);
|
|
721
|
+
sheet.addReset(hashedProperty);
|
|
722
|
+
for (const selector of parsedSelectors.all) {
|
|
723
|
+
sheet.addToggleFlagDeclaration(responsiveConfig, selector, toggleProperty);
|
|
746
724
|
}
|
|
747
|
-
for (const selector of
|
|
748
|
-
const
|
|
749
|
-
|
|
750
|
-
|
|
725
|
+
for (const selector of parsedSelectors.elements) {
|
|
726
|
+
const pseudoOwnerSelector = removePseudoElementSelector(selector);
|
|
727
|
+
const baseSelector = prop.isCustom ? pseudoOwnerSelector : selector;
|
|
728
|
+
sheet.addDeclaration(prop.layer, baseSelector, baseProperty, variantValue);
|
|
729
|
+
sheet.addDeclaration(prop.layer, pseudoOwnerSelector, hashedProperty, variantToggleValue);
|
|
730
|
+
if (prop.isGrid) {
|
|
731
|
+
sheet.addDeclaration(prop.layer, pseudoOwnerSelector, gridProperty3, gridToggleValue);
|
|
732
|
+
}
|
|
751
733
|
}
|
|
752
734
|
} else {
|
|
753
|
-
const propertyValue =
|
|
754
|
-
const
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
styles.atomic[template].add(selector);
|
|
735
|
+
const propertyValue = prop.isGrid ? createGridPropertyValue(prop.tokenProperty, "revert-layer") : createBasePropertyValue(prop.tokenProperty, "revert-layer");
|
|
736
|
+
for (const selector of parsedSelectors.elements) {
|
|
737
|
+
sheet.addDeclaration(prop.layer, selector, baseProperty, propertyValue);
|
|
738
|
+
if (prop.isGrid) {
|
|
739
|
+
sheet.addDeclaration(prop.layer, selector, gridProperty3, gridToggleValue);
|
|
740
|
+
}
|
|
760
741
|
}
|
|
761
742
|
}
|
|
762
743
|
}
|
|
763
744
|
}
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
745
|
+
const composeEntries = Object.entries(composeBlocks);
|
|
746
|
+
for (const [selector, tokenamiProperties] of composeEntries) {
|
|
747
|
+
const propertyEntries = Object.entries(tokenamiProperties);
|
|
748
|
+
for (let [key, value] of propertyEntries) {
|
|
749
|
+
sheet.addDeclaration(LAYERS.COMPONENTS, selector, key, value);
|
|
769
750
|
}
|
|
770
751
|
}
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
752
|
+
return sheet.toString();
|
|
753
|
+
}
|
|
754
|
+
var SELECTOR_TAG = "<selector>";
|
|
755
|
+
var CUSTOM_PROP_REGEX = /\(--[^-][\w-]+/g;
|
|
756
|
+
var Sheet = class {
|
|
757
|
+
config;
|
|
758
|
+
tokenValues;
|
|
759
|
+
themeTokenSelectors = [];
|
|
760
|
+
reset = /* @__PURE__ */ new Set();
|
|
761
|
+
toggles = {};
|
|
762
|
+
layers = {};
|
|
763
|
+
constructor(tokenValues, config) {
|
|
764
|
+
this.tokenValues = tokenValues;
|
|
765
|
+
this.config = config;
|
|
766
|
+
}
|
|
767
|
+
addReset(property) {
|
|
768
|
+
this.reset.add(`${property}: initial;`);
|
|
769
|
+
}
|
|
770
|
+
addThemeTokenSelectors(baseSelectors) {
|
|
771
|
+
this.themeTokenSelectors.push(...baseSelectors.map(removePseudoElementSelector));
|
|
772
|
+
}
|
|
773
|
+
addDeclaration(layer, selector, property, value) {
|
|
774
|
+
const declaration = `${property}: ${value}`;
|
|
775
|
+
const template = `@layer ${layer} { ${SELECTOR_TAG} { ${declaration} } }`;
|
|
776
|
+
this.layers[template] ??= /* @__PURE__ */ new Set();
|
|
777
|
+
this.layers[template].add(selector);
|
|
778
|
+
}
|
|
779
|
+
addToggleFlagDeclaration(responsiveConfig, selector, toggleProperty) {
|
|
780
|
+
let toggle = `${toggleProperty}: ;`;
|
|
781
|
+
const toggleKey = responsiveConfig || selector.join();
|
|
782
|
+
const responsiveSelectors = [responsiveConfig, ...selector].reverse();
|
|
783
|
+
for (const selector2 of responsiveSelectors) {
|
|
784
|
+
if (!selector2) continue;
|
|
785
|
+
const elemSelector = removePseudoElementSelector(selector2);
|
|
786
|
+
toggle = `${elemSelector} { ${toggle} }`;
|
|
777
787
|
}
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
788
|
+
this.toggles[toggleKey] ??= /* @__PURE__ */ new Set();
|
|
789
|
+
this.toggles[toggleKey].add(toggle);
|
|
790
|
+
}
|
|
791
|
+
#generateGlobalStyles() {
|
|
792
|
+
if (!this.config.globalStyles) return "";
|
|
793
|
+
return `@layer global { ${stringify(this.config.globalStyles)} }`;
|
|
794
|
+
}
|
|
795
|
+
#generateTokenamiResets() {
|
|
796
|
+
const themeTokenSelectors = unique(this.themeTokenSelectors);
|
|
797
|
+
return `
|
|
798
|
+
@layer tkb {
|
|
799
|
+
${this.#generateKeyframeRules()}
|
|
800
|
+
${this.#generateThemeTokens(themeTokenSelectors)}
|
|
801
|
+
* { ${Array.from(this.reset).join(" ")} }
|
|
802
|
+
${Object.values(this.toggles).flatMap((set) => Array.from(set)).join(" ")}
|
|
803
|
+
}
|
|
804
|
+
`;
|
|
805
|
+
}
|
|
806
|
+
#generatePlaceholderLayers(prefix) {
|
|
807
|
+
return `@layer ${Array.from({ length: 20 }).map((_, layer) => `${prefix}${layer}`).join(", ")};`;
|
|
808
|
+
}
|
|
809
|
+
#generateLayerStyles() {
|
|
810
|
+
const entries = Object.entries(this.layers);
|
|
811
|
+
const groupedBySelectors = /* @__PURE__ */ new Map();
|
|
812
|
+
for (const [template, selectors] of entries) {
|
|
813
|
+
const { pseudoElements, otherSelectors } = this.#separateSelectors(selectors);
|
|
814
|
+
if (otherSelectors.length > 0) {
|
|
815
|
+
const groupedSelector = otherSelectors.sort().join(",");
|
|
816
|
+
const existingStyles = groupedBySelectors.get(groupedSelector) || "";
|
|
817
|
+
const newStyles = template.replace(SELECTOR_TAG, groupedSelector);
|
|
818
|
+
groupedBySelectors.set(groupedSelector, existingStyles + " " + newStyles);
|
|
819
|
+
}
|
|
820
|
+
for (const pseudoElement of pseudoElements) {
|
|
821
|
+
const existingStyles = groupedBySelectors.get(pseudoElement) || "";
|
|
822
|
+
const newStyles = template.replace(SELECTOR_TAG, pseudoElement);
|
|
823
|
+
groupedBySelectors.set(pseudoElement, existingStyles + " " + newStyles);
|
|
824
|
+
}
|
|
785
825
|
}
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
}
|
|
826
|
+
return Array.from(groupedBySelectors.values()).join(" ");
|
|
827
|
+
}
|
|
828
|
+
#separateSelectors(selectors) {
|
|
829
|
+
const pseudoElements = [];
|
|
830
|
+
const otherSelectors = [];
|
|
831
|
+
for (const selector of selectors) {
|
|
832
|
+
if (isPseudoElementSelector(selector)) {
|
|
833
|
+
pseudoElements.push(selector);
|
|
834
|
+
} else {
|
|
835
|
+
otherSelectors.push(selector);
|
|
836
|
+
}
|
|
837
|
+
}
|
|
838
|
+
return { pseudoElements, otherSelectors };
|
|
839
|
+
}
|
|
840
|
+
#generateKeyframeRules() {
|
|
841
|
+
const themeValues = this.tokenValues.flatMap((tokenValue2) => {
|
|
842
|
+
return Object.values(getThemeValuesByThemeMode(tokenValue2, this.config.theme));
|
|
843
|
+
});
|
|
844
|
+
const rules = Object.entries(this.config.keyframes || {}).flatMap(([name, styles]) => {
|
|
845
|
+
const nameRegex = new RegExp(`\\b${name}\\b`);
|
|
846
|
+
const isUsingKeyframeName = themeValues.some((value) => nameRegex.test(value));
|
|
847
|
+
if (!isUsingKeyframeName) return [];
|
|
848
|
+
return [[`@keyframes ${name} { ${stringify(styles)} }`]];
|
|
849
|
+
});
|
|
850
|
+
return rules.join(" ");
|
|
851
|
+
}
|
|
852
|
+
#generateThemeTokens(styleSelector) {
|
|
853
|
+
const theme = getThemeFromConfig(this.config.theme);
|
|
854
|
+
const rootSelector = this.config.themeSelector("root");
|
|
855
|
+
const gridStyles = `${rootSelector} { ${Tokenami2.gridProperty()}: ${this.config.grid}; }`;
|
|
856
|
+
const rootStyles = this.#getThemeStyles(styleSelector, rootSelector, theme.root);
|
|
857
|
+
const themeToModes = {};
|
|
858
|
+
const modeEntries = Object.entries(theme.modes || {});
|
|
859
|
+
for (const [mode, theme2] of modeEntries) {
|
|
860
|
+
const themeKey = JSON.stringify(theme2);
|
|
861
|
+
if (themeKey in themeToModes) themeToModes[themeKey].push(mode);
|
|
862
|
+
else themeToModes[themeKey] = [mode];
|
|
863
|
+
}
|
|
864
|
+
const modeStyles = Object.entries(themeToModes).map(([theme2, modes]) => {
|
|
865
|
+
const selector = modes.map(this.config.themeSelector).join(", ");
|
|
866
|
+
return this.#getThemeStyles(styleSelector, selector, JSON.parse(theme2));
|
|
867
|
+
});
|
|
868
|
+
const themeTokens = [gridStyles, rootStyles, modeStyles.join(" ")];
|
|
869
|
+
return themeTokens.join(" ");
|
|
870
|
+
}
|
|
871
|
+
#getThemeStyles(styleSelector, selector, theme) {
|
|
872
|
+
const themeValues = getThemeValuesByTokenValues(this.tokenValues, theme);
|
|
873
|
+
const customPropertyThemeValues = this.#getCustomPropertyThemeValues(themeValues);
|
|
874
|
+
const customPropertyThemeKeys = Object.keys(customPropertyThemeValues);
|
|
875
|
+
const selectors = [selector].flat();
|
|
876
|
+
const elementSelector = selectors.at(-1);
|
|
877
|
+
const parentSelectors = selectors.slice(0, -1);
|
|
878
|
+
const elementThemeStyles = this.#getElementThemeStyles(
|
|
879
|
+
styleSelector,
|
|
880
|
+
elementSelector,
|
|
881
|
+
customPropertyThemeValues
|
|
882
|
+
);
|
|
883
|
+
for (const customKey of customPropertyThemeKeys) {
|
|
884
|
+
delete themeValues[customKey];
|
|
885
|
+
}
|
|
886
|
+
const themeStyles = selectors.reduceRight(
|
|
887
|
+
(declaration, selector2) => `${selector2} { ${declaration} }`,
|
|
888
|
+
stringify(themeValues)
|
|
889
|
+
);
|
|
890
|
+
const customPropertyThemeStyles = parentSelectors.reduceRight(
|
|
891
|
+
(declaration, selector2) => `${selector2} { ${declaration} }`,
|
|
892
|
+
elementThemeStyles
|
|
893
|
+
);
|
|
894
|
+
return themeStyles + " " + customPropertyThemeStyles;
|
|
895
|
+
}
|
|
896
|
+
#getCustomPropertyThemeValues(themeValues) {
|
|
897
|
+
const entries = Object.entries(themeValues).flatMap(([key, value]) => {
|
|
898
|
+
const valueWithCustomPrefixes = this.#getPrefixedCustomPropertyValues(value);
|
|
899
|
+
return valueWithCustomPrefixes ? [[key, valueWithCustomPrefixes]] : [];
|
|
900
|
+
});
|
|
901
|
+
return Object.fromEntries(entries);
|
|
902
|
+
}
|
|
903
|
+
#getPrefixedCustomPropertyValues(themeValue) {
|
|
904
|
+
const variables = themeValue.match(CUSTOM_PROP_REGEX);
|
|
905
|
+
if (!variables) return null;
|
|
906
|
+
return themeValue.replace(CUSTOM_PROP_REGEX, (m) => {
|
|
907
|
+
const match = m.replace("(", "");
|
|
908
|
+
const tokenProperty3 = Tokenami2.TokenProperty.safeParse(match);
|
|
909
|
+
if (!tokenProperty3.success) return m;
|
|
910
|
+
const parts = Tokenami2.getTokenPropertySplit(tokenProperty3.output);
|
|
911
|
+
const isCustom = Boolean(this.config.customProperties?.[parts.alias]);
|
|
912
|
+
if (!isCustom) return m;
|
|
913
|
+
const tokenPrefix = Tokenami2.tokenProperty("");
|
|
914
|
+
const customPrefixTokenValue = tokenProperty3.output.replace(tokenPrefix, CUSTOM_PROP_PREFIX);
|
|
915
|
+
return "(" + customPrefixTokenValue;
|
|
916
|
+
});
|
|
917
|
+
}
|
|
918
|
+
#getElementThemeStyles(styleSelector, selector, themeValues) {
|
|
919
|
+
const splitGroups = selector.split(",");
|
|
920
|
+
const themeStyles = splitGroups.map((selector2) => {
|
|
921
|
+
const elemSelector = [styleSelector].flat().map((s) => `${selector2} ${s}`);
|
|
922
|
+
return `${selector2}, ${elemSelector} { ${stringify(themeValues)} }`;
|
|
923
|
+
});
|
|
924
|
+
return themeStyles.join(" ");
|
|
925
|
+
}
|
|
926
|
+
toString() {
|
|
927
|
+
return `
|
|
928
|
+
${this.#generateGlobalStyles()}
|
|
929
|
+
${this.#generateTokenamiResets()}
|
|
930
|
+
${this.#generatePlaceholderLayers(LAYERS.BASE)}
|
|
931
|
+
${this.#generatePlaceholderLayers(LAYERS.LOGICAL)}
|
|
932
|
+
${this.#generatePlaceholderLayers(LAYERS.SELECTORS)}
|
|
933
|
+
${this.#generatePlaceholderLayers(LAYERS.SELECTORS_LOGICAL)}
|
|
934
|
+
${this.#generatePlaceholderLayers(LAYERS.COMPONENTS)}
|
|
935
|
+
${this.#generateLayerStyles()}
|
|
936
|
+
`;
|
|
937
|
+
}
|
|
938
|
+
};
|
|
798
939
|
function parseComposeBlocks(composeBlocks, config) {
|
|
799
940
|
let parsedComposeBlocks = {};
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
const
|
|
941
|
+
const entries = Object.entries(composeBlocks);
|
|
942
|
+
for (const [selector, tokenamiProperties] of entries) {
|
|
943
|
+
const entries2 = Object.entries(tokenamiProperties);
|
|
944
|
+
const aliasProperties = Tokenami2.iterateAliasProperties(entries2, config);
|
|
803
945
|
let styles = {};
|
|
804
946
|
for (let [key, value, propertyConfig] of aliasProperties) {
|
|
805
947
|
const tokenProperty3 = key;
|
|
@@ -815,21 +957,22 @@ function parseComposeBlocks(composeBlocks, config) {
|
|
|
815
957
|
}
|
|
816
958
|
return parsedComposeBlocks;
|
|
817
959
|
}
|
|
818
|
-
function getPropertySelectors(composeBlocks, prop,
|
|
960
|
+
function getPropertySelectors(composeBlocks, prop, selectorConfig) {
|
|
819
961
|
let selectors = [];
|
|
820
|
-
const selectorConfig = getPropertyConfigSelector(prop.selector, config);
|
|
821
962
|
const baseSelectors = getBaseSelectors(composeBlocks, prop.tokenProperty);
|
|
822
963
|
const elemSelector = selectorConfig.find(isElementSelector);
|
|
823
964
|
if (elemSelector) {
|
|
824
965
|
const parsedSelectors = getParsedSelectors(prop.selector, [elemSelector], baseSelectors);
|
|
825
|
-
if (elemSelector === "&") selectors.push(...parsedSelectors.flat());
|
|
826
966
|
selectors.push(...parsedSelectors.flat());
|
|
827
967
|
} else {
|
|
828
968
|
selectors.push(...baseSelectors);
|
|
829
969
|
}
|
|
970
|
+
const elementSelectors = unique(selectors).map((selector) => {
|
|
971
|
+
return isPseudoElementSelector(selector) ? selector : selector.replace(/:[^ ]+$/, "");
|
|
972
|
+
});
|
|
830
973
|
return {
|
|
831
|
-
|
|
832
|
-
|
|
974
|
+
all: getParsedSelectors(prop.selector, selectorConfig, baseSelectors),
|
|
975
|
+
elements: elementSelectors
|
|
833
976
|
};
|
|
834
977
|
}
|
|
835
978
|
function getBaseSelectors(composeBlocks, property) {
|
|
@@ -838,8 +981,17 @@ function getBaseSelectors(composeBlocks, property) {
|
|
|
838
981
|
});
|
|
839
982
|
return [DEFAULT_SELECTOR, ...composeSelectors];
|
|
840
983
|
}
|
|
984
|
+
function createBasePropertyValue(property, fallback) {
|
|
985
|
+
return `var(${property}${fallback ? ", " + fallback : ""})`;
|
|
986
|
+
}
|
|
987
|
+
function createGridPropertyValue(property, fallback) {
|
|
988
|
+
const hashGridProperty = hashVariantProperty("grid", property);
|
|
989
|
+
const baseProperty = createBasePropertyValue(property, fallback);
|
|
990
|
+
return `var(${hashGridProperty}, ${baseProperty})`;
|
|
991
|
+
}
|
|
841
992
|
function createVariantValue(cssProperty, prop, propertyConfigs) {
|
|
842
|
-
|
|
993
|
+
const customPropertyFallback = `var(${Tokenami2.tokenProperty(cssProperty)})`;
|
|
994
|
+
let variantValue = prop.isCustom ? customPropertyFallback : "revert-layer";
|
|
843
995
|
const seen = /* @__PURE__ */ new Set();
|
|
844
996
|
for (const propertyConfig of propertyConfigs) {
|
|
845
997
|
if (!propertyConfig.variant) continue;
|
|
@@ -848,62 +1000,19 @@ function createVariantValue(cssProperty, prop, propertyConfigs) {
|
|
|
848
1000
|
variantValue = `var(${hashedProperty}, ${variantValue})`;
|
|
849
1001
|
seen.add(propertyConfig.variant);
|
|
850
1002
|
}
|
|
851
|
-
|
|
852
|
-
const customPropertyValue = variantValue.replace("revert-layer", customPropertyFallback);
|
|
853
|
-
return prop.isCustom ? customPropertyValue : variantValue;
|
|
854
|
-
}
|
|
855
|
-
function createToggleDeclaration(responsiveSelector, selector, toggleProperty) {
|
|
856
|
-
let toggle = `${toggleProperty}: ;`;
|
|
857
|
-
const responsiveSelectors = [responsiveSelector, ...selector].reverse();
|
|
858
|
-
for (const selector2 of responsiveSelectors) {
|
|
859
|
-
if (!selector2) continue;
|
|
860
|
-
const elemSelector = removePseudoElementSelector(selector2);
|
|
861
|
-
toggle = `${elemSelector} { ${toggle} }`;
|
|
862
|
-
}
|
|
863
|
-
return toggle;
|
|
1003
|
+
return variantValue;
|
|
864
1004
|
}
|
|
865
|
-
function
|
|
866
|
-
const hashGridProperty = hashVariantProperty("grid", property);
|
|
1005
|
+
function createGridToggleValue(property) {
|
|
867
1006
|
const gridProperty3 = Tokenami2.gridProperty();
|
|
868
|
-
return
|
|
869
|
-
}
|
|
870
|
-
function getBasePropertyValue(property, config, revert = true) {
|
|
871
|
-
const hashGridProperty = hashVariantProperty("grid", property);
|
|
872
|
-
const baseProperty = `var(${property}${revert ? ", revert-layer" : ""})`;
|
|
873
|
-
return config.isGrid ? `var(${hashGridProperty}, ${baseProperty})` : baseProperty;
|
|
1007
|
+
return `var(${property}__calc) calc(var(${property}) * var(${gridProperty3}))`;
|
|
874
1008
|
}
|
|
875
|
-
function
|
|
876
|
-
|
|
1009
|
+
function createVariantToggleValue(toggleProperty, tokenProperty3) {
|
|
1010
|
+
const basePropertyValue = createBasePropertyValue(tokenProperty3);
|
|
1011
|
+
return `var(${toggleProperty}) ${basePropertyValue};`;
|
|
877
1012
|
}
|
|
878
|
-
function
|
|
879
|
-
const
|
|
880
|
-
|
|
881
|
-
const { pseudoElements, otherSelectors } = separateSelectors(selectors);
|
|
882
|
-
if (otherSelectors.length > 0) {
|
|
883
|
-
const groupedSelector = otherSelectors.sort().join(",");
|
|
884
|
-
const existingStyles = groupedBySelectors.get(groupedSelector) || "";
|
|
885
|
-
const newStyles = template.replace(SELECTOR_TAG, groupedSelector);
|
|
886
|
-
groupedBySelectors.set(groupedSelector, existingStyles + " " + newStyles);
|
|
887
|
-
}
|
|
888
|
-
for (const pseudoElement of pseudoElements) {
|
|
889
|
-
const existingStyles = groupedBySelectors.get(pseudoElement) || "";
|
|
890
|
-
const newStyles = template.replace(SELECTOR_TAG, pseudoElement);
|
|
891
|
-
groupedBySelectors.set(pseudoElement, existingStyles + " " + newStyles);
|
|
892
|
-
}
|
|
893
|
-
}
|
|
894
|
-
return Array.from(groupedBySelectors.values()).join(" ");
|
|
895
|
-
}
|
|
896
|
-
function separateSelectors(selectors) {
|
|
897
|
-
const pseudoElements = [];
|
|
898
|
-
const otherSelectors = [];
|
|
899
|
-
for (const selector of selectors) {
|
|
900
|
-
if (isPseudoElementSelector(selector)) {
|
|
901
|
-
pseudoElements.push(selector);
|
|
902
|
-
} else {
|
|
903
|
-
otherSelectors.push(selector);
|
|
904
|
-
}
|
|
905
|
-
}
|
|
906
|
-
return { pseudoElements, otherSelectors };
|
|
1013
|
+
function createGridVariantToggleValue(toggleProperty, tokenProperty3) {
|
|
1014
|
+
const basePropertyValue = createGridPropertyValue(tokenProperty3);
|
|
1015
|
+
return `var(${toggleProperty}) ${basePropertyValue};`;
|
|
907
1016
|
}
|
|
908
1017
|
function getPropertyConfigs(tokenProperties, config) {
|
|
909
1018
|
let propertyConfigs = /* @__PURE__ */ new Map();
|
|
@@ -915,7 +1024,7 @@ function getPropertyConfigs(tokenProperties, config) {
|
|
|
915
1024
|
const selectorOrder = parts.selector ? 2 : 0;
|
|
916
1025
|
const order = responsiveOrder + selectorOrder;
|
|
917
1026
|
for (const cssProperty of properties) {
|
|
918
|
-
const layerIndex =
|
|
1027
|
+
const layerIndex = getPropertyLayerIndex(cssProperty, config);
|
|
919
1028
|
if (layerIndex === -1) continue;
|
|
920
1029
|
const longhandProperty = Tokenami2.createLonghandProperty(property, cssProperty);
|
|
921
1030
|
const tokenProperty3 = Tokenami2.parseProperty(longhandProperty);
|
|
@@ -933,100 +1042,17 @@ function getPropertyConfigs(tokenProperties, config) {
|
|
|
933
1042
|
}
|
|
934
1043
|
return propertyConfigs;
|
|
935
1044
|
}
|
|
936
|
-
var SHORTHAND_TO_LONGHAND_ENTRIES =
|
|
937
|
-
function
|
|
1045
|
+
var SHORTHAND_TO_LONGHAND_ENTRIES = Array.from(Tokenami2.mapShorthandToLonghands.entries());
|
|
1046
|
+
function getPropertyLayerIndex(cssProperty, config) {
|
|
938
1047
|
const validProperties = getValidProperties(config);
|
|
939
1048
|
const isSupported = validProperties.has(cssProperty);
|
|
940
1049
|
const initialDepth = isSupported ? 1 : -1;
|
|
941
1050
|
if (cssProperty === "all") return 0;
|
|
942
1051
|
return SHORTHAND_TO_LONGHAND_ENTRIES.reduce((depth, [shorthand, longhands]) => {
|
|
943
1052
|
const isLonghand = longhands.includes(cssProperty);
|
|
944
|
-
return isLonghand ? depth +
|
|
1053
|
+
return isLonghand ? depth + getPropertyLayerIndex(shorthand, config) : depth;
|
|
945
1054
|
}, initialDepth);
|
|
946
1055
|
}
|
|
947
|
-
function generateKeyframeRules(tokenValues, config) {
|
|
948
|
-
const themeValues = tokenValues.flatMap((tokenValue2) => {
|
|
949
|
-
return Object.values(getThemeValuesByThemeMode(tokenValue2, config.theme));
|
|
950
|
-
});
|
|
951
|
-
const rules = Object.entries(config.keyframes || {}).flatMap(([name, styles]) => {
|
|
952
|
-
const nameRegex = new RegExp(`\\b${name}\\b`);
|
|
953
|
-
const isUsingKeyframeName = themeValues.some((value) => nameRegex.test(value));
|
|
954
|
-
if (!isUsingKeyframeName) return [];
|
|
955
|
-
return [[`@keyframes ${name} { ${stringify(styles)} }`]];
|
|
956
|
-
});
|
|
957
|
-
return rules.join(" ");
|
|
958
|
-
}
|
|
959
|
-
function generateThemeTokens(tokenValues, styleSelector, config) {
|
|
960
|
-
const theme = getThemeFromConfig(config.theme);
|
|
961
|
-
const rootSelector = config.themeSelector("root");
|
|
962
|
-
const gridStyles = `${rootSelector} { ${Tokenami2.gridProperty()}: ${config.grid}; }`;
|
|
963
|
-
const rootStyles = getThemeStyles(styleSelector, rootSelector, tokenValues, theme.root, config);
|
|
964
|
-
const themeToModes = {};
|
|
965
|
-
const modeEntries = Object.entries(theme.modes || {});
|
|
966
|
-
for (const [mode, theme2] of modeEntries) {
|
|
967
|
-
const themeKey = JSON.stringify(theme2);
|
|
968
|
-
if (themeKey in themeToModes) themeToModes[themeKey].push(mode);
|
|
969
|
-
else themeToModes[themeKey] = [mode];
|
|
970
|
-
}
|
|
971
|
-
const modeStyles = Object.entries(themeToModes).map(([theme2, modes]) => {
|
|
972
|
-
const selector = modes.map(config.themeSelector).join(", ");
|
|
973
|
-
return getThemeStyles(styleSelector, selector, tokenValues, JSON.parse(theme2), config);
|
|
974
|
-
});
|
|
975
|
-
const themeTokens = [gridStyles, rootStyles, modeStyles.join(" ")];
|
|
976
|
-
return themeTokens.join(" ");
|
|
977
|
-
}
|
|
978
|
-
var getThemeStyles = (styleSelector, selector, tokenValues, theme, config) => {
|
|
979
|
-
const themeValues = getThemeValuesByTokenValues(tokenValues, theme);
|
|
980
|
-
const customPropertyThemeValues = getCustomPropertyThemeValues(themeValues, config);
|
|
981
|
-
const selectors = [selector].flat();
|
|
982
|
-
for (const customKey of Object.keys(customPropertyThemeValues)) {
|
|
983
|
-
delete themeValues[customKey];
|
|
984
|
-
}
|
|
985
|
-
const themeStyles = selectors.reduceRight((declaration, selector2) => {
|
|
986
|
-
return `${selector2} { ${declaration} }`;
|
|
987
|
-
}, stringify(themeValues));
|
|
988
|
-
const elementSelector = selectors.at(-1);
|
|
989
|
-
const elementThemeStyles = getElementThemeStyles(
|
|
990
|
-
styleSelector,
|
|
991
|
-
elementSelector,
|
|
992
|
-
customPropertyThemeValues
|
|
993
|
-
);
|
|
994
|
-
const customPropertyThemeStyles = selectors.slice(0, -1).reduceRight((declaration, selector2) => {
|
|
995
|
-
return `${selector2} { ${declaration} }`;
|
|
996
|
-
}, elementThemeStyles);
|
|
997
|
-
return themeStyles + " " + customPropertyThemeStyles;
|
|
998
|
-
};
|
|
999
|
-
var getElementThemeStyles = (styleSelector, selector, themeValues) => {
|
|
1000
|
-
const splitGroups = selector.split(",");
|
|
1001
|
-
const themeStyles = splitGroups.map((selector2) => {
|
|
1002
|
-
const elemSelector = [styleSelector].flat().map((s) => `${selector2} ${s}`);
|
|
1003
|
-
return `${selector2}, ${elemSelector} { ${stringify(themeValues)} }`;
|
|
1004
|
-
});
|
|
1005
|
-
return themeStyles.join(" ");
|
|
1006
|
-
};
|
|
1007
|
-
function getCustomPropertyThemeValues(themeValues, config) {
|
|
1008
|
-
const entries = Object.entries(themeValues).flatMap(([key, value]) => {
|
|
1009
|
-
const valueWithCustomPrefixes = getPrefixedCustomPropertyValues(value, config.customProperties);
|
|
1010
|
-
return valueWithCustomPrefixes ? [[key, valueWithCustomPrefixes]] : [];
|
|
1011
|
-
});
|
|
1012
|
-
return Object.fromEntries(entries);
|
|
1013
|
-
}
|
|
1014
|
-
var CUSTOM_PROP_REGEX = /\(--[^-][\w-]+/g;
|
|
1015
|
-
var getPrefixedCustomPropertyValues = (themeValue, customProperties) => {
|
|
1016
|
-
const variables = themeValue.match(CUSTOM_PROP_REGEX);
|
|
1017
|
-
if (!variables) return null;
|
|
1018
|
-
return themeValue.replace(CUSTOM_PROP_REGEX, (m) => {
|
|
1019
|
-
const match = m.replace("(", "");
|
|
1020
|
-
const tokenProperty3 = Tokenami2.TokenProperty.safeParse(match);
|
|
1021
|
-
if (!tokenProperty3.success) return m;
|
|
1022
|
-
const parts = Tokenami2.getTokenPropertySplit(tokenProperty3.output);
|
|
1023
|
-
const isCustom = Boolean(customProperties?.[parts.alias]);
|
|
1024
|
-
if (!isCustom) return m;
|
|
1025
|
-
const tokenPrefix = Tokenami2.tokenProperty("");
|
|
1026
|
-
const customPrefixTokenValue = tokenProperty3.output.replace(tokenPrefix, CUSTOM_PROP_PREFIX);
|
|
1027
|
-
return "(" + customPrefixTokenValue;
|
|
1028
|
-
});
|
|
1029
|
-
};
|
|
1030
1056
|
function hashVariantProperty(variant, property) {
|
|
1031
1057
|
return `--_${Tokenami2.hash(variant + property)}`;
|
|
1032
1058
|
}
|
|
@@ -1048,7 +1074,7 @@ function isChildSelector(selector = "") {
|
|
|
1048
1074
|
function getResponsiveSelectorFromConfig(responsiveSelector, tokenamiConfig) {
|
|
1049
1075
|
return responsiveSelector && tokenamiConfig.responsive?.[responsiveSelector];
|
|
1050
1076
|
}
|
|
1051
|
-
function
|
|
1077
|
+
function getSelectorFromConfig(propertySelector, tokenamiConfig) {
|
|
1052
1078
|
const arbitrarySelector = Tokenami2.getArbitrarySelector(propertySelector);
|
|
1053
1079
|
const configSelector = propertySelector && tokenamiConfig.selectors?.[propertySelector];
|
|
1054
1080
|
const selector = arbitrarySelector?.replace(/_/g, " ") || configSelector;
|
|
@@ -1059,16 +1085,59 @@ function getParsedSelectors(propertySelector, selectorConfig, elementSelectors)
|
|
|
1059
1085
|
throw new Error(`Selector '${propertySelector}' must include '&'`);
|
|
1060
1086
|
}
|
|
1061
1087
|
const isSelectionVariant = selectorConfig.includes("&::selection");
|
|
1062
|
-
|
|
1088
|
+
const selectors = [];
|
|
1089
|
+
for (const elementSelector of elementSelectors) {
|
|
1063
1090
|
const isSelectionSelector = isSelectionVariant && elementSelector === DEFAULT_SELECTOR;
|
|
1064
1091
|
const tkSelector = isSelectionSelector ? `[style*="${propertySelector}_"]` : elementSelector;
|
|
1065
|
-
|
|
1066
|
-
|
|
1092
|
+
const selectorConfigs = expandSelectorConfigs(selectorConfig);
|
|
1093
|
+
for (const selectorConfig2 of selectorConfigs) {
|
|
1094
|
+
const selector = selectorConfig2.map((selector2) => selector2.replace(/&/g, tkSelector));
|
|
1095
|
+
selectors.push(selector);
|
|
1096
|
+
}
|
|
1097
|
+
}
|
|
1098
|
+
return selectors;
|
|
1099
|
+
}
|
|
1100
|
+
function expandSelectorConfigs(selectorConfig) {
|
|
1101
|
+
const parts = selectorConfig.map(parseSelectorList);
|
|
1102
|
+
let results = [[]];
|
|
1103
|
+
for (const group of parts) {
|
|
1104
|
+
const res = [];
|
|
1105
|
+
for (const existing of results) {
|
|
1106
|
+
for (const value of group) {
|
|
1107
|
+
res.push([...existing, value]);
|
|
1108
|
+
}
|
|
1109
|
+
}
|
|
1110
|
+
results = res;
|
|
1111
|
+
}
|
|
1112
|
+
return results;
|
|
1113
|
+
}
|
|
1114
|
+
function parseSelectorList(selector) {
|
|
1115
|
+
const selectors = [];
|
|
1116
|
+
let currentSelector = "";
|
|
1117
|
+
let depth = 0;
|
|
1118
|
+
for (const [index, char] of selector.split("").entries()) {
|
|
1119
|
+
if (char === "(") depth++;
|
|
1120
|
+
if (char === "[") depth++;
|
|
1121
|
+
if (char === ")") depth--;
|
|
1122
|
+
if (char === "]") depth--;
|
|
1123
|
+
const nextChar = selector[index + 1];
|
|
1124
|
+
if (!nextChar) {
|
|
1125
|
+
currentSelector += char;
|
|
1126
|
+
selectors.push(currentSelector.trim());
|
|
1127
|
+
currentSelector = "";
|
|
1128
|
+
} else if (char === "," && depth === 0) {
|
|
1129
|
+
selectors.push(currentSelector.trim());
|
|
1130
|
+
currentSelector = "";
|
|
1131
|
+
} else {
|
|
1132
|
+
currentSelector += char;
|
|
1133
|
+
}
|
|
1134
|
+
}
|
|
1135
|
+
return selectors;
|
|
1067
1136
|
}
|
|
1068
1137
|
|
|
1069
1138
|
// package.json
|
|
1070
1139
|
var package_default = {
|
|
1071
|
-
version: "0.0.
|
|
1140
|
+
version: "0.0.90"};
|
|
1072
1141
|
|
|
1073
1142
|
// src/ts-plugin/error-codes.ts
|
|
1074
1143
|
var INVALID_PROPERTY = 2353;
|
|
@@ -1334,9 +1403,12 @@ async function findUsedTokens(cwd, config) {
|
|
|
1334
1403
|
tokenProperties = [...tokenProperties, ...tokens.properties];
|
|
1335
1404
|
tokenValues = [...tokenValues, ...tokens.values];
|
|
1336
1405
|
for (const composeBlock of composeBlocksContents) {
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1406
|
+
try {
|
|
1407
|
+
const ast = acorn.parse(composeBlock, { ecmaVersion: "latest" });
|
|
1408
|
+
const composeBlockStyles = matchBaseComposeBlocks(ast);
|
|
1409
|
+
composeBlocks = { ...composeBlocks, ...composeBlockStyles };
|
|
1410
|
+
} catch {
|
|
1411
|
+
}
|
|
1340
1412
|
}
|
|
1341
1413
|
if (fileContent.includes(LAYERS.COMPONENTS)) {
|
|
1342
1414
|
const sheetComposeBlocks = findSheetComposeBlocks(fileContent);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tokenami",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.91",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.cjs",
|
|
@@ -45,8 +45,8 @@
|
|
|
45
45
|
},
|
|
46
46
|
"dependencies": {
|
|
47
47
|
"@stitches/stringify": "^1.2.8",
|
|
48
|
-
"@tokenami/config": "0.0.
|
|
49
|
-
"@tokenami/ds": "0.0.
|
|
48
|
+
"@tokenami/config": "0.0.91",
|
|
49
|
+
"@tokenami/ds": "0.0.91",
|
|
50
50
|
"acorn": "^8.11.3",
|
|
51
51
|
"acorn-walk": "^8.3.2",
|
|
52
52
|
"browserslist": "^4.26.3",
|
|
@@ -67,5 +67,5 @@
|
|
|
67
67
|
"peerDependencies": {
|
|
68
68
|
"typescript": ">= 5"
|
|
69
69
|
},
|
|
70
|
-
"gitHead": "
|
|
70
|
+
"gitHead": "aec7d0e54623aaf6e174a7be514f80b5ef97a6df"
|
|
71
71
|
}
|