Lazy-install sourceMapSupport (#6651)
* Install sourceMapSupport only when sourceMaps are requested * Add tests for sourceMapSupport * Fix sourceMap position for tests * Fix gen_error file * fix the test descriptions * Refactor install sourceMap support * Run sourceMapSupport only once * Handle cases where sourceMaps is undefined
This commit is contained in:
committed by
Logan Smyth
parent
aefbb1380e
commit
83cd3fb2c9
@@ -12,21 +12,23 @@ const maps = {};
|
||||
const transformOpts = {};
|
||||
let piratesRevert = null;
|
||||
|
||||
sourceMapSupport.install({
|
||||
handleUncaughtExceptions: false,
|
||||
environment: "node",
|
||||
retrieveSourceMap(source) {
|
||||
const map = maps && maps[source];
|
||||
if (map) {
|
||||
return {
|
||||
url: null,
|
||||
map: map,
|
||||
};
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
},
|
||||
});
|
||||
function installSourceMapSupport() {
|
||||
sourceMapSupport.install({
|
||||
handleUncaughtExceptions: false,
|
||||
environment: "node",
|
||||
retrieveSourceMap(source) {
|
||||
const map = maps && maps[source];
|
||||
if (map) {
|
||||
return {
|
||||
url: null,
|
||||
map: map,
|
||||
};
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
registerCache.load();
|
||||
let cache = registerCache.get();
|
||||
@@ -67,7 +69,7 @@ function compile(code, filename) {
|
||||
// Do not process config files since has already been done with the OptionManager
|
||||
// calls above and would introduce duplicates.
|
||||
babelrc: false,
|
||||
sourceMaps: "both",
|
||||
sourceMaps: opts.sourceMaps === undefined ? "both" : opts.sourceMaps,
|
||||
ast: false,
|
||||
}),
|
||||
);
|
||||
@@ -77,7 +79,12 @@ function compile(code, filename) {
|
||||
result.mtime = mtime(filename);
|
||||
}
|
||||
|
||||
maps[filename] = result.map;
|
||||
if (result.map) {
|
||||
if (Object.keys(maps).length === 0) {
|
||||
installSourceMapSupport();
|
||||
}
|
||||
maps[filename] = result.map;
|
||||
}
|
||||
|
||||
return result.code;
|
||||
}
|
||||
|
||||
4
packages/babel-register/test/__data__/gen_error.js
Normal file
4
packages/babel-register/test/__data__/gen_error.js
Normal file
@@ -0,0 +1,4 @@
|
||||
/* eslint-disable */
|
||||
module.exports = () => { 'use strict'; let error; try { const impossible_result = ''.toFixed(); } catch (e) { error = e; } return error.stack.toString().replace('\n', ''); }
|
||||
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import sourceMapSupport from "source-map-support";
|
||||
import chai from "chai";
|
||||
|
||||
const DATA_ES2015 = require.resolve("./__data__/es2015");
|
||||
const GEN_ERROR = require.resolve("./__data__/gen_error");
|
||||
|
||||
describe("@babel/register", function() {
|
||||
let babelRegister;
|
||||
@@ -38,6 +40,9 @@ describe("@babel/register", function() {
|
||||
const js = require("default-require-extensions/js");
|
||||
oldCompiler = require.extensions[".js"];
|
||||
require.extensions[".js"] = js;
|
||||
sourceMapSupport.install({
|
||||
emptyCacheBetweenOperations: true,
|
||||
});
|
||||
});
|
||||
|
||||
after(() => {
|
||||
@@ -47,6 +52,7 @@ describe("@babel/register", function() {
|
||||
afterEach(() => {
|
||||
revertRegister();
|
||||
delete require.cache[DATA_ES2015];
|
||||
delete require.cache[GEN_ERROR];
|
||||
});
|
||||
|
||||
it("registers correctly", () => {
|
||||
@@ -69,4 +75,32 @@ describe("@babel/register", function() {
|
||||
})
|
||||
.to.throw(SyntaxError);
|
||||
});
|
||||
|
||||
it("does not install source map support if asked not to", () => {
|
||||
setupRegister({
|
||||
sourceMaps: false,
|
||||
});
|
||||
|
||||
let gen_error;
|
||||
chai.expect((gen_error = require(GEN_ERROR))).to.be.ok;
|
||||
chai.expect(gen_error()).to.match(/gen_error\.js:8:34/);
|
||||
});
|
||||
|
||||
it("installs source map support by default", () => {
|
||||
setupRegister();
|
||||
|
||||
let gen_error;
|
||||
chai.expect((gen_error = require(GEN_ERROR))).to.be.ok;
|
||||
chai.expect(gen_error()).to.match(/gen_error\.js:2:86/);
|
||||
});
|
||||
|
||||
it("installs source map support when requested", () => {
|
||||
setupRegister({
|
||||
sourceMaps: "both",
|
||||
});
|
||||
|
||||
let gen_error;
|
||||
chai.expect((gen_error = require(GEN_ERROR))).to.be.ok;
|
||||
chai.expect(gen_error()).to.match(/gen_error\.js:2:86/);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user