Skip to content

Commit 49e3c66

Browse files
zelphirpaulRbr
authored andcommitted
Add exclude option
1 parent 88ef70d commit 49e3c66

File tree

4 files changed

+12
-1
lines changed

4 files changed

+12
-1
lines changed

index.js

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const defaultOptions = {
1919
destination: null,
2020
concurrency: 4,
2121
include: ["/"],
22+
exclude: [],
2223
userAgent: "ReactSnap",
2324
// 4 params below will be refactored to one: `puppeteer: {}`
2425
// https://github.com/stereobooster/react-snap/issues/120

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"clean-css": "4.2.1",
1414
"express": "4.17.1",
1515
"express-history-api-fallback": "2.2.1",
16+
"glob-to-regexp": "^0.4.1",
1617
"highland": "2.13.4",
1718
"html-minifier": "4.0.0",
1819
"minimalcss": "0.8.2",

src/puppeteer_utils.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
const puppeteer = require("puppeteer");
22
const _ = require("highland");
33
const url = require("url");
4+
const glob = require("glob-to-regexp");
5+
// @ts-ignore
46
const mapStackTrace = require("sourcemapped-stacktrace-node").default;
57
const path = require("path");
68
const fs = require("fs");
@@ -140,6 +142,7 @@ const crawl = async opt => {
140142
publicPath,
141143
sourceDir
142144
} = opt;
145+
const exclude = options.exclude.map(g => glob(g, { extended: true, globstar: true}));
143146
let shuttingDown = false;
144147
let streamClosed = false;
145148

@@ -173,7 +176,7 @@ const crawl = async opt => {
173176
* @returns {void}
174177
*/
175178
const addToQueue = newUrl => {
176-
const { hostname, search, hash, port } = url.parse(newUrl);
179+
const { hostname, search, hash, port, pathname } = url.parse(newUrl);
177180
newUrl = newUrl.replace(`${search || ""}${hash || ""}`, "");
178181

179182
// Ensures that only link on the same port are crawled
@@ -184,6 +187,7 @@ const crawl = async opt => {
184187
// Port can be null, therefore we need the null check
185188
const isOnAppPort = port && port.toString() === options.port.toString();
186189

190+
if (exclude.filter(regex => regex.test(pathname)).length > 0) return;
187191
if (hostname === "localhost" && isOnAppPort && !uniqueUrls.has(newUrl) && !streamClosed) {
188192
uniqueUrls.add(newUrl);
189193
enqued++;

yarn.lock

+5
Original file line numberDiff line numberDiff line change
@@ -1453,6 +1453,11 @@ getpass@^0.1.1:
14531453
dependencies:
14541454
assert-plus "^1.0.0"
14551455

1456+
glob-to-regexp@^0.4.1:
1457+
version "0.4.1"
1458+
resolved "https://registry.yarnpkg.com/glob-to-regexp/-/glob-to-regexp-0.4.1.tgz#c75297087c851b9a578bd217dd59a92f59fe546e"
1459+
integrity sha512-lkX1HJXwyMcprw/5YUZc2s7DrpAiHB21/V+E1rHUrVNokkvB6bqMzT0VfV6/86ZNabt1k14YOIaT7nDvOX3Iiw==
1460+
14561461
glob@^7.0.5, glob@^7.1.1, glob@^7.1.2:
14571462
version "7.1.2"
14581463
resolved "https://registry.yarnpkg.com/glob/-/glob-7.1.2.tgz#c19c9df9a028702d678612384a6552404c636d15"

0 commit comments

Comments
 (0)