Skip to content

Commit

Permalink
use correct path implementation for source map content
Browse files Browse the repository at this point in the history
  • Loading branch information
yousif-bugsnag committed Apr 10, 2024
1 parent aaf5107 commit 0617a58
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
30 changes: 25 additions & 5 deletions src/transformers/StripProjectRoot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,42 @@ export default async function stripProjectRoot (sourceMapPath: string, sourceMap
return maybeSourceMap
}

// function strip (sourceMapPath: string, map: UnsafeSourceMap, projectRoot: string): void {
// if (!map.sources) return
// map.sources = map.sources.map(sourcePath => {
// // leave sources for virtual webpack files untouched
// if (/^webpack:\/\/(.*)\/webpack/.test(sourcePath)) return sourcePath

// const isWindowsPath = sourcePath.includes('\\')
// const separator = isWindowsPath ? '\\' : '/'

// const absoluteSourcePath = path.resolve(
// path.dirname(sourceMapPath),
// sourcePath.replace(/webpack:\/\/.*\/\.\//, `${projectRoot}${separator}`)
// )
// const strippedSourcePath = absoluteSourcePath.replace(projectRoot, '').replace(/^(\/|\\)/, '')
// const normalizedSourcePath = isWindowsPath ? strippedSourcePath.replace(/\//g, '\\') : strippedSourcePath.replace(/\\/g, '/')
// return normalizedSourcePath
// })
// }

function strip (sourceMapPath: string, map: UnsafeSourceMap, projectRoot: string): void {
if (!map.sources) return
map.sources = map.sources.map(sourcePath => {
// leave sources for virtual webpack files untouched
if (/^webpack:\/\/(.*)\/webpack/.test(sourcePath)) return sourcePath

// detect if s uses windows file separators
const isWindowsPath = sourcePath.includes('\\')
const separator = isWindowsPath ? '\\' : '/'
const consistentPath = isWindowsPath ? path.win32 : path.posix

const absoluteSourcePath = path.resolve(
path.dirname(sourceMapPath),
const absoluteSourcePath = consistentPath.resolve(
consistentPath.dirname(sourceMapPath),
sourcePath.replace(/webpack:\/\/.*\/\.\//, `${projectRoot}${separator}`)
)
const strippedSourcePath = absoluteSourcePath.replace(projectRoot, '').replace(/^(\/|\\)/, '')
const normalizedSourcePath = isWindowsPath ? strippedSourcePath.replace(/\//g, '\\') : strippedSourcePath.replace(/\\/g, '/')
return normalizedSourcePath
// const normalizedSourcePath = isWindowsPath ? strippedSourcePath.replace(/\//g, '\\') : strippedSourcePath.replace(/\\/g, '/')
return strippedSourcePath
})
}

6 changes: 4 additions & 2 deletions src/transformers/__test__/StripProjectRoot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,10 @@ test('StripProjectRoot transformer: webpack example with namespace', async () =>

test('StripProjectRoot transformer: preserves path separators (posix)', async () => {
const projectRoot = path.join(__dirname, 'fixtures', 'typescript')
const projectRootPosix = path.join(__dirname, 'fixtures', 'typescript')
const absolutePath = path.join(projectRoot, 'dist', 'out.js.map')
const sourceMapJson = JSON.parse(await fs.readFile(absolutePath, 'utf-8'))
await StripProjectRoot(absolutePath, sourceMapJson, projectRoot, noopLogger)
await StripProjectRoot(absolutePath, sourceMapJson, projectRootPosix, noopLogger)
expect(sourceMapJson.sources).toStrictEqual([
'lib/a.ts',
'index.ts'
Expand All @@ -69,9 +70,10 @@ test('StripProjectRoot transformer: preserves path separators (posix)', async ()

test('StripProjectRoot transformer: preserves path separators (windows)', async () => {
const projectRoot = path.join(__dirname, 'fixtures', 'typescript-windows')
const projectRootWin32 = path.win32.join(__dirname, 'fixtures', 'typescript-windows')
const absolutePath = path.join(projectRoot, 'dist', 'out.js.map')
const sourceMapJson = JSON.parse(await fs.readFile(absolutePath, 'utf-8'))
await StripProjectRoot(absolutePath, sourceMapJson, projectRoot, noopLogger)
await StripProjectRoot(absolutePath, sourceMapJson, projectRootWin32, noopLogger)
expect(sourceMapJson.sources).toStrictEqual([
'lib\\a.ts',
'index.ts'
Expand Down

0 comments on commit 0617a58

Please sign in to comment.