Skip to content

Commit

Permalink
Properly populate cached js filename and fix misaligned columns (#528)
Browse files Browse the repository at this point in the history
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
  • Loading branch information
legendecas and kodiakhq[bot] committed Apr 20, 2020
1 parent a3aef08 commit 9aaee33
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -394,13 +394,16 @@ module.exports = (
assets[filename + '.map'] = { source: JSON.stringify(map), permissions: defaultPermissions };
map = undefined;
}
const columnOffset = -'(function (exports, require, module, __filename, __dirname) { '.length;
code =
`const { readFileSync, writeFileSync } = require('fs'), { Script } = require('vm'), { wrap } = require('module');\n` +
`const source = readFileSync(__dirname + '/${filename}.cache${ext}', 'utf-8');\n` +
`const cachedData = !process.pkg && require('process').platform !== 'win32' && readFileSync(__dirname + '/${filename}.cache');\n` +
`const script = new Script(wrap(source), cachedData ? { cachedData } : {});\n` +
`const basename = __dirname + '/${filename}';\n` +
`const source = readFileSync(basename + '.cache${ext}', 'utf-8');\n` +
`const cachedData = !process.pkg && require('process').platform !== 'win32' && readFileSync(basename + '.cache');\n` +
`const scriptOpts = { filename: basename + '.cache${ext}', columnOffset: ${columnOffset} }\n` +
`const script = new Script(wrap(source), cachedData ? Object.assign({ cachedData }, scriptOpts) : scriptOpts);\n` +
`(script.runInThisContext())(exports, require, module, __filename, __dirname);\n` +
`if (cachedData) process.on('exit', () => { try { writeFileSync(__dirname + '/${filename}.cache', script.createCachedData()); } catch(e) {} });\n`;
`if (cachedData) process.on('exit', () => { try { writeFileSync(basename + '.cache', script.createCachedData()); } catch(e) {} });\n`;
}

if (sourceMap && sourceMapRegister) {
Expand Down
4 changes: 4 additions & 0 deletions test/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
args: ["run", "--v8-cache", "test/integration/test.ts"],
expect: { code: 0 }
},
{
args: ["run", "--v8-cache", "test/integration/stack-trace.js"],
expect: { code: 0 }
},
{
args: ["run", "test/fixtures/error.js"],
expect: { code: 1 }
Expand Down
29 changes: 29 additions & 0 deletions test/integration/stack-trace.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const assert = require('assert');
const childProcess = require('child_process');
const path = require('path');

(function main() {
if (process.env.CHILD !== 'stack-trace') {
const cp = childProcess.fork(__filename, [], {
stdio: 'pipe',
env: Object.assign({}, process.env, {
CHILD: 'stack-trace',
})
});
let stdoutBuffers = []
cp.stdout.on('data', data => stdoutBuffers.push(data));

cp.on('exit', (code) => {
const stdout = Buffer.concat(stdoutBuffers).toString('utf8')
if (code !== 0) {
process.stderr.write(stdout);
return process.exit(1);
}
assert(stdout.match(/evalmachine.<anonymous>/) == null);
assert(stdout.match(path.basename(__filename)) != null);
});
return;
}

console.log(new Error('foo'));
})();

0 comments on commit 9aaee33

Please sign in to comment.