({sourceMappingUrl, resourcePath, readFile})
| 84 | } |
| 85 | |
| 86 | async function getSourceMapFromUrl({sourceMappingUrl, resourcePath, readFile}) { |
| 87 | // ======================================================================= |
| 88 | // This code is almost identical to code located in lbt/bundle/Builder.js |
| 89 | // Please try to update both places when making changes |
| 90 | // ======================================================================= |
| 91 | if (sourceMappingUrl.startsWith("data:")) { |
| 92 | // Data-URI indicates an inline source map |
| 93 | const expectedTypeAndEncoding = "data:application/json;charset=utf-8;base64,"; |
| 94 | if (sourceMappingUrl.startsWith(expectedTypeAndEncoding)) { |
| 95 | const base64Content = sourceMappingUrl.slice(expectedTypeAndEncoding.length); |
| 96 | // Create a resource with a path suggesting it's the source map for the resource |
| 97 | // (which it is but inlined) |
| 98 | return Buffer.from(base64Content, "base64").toString(); |
| 99 | } else { |
| 100 | log.warn( |
| 101 | `Source map reference in resource ${resourcePath} is a data URI but has an unexpected` + |
| 102 | `encoding: ${sourceMappingUrl}. Expected it to start with ` + |
| 103 | `"data:application/json;charset=utf-8;base64,"`); |
| 104 | } |
| 105 | } else if (httpPattern.test(sourceMappingUrl)) { |
| 106 | log.warn(`Source map reference in resource ${resourcePath} is an absolute URL. ` + |
| 107 | `Currently, only relative URLs are supported.`); |
| 108 | } else if (posixPath.isAbsolute(sourceMappingUrl)) { |
| 109 | log.warn(`Source map reference in resource ${resourcePath} is an absolute path. ` + |
| 110 | `Currently, only relative paths are supported.`); |
| 111 | } else { |
| 112 | const sourceMapPath = posixPath.join(posixPath.dirname(resourcePath), sourceMappingUrl); |
| 113 | |
| 114 | try { |
| 115 | const sourceMapContent = await readFile(sourceMapPath); |
| 116 | return sourceMapContent.toString(); |
| 117 | } catch (e) { |
| 118 | // No input source map |
| 119 | log.warn(`Unable to read source map for resource ${resourcePath}: ${e.message}`); |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * @public |
no test coverage detected