1 | # convert-source-map [![build status](https://secure.travis-ci.org/thlorenz/convert-source-map.svg?branch=master)](http://travis-ci.org/thlorenz/convert-source-map)
|
---|
2 |
|
---|
3 | Converts a source-map from/to different formats and allows adding/changing properties.
|
---|
4 |
|
---|
5 | ```js
|
---|
6 | var convert = require('convert-source-map');
|
---|
7 |
|
---|
8 | var json = convert
|
---|
9 | .fromComment('//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiYnVpbGQvZm9vLm1pbi5qcyIsInNvdXJjZXMiOlsic3JjL2Zvby5qcyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSIsInNvdXJjZVJvb3QiOiIvIn0=')
|
---|
10 | .toJSON();
|
---|
11 |
|
---|
12 | var modified = convert
|
---|
13 | .fromComment('//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiYnVpbGQvZm9vLm1pbi5qcyIsInNvdXJjZXMiOlsic3JjL2Zvby5qcyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSIsInNvdXJjZVJvb3QiOiIvIn0=')
|
---|
14 | .setProperty('sources', [ 'SRC/FOO.JS' ])
|
---|
15 | .toJSON();
|
---|
16 |
|
---|
17 | console.log(json);
|
---|
18 | console.log(modified);
|
---|
19 | ```
|
---|
20 |
|
---|
21 | ```json
|
---|
22 | {"version":3,"file":"build/foo.min.js","sources":["src/foo.js"],"names":[],"mappings":"AAAA","sourceRoot":"/"}
|
---|
23 | {"version":3,"file":"build/foo.min.js","sources":["SRC/FOO.JS"],"names":[],"mappings":"AAAA","sourceRoot":"/"}
|
---|
24 | ```
|
---|
25 |
|
---|
26 | ## API
|
---|
27 |
|
---|
28 | ### fromObject(obj)
|
---|
29 |
|
---|
30 | Returns source map converter from given object.
|
---|
31 |
|
---|
32 | ### fromJSON(json)
|
---|
33 |
|
---|
34 | Returns source map converter from given json string.
|
---|
35 |
|
---|
36 | ### fromBase64(base64)
|
---|
37 |
|
---|
38 | Returns source map converter from given base64 encoded json string.
|
---|
39 |
|
---|
40 | ### fromComment(comment)
|
---|
41 |
|
---|
42 | Returns source map converter from given base64 encoded json string prefixed with `//# sourceMappingURL=...`.
|
---|
43 |
|
---|
44 | ### fromMapFileComment(comment, mapFileDir)
|
---|
45 |
|
---|
46 | Returns source map converter from given `filename` by parsing `//# sourceMappingURL=filename`.
|
---|
47 |
|
---|
48 | `filename` must point to a file that is found inside the `mapFileDir`. Most tools store this file right next to the
|
---|
49 | generated file, i.e. the one containing the source map.
|
---|
50 |
|
---|
51 | ### fromSource(source)
|
---|
52 |
|
---|
53 | Finds last sourcemap comment in file and returns source map converter or returns null if no source map comment was found.
|
---|
54 |
|
---|
55 | ### fromMapFileSource(source, mapFileDir)
|
---|
56 |
|
---|
57 | Finds last sourcemap comment in file and returns source map converter or returns null if no source map comment was
|
---|
58 | found.
|
---|
59 |
|
---|
60 | The sourcemap will be read from the map file found by parsing `# sourceMappingURL=file` comment. For more info see
|
---|
61 | fromMapFileComment.
|
---|
62 |
|
---|
63 | ### toObject()
|
---|
64 |
|
---|
65 | Returns a copy of the underlying source map.
|
---|
66 |
|
---|
67 | ### toJSON([space])
|
---|
68 |
|
---|
69 | Converts source map to json string. If `space` is given (optional), this will be passed to
|
---|
70 | [JSON.stringify](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/JSON/stringify) when the
|
---|
71 | JSON string is generated.
|
---|
72 |
|
---|
73 | ### toBase64()
|
---|
74 |
|
---|
75 | Converts source map to base64 encoded json string.
|
---|
76 |
|
---|
77 | ### toComment([options])
|
---|
78 |
|
---|
79 | Converts source map to an inline comment that can be appended to the source-file.
|
---|
80 |
|
---|
81 | By default, the comment is formatted like: `//# sourceMappingURL=...`, which you would
|
---|
82 | normally see in a JS source file.
|
---|
83 |
|
---|
84 | When `options.multiline == true`, the comment is formatted like: `/*# sourceMappingURL=... */`, which you would find in a CSS source file.
|
---|
85 |
|
---|
86 | ### addProperty(key, value)
|
---|
87 |
|
---|
88 | Adds given property to the source map. Throws an error if property already exists.
|
---|
89 |
|
---|
90 | ### setProperty(key, value)
|
---|
91 |
|
---|
92 | Sets given property to the source map. If property doesn't exist it is added, otherwise its value is updated.
|
---|
93 |
|
---|
94 | ### getProperty(key)
|
---|
95 |
|
---|
96 | Gets given property of the source map.
|
---|
97 |
|
---|
98 | ### removeComments(src)
|
---|
99 |
|
---|
100 | Returns `src` with all source map comments removed
|
---|
101 |
|
---|
102 | ### removeMapFileComments(src)
|
---|
103 |
|
---|
104 | Returns `src` with all source map comments pointing to map files removed.
|
---|
105 |
|
---|
106 | ### commentRegex
|
---|
107 |
|
---|
108 | Provides __a fresh__ RegExp each time it is accessed. Can be used to find source map comments.
|
---|
109 |
|
---|
110 | ### mapFileCommentRegex
|
---|
111 |
|
---|
112 | Provides __a fresh__ RegExp each time it is accessed. Can be used to find source map comments pointing to map files.
|
---|
113 |
|
---|
114 | ### generateMapFileComment(file, [options])
|
---|
115 |
|
---|
116 | Returns a comment that links to an external source map via `file`.
|
---|
117 |
|
---|
118 | By default, the comment is formatted like: `//# sourceMappingURL=...`, which you would normally see in a JS source file.
|
---|
119 |
|
---|
120 | When `options.multiline == true`, the comment is formatted like: `/*# sourceMappingURL=... */`, which you would find in a CSS source file.
|
---|