UNPKG

4.04 kBMarkdownView Raw
1# convert-source-map [![build status](https://secure.travis-ci.org/thlorenz/convert-source-map.png)](http://travis-ci.org/thlorenz/convert-source-map)
2
3[![NPM](https://nodei.co/npm/convert-source-map.png?downloads=true&stars=true)](https://nodei.co/npm/convert-source-map/)
4
5Converts a source-map from/to different formats and allows adding/changing properties.
6
7```js
8var convert = require('convert-source-map');
9
10var json = convert
11 .fromComment('//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZm9vLmpzIiwic291cmNlcyI6WyJjb25zb2xlLmxvZyhcImhpXCIpOyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSIsInNvdXJjZVJvb3QiOiIvIn0=')
12 .toJSON();
13
14var modified = convert
15 .fromComment('//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZm9vLmpzIiwic291cmNlcyI6WyJjb25zb2xlLmxvZyhcImhpXCIpOyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSIsInNvdXJjZVJvb3QiOiIvIn0=')
16 .setProperty('sources', [ 'CONSOLE.LOG("HI");' ])
17 .toJSON();
18
19console.log(json);
20console.log(modified);
21```
22
23```json
24{"version":3,"file":"foo.js","sources":["console.log(\"hi\");"],"names":[],"mappings":"AAAA","sourceRoot":"/"}
25{"version":3,"file":"foo.js","sources":["CONSOLE.LOG(\"HI\");"],"names":[],"mappings":"AAAA","sourceRoot":"/"}
26```
27
28## API
29
30### fromObject(obj)
31
32Returns source map converter from given object.
33
34### fromJSON(json)
35
36Returns source map converter from given json string.
37
38### fromBase64(base64)
39
40Returns source map converter from given base64 encoded json string.
41
42### fromComment(comment)
43
44Returns source map converter from given base64 encoded json string prefixed with `//# sourceMappingURL=...`.
45
46### fromMapFileComment(comment, mapFileDir)
47
48Returns source map converter from given `filename` by parsing `//# sourceMappingURL=filename`.
49
50`filename` must point to a file that is found inside the `mapFileDir`. Most tools store this file right next to the
51generated file, i.e. the one containing the source map.
52
53### fromSource(source[, largeSource])
54
55Finds last sourcemap comment in file and returns source map converter or returns null if no source map comment was found.
56
57If `largeSource` is set to `true`, an algorithm that does not use regex is applied to find the source map. This is faster and especially useful if you're running into "call stack size exceeded" errors with the default algorithm.
58
59However, it is less accurate and may match content that isn't a source map comment.
60
61### fromMapFileSource(source, mapFileDir)
62
63Finds last sourcemap comment in file and returns source map converter or returns null if no source map comment was
64found.
65
66The sourcemap will be read from the map file found by parsing `# sourceMappingURL=file` comment. For more info see
67fromMapFileComment.
68
69### toObject()
70
71Returns a copy of the underlying source map.
72
73### toJSON([space])
74
75Converts source map to json string. If `space` is given (optional), this will be passed to
76[JSON.stringify](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/JSON/stringify) when the
77JSON string is generated.
78
79### toBase64()
80
81Converts source map to base64 encoded json string.
82
83### toComment()
84
85Converts source map to base64 encoded json string prefixed with `//# sourceMappingURL=...`.
86
87### addProperty(key, value)
88
89Adds given property to the source map. Throws an error if property already exists.
90
91### setProperty(key, value)
92
93Sets given property to the source map. If property doesn't exist it is added, otherwise its value is updated.
94
95### getProperty(key)
96
97Gets given property of the source map.
98
99### removeComments(src)
100
101Returns `src` with all source map comments removed
102
103### removeMapFileComments(src)
104
105Returns `src` with all source map comments pointing to map files removed.
106
107### commentRegex
108
109Returns the regex used to find source map comments.
110
111### mapFileCommentRegex
112
113Returns the regex used to find source map comments pointing to map files.
114
115
116[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/thlorenz/convert-source-map/trend.png)](https://bitdeli.com/free "Bitdeli Badge")
117