Skip to content

Commit 7901eb8

Browse files
rndmerlebahmutov
authored andcommitted
fix: replace sourcemap's path by the corresponding absolute file path (#23)
1 parent 95788c4 commit 7901eb8

File tree

4 files changed

+35
-30
lines changed

4 files changed

+35
-30
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@ cypress/screenshots
44
coverage/
55
.nyc_output/
66
dist/
7+
.cache/
8+
.vscode/

.vscode/settings.json

-3
This file was deleted.

cypress/integration/spec.js

+19-15
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,16 @@ context('Unit tests', () => {
3030
expect(add('foo', 'Bar')).to.equal('fooBar')
3131
})
3232

33-
it('fixes webpack loader source-map path', () => {
33+
it('fixes webpack loader source-map pathes', () => {
3434
const coverage = {
35-
'/folder/module.js': {
36-
inputSourceMap: {
37-
sources: ['/folder/module.js']
38-
}
39-
},
40-
'/folder/component.vue': {
35+
'/absolute/src/component.vue': {
36+
path: '/absolute/src/component.vue',
4137
inputSourceMap: {
4238
sources: [
43-
'/folder/node_modules/cache-loader/dist/cjs.js??ref--0-0!/folder/node_modules/vue-loader/lib/index.js??vue-loader-options!/folder/component.vue?vue&type=script&lang=ts&'
44-
]
39+
'/folder/node_modules/cache-loader/dist/cjs.js??ref--0-0!/folder/node_modules/vue-loader/lib/index.js??vue-loader-options!component.vue?vue&type=script&lang=ts&',
40+
'otherFile.js'
41+
],
42+
sourceRoot: 'src'
4543
}
4644
},
4745
'/folder/module-without-sourcemap.js': {
@@ -51,11 +49,17 @@ context('Unit tests', () => {
5149

5250
fixSourcePathes(coverage)
5351

54-
expect(coverage['/folder/module.js'].inputSourceMap.sources)
55-
.to.deep.equal(['/folder/module.js'])
56-
expect(coverage['/folder/component.vue'].inputSourceMap.sources)
57-
.to.deep.equal(['/folder/component.vue'])
58-
expect(coverage['/folder/module-without-sourcemap.js'].path)
59-
.to.eq('/folder/module-without-sourcemap.js')
52+
expect(coverage).to.deep.eq({
53+
'/absolute/src/component.vue': {
54+
path: '/absolute/src/component.vue',
55+
inputSourceMap: {
56+
sources: ['/absolute/src/component.vue', 'otherFile.js'],
57+
sourceRoot: ''
58+
}
59+
},
60+
'/folder/module-without-sourcemap.js': {
61+
path: '/folder/module-without-sourcemap.js'
62+
}
63+
})
6064
})
6165
})

utils.js

+14-12
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
module.exports = {
22
/**
3-
* Remove potential Webpack loaders string and query parameters from sourcemap path
3+
* Replace source-map's path by the corresponding absolute file path
4+
* (coverage report wouldn't work with source-map path being relative
5+
* or containing Webpack loaders and query parameters)
46
*/
5-
fixSourcePathes (coverage) {
6-
Object.keys(coverage).forEach(file => {
7-
const sourcemap = coverage[file].inputSourceMap
8-
if (!sourcemap) return
9-
sourcemap.sources = sourcemap.sources.map(source => {
10-
let cleaned = source
11-
if (cleaned.includes('!')) cleaned = cleaned.split('!').pop()
12-
if (cleaned.includes('?')) cleaned = cleaned.split('?').shift()
13-
return cleaned
14-
})
7+
fixSourcePathes(coverage) {
8+
Object.values(coverage).forEach(file => {
9+
const { path: absolutePath, inputSourceMap } = file
10+
const fileName = /([^\/\\]+)$/.exec(absolutePath)[1]
11+
if (!inputSourceMap || !fileName) return
12+
13+
if (inputSourceMap.sourceRoot) inputSourceMap.sourceRoot = ''
14+
inputSourceMap.sources = inputSourceMap.sources.map(source =>
15+
source.includes(fileName) ? absolutePath : source
16+
)
1517
})
1618
}
17-
}
19+
}

0 commit comments

Comments
 (0)