Skip to content

Commit 5c1b7a9

Browse files
coreyfarrellJaKXz
authored andcommitted
feat: Use source base name to prefix cache files (#1144)
1 parent fc1bbbf commit 5c1b7a9

File tree

4 files changed

+57
-3
lines changed

4 files changed

+57
-3
lines changed

index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,10 @@ class NYC {
9494
}
9595

9696
_createTransform (ext) {
97-
var opts = {
97+
const opts = {
9898
salt: Hash.salt(this.config),
9999
hashData: (input, metadata) => [metadata.filename],
100+
filenamePrefix: metadata => path.parse(metadata.filename).name + '-',
100101
onHash: (input, metadata, hash) => {
101102
this.hashCache[metadata.filename] = hash
102103
},

lib/source-maps.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ function SourceMaps (opts) {
1111
this._sourceMapCache = libSourceMaps.createSourceMapStore()
1212
}
1313

14+
SourceMaps.prototype.cachedPath = function (source, hash) {
15+
return path.join(
16+
this.cacheDirectory,
17+
`${path.parse(source).name}-${hash}.map`
18+
)
19+
}
20+
1421
SourceMaps.prototype.purgeCache = function () {
1522
this._sourceMapCache = libSourceMaps.createSourceMapStore()
1623
this.loadedMaps = {}
@@ -20,7 +27,7 @@ SourceMaps.prototype.extractAndRegister = function (code, filename, hash) {
2027
var sourceMap = convertSourceMap.fromSource(code) || convertSourceMap.fromMapFileSource(code, path.dirname(filename))
2128
if (sourceMap) {
2229
if (this.cache && hash) {
23-
var mapPath = path.join(this.cacheDirectory, hash + '.map')
30+
const mapPath = this.cachedPath(filename, hash)
2431
fs.writeFileSync(mapPath, sourceMap.toJSON())
2532
} else {
2633
this._sourceMapCache.registerMap(filename, sourceMap.sourcemap)
@@ -43,7 +50,7 @@ SourceMaps.prototype.reloadCachedSourceMaps = function (report) {
4350
var hash = fileReport.contentHash
4451
if (!(hash in this.loadedMaps)) {
4552
try {
46-
var mapPath = path.join(this.cacheDirectory, hash + '.map')
53+
const mapPath = this.cachedPath(absFile, hash)
4754
this.loadedMaps[hash] = JSON.parse(fs.readFileSync(mapPath, 'utf8'))
4855
} catch (e) {
4956
// set to false to avoid repeatedly trying to load the map

tap-snapshots/test-nyc-integration.js-TAP.test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,26 @@ All files | 100 | 100 | 100 | 100 |
369369
370370
`
371371

372+
exports[`test/nyc-integration.js TAP caches inline source-maps > stdout 1`] = `
373+
----------|---------|----------|---------|---------|-------------------
374+
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
375+
----------|---------|----------|---------|---------|-------------------
376+
All files | 100 | 100 | 100 | 100 |
377+
s2.js | 100 | 100 | 100 | 100 |
378+
----------|---------|----------|---------|---------|-------------------
379+
380+
`
381+
382+
exports[`test/nyc-integration.js TAP caches source-maps from .map files > stdout 1`] = `
383+
----------|---------|----------|---------|---------|-------------------
384+
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
385+
----------|---------|----------|---------|---------|-------------------
386+
All files | 80 | 100 | 50 | 80 |
387+
s1.js | 80 | 100 | 50 | 80 | 7
388+
----------|---------|----------|---------|---------|-------------------
389+
390+
`
391+
372392
exports[`test/nyc-integration.js TAP can run "npm test" which directly invokes a test file > stdout 1`] = `
373393
374394
> @ test .

test/nyc-integration.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,32 @@ t.test('--all uses source-maps to exclude original sources from reports', t => t
404404
cwd: fixturesSourceMaps
405405
}))
406406

407+
t.test('caches source-maps from .map files', t => {
408+
return testSuccess(t, {
409+
args: [
410+
process.execPath,
411+
'./instrumented/s1.min.js'
412+
],
413+
cwd: fixturesSourceMaps
414+
}).then(() => {
415+
const files = fs.readdirSync(path.resolve(fixturesSourceMaps, 'node_modules/.cache/nyc'))
416+
t.true(files.some(f => f.startsWith('s1.min-') && f.endsWith('.map')))
417+
})
418+
})
419+
420+
t.test('caches inline source-maps', t => {
421+
return testSuccess(t, {
422+
args: [
423+
process.execPath,
424+
'./instrumented/s2.min.js'
425+
],
426+
cwd: fixturesSourceMaps
427+
}).then(() => {
428+
const files = fs.readdirSync(path.resolve(fixturesSourceMaps, 'node_modules/.cache/nyc'))
429+
t.true(files.some(f => f.startsWith('s2.min-') && f.endsWith('.map')))
430+
})
431+
})
432+
407433
t.test('appropriately instruments file with corresponding .map file', t => testSuccess(t, {
408434
args: [
409435
'--cache=false',

0 commit comments

Comments
 (0)