diff --git a/src/index.js b/src/index.js index 3e708615..71178f54 100644 --- a/src/index.js +++ b/src/index.js @@ -47,37 +47,20 @@ function readFilePromise (filepath, encoding) { }) } -function prepareForceIncludeForSerialization (forceInclude = []) { +function prepareForceSelectorsForSerialization (forceSelectors = []) { // need to annotate forceInclude values to allow RegExp to pass through JSON serialization - return forceInclude.map(function (forceIncludeValue) { + return forceSelectors.map(function (forceSelectorValue) { if ( - typeof forceIncludeValue === 'object' && - forceIncludeValue.constructor.name === 'RegExp' + typeof forceSelectorValue === 'object' && + forceSelectorValue.constructor.name === 'RegExp' ) { return { type: 'RegExp', - source: forceIncludeValue.source, - flags: forceIncludeValue.flags + source: forceSelectorValue.source, + flags: forceSelectorValue.flags } } - return { value: forceIncludeValue } - }) -} - -function prepareForceExcludeForSerialization (forceExclude = []) { - // need to annotate forceExclude values to allow RegExp to pass through JSON serialization - return forceExclude.map(function (forceExcludeValue) { - if ( - typeof forceExcludeValue === 'object' && - forceExcludeValue.constructor.name === 'RegExp' - ) { - return { - type: 'RegExp', - source: forceExcludeValue.source, - flags: forceExcludeValue.flags - } - } - return { value: forceExcludeValue } + return { value: forceSelectorValue } }) } @@ -95,15 +78,13 @@ const generateCriticalCssWrapped = async function generateCriticalCssWrapped ( // always forceInclude '*', 'html', and 'body' selectors; // yields slight performance improvement - const forceInclude = prepareForceIncludeForSerialization( + const forceInclude = prepareForceSelectorsForSerialization( ['*', '*:before', '*:after', 'html', 'body'].concat( options.forceInclude || [] ) ) - const forceExclude = prepareForceExcludeForSerialization( - ['someStyle'].concat( - options.forceExclude || [] - ) + const forceExclude = prepareForceSelectorsForSerialization( + options.forceExclude || [] ) // promise so we can handle errors and reject, diff --git a/src/selectors-profile.js b/src/selectors-profile.js index 7b1e812d..bd65c373 100644 --- a/src/selectors-profile.js +++ b/src/selectors-profile.js @@ -19,25 +19,14 @@ var pseudoSelectorsToKeepRegex = pseudoSelectorsToKeep // we will replace all instances of these pseudo selectors; hence global flag var PSUEDO_SELECTOR_REGEXP = new RegExp(pseudoSelectorsToKeepRegex, 'g') -function matchesForceInclude (selector, forceInclude) { - return forceInclude.some(function (includeSelector) { - if (includeSelector.type === 'RegExp') { - const { source, flags } = includeSelector +function matchesSelectors (selector, selectors) { + return selectors.some(function (toMatchSelector) { + if (toMatchSelector.type === 'RegExp') { + const { source, flags } = toMatchSelector const re = new RegExp(source, flags) return re.test(selector) } - return includeSelector.value === selector - }) -} - -function matchesForceExclude (selector, forceExclude) { - return forceExclude.some(function (excludeSelector) { - if (excludeSelector.type === 'RegExp') { - const { source, flags } = excludeSelector - const re = new RegExp(source, flags) - return re.test(selector) - } - return excludeSelector.value === selector + return toMatchSelector.value === selector }) } @@ -51,11 +40,11 @@ function normalizeSelector (selectorNode, forceInclude, forceExclude) { // In these cases we test a slightly modified selector instead let modifiedSelector = selector.trim() - if (matchesForceInclude(modifiedSelector, forceInclude)) { + if (matchesSelectors(modifiedSelector, forceInclude)) { return true } - if (matchesForceExclude(modifiedSelector, forceExclude)) { + if (matchesSelectors(modifiedSelector, forceExclude)) { return false } @@ -93,7 +82,11 @@ function normalizeSelector (selectorNode, forceInclude, forceExclude) { return modifiedSelector } -export default async function buildSelectorProfile (ast, forceInclude, forceExclude) { +export default async function buildSelectorProfile ( + ast, + forceInclude, + forceExclude +) { debuglog('buildSelectorProfile START') const selectors = new Set() const selectorNodeMap = new WeakMap() @@ -127,7 +120,11 @@ export default async function buildSelectorProfile (ast, forceInclude, forceExcl // collect selectors and build a map rule.prelude.children.each(selectorNode => { - const selector = normalizeSelector(selectorNode, forceInclude, forceExclude) + const selector = normalizeSelector( + selectorNode, + forceInclude, + forceExclude + ) if (typeof selector === 'string') { selectors.add(selector) }