From f45791dd8bf8d86ce7718a8421251daae7a89ee7 Mon Sep 17 00:00:00 2001 From: Joel Einbinder Date: Thu, 13 Aug 2020 23:17:46 -0700 Subject: [PATCH] feat(testrunner): support sourcemaps (#3459) --- package.json | 1 + test/runner/transform.js | 28 ++++++++++++++++++++++++++-- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 02dbfa0718..659342f8de 100644 --- a/package.json +++ b/package.json @@ -78,6 +78,7 @@ "pirates": "^4.0.1", "pixelmatch": "^4.0.2", "socksv5": "0.0.6", + "source-map-support": "^0.5.19", "text-diff": "^1.0.1", "ts-loader": "^6.1.2", "typescript": "^3.8.3", diff --git a/test/runner/transform.js b/test/runner/transform.js index c9d7cab257..cf0bcdceef 100644 --- a/test/runner/transform.js +++ b/test/runner/transform.js @@ -19,8 +19,28 @@ const path = require('path'); const fs = require('fs'); const pirates = require('pirates'); const babel = require('@babel/core'); -const version = 0; +const sourceMapSupport = require('source-map-support'); + +const version = 1; const cacheDir = path.join(os.tmpdir(), 'playwright-transform-cache'); +/** @type {Map} */ +const sourceMaps = new Map(); + +sourceMapSupport.install({ + environment: 'node', + handleUncaughtExceptions: false, + retrieveSourceMap(source) { + if (!sourceMaps.has(source)) + return null; + const sourceMapPath = sourceMaps.get(source); + if (!fs.existsSync(sourceMapPath)) + return null; + return { + map: JSON.parse(fs.readFileSync(sourceMapPath, 'utf-8')), + url: source + }; + } +}); /** * @param {string} content @@ -38,6 +58,8 @@ function installTransform() { return pirates.addHook((code, filename) => { const cachePath = calculateCachePath(code, filename); const codePath = cachePath + '.js'; + const sourceMapPath = cachePath + '.map'; + sourceMaps.set(filename, sourceMapPath); if (fs.existsSync(codePath)) return fs.readFileSync(codePath, 'utf8'); @@ -45,12 +67,14 @@ function installTransform() { presets: [ ['@babel/preset-env', {targets: {node: 'current'}}], '@babel/preset-typescript'], + sourceMaps: true, }); if (result.code) { fs.mkdirSync(path.dirname(cachePath), {recursive: true}); + if (result.map) + fs.writeFileSync(sourceMapPath, JSON.stringify(result.map), 'utf8'); fs.writeFileSync(codePath, result.code, 'utf8'); } - // TODO(einbinder) sourcemaps return result.code; }, { exts: ['.ts']