From 5c1b7a9c43771f3439af44a1104e5426519e1123 Mon Sep 17 00:00:00 2001 From: Corey Farrell Date: Fri, 23 Aug 2019 17:20:51 -0400 Subject: [PATCH] feat: Use source base name to prefix cache files (#1144) --- index.js | 3 ++- lib/source-maps.js | 11 ++++++-- .../test-nyc-integration.js-TAP.test.js | 20 ++++++++++++++ test/nyc-integration.js | 26 +++++++++++++++++++ 4 files changed, 57 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 791da87b1..c0f4d6e0d 100755 --- a/index.js +++ b/index.js @@ -94,9 +94,10 @@ class NYC { } _createTransform (ext) { - var opts = { + const opts = { salt: Hash.salt(this.config), hashData: (input, metadata) => [metadata.filename], + filenamePrefix: metadata => path.parse(metadata.filename).name + '-', onHash: (input, metadata, hash) => { this.hashCache[metadata.filename] = hash }, diff --git a/lib/source-maps.js b/lib/source-maps.js index b36285d75..89d0796d2 100644 --- a/lib/source-maps.js +++ b/lib/source-maps.js @@ -11,6 +11,13 @@ function SourceMaps (opts) { this._sourceMapCache = libSourceMaps.createSourceMapStore() } +SourceMaps.prototype.cachedPath = function (source, hash) { + return path.join( + this.cacheDirectory, + `${path.parse(source).name}-${hash}.map` + ) +} + SourceMaps.prototype.purgeCache = function () { this._sourceMapCache = libSourceMaps.createSourceMapStore() this.loadedMaps = {} @@ -20,7 +27,7 @@ SourceMaps.prototype.extractAndRegister = function (code, filename, hash) { var sourceMap = convertSourceMap.fromSource(code) || convertSourceMap.fromMapFileSource(code, path.dirname(filename)) if (sourceMap) { if (this.cache && hash) { - var mapPath = path.join(this.cacheDirectory, hash + '.map') + const mapPath = this.cachedPath(filename, hash) fs.writeFileSync(mapPath, sourceMap.toJSON()) } else { this._sourceMapCache.registerMap(filename, sourceMap.sourcemap) @@ -43,7 +50,7 @@ SourceMaps.prototype.reloadCachedSourceMaps = function (report) { var hash = fileReport.contentHash if (!(hash in this.loadedMaps)) { try { - var mapPath = path.join(this.cacheDirectory, hash + '.map') + const mapPath = this.cachedPath(absFile, hash) this.loadedMaps[hash] = JSON.parse(fs.readFileSync(mapPath, 'utf8')) } catch (e) { // set to false to avoid repeatedly trying to load the map diff --git a/tap-snapshots/test-nyc-integration.js-TAP.test.js b/tap-snapshots/test-nyc-integration.js-TAP.test.js index 5c7adcee6..5e55b5bf4 100644 --- a/tap-snapshots/test-nyc-integration.js-TAP.test.js +++ b/tap-snapshots/test-nyc-integration.js-TAP.test.js @@ -369,6 +369,26 @@ All files | 100 | 100 | 100 | 100 | ` +exports[`test/nyc-integration.js TAP caches inline source-maps > stdout 1`] = ` +----------|---------|----------|---------|---------|------------------- +File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s +----------|---------|----------|---------|---------|------------------- +All files | 100 | 100 | 100 | 100 | + s2.js | 100 | 100 | 100 | 100 | +----------|---------|----------|---------|---------|------------------- + +` + +exports[`test/nyc-integration.js TAP caches source-maps from .map files > stdout 1`] = ` +----------|---------|----------|---------|---------|------------------- +File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s +----------|---------|----------|---------|---------|------------------- +All files | 80 | 100 | 50 | 80 | + s1.js | 80 | 100 | 50 | 80 | 7 +----------|---------|----------|---------|---------|------------------- + +` + exports[`test/nyc-integration.js TAP can run "npm test" which directly invokes a test file > stdout 1`] = ` > @ test . diff --git a/test/nyc-integration.js b/test/nyc-integration.js index b44ca9bce..c17b94b0e 100644 --- a/test/nyc-integration.js +++ b/test/nyc-integration.js @@ -404,6 +404,32 @@ t.test('--all uses source-maps to exclude original sources from reports', t => t cwd: fixturesSourceMaps })) +t.test('caches source-maps from .map files', t => { + return testSuccess(t, { + args: [ + process.execPath, + './instrumented/s1.min.js' + ], + cwd: fixturesSourceMaps + }).then(() => { + const files = fs.readdirSync(path.resolve(fixturesSourceMaps, 'node_modules/.cache/nyc')) + t.true(files.some(f => f.startsWith('s1.min-') && f.endsWith('.map'))) + }) +}) + +t.test('caches inline source-maps', t => { + return testSuccess(t, { + args: [ + process.execPath, + './instrumented/s2.min.js' + ], + cwd: fixturesSourceMaps + }).then(() => { + const files = fs.readdirSync(path.resolve(fixturesSourceMaps, 'node_modules/.cache/nyc')) + t.true(files.some(f => f.startsWith('s2.min-') && f.endsWith('.map'))) + }) +}) + t.test('appropriately instruments file with corresponding .map file', t => testSuccess(t, { args: [ '--cache=false',