Skip to content
Merged
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
11 changes: 10 additions & 1 deletion lib/rules/classnames-order.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ const createContextFallback = require('tailwindcss/lib/lib/setupContextUtils').c
// messageId will still be usable in tests.
const INVALID_CLASSNAMES_ORDER_MSG = 'Invalid Tailwind CSS classnames order';

const contextFallbackCache = new WeakMap();

module.exports = {
meta: {
docs: {
Expand Down Expand Up @@ -88,7 +90,14 @@ module.exports = {
const removeDuplicates = getOption(context, 'removeDuplicates');

const mergedConfig = customConfig.resolve(twConfig);
const contextFallback = officialSorting ? createContextFallback(mergedConfig) : null;
const contextFallback = officialSorting
? (
// Set the created contextFallback in the cache if it does not exist yet.
contextFallbackCache.has(mergedConfig)
? contextFallbackCache
: contextFallbackCache.set(mergedConfig, createContextFallback(mergedConfig))
).get(mergedConfig)
: null;

//----------------------------------------------------------------------
// Helpers
Expand Down
15 changes: 12 additions & 3 deletions lib/util/groupMethods.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,14 +261,23 @@ function generateOptions(propName, keys, config, isNegative = false) {
}
}

const cachedRegexes = new WeakMap();

/**
* Customize the regex based on config
*
* @param {String} re Regular expression
* @param {Object} config The merged Tailwind CSS config
* @returns {String} Patched version with config values and additinal parameters
* @returns {String} Patched version with config values and additional parameters
*/
function patchRegex(re, config) {
if (!cachedRegexes.has(config)) {
cachedRegexes.set(config, {});
}
const cache = cachedRegexes.get(config);
if (re in cache) {
return cache[re];
}
let patched = '\\!?';
// Prefix
if (config.prefix.length) {
Expand All @@ -281,7 +290,7 @@ function patchRegex(re, config) {
const resArray = [...res];
const props = resArray.map((arr) => arr[1]);
if (props.length === 0) {
return `${patched}(${replaced})`;
return cache[re] = `${patched}(${replaced})`;
}
// e.g. backgroundColor, letterSpacing, -margin...
props.forEach((prop) => {
Expand Down Expand Up @@ -336,7 +345,7 @@ function patchRegex(re, config) {
const opts = generateOptions(absoluteProp, keys, config, isNegative);
replaced = replaced.replace(token, opts);
});
return `${patched}(${replaced})`;
return cache[re] = `${patched}(${replaced})`;
}

/**
Expand Down