Skip to content

Commit

Permalink
feat(core): add RegExp support for url filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
harlan-zw committed May 10, 2023
1 parent 5947ee7 commit 4a3be94
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
6 changes: 3 additions & 3 deletions packages/core/src/puppeteer/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type {
UnlighthouseTask,
UnlighthouseWorker, UnlighthouseWorkerStats,
} from '../types'
import { ReportArtifacts, createTaskReportFromRoute } from '../util'
import {ReportArtifacts, createTaskReportFromRoute, asRegExp} from '../util'
import { useUnlighthouse } from '../unlighthouse'
import { useLogger } from '../logger'
import {
Expand Down Expand Up @@ -84,13 +84,13 @@ export async function createUnlighthouseWorker(tasks: Record<UnlighthouseTask, T

if (resolvedConfig.scanner.include) {
// must match
if (resolvedConfig.scanner.include.filter(rule => (new RegExp(rule).test(path))).length === 0)
if (resolvedConfig.scanner.include.filter(rule => asRegExp(rule).test(path)).length === 0)
return
}

if (resolvedConfig.scanner.exclude) {
// must not match
if (resolvedConfig.scanner.exclude.filter(rule => (new RegExp(rule).test(path))).length > 0)
if (resolvedConfig.scanner.exclude.filter(rule => asRegExp(rule).test(path)).length > 0)
return
}

Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,13 +350,13 @@ export interface ResolvedUserConfig {
*
* @see https://unlighthouse.dev/guide/large-sites.html#include-url-patterns
*/
include?: string[]
include?: (string | RegExp)[]
/**
* Paths to ignore from scanning.
*
* @see https://unlighthouse.dev/guide/large-sites.html#exclude-url-patterns
*/
exclude?: string[]
exclude?: (string | RegExp)[]
/**
* Does javascript need to be executed in order to fetch internal links and SEO data.
*
Expand Down
4 changes: 4 additions & 0 deletions packages/core/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,7 @@ export async function fetchUrlRaw(url: string, resolvedConfig: ResolvedUserConfi
}
}
}

export function asRegExp(rule: string | RegExp): RegExp {
return rule instanceof RegExp ? rule : new RegExp(rule)
}

0 comments on commit 4a3be94

Please sign in to comment.