launch.js 1.0 KB

123456789101112131415161718192021222324252627282930313233343536
  1. const fs = require('fs');
  2. const puppeteer = require('puppeteer');
  3. const stacktraceParser = require('stacktrace-parser');
  4. function convertStack(frames) {
  5. return frames.map((item) => {
  6. var match = item.file.match(/^.*\/(.*?)$/);
  7. var fileName, absPath;
  8. if (match && match[1]) {
  9. fileName = match[1];
  10. absPath = 'http://example.com/' + fileName;
  11. } else {
  12. fileName = absPath = item.file;
  13. }
  14. return {
  15. abs_path: absPath,
  16. filename: fileName,
  17. lineno: item.lineNumber,
  18. colno: item.column,
  19. 'function': item.methodName,
  20. };
  21. });
  22. }
  23. (async() => {
  24. const browser = await puppeteer.launch();
  25. const page = await browser.newPage();
  26. await page.goto('file://' + __dirname + '/index.html');
  27. const stack = await page.evaluate('produceStack()');
  28. const frames = convertStack(stacktraceParser.parse(stack));
  29. frames.pop();
  30. const stackDump = JSON.stringify(frames, null, 2) + '\n';
  31. fs.writeFile('minifiedError.json', stackDump, function() {
  32. browser.close();
  33. });
  34. })();