File: fakewebpack-extract-source-map.py

package info (click to toggle)
ipywidgets 6.0.0-4
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 3,860 kB
  • sloc: python: 2,893; sh: 133; makefile: 126
file content (40 lines) | stat: -rwxr-xr-x 1,164 bytes parent folder | download
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
#!/usr/bin/python3
# Extract sourcesContent from a javascript source map produced by webpack.
#
# Usage: $0 <output.js.map> <extraction directory> > <filelist>
#

import json
import os
import re
import sys

PREFIX = "webpack:///"
FOOTER = re.compile(r"^(.*)\n\n/*\n/[\*/][\s\*]* WEBPACK FOOTER.*$", re.DOTALL)

remove_footer = True


def main(fn, outdir, *args):
    with open(fn) as fp:
        j = json.load(fp)
        pairs = zip(j["sources"], j["sourcesContent"])
        for f, c in pairs:
            if f.startswith(PREFIX):
                p = f[len(PREFIX):].replace("/~/", "/node_modules/")
                assert not os.path.isabs(p)
                print(p)
                p = os.path.join(outdir, p)
                dirname, basename = os.path.split(p)
                if dirname:
                    os.makedirs(dirname, exist_ok=True)
                if remove_footer:
                    c = re.sub(FOOTER, "\\1", c)
                with open(p, 'w') as wfp:
                    wfp.write(c)
            else:
                print("unrecognised; ignoring: %s" % f, file=sys.stderr)


if __name__ == "__main__":
    sys.exit(main(*sys.argv[1:]))