-
-
Notifications
You must be signed in to change notification settings - Fork 141
Expand file tree
/
Copy pathsourceMap.js
More file actions
38 lines (34 loc) · 1.12 KB
/
sourceMap.js
File metadata and controls
38 lines (34 loc) · 1.12 KB
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
// shamelessly copied and modified
// from https://github.com/emotion-js/emotion/blob/master/packages/babel-plugin-emotion/src/utils/source-maps.js
import { SourceMapGenerator } from 'source-map'
import convert from 'convert-source-map'
function getGeneratorOpts(file) {
return file.opts.generatorOpts ? file.opts.generatorOpts : file.opts
}
function makeSourceMapGenerator(file) {
const generatorOpts = getGeneratorOpts(file)
const filename = generatorOpts.sourceFileName
const generator = new SourceMapGenerator({
file: filename,
sourceRoot: generatorOpts.sourceRoot,
})
generator.setSourceContent(filename, file.code)
return generator
}
export function getSourceMap(offset, state) {
const { line, column } = offset
const generator = makeSourceMapGenerator(state.file)
const generatorOpts = getGeneratorOpts(state.file)
if (generatorOpts.sourceFileName) {
generator.addMapping({
generated: {
line: 1,
column: 0,
},
source: generatorOpts.sourceFileName,
original: offset,
})
return convert.fromObject(generator).toComment({ multiline: true })
}
return ''
}