Skip to content

Commit

Permalink
Merge pull request #95 from bugsnag/PLAT-11374/preserve-path-separators
Browse files Browse the repository at this point in the history
Normalise path separators for Webpack paths on Windows
  • Loading branch information
yousif-bugsnag committed Apr 17, 2024
2 parents b7d1235 + e36e1a3 commit 47a6aa0
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## TBD

### Fixed

- Normalise path separators for Webpack paths on Windows [#95](https://github.com/bugsnag/bugsnag-source-maps/pull/95)

## 2.3.2 (2024-03-04)

### Security
Expand Down
9 changes: 8 additions & 1 deletion src/transformers/StripProjectRoot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,17 @@ function strip (sourceMapPath: string, map: UnsafeSourceMap, projectRoot: string
map.sources = map.sources.map(s => {
// leave sources for virtual webpack files untouched
if (/^webpack:\/\/(.*)\/webpack/.test(s)) return s

// If the source path is a webpack path and we are running on Windows,
// we normalize the path separators to URI format
const isWebPackOnWindows = s.indexOf('webpack') > -1 && process.platform === 'win32'

const absoluteSourcePath = path.resolve(
path.dirname(sourceMapPath),
s.replace(/webpack:\/\/.*\/\.\//, `${projectRoot}/`)
)
return absoluteSourcePath.replace(projectRoot, '').replace(/^(\/|\\)/, '')

const strippedSourcePath = absoluteSourcePath.replace(projectRoot, '').replace(/^(\/|\\)/, '')
return isWebPackOnWindows ? strippedSourcePath.replace(/\\/g, '/') : strippedSourcePath
})
}
6 changes: 3 additions & 3 deletions src/transformers/__test__/StripProjectRoot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ test('StripProjectRoot transformer: webpack example', async () => {
const sourceMapJson = JSON.parse(await fs.readFile(absolutePath, 'utf-8'))
await StripProjectRoot(absolutePath, sourceMapJson, projectRoot, noopLogger)
expect(sourceMapJson.sources).toStrictEqual([
path.join('lib', 'a.js'),
'lib/a.js',
'webpack:https:///webpack/bootstrap',
'index.js'
])
Expand All @@ -38,7 +38,7 @@ test('StripProjectRoot transformer: webpack example (synthetic sections)', async
const sourceMapJson = { sections: [ { map: JSON.parse(await fs.readFile(absolutePath, 'utf-8')) } ] }
await StripProjectRoot(absolutePath, sourceMapJson, projectRoot, noopLogger)
expect(sourceMapJson.sections[0].map.sources).toStrictEqual([
path.join('lib', 'a.js'),
'lib/a.js',
'webpack:https:///webpack/bootstrap',
'index.js'
])
Expand All @@ -50,7 +50,7 @@ test('StripProjectRoot transformer: webpack example with namespace', async () =>
const sourceMapJson = { sections: [ { map: JSON.parse(await fs.readFile(absolutePath, 'utf-8')) } ] }
await StripProjectRoot(absolutePath, sourceMapJson, projectRoot, noopLogger)
expect(sourceMapJson.sections[0].map.sources).toStrictEqual([
path.join('lib', 'a.js'),
'lib/a.js',
'webpack:https://this-package-name-is-used-in-the-source-paths/webpack/bootstrap',
'index.js'
])
Expand Down

0 comments on commit 47a6aa0

Please sign in to comment.