Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds caching for rule sets and rule translations #36

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
70 changes: 38 additions & 32 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import translate from './translate.js';
import dictionary from './dictionary.js';
import defaultTheme, { globals } from './theme.js';

import merge from './util/merge.js';
Expand All @@ -16,42 +16,48 @@ const warn = (theme) => (message) => {
console.warn(msg);
};

export const process = (theme) => (strings, values) => {
const translate = (theme, rule) => {
// Split the rule into parts
rule = rule.split(':');
// Seperate out directive from variants
let directive = rule.pop();
let variants = rule;
// Lookup translation in dictionary
let translation = dictionary(theme)(directive);
// Warn if there was no translation for the given directive
if (!Object.keys(translation)[0] || !Object.values(translation)[0]) {
warn(theme)(`No translation for "${directive}"`);
}
// Apply variants to the translation
variants.reverse().forEach((variant) => {
let size = theme.screens[variant];
if (size) {
translation = {
'@media': { [`(min-width: ${size})`]: translation },
};
} else translation = { [`:${variant}`]: translation };
});
return translation;
};

export const process = (theme, cache = {}) => (strings, values) => {
// Normalize rules into an array
const rules = normalize(strings, values);
// Keep track of processed rules this rule set
const seen = {};
// Go through each rule in the array and translate to css
const styles = rules.map((rule) => {
// Warn about any duplicate rule declarations
if (seen[rule]) warn(theme)(`Duplicate declaration of "${rule}"`);
// Mark rule as seen
seen[rule] = true;
// Split the rule into parts
rule = rule.split(':');
// Seperate out directive from variants
let directive = rule.pop();
let variants = rule;
// Lookup translation for directive
let translation = translate(theme)(directive);
// Warn if there was no translation for the given directive
if (!Object.keys(translation)[0] || !Object.values(translation)[0]) {
warn(theme)(`No translation for "${directive}"`);
}
// Apply variants to the translation
variants.reverse().forEach((variant) => {
let size = theme.screens[variant];
if (size) {
translation = {
'@media': { [`(min-width: ${size})`]: translation },
};
} else translation = { [`:${variant}`]: translation };
});
// Return translation with variants applied
return translation;
});
// Deep merge all translations
return merge(...styles);
return (cache[rules] =
cache[rules] ||
merge(
...rules.map((rule) => {
// Warn about any duplicate rule declarations
if (seen[rule]) warn(theme)(`Duplicate declaration of "${rule}"`);
// Mark rule as seen
seen[rule] = true;
// Return translation with variants applied
return (cache[rule] = cache[rule] || translate(theme, rule));
})
));
};

// Utility for merging a provided theme with the default theme
Expand Down
Loading