Add path separator to @babel/register sourceRoot (#11249)
This commit is contained in:
parent
78ace99615
commit
6892d51472
@ -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,
|
||||
},
|
||||
|
||||
3
packages/babel-register/test/fixtures/source-map/foo/bar.js
vendored
Normal file
3
packages/babel-register/test/fixtures/source-map/foo/bar.js
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
function add(a, b) {
|
||||
return a + b;
|
||||
}
|
||||
7
packages/babel-register/test/fixtures/source-map/index.js
vendored
Normal file
7
packages/babel-register/test/fixtures/source-map/index.js
vendored
Normal 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)));
|
||||
@ -1,5 +1,6 @@
|
||||
import fs from "fs";
|
||||
import path from "path";
|
||||
import child from "child_process";
|
||||
|
||||
let currentHook;
|
||||
let currentOptions;
|
||||
@ -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 {
|
||||
@ -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,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user