Skip to content

Commit f0f0c3b

Browse files
committed
Allow testOptions to disable source maps
To be honest I'm not really sure if this is useful, but it allows us to more easily test edge-case behavior where source maps are missing. Fixes #1804.
1 parent 4c6eef3 commit f0f0c3b

File tree

3 files changed

+60
-25
lines changed

3 files changed

+60
-25
lines changed

lib/babel-pipeline.js

+15-10
Original file line numberDiff line numberDiff line change
@@ -122,13 +122,13 @@ function build(projectDir, cacheDir, userOptions, compileEnhancements) {
122122
const partialTestConfig = babel.loadPartialConfig(Object.assign({
123123
babelrc: false,
124124
babelrcRoots: [projectDir],
125-
configFile: false
125+
configFile: false,
126+
sourceMaps: true
126127
}, userOptions && userOptions.testOptions, {
127128
cwd: projectDir,
128129
envName,
129130
inputSourceMap,
130-
filename,
131-
sourceMaps: true
131+
filename
132132
}));
133133

134134
// TODO: Check for `partialTestConfig.config` and include a hash of the file
@@ -178,14 +178,19 @@ function build(projectDir, cacheDir, userOptions, compileEnhancements) {
178178

179179
const {code, map} = babel.transformSync(inputCode, options);
180180

181-
// Save source map
182-
const mapPath = `${cachePath}.map`;
183-
writeFileAtomic.sync(mapPath, JSON.stringify(map));
181+
if (map) {
182+
// Save source map
183+
const mapPath = `${cachePath}.map`;
184+
writeFileAtomic.sync(mapPath, JSON.stringify(map));
185+
186+
// Append source map comment to transformed code so that other libraries
187+
// (like nyc) can find the source map.
188+
const comment = convertSourceMap.generateMapFileComment(mapPath);
189+
writeFileAtomic.sync(cachePath, `${code}\n${comment}`);
190+
} else {
191+
writeFileAtomic.sync(cachePath, code);
192+
}
184193

185-
// Append source map comment to transformed code so that other libraries
186-
// (like nyc) can find the source map.
187-
const comment = convertSourceMap.generateMapFileComment(mapPath);
188-
writeFileAtomic.sync(cachePath, `${code}\n${comment}`);
189194
return cachePath;
190195
};
191196
}

lib/worker/precompiler-hook.js

+18-15
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,25 @@ const sourceMapSupport = require('source-map-support');
44
const installPrecompiler = require('require-precompiled');
55
const options = require('./options').get();
66

7-
const sourceMapCache = new Map();
8-
97
function installSourceMapSupport() {
108
sourceMapSupport.install({
119
environment: 'node',
1210
handleUncaughtExceptions: false,
13-
retrieveSourceMap(source) {
14-
if (sourceMapCache.has(source)) {
15-
return {
16-
url: source,
17-
map: fs.readFileSync(sourceMapCache.get(source), 'utf8')
18-
};
11+
retrieveSourceMap(url) {
12+
const precompiled = options.precompiled[url];
13+
if (!precompiled) {
14+
return null;
15+
}
16+
17+
try {
18+
const map = fs.readFileSync(`${precompiled}.map`, 'utf8');
19+
return {url, map};
20+
} catch (err) {
21+
if (err.code === 'ENOENT') {
22+
return null;
23+
}
24+
25+
throw err;
1926
}
2027
}
2128
});
@@ -26,13 +33,9 @@ function install() {
2633

2734
installPrecompiler(filename => {
2835
const precompiled = options.precompiled[filename];
29-
30-
if (precompiled) {
31-
sourceMapCache.set(filename, `${precompiled}.map`);
32-
return fs.readFileSync(precompiled, 'utf8');
33-
}
34-
35-
return null;
36+
return precompiled ?
37+
fs.readFileSync(precompiled, 'utf8') :
38+
null;
3639
});
3740
}
3841
exports.install = install;

test/api.js

+27
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,33 @@ test('stack traces for exceptions are corrected using a source map file', t => {
373373
});
374374
});
375375

376+
test('babel.testOptions can disable sourceMaps', t => {
377+
t.plan(3);
378+
379+
const api = apiCreator({
380+
babelConfig: {
381+
testOptions: {
382+
sourceMaps: false
383+
}
384+
},
385+
cacheEnabled: true
386+
});
387+
388+
api.on('run', plan => {
389+
plan.status.on('stateChange', evt => {
390+
if (evt.type === 'uncaught-exception') {
391+
t.match(evt.err.message, /Thrown by source-map-fixtures/);
392+
t.match(evt.err.stack, /^.*?Immediate\b.*source-map-file.js:7.*$/m);
393+
}
394+
});
395+
});
396+
397+
return api.run([path.join(__dirname, 'fixture/source-map-file.js')])
398+
.then(runStatus => {
399+
t.is(runStatus.stats.passedTests, 1);
400+
});
401+
});
402+
376403
test('stack traces for exceptions are corrected using a source map file in what looks like a browser env', t => {
377404
t.plan(4);
378405

0 commit comments

Comments
 (0)