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

Add path separator to @babel/register sourceRoot #11249

Merged
merged 1 commit into from Mar 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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,
andrewdotn marked this conversation as resolved.
Show resolved Hide resolved
...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