Add path separator to @babel/register sourceRoot (#11249)

This commit is contained in:
Andrew Neitsch 2020-03-18 15:34:19 -06:00 committed by GitHub
parent 78ace99615
commit 6892d51472
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 55 additions and 1 deletions

View File

@ -41,7 +41,7 @@ function compile(code, filename) {
const opts = new OptionManager().init( const opts = new OptionManager().init(
// sourceRoot can be overwritten // sourceRoot can be overwritten
{ {
sourceRoot: path.dirname(filename), sourceRoot: path.dirname(filename) + path.sep,
...deepClone(transformOpts), ...deepClone(transformOpts),
filename, filename,
}, },

View File

@ -0,0 +1,3 @@
function add(a, b) {
return a + b;
}

View File

@ -0,0 +1,7 @@
const { retrieveSourceMap } = require('source-map-support');
const path = require.resolve('./foo/bar');
require('./foo/bar');
console.log(JSON.stringify(retrieveSourceMap(path)));

View File

@ -1,5 +1,6 @@
import fs from "fs"; import fs from "fs";
import path from "path"; import path from "path";
import child from "child_process";
let currentHook; let currentHook;
let currentOptions; let currentOptions;
@ -8,6 +9,10 @@ let sourceMapSupport = false;
const registerFile = require.resolve("../lib/node"); const registerFile = require.resolve("../lib/node");
const testFile = require.resolve("./fixtures/babelrc/es2015"); const testFile = require.resolve("./fixtures/babelrc/es2015");
const testFileContent = fs.readFileSync(testFile); const testFileContent = fs.readFileSync(testFile);
const sourceMapTestFile = require.resolve("./fixtures/source-map/index");
const sourceMapNestedTestFile = require.resolve(
"./fixtures/source-map/foo/bar",
);
jest.mock("pirates", () => { jest.mock("pirates", () => {
return { return {
@ -110,6 +115,45 @@ describe("@babel/register", function() {
expect(sourceMapSupport).toBe(false); expect(sourceMapSupport).toBe(false);
}); });
it("returns concatenatable sourceRoot and sources", callback => {
// The Source Maps R3 standard https://sourcemaps.info/spec.html states
// that `sourceRoot` is “prepended to the individual entries in the
// source field.” If `sources` contains file names, and `sourceRoot`
// is intended to refer to a directory but doesnt end with a trailing
// slash, any consumers of the source map are in for a bad day.
//
// The underlying problem seems to only get triggered if one file
// requires() another with @babel/register active, and I couldnt get
// that working inside a test, possibly because of jests mocking
// hooks, so we spawn a separate process.
const args = ["-r", registerFile, sourceMapTestFile];
const spawn = child.spawn(process.execPath, args, { cwd: __dirname });
let output = "";
for (const stream of [spawn.stderr, spawn.stdout]) {
stream.on("data", chunk => {
output += chunk;
});
}
spawn.on("close", function() {
let err;
try {
const sourceMap = JSON.parse(output);
expect(sourceMap.map.sourceRoot + sourceMap.map.sources[0]).toBe(
sourceMapNestedTestFile,
);
} catch (e) {
err = e;
}
callback(err);
});
});
test("hook transpiles with config", () => { test("hook transpiles with config", () => {
setupRegister({ setupRegister({
babelrc: false, babelrc: false,