Skip to content

Commit

Permalink
feat(testrunner): support sourcemaps (#3459)
Browse files Browse the repository at this point in the history
  • Loading branch information
JoelEinbinder authored Aug 14, 2020
1 parent 4dde288 commit f45791d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
28 changes: 26 additions & 2 deletions test/runner/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string>} */
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
Expand All @@ -38,19 +58,23 @@ 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');

const result = babel.transformFileSync(filename, {
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']
Expand Down

0 comments on commit f45791d

Please sign in to comment.