Skip to content

Commit

Permalink
Add path separator to @babel/register sourceRoot (#11249)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewdotn committed Mar 18, 2020
1 parent 78ace99 commit 6892d51
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/babel-register/src/node.js
Expand Up @@ -41,7 +41,7 @@ function compile(code, filename) {
const opts = new OptionManager().init(
// sourceRoot can be overwritten
{
sourceRoot: path.dirname(filename),
sourceRoot: path.dirname(filename) + path.sep,
...deepClone(transformOpts),
filename,
},
Expand Down
3 changes: 3 additions & 0 deletions packages/babel-register/test/fixtures/source-map/foo/bar.js
@@ -0,0 +1,3 @@
function add(a, b) {
return a + b;
}
7 changes: 7 additions & 0 deletions packages/babel-register/test/fixtures/source-map/index.js
@@ -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)));
44 changes: 44 additions & 0 deletions packages/babel-register/test/index.js
@@ -1,5 +1,6 @@
import fs from "fs";
import path from "path";
import child from "child_process";

let currentHook;
let currentOptions;
Expand All @@ -8,6 +9,10 @@ let sourceMapSupport = false;
const registerFile = require.resolve("../lib/node");
const testFile = require.resolve("./fixtures/babelrc/es2015");
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", () => {
return {
Expand Down Expand Up @@ -110,6 +115,45 @@ describe("@babel/register", function() {
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 doesn’t 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 couldn’t get
// that working inside a test, possibly because of jest’s 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", () => {
setupRegister({
babelrc: false,
Expand Down

0 comments on commit 6892d51

Please sign in to comment.