-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathStripProjectRoot.ts
40 lines (32 loc) · 1.51 KB
/
StripProjectRoot.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import { Logger } from '../Logger';
import { UnsafeSourceMap } from '../SourceMap'
import path from 'path'
export default async function stripProjectRoot (sourceMapPath: string, sourceMap: unknown, projectRoot: string, logger: Logger): Promise<unknown> {
logger.debug('Stripping project root from sources')
if (!sourceMap || typeof sourceMap !== 'object') return sourceMap
const maybeSourceMap: UnsafeSourceMap = sourceMap as UnsafeSourceMap
if (maybeSourceMap.sections) {
for (const section of maybeSourceMap.sections) {
if (section.map) strip(sourceMapPath, section.map, projectRoot)
}
} else {
strip(sourceMapPath, maybeSourceMap, projectRoot)
}
return maybeSourceMap
}
function strip (sourceMapPath: string, map: UnsafeSourceMap, projectRoot: string): void {
if (!map.sources) return
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}/`)
)
const strippedSourcePath = absoluteSourcePath.replace(projectRoot, '').replace(/^(\/|\\)/, '')
return isWebPackOnWindows ? strippedSourcePath.replace(/\\/g, '/') : strippedSourcePath
})
}