'use strict'; var fs = require('fs'); var util = require('util'); var pluginutils = require('@rollup/pluginutils'); var atob = require('atob'); var urlLib = require('url'); var decodeUriComponentLib = require('decode-uri-component'); function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; } function _interopNamespace(e) { if (e && e.__esModule) return e; var n = Object.create(null); if (e) { Object.keys(e).forEach(function (k) { if (k !== 'default') { var d = Object.getOwnPropertyDescriptor(e, k); Object.defineProperty(n, k, d.get ? d : { enumerable: true, get: function () { return e[k]; } }); } }); } n["default"] = e; return Object.freeze(n); } var fs__default = /*#__PURE__*/_interopDefaultLegacy(fs); var atob__default = /*#__PURE__*/_interopDefaultLegacy(atob); var urlLib__namespace = /*#__PURE__*/_interopNamespace(urlLib); var decodeUriComponentLib__default = /*#__PURE__*/_interopDefaultLegacy(decodeUriComponentLib); function resolveUrl(...args) { return args.reduce((resolved, nextUrl) => urlLib__namespace.resolve(resolved, nextUrl)); } function customDecodeUriComponent(string) { return decodeUriComponentLib__default["default"](string.replace(/\+/g, '%2B')); } function parseMapToJSON(string) { return JSON.parse(string.replace(/^\)\]\}'/, '')); } const sourceMappingURLRegex = RegExp('(?:/\\*(?:\\s*\\r?\\n(?://)?)?(?:[#@] sourceMappingURL=([^\\s\'"]*))\\s*\\*/|//(?:[#@] sourceMappingURL=([^\\s\'"]*)))\\s*'); function getSourceMappingUrl(code) { const match = sourceMappingURLRegex.exec(code); return match ? match[1] || match[2] || '' : null; } async function resolveSourceMap(code, codeUrl, read) { const sourceMappingURL = getSourceMappingUrl(code); if (!sourceMappingURL) { return null; } const dataUri = /^data:([^,;]*)(;[^,;]*)*(?:,(.*))?$/.exec(sourceMappingURL); if (dataUri) { const mimeType = dataUri[1] || 'text/plain'; if (!/^(?:application|text)\/json$/.test(mimeType)) { throw new Error('Unuseful data uri mime type: ' + mimeType); } const map = parseMapToJSON((dataUri[2] === ';base64' ? atob__default["default"] : decodeURIComponent)(dataUri[3] || '')); return { sourceMappingURL, url: null, sourcesRelativeTo: codeUrl, map }; } const url = resolveUrl(codeUrl, sourceMappingURL); const map = parseMapToJSON(String(await read(customDecodeUriComponent(url)))); return { sourceMappingURL, url, sourcesRelativeTo: url, map }; } async function resolveSources(map, mapUrl, read) { const sourcesResolved = []; const sourcesContent = []; for (let index = 0, len = map.sources.length; index < len; index++) { const sourceRoot = map.sourceRoot; const sourceContent = (map.sourcesContent || [])[index]; const resolvePaths = [mapUrl, map.sources[index]]; if (sourceRoot !== undefined && sourceRoot !== '') { resolvePaths.splice(1, 0, sourceRoot.replace(/\/?$/, '/')); } sourcesResolved[index] = resolveUrl(...resolvePaths); if (typeof sourceContent === 'string') { sourcesContent[index] = sourceContent; continue; } try { const source = await read(customDecodeUriComponent(sourcesResolved[index])); sourcesContent[index] = String(source); } catch (error) { sourcesContent[index] = error; } } return { sourcesResolved, sourcesContent }; } function sourcemaps({ include, exclude, readFile = fs__default["default"].readFile, } = {}) { const filter = pluginutils.createFilter(include, exclude); const promisifiedReadFile = util.promisify(readFile); return { name: 'sourcemaps', async load(id) { if (!filter(id)) { return null; } let code; try { code = (await promisifiedReadFile(id)).toString(); } catch (_a) { this.warn('Failed reading file'); return null; } let map; try { const result = await resolveSourceMap(code, id, promisifiedReadFile); // The code contained no sourceMappingURL if (result === null) { return code; } map = result.map; } catch (_b) { this.warn('Failed resolving source map'); return code; } // Resolve sources if they're not included if (map.sourcesContent === undefined) { try { const { sourcesContent } = await resolveSources(map, id, promisifiedReadFile); if (sourcesContent.every(item => typeof item === 'string')) { map.sourcesContent = sourcesContent; } } catch (_c) { this.warn('Failed resolving sources for source map'); } } return { code, map }; }, }; } module.exports = sourcemaps; //# sourceMappingURL=index.cjs.map