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

Performance: Cache missingSupport and refactor Detector #182

Merged
merged 4 commits into from
Aug 23, 2024
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
17 changes: 15 additions & 2 deletions lib/BrowserSelection.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,16 +166,29 @@ export default class BrowserSelection {
return result;
}

/** @type {Map<string, MissingSupportResult>} @readonly */
static #missingSupportCache = new Map();
cmrn marked this conversation as resolved.
Show resolved Hide resolved

/**
* @see BrowserSelection.compileBrowserSupport
* @param {ConstructorParameters<typeof this>} constructorParameters
* @return {MissingSupportResult} `{browsers, features}`
*/
static missingSupport(...constructorParameters) {
const selection = new BrowserSelection(...constructorParameters);
return {
const [query, path] = constructorParameters;
// browserslist only uses `path` if `query` is not provided.
const key = query ? JSON.stringify(query) : path;

if (key && this.#missingSupportCache.has(key)) {
return this.#missingSupportCache.get(key);
}

const selection = new BrowserSelection(query, path);
const result = {
browsers: selection.list(),
features: selection.compileBrowserSupport(),
};
if (key) this.#missingSupportCache.set(key, result);
return result;
}
}
78 changes: 47 additions & 31 deletions lib/Detector.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import FEATURES from '../data/features.js';
import { performFeatureCheck, stripUrls } from '../utils/util.js';

/** @typedef {import('../data/features.js').FeatureKeys} FeatureKeys */
/** @typedef {import('../data/features.js').RuleCheck} RuleCheck */

/**
* @typedef DetectorCallbackArgument
* @prop {!import('postcss').ChildNode} usage
* @prop {keyof FEATURES} feature
* @prop {(keyof FEATURES & string)[]} ignore
* @prop {FeatureKeys} feature
* @prop {(FeatureKeys & string)[]} ignore
*/

/**
Expand All @@ -18,6 +21,35 @@ const PLUGIN_OPTION_COMMENT = 'doiuse-';
const DISABLE_FEATURE_COMMENT = `${PLUGIN_OPTION_COMMENT}disable`;
const ENABLE_FEATURE_COMMENT = `${PLUGIN_OPTION_COMMENT}enable`;

/**
* Normalise a Feature into a RuleCheck function.
* @param {import('../data/features.js').Feature} feature
* @return {RuleCheck}
*/
function normaliseFeature(feature) {
if (typeof feature === 'function') {
return feature;
}
if (Array.isArray(feature)) {
return (child) => feature.some((function_) => function_(child));
}
if (typeof feature === 'object') {
const properties = Object.entries(feature);
return (child) => {
if (child.type !== 'decl') {
return false;
}
return properties.some(([property, value]) => {
if (property !== '' && property !== child.prop) return false;
if (value === true) return true;
if (value === false) return false;
return performFeatureCheck(value, stripUrls(child.value));
});
};
}
throw new TypeError(`Invalid feature definition: ${feature}`);
}

/**
* Detect the use of any of a given list of CSS features.
* ```
Expand All @@ -38,17 +70,18 @@ const ENABLE_FEATURE_COMMENT = `${PLUGIN_OPTION_COMMENT}enable`;
*/
export default class Detector {
/**
* @param {(keyof FEATURES & string)[]} featureList an array of feature slugs (see caniuse-db)
* @param {(FeatureKeys & string)[]} featureList an array of feature slugs (see caniuse-db)
*/
constructor(featureList) {
/** @type {Partial<FEATURES>} */
this.features = {};
for (const feature of featureList) {
if (FEATURES[feature]) {
this.features[feature] = FEATURES[feature];
/** @type {Map<FeatureKeys, RuleCheck>} */
this.features = new Map();
for (const featureName of featureList) {
const feature = FEATURES[featureName];
if (feature != null) {
this.features.set(featureName, normaliseFeature(feature));
}
}
/** @type {(keyof FEATURES & string)[]} */
/** @type {(FeatureKeys & string)[]} */
this.ignore = [];
}

Expand All @@ -66,8 +99,7 @@ export default class Detector {
switch (option) {
case DISABLE_FEATURE_COMMENT: {
if (value === '') {
// @ts-expect-error Skip cast
this.ignore = Object.keys(this.features);
this.ignore = [...this.features.keys()];
} else {
for (const feat of value.split(',')) {
/** @type {any} */
Expand Down Expand Up @@ -104,28 +136,12 @@ export default class Detector {
return;
}

for (const [feat] of Object.entries(this.features).filter(([, featValue]) => {
if (!featValue) return false;
if (typeof featValue === 'function') {
return featValue(child);
}
if (Array.isArray(featValue)) {
return featValue.some((function_) => function_(child));
for (const [feature, ruleCheck] of this.features) {
if (ruleCheck(child)) {
callback({ usage: child, feature, ignore: this.ignore });
}
if (child.type !== 'decl') {
return false;
}

return Object.entries(featValue).some(([property, value]) => {
if (property !== '' && property !== child.prop) return false;
if (value === true) return true;
if (value === false) return false;
return performFeatureCheck(value, stripUrls(child.value));
});
})) {
const feature = /** @type {keyof FEATURES} */ (feat);
callback({ usage: child, feature, ignore: this.ignore });
}

if (child.type !== 'decl') {
this.node(child, callback);
}
Expand Down
Loading