| 1 | "use strict";
|
|---|
| 2 |
|
|---|
| 3 | Object.defineProperty(exports, "__esModule", {
|
|---|
| 4 | value: true
|
|---|
| 5 | });
|
|---|
| 6 | exports.fetchFromURL = fetchFromURL;
|
|---|
| 7 | exports.flattenSourceMap = flattenSourceMap;
|
|---|
| 8 | exports.getSourceMappingURL = getSourceMappingURL;
|
|---|
| 9 | exports.isURL = isURL;
|
|---|
| 10 |
|
|---|
| 11 | var _path = _interopRequireDefault(require("path"));
|
|---|
| 12 |
|
|---|
| 13 | var _url = _interopRequireDefault(require("url"));
|
|---|
| 14 |
|
|---|
| 15 | var _sourceMapJs = _interopRequireDefault(require("source-map-js"));
|
|---|
| 16 |
|
|---|
| 17 | var _iconvLite = require("iconv-lite");
|
|---|
| 18 |
|
|---|
| 19 | var _parseDataUrl = _interopRequireDefault(require("./parse-data-url"));
|
|---|
| 20 |
|
|---|
| 21 | var _labelsToNames = _interopRequireDefault(require("./labels-to-names"));
|
|---|
| 22 |
|
|---|
| 23 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|---|
| 24 |
|
|---|
| 25 | // Matches only the last occurrence of sourceMappingURL
|
|---|
| 26 | const innerRegex = /\s*[#@]\s*sourceMappingURL\s*=\s*([^\s'"]*)\s*/;
|
|---|
| 27 | /* eslint-disable prefer-template */
|
|---|
| 28 |
|
|---|
| 29 | const sourceMappingURLRegex = RegExp("(?:" + "/\\*" + "(?:\\s*\r?\n(?://)?)?" + "(?:" + innerRegex.source + ")" + "\\s*" + "\\*/" + "|" + "//(?:" + innerRegex.source + ")" + ")" + "\\s*");
|
|---|
| 30 | /* eslint-enable prefer-template */
|
|---|
| 31 |
|
|---|
| 32 | function labelToName(label) {
|
|---|
| 33 | const labelLowercase = String(label).trim().toLowerCase();
|
|---|
| 34 | return _labelsToNames.default[labelLowercase] || null;
|
|---|
| 35 | }
|
|---|
| 36 |
|
|---|
| 37 | async function flattenSourceMap(map) {
|
|---|
| 38 | const consumer = await new _sourceMapJs.default.SourceMapConsumer(map);
|
|---|
| 39 | const generatedMap = map.file ? new _sourceMapJs.default.SourceMapGenerator({
|
|---|
| 40 | file: map.file
|
|---|
| 41 | }) : new _sourceMapJs.default.SourceMapGenerator();
|
|---|
| 42 | consumer.sources.forEach(sourceFile => {
|
|---|
| 43 | const sourceContent = consumer.sourceContentFor(sourceFile, true);
|
|---|
| 44 | generatedMap.setSourceContent(sourceFile, sourceContent);
|
|---|
| 45 | });
|
|---|
| 46 | consumer.eachMapping(mapping => {
|
|---|
| 47 | const {
|
|---|
| 48 | source
|
|---|
| 49 | } = consumer.originalPositionFor({
|
|---|
| 50 | line: mapping.generatedLine,
|
|---|
| 51 | column: mapping.generatedColumn
|
|---|
| 52 | });
|
|---|
| 53 | const mappings = {
|
|---|
| 54 | source,
|
|---|
| 55 | original: {
|
|---|
| 56 | line: mapping.originalLine,
|
|---|
| 57 | column: mapping.originalColumn
|
|---|
| 58 | },
|
|---|
| 59 | generated: {
|
|---|
| 60 | line: mapping.generatedLine,
|
|---|
| 61 | column: mapping.generatedColumn
|
|---|
| 62 | }
|
|---|
| 63 | };
|
|---|
| 64 |
|
|---|
| 65 | if (source) {
|
|---|
| 66 | generatedMap.addMapping(mappings);
|
|---|
| 67 | }
|
|---|
| 68 | });
|
|---|
| 69 | return generatedMap.toJSON();
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | function getSourceMappingURL(code) {
|
|---|
| 73 | const lines = code.split(/^/m);
|
|---|
| 74 | let match;
|
|---|
| 75 |
|
|---|
| 76 | for (let i = lines.length - 1; i >= 0; i--) {
|
|---|
| 77 | match = lines[i].match(sourceMappingURLRegex);
|
|---|
| 78 |
|
|---|
| 79 | if (match) {
|
|---|
| 80 | break;
|
|---|
| 81 | }
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | const sourceMappingURL = match ? match[1] || match[2] || "" : null;
|
|---|
| 85 | return {
|
|---|
| 86 | sourceMappingURL: sourceMappingURL ? decodeURI(sourceMappingURL) : sourceMappingURL,
|
|---|
| 87 | replacementString: match ? match[0] : null
|
|---|
| 88 | };
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | function getAbsolutePath(context, request, sourceRoot) {
|
|---|
| 92 | if (isURL(sourceRoot)) {
|
|---|
| 93 | return new URL(request, sourceRoot).toString();
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | if (sourceRoot) {
|
|---|
| 97 | if (_path.default.isAbsolute(sourceRoot)) {
|
|---|
| 98 | return _path.default.join(sourceRoot, request);
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | return _path.default.join(context, sourceRoot, request);
|
|---|
| 102 | }
|
|---|
| 103 |
|
|---|
| 104 | return _path.default.join(context, request);
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 | function fetchFromDataURL(loaderContext, sourceURL) {
|
|---|
| 108 | const dataURL = (0, _parseDataUrl.default)(sourceURL);
|
|---|
| 109 |
|
|---|
| 110 | if (dataURL) {
|
|---|
| 111 | // https://tools.ietf.org/html/rfc4627
|
|---|
| 112 | // JSON text SHALL be encoded in Unicode. The default encoding is UTF-8.
|
|---|
| 113 | const encodingName = labelToName(dataURL.parameters.get("charset")) || "UTF-8";
|
|---|
| 114 | return (0, _iconvLite.decode)(dataURL.body, encodingName);
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| 117 | throw new Error(`Failed to parse source map from "data" URL: ${sourceURL}`);
|
|---|
| 118 | }
|
|---|
| 119 |
|
|---|
| 120 | async function fetchFromFilesystem(loaderContext, sourceURL) {
|
|---|
| 121 | let buffer;
|
|---|
| 122 |
|
|---|
| 123 | if (isURL(sourceURL)) {
|
|---|
| 124 | return {
|
|---|
| 125 | path: sourceURL
|
|---|
| 126 | };
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 129 | try {
|
|---|
| 130 | buffer = await new Promise((resolve, reject) => {
|
|---|
| 131 | loaderContext.fs.readFile(sourceURL, (error, data) => {
|
|---|
| 132 | if (error) {
|
|---|
| 133 | return reject(error);
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 | return resolve(data);
|
|---|
| 137 | });
|
|---|
| 138 | });
|
|---|
| 139 | } catch (error) {
|
|---|
| 140 | throw new Error(`Failed to parse source map from '${sourceURL}' file: ${error}`);
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 143 | return {
|
|---|
| 144 | path: sourceURL,
|
|---|
| 145 | data: buffer.toString()
|
|---|
| 146 | };
|
|---|
| 147 | }
|
|---|
| 148 |
|
|---|
| 149 | async function fetchPathsFromFilesystem(loaderContext, possibleRequests, errorsAccumulator = "") {
|
|---|
| 150 | let result;
|
|---|
| 151 |
|
|---|
| 152 | try {
|
|---|
| 153 | result = await fetchFromFilesystem(loaderContext, possibleRequests[0], errorsAccumulator);
|
|---|
| 154 | } catch (error) {
|
|---|
| 155 | // eslint-disable-next-line no-param-reassign
|
|---|
| 156 | errorsAccumulator += `${error.message}\n\n`;
|
|---|
| 157 | const [, ...tailPossibleRequests] = possibleRequests;
|
|---|
| 158 |
|
|---|
| 159 | if (tailPossibleRequests.length === 0) {
|
|---|
| 160 | error.message = errorsAccumulator;
|
|---|
| 161 | throw error;
|
|---|
| 162 | }
|
|---|
| 163 |
|
|---|
| 164 | return fetchPathsFromFilesystem(loaderContext, tailPossibleRequests, errorsAccumulator);
|
|---|
| 165 | }
|
|---|
| 166 |
|
|---|
| 167 | return result;
|
|---|
| 168 | }
|
|---|
| 169 |
|
|---|
| 170 | function isURL(value) {
|
|---|
| 171 | return /^[a-z][a-z0-9+.-]*:/i.test(value) && !_path.default.win32.isAbsolute(value);
|
|---|
| 172 | }
|
|---|
| 173 |
|
|---|
| 174 | async function fetchFromURL(loaderContext, context, url, sourceRoot, skipReading = false) {
|
|---|
| 175 | // 1. It's an absolute url and it is not `windows` path like `C:\dir\file`
|
|---|
| 176 | if (isURL(url)) {
|
|---|
| 177 | const {
|
|---|
| 178 | protocol
|
|---|
| 179 | } = _url.default.parse(url);
|
|---|
| 180 |
|
|---|
| 181 | if (protocol === "data:") {
|
|---|
| 182 | if (skipReading) {
|
|---|
| 183 | return {
|
|---|
| 184 | sourceURL: ""
|
|---|
| 185 | };
|
|---|
| 186 | }
|
|---|
| 187 |
|
|---|
| 188 | const sourceContent = fetchFromDataURL(loaderContext, url);
|
|---|
| 189 | return {
|
|---|
| 190 | sourceURL: "",
|
|---|
| 191 | sourceContent
|
|---|
| 192 | };
|
|---|
| 193 | }
|
|---|
| 194 |
|
|---|
| 195 | if (skipReading) {
|
|---|
| 196 | return {
|
|---|
| 197 | sourceURL: url
|
|---|
| 198 | };
|
|---|
| 199 | }
|
|---|
| 200 |
|
|---|
| 201 | if (protocol === "file:") {
|
|---|
| 202 | const pathFromURL = _url.default.fileURLToPath(url);
|
|---|
| 203 |
|
|---|
| 204 | const sourceURL = _path.default.normalize(pathFromURL);
|
|---|
| 205 |
|
|---|
| 206 | const {
|
|---|
| 207 | data: sourceContent
|
|---|
| 208 | } = await fetchFromFilesystem(loaderContext, sourceURL);
|
|---|
| 209 | return {
|
|---|
| 210 | sourceURL,
|
|---|
| 211 | sourceContent
|
|---|
| 212 | };
|
|---|
| 213 | }
|
|---|
| 214 |
|
|---|
| 215 | throw new Error(`Failed to parse source map: '${url}' URL is not supported`);
|
|---|
| 216 | } // 2. It's a scheme-relative
|
|---|
| 217 |
|
|---|
| 218 |
|
|---|
| 219 | if (/^\/\//.test(url)) {
|
|---|
| 220 | throw new Error(`Failed to parse source map: '${url}' URL is not supported`);
|
|---|
| 221 | } // 3. Absolute path
|
|---|
| 222 |
|
|---|
| 223 |
|
|---|
| 224 | if (_path.default.isAbsolute(url)) {
|
|---|
| 225 | let sourceURL = _path.default.normalize(url);
|
|---|
| 226 |
|
|---|
| 227 | let sourceContent;
|
|---|
| 228 |
|
|---|
| 229 | if (!skipReading) {
|
|---|
| 230 | const possibleRequests = [sourceURL];
|
|---|
| 231 |
|
|---|
| 232 | if (url.startsWith("/")) {
|
|---|
| 233 | possibleRequests.push(getAbsolutePath(context, sourceURL.slice(1), sourceRoot));
|
|---|
| 234 | }
|
|---|
| 235 |
|
|---|
| 236 | const result = await fetchPathsFromFilesystem(loaderContext, possibleRequests);
|
|---|
| 237 | sourceURL = result.path;
|
|---|
| 238 | sourceContent = result.data;
|
|---|
| 239 | }
|
|---|
| 240 |
|
|---|
| 241 | return {
|
|---|
| 242 | sourceURL,
|
|---|
| 243 | sourceContent
|
|---|
| 244 | };
|
|---|
| 245 | } // 4. Relative path
|
|---|
| 246 |
|
|---|
| 247 |
|
|---|
| 248 | const sourceURL = getAbsolutePath(context, url, sourceRoot);
|
|---|
| 249 | let sourceContent;
|
|---|
| 250 |
|
|---|
| 251 | if (!skipReading) {
|
|---|
| 252 | const {
|
|---|
| 253 | data
|
|---|
| 254 | } = await fetchFromFilesystem(loaderContext, sourceURL);
|
|---|
| 255 | sourceContent = data;
|
|---|
| 256 | }
|
|---|
| 257 |
|
|---|
| 258 | return {
|
|---|
| 259 | sourceURL,
|
|---|
| 260 | sourceContent
|
|---|
| 261 | };
|
|---|
| 262 | } |
|---|