react-visualizer 5.2.1 → 5.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -2,7 +2,6 @@
|
|
|
2
2
|
const { parseArgs } = require("node:util");
|
|
3
3
|
const makeVisualizerPage = require("../src/makeVisualizerPage");
|
|
4
4
|
const { writeFileSync } = require("node:fs");
|
|
5
|
-
const assert = require("node:assert");
|
|
6
5
|
const fs = require("node:fs");
|
|
7
6
|
let argv = process.argv.slice(2);
|
|
8
7
|
const startIndex = argv.findIndex((arg) => arg === "makeVisualizerPage");
|
package/dist/src/Visualizer.js
CHANGED
|
@@ -5,33 +5,16 @@ function relativeUrl(from, to) {
|
|
|
5
5
|
return new URL(to, from).href;
|
|
6
6
|
}
|
|
7
7
|
function Visualizer(props) {
|
|
8
|
-
const
|
|
9
|
-
let viewURL = props
|
|
10
|
-
let dataURL = props.dataURL || "";
|
|
11
|
-
let config = props.config || "";
|
|
8
|
+
const currentHref2 = window.location.href;
|
|
9
|
+
let { viewURL, dataURL, config } = props;
|
|
12
10
|
if (viewURL) {
|
|
13
|
-
viewURL = relativeUrl(
|
|
11
|
+
viewURL = relativeUrl(currentHref2, viewURL);
|
|
14
12
|
}
|
|
15
13
|
if (dataURL) {
|
|
16
|
-
dataURL = relativeUrl(
|
|
14
|
+
dataURL = relativeUrl(currentHref2, dataURL);
|
|
17
15
|
}
|
|
18
|
-
if (typeof config
|
|
19
|
-
|
|
20
|
-
config = JSON.parse(JSON.stringify(config));
|
|
21
|
-
const els = config.header.elements;
|
|
22
|
-
for (let i = 0; i < els.length; i++) {
|
|
23
|
-
if (els[i].url) els[i].url = relativeUrl(currentHref, els[i].url);
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
const configJson = JSON.stringify(config);
|
|
27
|
-
if (!configs.has(configJson))
|
|
28
|
-
configs.set(
|
|
29
|
-
configJson,
|
|
30
|
-
URL.createObjectURL(
|
|
31
|
-
new Blob([configJson], { type: "application/json" })
|
|
32
|
-
)
|
|
33
|
-
);
|
|
34
|
-
config = configs.get(configJson);
|
|
16
|
+
if (typeof config !== "string") {
|
|
17
|
+
config = createBlobConfig(config);
|
|
35
18
|
}
|
|
36
19
|
const style = props.style || {
|
|
37
20
|
width: "100%",
|
|
@@ -60,4 +43,25 @@ function Visualizer(props) {
|
|
|
60
43
|
}
|
|
61
44
|
);
|
|
62
45
|
}
|
|
46
|
+
function createBlobConfig(config) {
|
|
47
|
+
if (typeof config === "object" && config !== null) {
|
|
48
|
+
let processedConfig = config;
|
|
49
|
+
if (config.header?.elements) {
|
|
50
|
+
processedConfig = structuredClone(config);
|
|
51
|
+
const els = processedConfig.header.elements;
|
|
52
|
+
for (let i = 0; i < els.length; i++) {
|
|
53
|
+
if (els[i].url) els[i].url = relativeUrl(currentHref, els[i].url);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
const configJson = JSON.stringify(processedConfig);
|
|
57
|
+
if (configs.has(configJson)) {
|
|
58
|
+
return configs.get(configJson);
|
|
59
|
+
}
|
|
60
|
+
const blob = URL.createObjectURL(
|
|
61
|
+
new Blob([configJson], { type: "application/json" })
|
|
62
|
+
);
|
|
63
|
+
configs.set(configJson, blob);
|
|
64
|
+
}
|
|
65
|
+
return null;
|
|
66
|
+
}
|
|
63
67
|
module.exports = React.memo(Visualizer);
|
|
@@ -6,10 +6,16 @@ function makeVisualizerPage(options = {}) {
|
|
|
6
6
|
fallbackVersion = "latest",
|
|
7
7
|
queryType = "fragment",
|
|
8
8
|
loadversion = "none",
|
|
9
|
-
|
|
9
|
+
config = "",
|
|
10
10
|
viewURL = "",
|
|
11
11
|
scripts = []
|
|
12
12
|
} = options;
|
|
13
|
+
let configTemplate = "";
|
|
14
|
+
if (typeof config === "string") {
|
|
15
|
+
configTemplate = `'${config}'`;
|
|
16
|
+
} else if (typeof config === "object" && typeof config !== null) {
|
|
17
|
+
configTemplate = JSON.stringify(config);
|
|
18
|
+
}
|
|
13
19
|
validateQueryType(queryType);
|
|
14
20
|
validateLoadVersion(loadversion);
|
|
15
21
|
const scriptsStr = scripts.reduce(function(value, script) {
|
|
@@ -23,7 +29,7 @@ function makeVisualizerPage(options = {}) {
|
|
|
23
29
|
throw new Error("script must have url or content");
|
|
24
30
|
}
|
|
25
31
|
}, "");
|
|
26
|
-
return template.replace("{{ cdn }}", cdn).replace("{{ fallbackVersion }}", fallbackVersion).replace("{{ scripts }}", scriptsStr).replace("{{ queryType }}", queryType).replace("{{ loadversion }}", loadversion).replace("{{
|
|
32
|
+
return template.replace("{{ cdn }}", cdn).replace("{{ fallbackVersion }}", fallbackVersion).replace("{{ scripts }}", scriptsStr).replace("{{ queryType }}", queryType).replace("{{ loadversion }}", loadversion).replace("{{ viewURL }}", viewURL).replace("{{ config }}", configTemplate);
|
|
27
33
|
}
|
|
28
34
|
module.exports = makeVisualizerPage;
|
|
29
35
|
const validLoadversion = ["none", "exact", "latest-major"];
|
|
@@ -34,7 +34,7 @@ window.onload = function () {
|
|
|
34
34
|
const queryType = '{{ queryType }}';
|
|
35
35
|
const query = queryType === 'fragment' ? window.location.hash : window.location.search;
|
|
36
36
|
const search = new URLSearchParams(query.startsWith("#") ? query.slice(1) : query);
|
|
37
|
-
const viewURL = search.get('viewURL');
|
|
37
|
+
const viewURL = search.get('viewURL') || '{{ viewURL }}';
|
|
38
38
|
const v = search.get('v');
|
|
39
39
|
const loadversion = search.get('loadversion') || '{{ loadversion }}';
|
|
40
40
|
const fallbackVersion = search.get('fallbackVersion') || '{{ fallbackVersion }}';
|
|
@@ -72,8 +72,14 @@ window.onload = function () {
|
|
|
72
72
|
|
|
73
73
|
function addVisualizer(version) {
|
|
74
74
|
const cdn = '{{ cdn }}';
|
|
75
|
-
|
|
76
|
-
const
|
|
75
|
+
let configURL = '';
|
|
76
|
+
const config = {{ config }};
|
|
77
|
+
if(typeof config === 'string') {
|
|
78
|
+
configURL = config;
|
|
79
|
+
} else if(typeof config === 'object' && config !== null) {
|
|
80
|
+
configURL = createBlobConfig(config);
|
|
81
|
+
}
|
|
82
|
+
|
|
77
83
|
const visualizer = document.createElement('script');
|
|
78
84
|
const datamain = cdn + '/' + version + '/init';
|
|
79
85
|
const requirejs = cdn + '/' + version + '/components/requirejs/require.js';
|
|
@@ -91,6 +97,26 @@ window.onload = function () {
|
|
|
91
97
|
visualizer.setAttribute('src', requirejs);
|
|
92
98
|
document.head.appendChild(visualizer);
|
|
93
99
|
}
|
|
100
|
+
|
|
101
|
+
function createBlobConfig(config) {
|
|
102
|
+
if (typeof config === 'object' && config !== null) {
|
|
103
|
+
let processedConfig = config;
|
|
104
|
+
if (config.header?.elements) {
|
|
105
|
+
processedConfig = structuredClone(config);
|
|
106
|
+
const elements = processedConfig.header.elements;
|
|
107
|
+
for (let i = 0; i < elements.length; i++) {
|
|
108
|
+
// Transform relative URLs to absolute urls using the parent's location
|
|
109
|
+
// Parent points to self if called on the top window
|
|
110
|
+
if (elements[i].url) elements[i].url = new URL(elements[i].url, window.parent.location.href).href;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
const configJson = JSON.stringify(processedConfig);
|
|
114
|
+
return URL.createObjectURL(
|
|
115
|
+
new Blob([configJson], { type: 'application/json' }),
|
|
116
|
+
);
|
|
117
|
+
}
|
|
118
|
+
return null;
|
|
119
|
+
}
|
|
94
120
|
|
|
95
121
|
async function fetchUrl(url) {
|
|
96
122
|
const response = await fetch(url, {credentials: 'include'});
|