Skip to content

Commit 347d2f4

Browse files
committed
Drop require.js and load d3 directly in browser entry scripts
1 parent d075303 commit 347d2f4

2 files changed

Lines changed: 33 additions & 33 deletions

File tree

tskit_arg_visualizer/alternative_plots/genome_bar.js

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,26 @@
1-
function ensureRequire() {
2-
// Needed e.g. in Jupyter notebooks: if require is already available, return resolved promise
3-
if (typeof require !== 'undefined') {
4-
return Promise.resolve(require);
5-
}
6-
7-
// Otherwise, dynamically load require.js
1+
function loadScript(src) {
82
return new Promise((resolve, reject) => {
93
const script = document.createElement('script');
10-
script.src = 'https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.6/require.min.js';
11-
script.onload = () => resolve(require);
4+
script.src = src;
5+
script.async = true;
6+
script.onload = () => resolve();
127
script.onerror = reject;
138
document.head.appendChild(script);
149
});
15-
};
10+
}
1611

17-
ensureRequire()
18-
.then(require => {
19-
require.config({ paths: {d3: 'https://d3js.org/d3.v7.min'}});
20-
require(["d3"], draw_genome_bar);
21-
})
22-
.catch(err => console.error('Failed to load require.js:', err));
12+
if (typeof d3 !== 'undefined') {
13+
draw_genome_bar(d3);
14+
} else {
15+
loadScript('https://d3js.org/d3.v7.min.js')
16+
.then(() => {
17+
if (typeof d3 === 'undefined') {
18+
throw new Error('D3 loaded but global "d3" is unavailable.');
19+
}
20+
draw_genome_bar(d3);
21+
})
22+
.catch(err => console.error('Failed to load d3 script:', err));
23+
}
2324

2425

2526
function draw_genome_bar(d3) {

tskit_arg_visualizer/visualizer.js

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,16 @@ function parseRgbString(rgbString) {
1010
return null; // Or throw an error for invalid format
1111
}
1212

13-
function ensureRequire() {
14-
// Needed e.g. in Jupyter notebooks: if require is already available, return resolved promise
15-
if (typeof require !== 'undefined') {
16-
return Promise.resolve(require);
17-
}
18-
19-
// Otherwise, dynamically load require.js
13+
function loadScript(src) {
2014
return new Promise((resolve, reject) => {
2115
const script = document.createElement('script');
22-
script.src = 'https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.6/require.min.js';
23-
script.onload = () => resolve(require);
16+
script.src = src;
17+
script.async = true;
18+
script.onload = () => resolve();
2419
script.onerror = reject;
2520
document.head.appendChild(script);
2621
});
27-
};
22+
}
2823

2924
function main_visualizer(
3025
d3,
@@ -1371,12 +1366,16 @@ function main_visualizer(
13711366
can be used to pass in the appropriate data
13721367
*/
13731368

1374-
ensureRequire()
1375-
.then(require => {
1376-
require.config({ paths: {d3: 'https://d3js.org/d3.v7.min'}});
1377-
require(["d3"], function(d3) {
1369+
if (typeof d3 !== 'undefined') {
1370+
main_visualizer(d3, $divnum, $data, $width, $height, $y_axis, $edges, $condense_mutations, $label_mutations, $tree_highlighting, $title, $rotate_tip_labels, $plot_type, $preamble, $source, $save_filename)
1371+
} else {
1372+
loadScript('https://d3js.org/d3.v7.min.js')
1373+
.then(() => {
1374+
if (typeof d3 === 'undefined') {
1375+
throw new Error('D3 loaded but global "d3" is unavailable.');
1376+
}
13781377
main_visualizer(d3, $divnum, $data, $width, $height, $y_axis, $edges, $condense_mutations, $label_mutations, $tree_highlighting, $title, $rotate_tip_labels, $plot_type, $preamble, $source, $save_filename)
1379-
});
1380-
})
1381-
.catch(err => console.error('Failed to load require.js:', err));
1378+
})
1379+
.catch(err => console.error('Failed to load d3 script:', err));
1380+
}
13821381

0 commit comments

Comments
 (0)