UNPKG

@typed/content-hash

Version:

Content hash a directory of HTML/JS/CSS files and other static assets

53 lines 1.63 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.rewriteSourceMapUrl = void 0; const os_1 = require("os"); const path_1 = require("path"); const innerRegex = /[#@] sourceMappingURL=([^\s'"]*)/; const regex = RegExp('(?:' + '/\\*' + '(?:\\s*\r?\n(?://)?)?' + '(?:' + innerRegex.source + ')' + '\\s*' + '\\*/' + '|' + '//(?:' + innerRegex.source + ')' + ')' + '\\s*'); function rewriteSourceMapUrl(ms, hashedPath) { const contents = ms.original; const sourceMapUrl = getSourceMapUrl(contents); const textRange = getSourceMapTextRange(contents); const hashedUrl = path_1.posix.basename(hashedPath); const ext = path_1.posix.extname(hashedUrl); const hashedSourceMapUrl = (ext === '.css' ? cssTemplate(hashedUrl) : jsTemplate(hashedUrl)) + os_1.EOL; if (!sourceMapUrl || !textRange) { ms.append(hashedSourceMapUrl); return; } ms.overwrite(...textRange, hashedSourceMapUrl); } exports.rewriteSourceMapUrl = rewriteSourceMapUrl; function cssTemplate(path) { return `/*# sourceMappingURL=${path}.map *`; } function jsTemplate(path) { return `//# sourceMappingURL=${path}.map`; } function getSourceMapUrl(code) { const match = code.match(regex); return match ? match[1] || match[2] || '' : null; } function getSourceMapTextRange(code) { const match = code.match(regex); if (!match || !match.length || !match.index) { return null; } const start = match.index; return [start, start + match[0].length]; } //# sourceMappingURL=rewriteSourceMapUrl.js.map