Skip to content

Commit 47a6aa0

Browse files
Merge pull request #95 from bugsnag/PLAT-11374/preserve-path-separators
Normalise path separators for Webpack paths on Windows
2 parents b7d1235 + e36e1a3 commit 47a6aa0

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## TBD
4+
5+
### Fixed
6+
7+
- Normalise path separators for Webpack paths on Windows [#95](https://github.com/bugsnag/bugsnag-source-maps/pull/95)
8+
39
## 2.3.2 (2024-03-04)
410

511
### Security

src/transformers/StripProjectRoot.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,17 @@ function strip (sourceMapPath: string, map: UnsafeSourceMap, projectRoot: string
2424
map.sources = map.sources.map(s => {
2525
// leave sources for virtual webpack files untouched
2626
if (/^webpack:\/\/(.*)\/webpack/.test(s)) return s
27+
28+
// If the source path is a webpack path and we are running on Windows,
29+
// we normalize the path separators to URI format
30+
const isWebPackOnWindows = s.indexOf('webpack') > -1 && process.platform === 'win32'
31+
2732
const absoluteSourcePath = path.resolve(
2833
path.dirname(sourceMapPath),
2934
s.replace(/webpack:\/\/.*\/\.\//, `${projectRoot}/`)
3035
)
31-
return absoluteSourcePath.replace(projectRoot, '').replace(/^(\/|\\)/, '')
36+
37+
const strippedSourcePath = absoluteSourcePath.replace(projectRoot, '').replace(/^(\/|\\)/, '')
38+
return isWebPackOnWindows ? strippedSourcePath.replace(/\\/g, '/') : strippedSourcePath
3239
})
3340
}

src/transformers/__test__/StripProjectRoot.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ test('StripProjectRoot transformer: webpack example', async () => {
2626
const sourceMapJson = JSON.parse(await fs.readFile(absolutePath, 'utf-8'))
2727
await StripProjectRoot(absolutePath, sourceMapJson, projectRoot, noopLogger)
2828
expect(sourceMapJson.sources).toStrictEqual([
29-
path.join('lib', 'a.js'),
29+
'lib/a.js',
3030
'webpack:///webpack/bootstrap',
3131
'index.js'
3232
])
@@ -38,7 +38,7 @@ test('StripProjectRoot transformer: webpack example (synthetic sections)', async
3838
const sourceMapJson = { sections: [ { map: JSON.parse(await fs.readFile(absolutePath, 'utf-8')) } ] }
3939
await StripProjectRoot(absolutePath, sourceMapJson, projectRoot, noopLogger)
4040
expect(sourceMapJson.sections[0].map.sources).toStrictEqual([
41-
path.join('lib', 'a.js'),
41+
'lib/a.js',
4242
'webpack:///webpack/bootstrap',
4343
'index.js'
4444
])
@@ -50,7 +50,7 @@ test('StripProjectRoot transformer: webpack example with namespace', async () =>
5050
const sourceMapJson = { sections: [ { map: JSON.parse(await fs.readFile(absolutePath, 'utf-8')) } ] }
5151
await StripProjectRoot(absolutePath, sourceMapJson, projectRoot, noopLogger)
5252
expect(sourceMapJson.sections[0].map.sources).toStrictEqual([
53-
path.join('lib', 'a.js'),
53+
'lib/a.js',
5454
'webpack://this-package-name-is-used-in-the-source-paths/webpack/bootstrap',
5555
'index.js'
5656
])

0 commit comments

Comments
 (0)