forked from stereobooster/react-snap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
317 lines (298 loc) · 9.98 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
const crawl = require("./src/puppeteer_utils.js").crawl;
const http = require("http");
const express = require("express");
const serveStatic = require("serve-static");
// @ts-ignore
const fallback = require("express-history-api-fallback");
const path = require("path");
const fs = require("fs");
const mkdirp = require("mkdirp");
const minify = require("html-minifier").minify;
const url = require("url");
// @ts-ignore https://github.com/peterbe/minimalcss/pull/30
const minimalcss = require("minimalcss");
const defaultOptions = {
port: 45678,
source: "build",
destination: null,
concurrency: 4,
viewport: false,
include: ["/", "/404"],
removeStyleTags: false,
inlineCss: false, // experimental
sourceMaps: false, // experimental
preloadResources: false,
headless: true,
userAgent: "ReactSnap",
saveAs: "html",
crawl: true,
waitFor: false,
externalServer: false,
// workaround for https://github.com/geelen/react-snapshot/issues/66#issuecomment-331718560
fixWebpackChunksIssue: false, // experimental
skipThirdPartyRequests: false,
asyncJs: false, //add async true to scripts and move them to the header, to start download earlier
minifyOptions: {
minifyCSS: true,
collapseBooleanAttributes: true,
collapseWhitespace: true,
collapseInlineTagWhitespace: true,
decodeEntities: true,
keepClosingSlash: true,
sortAttributes: true,
sortClassName: true
}
};
/**
*
* @param {{source: ?string, destination: ?string, include: ?Array<string>, sourceMaps: ?boolean, skipThirdPartyRequests: ?boolean }} userOptions
* @return {*}
*/
const defaults = userOptions => {
const options = {
...defaultOptions,
...userOptions
};
options.destination = options.destination || options.source;
if (!options.include || !options.include.length)
throw new Error("include should be an array");
return options;
};
/**
*
* @param {{page: Page, basePath: string}} opt
*/
const preloadResources = opt => {
const { page, basePath } = opt;
const uniqueResources = {};
page.on("response", async response => {
// TODO: this can be improved
const responseUrl = response.url;
if (/^data:/i.test(responseUrl)) return;
const ct = response.headers["content-type"] || "";
const route = responseUrl.replace(basePath, "");
if (/^http:\/\/localhost/i.test(responseUrl)) {
if (uniqueResources[responseUrl]) return;
if (/\.(png|jpg|jpeg|webp|gif)$/.test(responseUrl)) {
await page.evaluate(route => {
var linkTag = document.createElement("link");
linkTag.setAttribute("rel", "preload");
linkTag.setAttribute("as", "image");
linkTag.setAttribute("href", route);
document.body.appendChild(linkTag);
}, route);
} else if (ct.indexOf("json") > -1) {
const text = await response.text();
await page.evaluate(
(route, text) => {
var scriptTag = document.createElement("script");
scriptTag.type = "text/javascript";
scriptTag.text = [
'window.snapStore = window.snapStore || {}; window.snapStore["',
route,
'"] = ',
text,
";"
].join("");
document.body.appendChild(scriptTag);
},
route,
text
);
}
uniqueResources[responseUrl] = true;
} else {
const urlObj = url.parse(responseUrl);
const domain = `${urlObj.protocol}//${urlObj.host}`;
if (uniqueResources[domain]) return;
await page.evaluate(route => {
var linkTag = document.createElement("link");
linkTag.setAttribute("rel", "preconnect");
linkTag.setAttribute("href", route);
document.head.appendChild(linkTag);
}, domain);
uniqueResources[domain] = true;
}
});
};
const removeStyleTags = ({ page }) =>
page.evaluate(() => {
var x = Array.from(document.querySelectorAll("style"));
for (var i = x.length - 1; i >= 0; i--) {
const ell = x[i];
ell.parentElement && ell.parentElement.removeChild(ell);
}
});
const preloadPolyfill = fs.readFileSync(
`${__dirname}/vendor/preload_polyfill.min.js`,
"utf8"
);
/**
* @param {{page: Page, pageUrl: string, options: {skipThirdPartyRequests: boolean}, basePath: string}} opt
* @return {Promise}
*/
const inlineCss = async opt => {
const { page, pageUrl, options, basePath } = opt;
const minimalcssResult = await minimalcss.minimize({
urls: [pageUrl],
skippable: request =>
options.skipThirdPartyRequests && !request.url.startsWith(basePath)
});
const cssText = minimalcssResult.finalCss;
console.log("inline css", cssText.length);
return page.evaluate(
(cssText, preloadPolyfill) => {
var head = document.head || document.getElementsByTagName("head")[0],
style = document.createElement("style");
style.type = "text/css";
style.appendChild(document.createTextNode(cssText));
head.appendChild(style);
var stylesheets = Array.from(
document.querySelectorAll("link[rel=stylesheet]")
);
stylesheets.forEach(link => {
// TODO: this doesn't work
// var wrap = document.createElement('div');
// wrap.appendChild(link.cloneNode(false));
// var noscriptTag = document.createElement('noscript');
// noscriptTag.innerHTML = wrap.innerHTML;
// document.head.appendChild(noscriptTag);
link.parentNode && link.parentNode.removeChild(link);
link.setAttribute("rel", "preload");
link.setAttribute("as", "style");
link.setAttribute("onload", "this.rel='stylesheet'");
document.head.appendChild(link);
});
var scriptTag = document.createElement("script");
scriptTag.type = "text/javascript";
scriptTag.text = preloadPolyfill;
// scriptTag.id = "preloadPolyfill";
document.body.appendChild(scriptTag);
},
cssText,
preloadPolyfill
);
};
const asyncJs = ({ page }) => {
return page.evaluate(() => {
Array.from(document.querySelectorAll("script[src]")).forEach(x => {
x.parentNode && x.parentNode.removeChild(x);
x.setAttribute("async", "true");
document.head.appendChild(x);
});
});
};
const fixWebpackChunksIssue = ({ page, basePath, asyncJs }) => {
return page.evaluate(
(basePath, asyncJs) => {
const localScripts = Array.from(document.scripts).filter(
x => x.src && x.src.startsWith(basePath)
);
const mainRegexp = /main\.[\w]{8}.js/;
const mainScript = localScripts.filter(x => mainRegexp.test(x.src))[0];
const chunkRegexp = /\.[\w]{8}\.chunk\.js/;
const chunkSripts = localScripts.filter(x => chunkRegexp.test(x.src));
chunkSripts.forEach(x => {
if (x.parentElement && mainScript.parentNode) {
x.parentElement.removeChild(x);
if (asyncJs) {
x.setAttribute("async", "true");
}
mainScript.parentNode.insertBefore(x, mainScript.nextSibling);
}
});
},
basePath,
asyncJs
);
};
const saveAsHtml = async ({ page, filePath, options, route }) => {
const content = await page.evaluate(() => document.documentElement.outerHTML);
const minifiedContent = options.minifyOptions
? minify(content, options.minifyOptions)
: content;
filePath = filePath.replace(/\//g, path.sep);
if (filePath.endsWith(path.sep)) {
mkdirp.sync(filePath);
fs.writeFileSync(path.join(filePath, "index.html"), minifiedContent);
} else {
mkdirp.sync(path.dirname(filePath));
fs.writeFileSync(`${filePath}.html`, minifiedContent);
}
};
const saveAsPng = ({ page, filePath, options, route }) => {
mkdirp.sync(path.dirname(filePath));
let screenshotPath;
if (route === "/") {
screenshotPath = `${filePath}/index.png`;
} else {
screenshotPath = `${filePath.replace(/\/$/, "")}.png`;
}
return page.screenshot({ path: screenshotPath });
};
const run = async userOptions => {
const options = defaults(userOptions);
const sourceDir = path.normalize(`${process.cwd()}/${options.source}`);
const destinationDir = path.normalize(
`${process.cwd()}/${options.destination}`
);
const startServer = options => {
const app = express()
.use(serveStatic(sourceDir))
.use(fallback("200.html", { root: sourceDir }));
const server = http.createServer(app);
server.listen(options.port);
return server;
};
fs
.createReadStream(path.join(sourceDir, "index.html"))
.pipe(fs.createWriteStream(path.join(sourceDir, "200.html")));
if (destinationDir !== sourceDir && options.saveAs === "html") {
mkdirp.sync(destinationDir);
fs
.createReadStream(path.join(sourceDir, "index.html"))
.pipe(fs.createWriteStream(path.join(destinationDir, "200.html")));
}
const server = options.externalServer ? null : startServer(options);
const basePath = `http://localhost:${options.port}`;
await crawl({
options,
basePath,
beforeFetch: async ({ page }) => {
if (options.preloadResources) preloadResources({ page, basePath });
},
afterFetch: async ({ page, route }) => {
const pageUrl = `${basePath}${route}`;
if (options.removeStyleTags) await removeStyleTags({ page });
if (options.inlineCss)
await inlineCss({
page,
pageUrl,
options,
basePath
});
if (options.fixWebpackChunksIssue) {
await fixWebpackChunksIssue({
page,
basePath,
asyncJs: options.asyncJs
});
} else if (options.asyncJs) {
await asyncJs({ page });
}
const filePath = path.join(destinationDir, route);
if (options.saveAs === "html") {
await saveAsHtml({ page, filePath, options, route });
} else if (options.saveAs === "png") {
await saveAsPng({ page, filePath, options, route });
} else {
throw new Error(`Unexpected value for saveAs: ${options.saveAs}`);
}
},
onEnd: () => {
if (server) server.close();
}
});
};
exports.defaultOptions = defaultOptions;
exports.run = run;