UNPKG

4.3 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([options])
84
85Converts source map to an inline comment that can be appended to the source-file.
86
87By default, the comment is formatted like: `//# sourceMappingURL=...`, which you would
88normally see in a JS source file.
89
90When `options.multiline == true`, the comment is formatted like: `/*# sourceMappingURL=... */`, which you would find in a CSS source file.
91
92### addProperty(key, value)
93
94Adds given property to the source map. Throws an error if property already exists.
95
96### setProperty(key, value)
97
98Sets given property to the source map. If property doesn't exist it is added, otherwise its value is updated.
99
100### getProperty(key)
101
102Gets given property of the source map.
103
104### removeComments(src)
105
106Returns `src` with all source map comments removed
107
108### removeMapFileComments(src)
109
110Returns `src` with all source map comments pointing to map files removed.
111
112### commentRegex
113
114Returns the regex used to find source map comments.
115
116### mapFileCommentRegex
117
118Returns the regex used to find source map comments pointing to map files.
119
120
121[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/thlorenz/convert-source-map/trend.png)](https://bitdeli.com/free "Bitdeli Badge")