1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-23 10:30:04 +01:00
metamask-extension/development/sourcemap-validator.js

53 lines
2.2 KiB
JavaScript
Raw Normal View History

2018-04-28 03:17:21 +02:00
const fs = require('fs')
const { SourceMapConsumer } = require('source-map')
2018-07-03 00:56:51 +02:00
const path = require('path')
2018-04-28 03:17:21 +02:00
//
// Utility to help check if sourcemaps are working
//
// searches `dist/chrome/inpage.js` for "new Error" statements
// and prints their source lines using the sourcemaps.
// if not working it may error or print minified garbage
//
start()
2018-07-03 00:56:51 +02:00
2018-07-03 00:49:33 +02:00
async function start () {
2018-07-03 00:56:51 +02:00
const rawBuild = fs.readFileSync(path.join(__dirname, '/../dist/chrome/', 'inpage.js')
, 'utf8')
const rawSourceMap = fs.readFileSync(path.join(__dirname, '/../dist/sourcemaps/', 'inpage.js.map'), 'utf8')
2018-04-28 03:17:21 +02:00
const consumer = await new SourceMapConsumer(rawSourceMap)
console.log('hasContentsOfAllSources:', consumer.hasContentsOfAllSources(), '\n')
console.log('sources:')
consumer.sources.map((sourcePath) => console.log(sourcePath))
console.log('\nexamining "new Error" statements:\n')
const sourceLines = rawBuild.split('\n')
sourceLines.map(line => indicesOf('new Error', line))
.forEach((errorIndices, lineIndex) => {
// if (errorIndex === null) return console.log('line does not contain "new Error"')
errorIndices.forEach((errorIndex) => {
const position = { line: lineIndex + 1, column: errorIndex }
const result = consumer.originalPositionFor(position)
if (!result.source) return console.warn(`!! missing source for position: ${position}`)
// filter out deps distributed minified without sourcemaps
if (result.source === 'node_modules/browserify/node_modules/browser-pack/_prelude.js') return // minified mess
if (result.source === 'node_modules/web3/dist/web3.min.js') return // minified mess
const sourceContent = consumer.sourceContentFor(result.source)
const sourceLines = sourceContent.split('\n')
2018-07-03 00:49:33 +02:00
const line = sourceLines[result.line - 1]
2018-04-28 03:17:21 +02:00
console.log(`\n========================== ${result.source} ====================================\n`)
console.log(line)
console.log(`\n==============================================================================\n`)
})
})
}
2018-07-03 00:49:33 +02:00
function indicesOf (substring, string) {
2018-07-03 00:56:51 +02:00
var a = []
var i = -1
2018-07-03 00:49:33 +02:00
while ((i = string.indexOf(substring, i + 1)) >= 0) a.push(i)
return a
2018-04-28 03:17:21 +02:00
}