| 53 | |
| 54 | |
| 55 | class Prefixes: |
| 56 | def __init__(self, args, base_path=None, preserve_deterministic_prefix=True): |
| 57 | prefixes = [] |
| 58 | for p in args: |
| 59 | if '=' in p: |
| 60 | prefix, replacement = p.split('=') |
| 61 | prefixes.append({'prefix': utils.normalize_path(prefix), 'replacement': replacement}) |
| 62 | else: |
| 63 | prefixes.append({'prefix': utils.normalize_path(p), 'replacement': ''}) |
| 64 | self.base_path = utils.normalize_path(base_path) if base_path is not None else None |
| 65 | self.preserve_deterministic_prefix = preserve_deterministic_prefix |
| 66 | self.prefixes = prefixes |
| 67 | self.cache = {} |
| 68 | |
| 69 | def resolve(self, name): |
| 70 | if name in self.cache: |
| 71 | return self.cache[name] |
| 72 | |
| 73 | source = name |
| 74 | if not self.preserve_deterministic_prefix and name.startswith(DETERMINISTIC_PREFIX): |
| 75 | source = EMSCRIPTEN_PREFIX + name.removeprefix(DETERMINISTIC_PREFIX) |
| 76 | |
| 77 | provided = False |
| 78 | for p in self.prefixes: |
| 79 | if source.startswith(p['prefix']): |
| 80 | source = p['replacement'] + source.removeprefix(p['prefix']) |
| 81 | provided = True |
| 82 | break |
| 83 | |
| 84 | # If prefixes were provided, we use that; otherwise if base_path is set, we |
| 85 | # emit a relative path. For files with deterministic prefix, we never use |
| 86 | # a relative path, precisely to preserve determinism, and because it would |
| 87 | # still point to the wrong location, so we leave the filepath untouched to |
| 88 | # let users map it to the proper location using prefix options. |
| 89 | if not (source.startswith(DETERMINISTIC_PREFIX) or provided or self.base_path is None): |
| 90 | try: |
| 91 | source = os.path.relpath(source, self.base_path) |
| 92 | except ValueError: |
| 93 | source = os.path.abspath(source) |
| 94 | source = utils.normalize_path(source) |
| 95 | |
| 96 | self.cache[name] = source |
| 97 | return source |
| 98 | |
| 99 | |
| 100 | # SourceMapPrefixes contains resolver for file names that are: |