Skip to content

Commit 592ff13

Browse files
committed
ensure source map can be resolved from cached code
When loading the test file, test workers intercept the require call and load the cached code instead. Libraries like nyc may also be intercepting require calls, however they won't know that different code was loaded. They may then attempt to resolve a source map from the original file location. This commit adds a source map file comment to the cached code. The file path is relative from the directory of the original file to where the source map is cached. Add a test which mimics how nyc resolves the source map.
1 parent 934d57f commit 592ff13

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

lib/caching-precompiler.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ var cachingTransform = require('caching-transform');
44
var md5Hex = require('md5-hex');
55
var stripBom = require('strip-bom');
66
var objectAssign = require('object-assign');
7+
var generateMapFileComment = require('convert-source-map').generateMapFileComment;
78

89
module.exports = CachingPrecompiler;
910

@@ -61,7 +62,20 @@ CachingPrecompiler.prototype._factory = function (babelConfig, cacheDir) {
6162
var result = babel.transform(code, options);
6263
var mapFile = path.join(cacheDir, hash + '.js.map');
6364
fs.writeFileSync(mapFile, JSON.stringify(result.map));
64-
return result.code;
65+
66+
// When loading the test file, test workers intercept the require call and
67+
// load the cached code instead. Libraries like nyc may also be intercepting
68+
// require calls, however they won't know that different code was loaded.
69+
// They may then attempt to resolve a source map from the original file
70+
// location.
71+
//
72+
// Add a source map file comment to the cached code. The file path is
73+
// relative from the directory of the original file to where the source map
74+
// is cached. This will allow the source map to be resolved.
75+
var sourceDir = path.dirname(filename);
76+
var relMapFile = path.relative(sourceDir, mapFile);
77+
var mapFileComment = generateMapFileComment(relMapFile);
78+
return result.code + '\n' + mapFileComment;
6579
};
6680
};
6781

test/caching-precompiler.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ var uniqueTempDir = require('unique-temp-dir');
66
var sinon = require('sinon');
77
var babel = require('babel-core');
88
var transformRuntime = require('babel-plugin-transform-runtime');
9+
var fromMapFileSource = require('convert-source-map').fromMapFileSource;
910

1011
var CachingPrecompiler = require('../lib/caching-precompiler');
1112

@@ -54,6 +55,34 @@ test('adds files and source maps to the cache directory as needed', function (t)
5455
t.end();
5556
});
5657

58+
test('adds a map file comment to the cached files', function (t) {
59+
var tempDir = uniqueTempDir();
60+
var precompiler = new CachingPrecompiler(tempDir, null);
61+
62+
precompiler.precompileFile(fixture('es2015.js'));
63+
64+
var cachedCode;
65+
var cachedMap;
66+
fs.readdirSync(tempDir).map(function (file) {
67+
return path.join(tempDir, file);
68+
}).forEach(function (file) {
69+
if (endsWithJs(file)) {
70+
cachedCode = fs.readFileSync(file, 'utf8');
71+
} else if (endsWithMap(file)) {
72+
cachedMap = fs.readFileSync(file, 'utf8');
73+
}
74+
});
75+
76+
// This is comparable to how nyc resolves the source map. It has access to the
77+
// cached code but believes it to come from the original es2015.js fixture.
78+
// Ensure the cached map can be resolved from the cached code. Also see
79+
// <https://github.com/bcoe/nyc/blob/69ed03b29c423c0fd7bd41f9dc8e7a3a68f7fe50/index.js#L244>.
80+
var foundMap = fromMapFileSource(cachedCode, path.join(__dirname, 'fixture'));
81+
t.ok(foundMap);
82+
t.is(foundMap.toJSON(), cachedMap);
83+
t.end();
84+
});
85+
5786
test('uses default babel options when babelConfig === "default"', function (t) {
5887
var tempDir = uniqueTempDir();
5988
var precompiler = new CachingPrecompiler(tempDir, 'default');

0 commit comments

Comments
 (0)