UNPKG

811 BJavaScriptView Raw
1const { isAbsolute, join, dirname } = require('path')
2const { readFileSync } = require('fs')
3/**
4 * Extract the sourcemap url from a source file
5 * reference: https://sourcemaps.info/spec.html
6 * @param {String} file - compilation target file
7 * @returns {String} full path to source map file
8 * @private
9 */
10function getSourceMapFromFile (file) {
11 const fileBody = readFileSync(file).toString()
12 const sourceMapLineRE = /\/\/[#@] ?sourceMappingURL=([^\s'"]+)\s*$/mg
13 const results = fileBody.match(sourceMapLineRE)
14 if (results !== null) {
15 const sourceMap = results[results.length - 1].split('=')[1]
16 if (isAbsolute(sourceMap)) {
17 return sourceMap.trim()
18 } else {
19 const base = dirname(file)
20 return join(base, sourceMap).trim()
21 }
22 }
23}
24
25module.exports = getSourceMapFromFile