Skip to content

fix: replace sourcemap's path by the corresponding absolute file path #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ cypress/screenshots
coverage/
.nyc_output/
dist/
.cache/
.vscode/
3 changes: 0 additions & 3 deletions .vscode/settings.json

This file was deleted.

34 changes: 19 additions & 15 deletions cypress/integration/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,16 @@ context('Unit tests', () => {
expect(add('foo', 'Bar')).to.equal('fooBar')
})

it('fixes webpack loader source-map path', () => {
it('fixes webpack loader source-map pathes', () => {
const coverage = {
'/folder/module.js': {
inputSourceMap: {
sources: ['/folder/module.js']
}
},
'/folder/component.vue': {
'/absolute/src/component.vue': {
path: '/absolute/src/component.vue',
inputSourceMap: {
sources: [
'/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&'
]
'/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&',
'otherFile.js'
],
sourceRoot: 'src'
}
},
'/folder/module-without-sourcemap.js': {
Expand All @@ -51,11 +49,17 @@ context('Unit tests', () => {

fixSourcePathes(coverage)

expect(coverage['/folder/module.js'].inputSourceMap.sources)
.to.deep.equal(['/folder/module.js'])
expect(coverage['/folder/component.vue'].inputSourceMap.sources)
.to.deep.equal(['/folder/component.vue'])
expect(coverage['/folder/module-without-sourcemap.js'].path)
.to.eq('/folder/module-without-sourcemap.js')
expect(coverage).to.deep.eq({
'/absolute/src/component.vue': {
path: '/absolute/src/component.vue',
inputSourceMap: {
sources: ['/absolute/src/component.vue', 'otherFile.js'],
sourceRoot: ''
}
},
'/folder/module-without-sourcemap.js': {
path: '/folder/module-without-sourcemap.js'
}
})
})
})
26 changes: 14 additions & 12 deletions utils.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
module.exports = {
/**
* Remove potential Webpack loaders string and query parameters from sourcemap path
* Replace source-map's path by the corresponding absolute file path
* (coverage report wouldn't work with source-map path being relative
* or containing Webpack loaders and query parameters)
*/
fixSourcePathes (coverage) {
Object.keys(coverage).forEach(file => {
const sourcemap = coverage[file].inputSourceMap
if (!sourcemap) return
sourcemap.sources = sourcemap.sources.map(source => {
let cleaned = source
if (cleaned.includes('!')) cleaned = cleaned.split('!').pop()
if (cleaned.includes('?')) cleaned = cleaned.split('?').shift()
return cleaned
})
fixSourcePathes(coverage) {
Object.values(coverage).forEach(file => {
const { path: absolutePath, inputSourceMap } = file
const fileName = /([^\/\\]+)$/.exec(absolutePath)[1]
if (!inputSourceMap || !fileName) return

if (inputSourceMap.sourceRoot) inputSourceMap.sourceRoot = ''
inputSourceMap.sources = inputSourceMap.sources.map(source =>
source.includes(fileName) ? absolutePath : source
)
})
}
}
}