Skip to content

Commit

Permalink
tidy up forceExclude code addition
Browse files Browse the repository at this point in the history
  • Loading branch information
pocketjoso committed May 11, 2019
1 parent bbc4d3a commit c8acaaa
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 49 deletions.
39 changes: 10 additions & 29 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
})
}

Expand All @@ -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,
Expand Down
37 changes: 17 additions & 20 deletions src/selectors-profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
}

Expand All @@ -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
}

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit c8acaaa

Please sign in to comment.