@@ -16,11 +16,12 @@ This is a library to generate and consume the source map format
16
16
``` html
17
17
<
script src =
" https://unpkg.com/[email protected] /dist/source-map.js" ></
script >
18
18
<script >
19
- sourceMap .SourceMapConsumer .initialize ({
20
- " lib/mappings.wasm" : " https://unpkg.com/[email protected] /lib/mappings.wasm"
21
- });
19
+ sourceMap .SourceMapConsumer .initialize ({
20
+ " lib/mappings.wasm" : " https://unpkg.com/[email protected] /lib/mappings.wasm" ,
21
+ });
22
22
</script >
23
23
```
24
+
24
25
---
25
26
26
27
<!-- `npm run toc` to regenerate the Table of Contents -->
@@ -81,7 +82,8 @@ const rawSourceMap = {
81
82
names: [" bar" , " baz" , " n" ],
82
83
sources: [" one.js" , " two.js" ],
83
84
sourceRoot: " http://example.com/www/js/" ,
84
- mappings: " CAAC,IAAI,IAAM,SAAUA,GAClB,OAAOC,IAAID;CCDb,IAAI,IAAM,SAAUE,GAClB,OAAOA"
85
+ mappings:
86
+ " CAAC,IAAI,IAAM,SAAUA,GAClB,OAAOC,IAAID;CCDb,IAAI,IAAM,SAAUE,GAClB,OAAOA" ,
85
87
};
86
88
87
89
const whatever = await SourceMapConsumer .with (rawSourceMap, null , consumer => {
@@ -92,7 +94,7 @@ const whatever = await SourceMapConsumer.with(rawSourceMap, null, consumer => {
92
94
console .log (
93
95
consumer .originalPositionFor ({
94
96
line: 2 ,
95
- column: 28
97
+ column: 28 ,
96
98
})
97
99
);
98
100
// { source: 'http://example.com/www/js/two.js',
@@ -104,12 +106,12 @@ const whatever = await SourceMapConsumer.with(rawSourceMap, null, consumer => {
104
106
consumer .generatedPositionFor ({
105
107
source: " http://example.com/www/js/two.js" ,
106
108
line: 2 ,
107
- column: 10
109
+ column: 10 ,
108
110
})
109
111
);
110
112
// { line: 2, column: 28 }
111
113
112
- consumer .eachMapping (function (m ) {
114
+ consumer .eachMapping (function (m ) {
113
115
// ...
114
116
});
115
117
@@ -128,13 +130,19 @@ In depth guide:
128
130
function compile (ast ) {
129
131
switch (ast .type ) {
130
132
case " BinaryExpression" :
131
- return new SourceNode (ast .location .line , ast .location .column , ast .location .source , [
132
- compile (ast .left ),
133
- " + " ,
134
- compile (ast .right )
135
- ]);
133
+ return new SourceNode (
134
+ ast .location .line ,
135
+ ast .location .column ,
136
+ ast .location .source ,
137
+ [compile (ast .left ), " + " , compile (ast .right )]
138
+ );
136
139
case " Literal" :
137
- return new SourceNode (ast .location .line , ast .location .column , ast .location .source , String (ast .value ));
140
+ return new SourceNode (
141
+ ast .location .line ,
142
+ ast .location .column ,
143
+ ast .location .source ,
144
+ String (ast .value )
145
+ );
138
146
// ...
139
147
default :
140
148
throw new Error (" Bad AST" );
@@ -144,7 +152,7 @@ function compile(ast) {
144
152
var ast = parse (" 40 + 2" , " add.js" );
145
153
console .log (
146
154
compile (ast).toStringWithSourceMap ({
147
- file: " add.js"
155
+ file: " add.js" ,
148
156
})
149
157
);
150
158
// { code: '40 + 2',
@@ -155,20 +163,20 @@ console.log(
155
163
156
164
``` js
157
165
var map = new SourceMapGenerator ({
158
- file: " source-mapped.js"
166
+ file: " source-mapped.js" ,
159
167
});
160
168
161
169
map .addMapping ({
162
170
generated: {
163
171
line: 10 ,
164
- column: 35
172
+ column: 35 ,
165
173
},
166
174
source: " foo.js" ,
167
175
original: {
168
176
line: 33 ,
169
- column: 2
177
+ column: 2 ,
170
178
},
171
- name: " christopher"
179
+ name: " christopher" ,
172
180
});
173
181
174
182
console .log (map .toString ());
@@ -209,7 +217,7 @@ The options object has the following properties:
209
217
210
218
``` js
211
219
sourceMap .SourceMapConsumer .initialize ({
212
- " lib/mappings.wasm" : " https://example.com/source-map/lib/mappings.wasm"
220
+ " lib/mappings.wasm" : " https://example.com/source-map/lib/mappings.wasm" ,
213
221
});
214
222
```
215
223
@@ -261,13 +269,17 @@ By using `with`, you do not have to remember to manually call `destroy` on
261
269
the consumer, since it will be called automatically once ` f ` completes.
262
270
263
271
``` js
264
- const xSquared = await SourceMapConsumer .with (myRawSourceMap, null , async function (consumer ) {
265
- // Use `consumer` inside here and don't worry about remembering
266
- // to call `destroy`.
267
-
268
- const x = await whatever (consumer);
269
- return x * x;
270
- });
272
+ const xSquared = await SourceMapConsumer .with (
273
+ myRawSourceMap,
274
+ null ,
275
+ async function (consumer ) {
276
+ // Use `consumer` inside here and don't worry about remembering
277
+ // to call `destroy`.
278
+
279
+ const x = await whatever (consumer);
280
+ return x * x;
281
+ }
282
+ );
271
283
272
284
// You may not use that `consumer` anymore out here; it has
273
285
// been destroyed. But you can use `xSquared`.
@@ -357,7 +369,7 @@ consumer.originalPositionFor({ line: 2, column: 10 });
357
369
358
370
consumer .originalPositionFor ({
359
371
line: 99999999999999999 ,
360
- column: 999999999999999
372
+ column: 999999999999999 ,
361
373
});
362
374
// { source: null,
363
375
// line: null,
@@ -490,7 +502,7 @@ generated line/column in this source map.
490
502
` SourceMapConsumer.GENERATED_ORDER ` .
491
503
492
504
``` js
493
- consumer .eachMapping (function (m ) {
505
+ consumer .eachMapping (function (m ) {
494
506
console .log (m);
495
507
});
496
508
// ...
@@ -531,7 +543,7 @@ You may pass an object with the following properties:
531
543
``` js
532
544
var generator = new sourceMap.SourceMapGenerator ({
533
545
file: " my-generated-javascript-file.js" ,
534
- sourceRoot: " http://example.com/app/js/"
546
+ sourceRoot: " http://example.com/app/js/" ,
535
547
});
536
548
```
537
549
@@ -563,7 +575,7 @@ should have the following properties:
563
575
generator .addMapping ({
564
576
source: " module-one.scm" ,
565
577
original: { line: 128 , column: 0 },
566
- generated: { line: 3 , column: 456 }
578
+ generated: { line: 3 , column: 456 },
567
579
});
568
580
```
569
581
@@ -576,7 +588,10 @@ Set the source content for an original source file.
576
588
- ` sourceContent ` the content of the source file.
577
589
578
590
``` js
579
- generator .setSourceContent (" module-one.scm" , fs .readFileSync (" path/to/module-one.scm" ));
591
+ generator .setSourceContent (
592
+ " module-one.scm" ,
593
+ fs .readFileSync (" path/to/module-one.scm" )
594
+ );
580
595
```
581
596
582
597
#### SourceMapGenerator.prototype.applySourceMap(sourceMapConsumer[ , sourceFile[ , sourceMapPath]] )
@@ -640,7 +655,7 @@ use before outputting the generated JS and source map.
640
655
var node = new SourceNode (1 , 2 , " a.cpp" , [
641
656
new SourceNode (3 , 4 , " b.cpp" , " extern int status;\n " ),
642
657
new SourceNode (5 , 6 , " c.cpp" , " std::string* make_string(size_t n);\n " ),
643
- new SourceNode (7 , 8 , " d.cpp" , " int main(int argc, char** argv) {}\n " )
658
+ new SourceNode (7 , 8 , " d.cpp" , " int main(int argc, char** argv) {}\n " ),
644
659
]);
645
660
```
646
661
@@ -656,8 +671,13 @@ Creates a SourceNode from generated code and a SourceMapConsumer.
656
671
should be relative to.
657
672
658
673
``` js
659
- const consumer = await new SourceMapConsumer (fs .readFileSync (" path/to/my-file.js.map" , " utf8" ));
660
- const node = SourceNode .fromStringWithSourceMap (fs .readFileSync (" path/to/my-file.js" ), consumer);
674
+ const consumer = await new SourceMapConsumer (
675
+ fs .readFileSync (" path/to/my-file.js.map" , " utf8" )
676
+ );
677
+ const node = SourceNode .fromStringWithSourceMap (
678
+ fs .readFileSync (" path/to/my-file.js" ),
679
+ consumer
680
+ );
661
681
```
662
682
663
683
#### SourceNode.prototype.add(chunk)
@@ -694,7 +714,10 @@ Set the source content for a source file. This will be added to the
694
714
- ` sourceContent ` : The content of the source file
695
715
696
716
``` js
697
- node .setSourceContent (" module-one.scm" , fs .readFileSync (" path/to/module-one.scm" ));
717
+ node .setSourceContent (
718
+ " module-one.scm" ,
719
+ fs .readFileSync (" path/to/module-one.scm" )
720
+ );
698
721
```
699
722
700
723
#### SourceNode.prototype.walk(fn)
@@ -709,10 +732,10 @@ the its original associated source's line/column location.
709
732
var node = new SourceNode (1 , 2 , " a.js" , [
710
733
new SourceNode (3 , 4 , " b.js" , " uno" ),
711
734
" dos" ,
712
- [" tres" , new SourceNode (5 , 6 , " c.js" , " quatro" )]
735
+ [" tres" , new SourceNode (5 , 6 , " c.js" , " quatro" )],
713
736
]);
714
737
715
- node .walk (function (code , loc ) {
738
+ node .walk (function (code , loc ) {
716
739
console .log (" WALK:" , code, loc);
717
740
});
718
741
// WALK: uno { source: 'b.js', line: 3, column: 4, name: null }
@@ -737,7 +760,7 @@ var c = new SourceNode(1, 2, "c.js", "generated from c");
737
760
c .setSourceContent (" c.js" , " original c" );
738
761
739
762
var node = new SourceNode (null , null , null , [a, b, c]);
740
- node .walkSourceContents (function (source , contents ) {
763
+ node .walkSourceContents (function (source , contents ) {
741
764
console .log (" WALK:" , source, " :" , contents);
742
765
});
743
766
// WALK: a.js : original a
@@ -784,7 +807,7 @@ concatenates all the various snippets together to one string.
784
807
var node = new SourceNode (1 , 2 , " a.js" , [
785
808
new SourceNode (3 , 4 , " b.js" , " uno" ),
786
809
" dos" ,
787
- [" tres" , new SourceNode (5 , 6 , " c.js" , " quatro" )]
810
+ [" tres" , new SourceNode (5 , 6 , " c.js" , " quatro" )],
788
811
]);
789
812
790
813
node .toString ();
@@ -803,11 +826,10 @@ The arguments are the same as those to `new SourceMapGenerator`.
803
826
var node = new SourceNode (1 , 2 , " a.js" , [
804
827
new SourceNode (3 , 4 , " b.js" , " uno" ),
805
828
" dos" ,
806
- [" tres" , new SourceNode (5 , 6 , " c.js" , " quatro" )]
829
+ [" tres" , new SourceNode (5 , 6 , " c.js" , " quatro" )],
807
830
]);
808
831
809
832
node .toStringWithSourceMap ({ file: " my-output-file.js" });
810
833
// { code: 'unodostresquatro',
811
834
// map: [object SourceMapGenerator] }
812
835
```
813
-
0 commit comments