forked from matplotlib/matplotlib
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a ponyfill for ResizeObserver on older browsers.
This fixes nbagg on Firefox ESR. Fixes matplotlib#18481.
- Loading branch information
Showing
4 changed files
with
93 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
""" | ||
Script to embed JavaScript dependencies in mpl.js. | ||
""" | ||
|
||
from pathlib import Path | ||
import re | ||
import subprocess | ||
import sys | ||
|
||
|
||
# The list of packages to embed, in some form that `npm install` can use. | ||
JAVASCRIPT_PACKAGES = [ | ||
# Polyfill/ponyfill for ResizeObserver. | ||
'@jsxtools/resize-observer', | ||
] | ||
# This is the magic line that must exist in mpl.js, after which the embedded | ||
# JavaScript will be appended. | ||
MPLJS_MAGIC_HEADER = ( | ||
"///////////////// REMAINING CONTENT GENERATED BY embed_js.py " | ||
"/////////////////\n") | ||
|
||
|
||
def safe_name(name): | ||
""" | ||
Make *name* safe to use as a JavaScript variable name. | ||
""" | ||
return '_'.join(re.split(r'[@/-]', name)).upper() | ||
|
||
|
||
def gen_embedded_lines(web_backend_path): | ||
for pkg in JAVASCRIPT_PACKAGES: | ||
index = web_backend_path / 'node_modules' / pkg / 'index.js' | ||
if not index.exists(): | ||
# Exact version should already be saved in package.json, so we use | ||
# --no-save here. | ||
try: | ||
subprocess.run(['npm', 'install', '--no-save', pkg], | ||
cwd=web_backend_path) | ||
except FileNotFoundError as err: | ||
raise ValueError( | ||
f'npm must be installed to fetch {pkg}') from err | ||
name = safe_name(pkg) | ||
print('Embedding', index, 'as', name) | ||
yield '// prettier-ignore\n' | ||
for line in index.read_text().splitlines(keepends=True): | ||
line = line.replace('module.exports=function', | ||
f'var {name}=function') | ||
yield line | ||
|
||
|
||
def build_mpljs(web_backend_path): | ||
mpljs_path = web_backend_path / "js/mpl.js" | ||
mpljs_orig = mpljs_path.read_text().splitlines(keepends=True) | ||
try: | ||
mpljs_orig = mpljs_orig[:mpljs_orig.index(MPLJS_MAGIC_HEADER) + 1] | ||
except IndexError as err: | ||
raise ValueError( | ||
f'The mpl.js file *must* have the exact line: {MPLJS_MAGIC_HEADER}' | ||
) from err | ||
|
||
with mpljs_path.open('w') as mpljs: | ||
mpljs.writelines(mpljs_orig) | ||
mpljs.writelines(gen_embedded_lines(web_backend_path)) | ||
|
||
|
||
if __name__ == '__main__': | ||
# Write the mpl.js file. | ||
if len(sys.argv) > 1: | ||
web_backend_path = Path(sys.argv[1]) | ||
else: | ||
web_backend_path = (Path(__file__).parent.parent / | ||
"lib/matplotlib/backends/web_backend") | ||
build_mpljs(web_backend_path) |