Skip to content

Commit

Permalink
Reduce Memory Footprint (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
billyma authored Mar 4, 2021
1 parent d041e93 commit 25f4430
Show file tree
Hide file tree
Showing 28 changed files with 307 additions and 331 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,5 @@ public class RegexPattern {
public static final String VAR_FUNCTION = "var\\(\\s*--lwc-(?<token>[\\w\\d-]+)\\s*(,\\s*(?<fallback>"+
COLOR_PATTERN + "|" + NUMERIC_PATTERN + "|" + WORD_FRAGMENT + ")\\s*)?\\)";

public static final String IMPORT_AND_EXPORT_TOKENS = "(:?\\s+|^)import\\s+|(:?\\s+|^)export\\s+";
public static final String IMPORT_AND_EXPORT_TOKENS = "(:?^\\s*)import\\s+|(:?^\\s*)export\\s+";
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,20 @@ public enum EntityType {LWC, AURA, OTHER}

private String path;
private final List<String> rawContent;
private Bundle bundle;

private String componentName;
private EntityType entityType;

private Entry(List<Recommendation> recommendation, List<ComponentOverride> overrides,
private Entry(
List<Input> inputs, String path, List<String> rawContent,
EntityType entityType, String componentName) {

this.overrides = overrides;
this.recommendation = recommendation;
this.inputs = inputs;
this.path = path;
this.rawContent = rawContent;
this.entityType = entityType;
this.componentName = componentName;
}

public boolean hasRecommendation() {
return this.getRecommendation().isEmpty() == false;
}

public List<Recommendation> getRecommendation() {
if (this.recommendation == null) {
this.recommendation = new ArrayList<>();
Expand All @@ -60,10 +52,6 @@ public void setRecommendation(List<Recommendation> recommendation) {
this.recommendation = recommendation;
}

public boolean hasOverrides() {
return this.getOverrides().isEmpty() == false;
}

public List<ComponentOverride> getOverrides() {
if (this.overrides == null) {
this.overrides = new ArrayList<>();
Expand Down Expand Up @@ -108,18 +96,6 @@ public List<String> getRawContent() {
return this.rawContent;
}

public void setBundle(Bundle bundle) {
this.bundle = bundle;
}

public Bundle getBundle() {
if (this.bundle == null) {
this.bundle = new Bundle();
}

return this.bundle;
}

public void setComponentName(String componentName) {
this.componentName = componentName;
}
Expand All @@ -141,25 +117,17 @@ public static EntryBuilder builder() {
}

public static class EntryBuilder {
private List<Recommendation> recommendation;
private List<Input> inputs;
private String path;
private List<String> rawContent;
private List<ComponentOverride> overrides;
private EntityType entityType;
private String componentName;

public EntryBuilder recommendation(List<Recommendation> recommendation) {
this.recommendation = recommendation;
return this;
}

public EntryBuilder inputs(List<Input> inputs) {
this.inputs = inputs;
return this;
}


public EntryBuilder path(String path) {
this.path = path;
return this;
Expand All @@ -170,11 +138,6 @@ public EntryBuilder rawContent(List<String> rawContent) {
return this;
}

public EntryBuilder overrides(List<ComponentOverride> overrides) {
this.overrides = overrides;
return this;
}

public EntryBuilder entityType(EntityType entityType) {
this.entityType = entityType;
return this;
Expand All @@ -186,7 +149,7 @@ public EntryBuilder componentName(String componentName) {
}

public Entry build() {
return new Entry(recommendation, overrides, inputs, path, rawContent, entityType, componentName);
return new Entry(inputs, path, rawContent, entityType, componentName);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public class RuleSet extends Input implements Comparable<RuleSet>, RangeProvider
private final Range range;
private final List<String> raw;
private final List<Style> styles;
private List<Style> stylesWithAnnotationType;

private RuleSet(Rule rule, Range range, List<String> raw) {
this.rule = rule;
Expand All @@ -48,28 +49,32 @@ public List<Style> getStyles() {
}

public List<Style> getStylesWithAnnotationType() {
Optional<AnnotationType> ruleAnnotationType = getAnnotationType();
List<Style> annotatedStyles = new ArrayList<>();

for (Style style : getStyles()) {
Style.StyleBuilder styleBuilder = Style.builder();
styleBuilder
.property(style.getProperty())
.value(style.getValue())
.range(style.getRange())
.declaration(style.getDeclaration())
.condition(style.getCondition());

if (style.getAnnotationType() == null) {
styleBuilder.annotationType(ruleAnnotationType.orElse(AnnotationType.NONE));
} else {
styleBuilder.annotationType(style.getAnnotationType());
if (stylesWithAnnotationType == null) {
Optional<AnnotationType> ruleAnnotationType = getAnnotationType();
List<Style> annotatedStyles = new ArrayList<>();

for (Style style : getStyles()) {
Style.StyleBuilder styleBuilder = Style.builder();
styleBuilder
.property(style.getProperty())
.value(style.getValue())
.range(style.getRange())
.declaration(style.getDeclaration())
.condition(style.getCondition());

if (style.getAnnotationType() == null) {
styleBuilder.annotationType(ruleAnnotationType.orElse(AnnotationType.NONE));
} else {
styleBuilder.annotationType(style.getAnnotationType());
}

annotatedStyles.add(styleBuilder.build());
}

annotatedStyles.add(styleBuilder.build());
stylesWithAnnotationType = annotatedStyles;
}

return annotatedStyles;
return stylesWithAnnotationType;
}

public List<Annotation> getAnnotations() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,42 +7,42 @@
package com.salesforce.slds.shared.utils;

import com.salesforce.slds.shared.RegexPattern;
import com.salesforce.slds.shared.models.core.Bundle;
import com.salesforce.slds.shared.models.core.Entry;
import com.salesforce.slds.shared.models.core.Input;

import java.util.List;
import java.util.Optional;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class EntryUtilities {

private static Pattern IMPORT_AND_EXPORT_PATTERN = Pattern.compile(RegexPattern.IMPORT_AND_EXPORT_TOKENS);

public static Entry.EntityType getType(Entry entry) {
public static Entry.EntityType getType(Bundle bundle) {
List<Entry.EntityType> types = bundle.getEntries().stream()
.map(entry -> {

if (entry.getPath().endsWith(".js")) {
if (entry.getRawContent().stream()
.anyMatch(content -> IMPORT_AND_EXPORT_PATTERN.matcher(content).find())) {
return Entry.EntityType.LWC;
}
}
if (entry.getPath().endsWith(".js")) {
if (entry.getRawContent().stream()
.anyMatch(content -> IMPORT_AND_EXPORT_PATTERN.matcher(content).find())) {
return Entry.EntityType.LWC;
}
}

for (Input input : entry.getInputs()) {
Optional<Entry.EntityType> type = getType(entry.getComponentName(), input);
if (type.isPresent()) {
return type.get();
}
}
for (Input input : entry.getInputs()) {
Optional<Entry.EntityType> type = getType(entry.getComponentName(), input);
if (type.isPresent()) {
return type.get();
}
}

for (Entry bundledEntry : entry.getBundle().getEntries()) {
for (Input input : bundledEntry.getInputs()) {
Optional<Entry.EntityType> type = getType(bundledEntry.getComponentName(), input);
if (type.isPresent()) {
return type.get();
}
}
}
return Entry.EntityType.OTHER;
}).filter(entityType -> entityType != Entry.EntityType.OTHER)
.distinct().collect(Collectors.toList());

return Entry.EntityType.OTHER;
return types.isEmpty() ? Entry.EntityType.OTHER : types.get(0);
}

static Optional<Entry.EntityType> getType(String componentName, Input input) {
Expand Down Expand Up @@ -79,27 +79,22 @@ static Optional<Entry.EntityType> getType(String componentName, Input input) {
return Optional.empty();
}

public static String getComponentName(Entry entry) {
if (entry.getEntityType() != Entry.EntityType.OTHER) {
boolean containMarkupOrStyle = entry.getInputs().stream()
.anyMatch(input -> input.getType() == Input.Type.MARKUP || input.getType() == Input.Type.STYLE);
public static String getComponentName(Bundle bundle) {
List<String> possibleName = bundle.getEntries().stream()
.map(entry -> {
if (entry.getEntityType() != Entry.EntityType.OTHER) {
boolean containMarkupOrStyle = entry.getInputs().stream()
.anyMatch(input -> input.getType() == Input.Type.MARKUP || input.getType() == Input.Type.STYLE);

if (containMarkupOrStyle) {
return extractComponentName(entry.getPath());
}

for (Entry bundledEntry : entry.getBundle().getEntries()) {
containMarkupOrStyle = bundledEntry.getInputs().stream()
.anyMatch(input -> input.getType() == Input.Type.MARKUP || input.getType() == Input.Type.STYLE);

if (containMarkupOrStyle) {
return extractComponentName(bundledEntry.getPath());
}
}
}
if (containMarkupOrStyle) {
return extractComponentName(entry.getPath());
}
}

return extractComponentName(entry.getPath());
}).distinct().collect(Collectors.toList());

return extractComponentName(entry.getPath());
return possibleName.get(0);
}

private static String extractComponentName(String path) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,6 @@ public interface TokenRegistry {

List<DesignToken> getDesignTokens();

Set<String> getValidUtilityClasses();

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collector;
import java.util.stream.Collectors;

Expand All @@ -39,6 +42,33 @@ public Set<ComponentBlueprint> getComponentBlueprints() {
return getComponentsInternal();
}

@Override
public Set<String> getValidUtilityClasses() {
if (this.validUtilityClasses == null) {
this.validUtilityClasses = new HashSet<>();

getComponentBlueprints().stream()
.forEach(componentBlueprint -> {
componentBlueprint.getSelectors().forEach(selector ->
validUtilityClasses.addAll(processTokens(selector)));

componentBlueprint.getTokens().forEach((name, componentDesignToken) -> {
componentDesignToken.getCssSelectors().forEach(selector ->
validUtilityClasses.addAll(processTokens(selector)));
});
});

getUtilityClasses().stream()
.map(utilityClass -> Arrays.asList(utilityClass.getName().split(" ")))
.flatMap(List::stream)
.forEach(s ->
validUtilityClasses.addAll(processTokens(s.trim()))
);
}

return this.validUtilityClasses;
}

@Override
public Optional<ComponentBlueprint> getComponentBlueprint(String component) {
return getComponentsInternal().stream()
Expand Down Expand Up @@ -93,8 +123,10 @@ public void afterPropertiesSet() throws Exception {
private Set<ComponentBlueprint> components;
private Map<String, DesignToken> tokens;
private List<UtilityClass> utilityClasses;
private Set<String> validUtilityClasses;
private ObjectMapper mapper = new ObjectMapper();

private static final String SLDS = "slds-[^\\s,\\[:\\]\\.\";]*";
private static final Pattern SLDSPattern = Pattern.compile(SLDS);

private Set<ComponentBlueprint> getComponentsInternal() {
if (this.components == null) {
Expand Down Expand Up @@ -147,6 +179,18 @@ public Map<String, DesignToken> getDesignTokensInternal() {
return this.tokens;
}

private Set<String> processTokens(String tokens) {
Set<String> possibleTokens = new HashSet<>();

Matcher matcher = SLDSPattern.matcher(tokens);

while(matcher.find()) {
possibleTokens.add(matcher.group());
}

return possibleTokens;
}

private static class TokenStatusDeserializer extends StdDeserializer<TokenStatus> {

public TokenStatusDeserializer() {super(TokenStatus.class);}
Expand Down
Loading

0 comments on commit 25f4430

Please sign in to comment.