Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properly populate cached js filename and fix misaligned columns #528

Merged
merged 2 commits into from
Apr 20, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -393,13 +393,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'));
styfle marked this conversation as resolved.
Show resolved Hide resolved
})();