forked from ChromeDevTools/devtools-frontend
-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathgenerate_html_entrypoint.js
More file actions
67 lines (52 loc) · 2.35 KB
/
Copy pathgenerate_html_entrypoint.js
File metadata and controls
67 lines (52 loc) · 2.35 KB
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
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
const fs = require('fs');
const path = require('path');
const {argv} = require('yargs');
const {writeIfChanged} = require('./ninja/write-if-changed.js');
const {template} = argv;
if (!template) {
throw new Error('Must specify --template location with the location of the HTML entrypoint template.');
}
const {outDirectory} = argv;
if (!outDirectory) {
throw new Error('Must specify --out-directory location where the outputs must live.');
}
const {entrypoints} = argv;
if (!entrypoints) {
throw new Error('Must specify at least one entrypoint name.');
}
if (!Array.isArray(entrypoints)) {
throw new Error('Must specify multiple entrypoints as array');
}
const templateContent = fs.readFileSync(template, 'utf-8');
const REACT_NATIVE_ENTRYPOINT_TO_TITLE = new Map([
['rn_inspector', 'DevTools (React Native)'],
['rn_fusebox', 'React Native DevTools']
]);
for (const entrypoint of entrypoints) {
let rewrittenTemplateContent = templateContent.replace(new RegExp('%ENTRYPOINT_NAME%', 'g'), entrypoint);
if (REACT_NATIVE_ENTRYPOINT_TO_TITLE.has(entrypoint)) {
const rewrittenTitle = REACT_NATIVE_ENTRYPOINT_TO_TITLE.get(entrypoint);
rewrittenTemplateContent = rewrittenTemplateContent.replace(
new RegExp('(?<=<title>)(.*)(?=</title>)', 'g'),
rewrittenTitle
);
}
// React DevTools uses Web Workers API for parsing hook names and uploading large tracing files
// https://github.com/facebook/react/blob/fd35655fae9f88284e01754cadb0707abaca795b/packages/react-devtools-shared/src/hooks/parseHookNames/loadSourceAndMetadata.js
// Here we update the CSP header to allow workers from self
if (entrypoint === 'rn_fusebox') {
const cspHeaderRegex = /(?<=<meta http-equiv="Content-Security-Policy" content=")(.*)(?=">)/;
const cspHeaderMatch = rewrittenTemplateContent.match(cspHeaderRegex);
if (cspHeaderMatch === null) {
throw new Error('Couldn\'t find CSP header for rn_fusebox entrypoint: this can break React DevTools panel');
}
rewrittenTemplateContent = rewrittenTemplateContent.replace(
cspHeaderRegex,
'$1; worker-src \'self\' blob:'
);
}
writeIfChanged(path.join(outDirectory, `${entrypoint}.html`), rewrittenTemplateContent);
}