How to use the SourceMapConsumer function from source-map

Find comprehensive JavaScript source-map.SourceMapConsumer code examples handpicked from public code repositorys.

8
9
10
11
12
13
14
15
16
var sourceMap = require('source-map');
var through = require('through');
var UglifyJS = require('uglify-js');
var UglifyCSS = require('uglifycss');

var SourceMapConsumer = sourceMap.SourceMapConsumer;
var SourceMapGenerator = sourceMap.SourceMapGenerator;
var SourceNode = sourceMap.SourceNode;

fork icon547
star icon3
watch icon3

221
222
223
224
225
226
227
228
229
* `mappings`: A string of base64 VLQs which contain the actual mappings.

* `file`: Optional. The generated filename this source map is associated with.

```js
var consumer = new sourceMap.SourceMapConsumer(rawSourceMapJsonData);
```

#### SourceMapConsumer.prototype.computeColumnSpans()
fork icon357
star icon73
watch icon2

14
15
16
17
18
19
20
21
22

## Use on the Web

    <script src="https://unpkg.com/source-map@0.7.2/dist/source-map.js"></script>
    <script>
        sourceMap.SourceMapConsumer.initialize({
            "lib/mappings.wasm": "https://unpkg.com/source-map@0.7.2/lib/mappings.wasm"
        });
    </script>
fork icon20
star icon25
watch icon2

+ 27 other calls in file

14
15
16
17
18
19
20
21
22
23

  return new SourceNode(1, 0, null, lines);
};

const prepSourceMap = (code, sourceMap) => {
  const consumer = new SourceMapConsumer(typeof sourceMap === 'string' ?
    JSON.parse(sourceMap) : sourceMap
  );

  return SourceNode.fromStringWithSourceMap(code, consumer);
fork icon474
star icon0
watch icon135

+ 13 other calls in file

68
69
70
71
72
73
74
75
76
77
// https://github.com/webpack/imports-loader/blob/master/index.js#L34-L44
// https://webpack.github.io/docs/loaders.html#writing-a-loader
if (sourceMap) {
    var currentRequest = loaderUtils.getCurrentRequest(this);
    var SourceNode = SourceMap.SourceNode;
    var SourceMapConsumer = SourceMap.SourceMapConsumer;
    var sourceMapConsumer = new SourceMapConsumer(sourceMap);
    var node = SourceNode.fromStringWithSourceMap(source, sourceMapConsumer);

    node.prepend(inject);
fork icon16
star icon87
watch icon0

65
66
67
68
69
70
71
72
73
74
}

if (!map) {
  map = makeIdentitySourceMap(source, resourcePath); // eslint-disable-line no-param-reassign
}
const sourceMapConsumer = new SourceMapConsumer(map);

const onSourceMapReady = consumedMap => {
  const node = new SourceNode(null, null, null, [
    SourceNode.fromStringWithSourceMap(source, consumedMap),
fork icon837
star icon0
watch icon0

55
56
57
58
59
60
61
62
63
64
  logger(message, 'info');
}

const node = new SourceNode(null, null, null, [
  new SourceNode(null, null, this.resourcePath, prependText),
  SourceNode.fromStringWithSourceMap(processedSource, new SourceMapConsumer(map))
]).join(separator);
const result = node.toStringWithSourceMap();

return this.callback(null, result.code, result.map.toString());
fork icon16
star icon33
watch icon3

+ 8 other calls in file

10
11
12
13
14
15
16
17
18
19
let map
if (this.sourceMap) {
  const ssSourceMap = JSON.parse(sst.toSourceMap())
  if (prevMap) {
    const destConsumer = new SourceMapConsumer(ssSourceMap)
    const srcConsumer = new SourceMapConsumer(prevMap)
    Promise.all([destConsumer, srcConsumer])
      .then(([destConsumer, srcConsumer]) => {
        const gen = SourceMapGenerator.fromSourceMap(destConsumer)
        gen.applySourceMap(srcConsumer, this.resourcePath)
fork icon9
star icon47
watch icon5

205
206
207
208
209
210
211
212
213
214
215
216
function applySourceMap(generatedId, url, code, mappings) {
  const generator = new SourceMapGenerator({ file: url });
  mappings.forEach(mapping => generator.addMapping(mapping));
  generator.setSourceContent(url, code);


  const map = SourceMapConsumer(generator.toJSON());
  sourceMapRequests.set(generatedId, Promise.resolve(map));
}


const publicInterface = {
fork icon5
star icon32
watch icon0

5
6
7
8
9
10
11
12
13
14
if (result && result.code) {
  if (result.map) {
    node.add(
      SourceNode.fromStringWithSourceMap(
        result.code,
        new SourceMapConsumer(result.map)
      )
    )
  } else {
    node.add(result.code)
fork icon2
star icon0
watch icon0

118
119
120
121
122
123
124
125
126
127

// source-map
const options = loaderUtils.getOptions(this) || {};
if(options.sourceMap && sourceMap) {
  const currentRequest = loaderUtils.getCurrentRequest();
  const node = SourceNode.fromStringWithSourceMap(content, new SourceMapConsumer(sourceMap));
  node.prepend(useStrictPrefix);
  const result = node.toStringWithSourceMap({ file: currentRequest });
  const callback = this.async();
  callback(null, result.code, result.map.toJSON());
fork icon1
star icon10
watch icon2

-4
fork icon0
star icon0
watch icon1

+ 3 other calls in file

10
11
12
13
14
15
16
17
18
19
exports.process = (code, filename) => {
  const pc = Handlebars.precompile(code, {srcName: filename});
  const out = new SourceNode(null, null, null, [
    'const Handlebars = require("handlebars/dist/cjs/handlebars.runtime.js");\n',
    'module.exports = Handlebars.template(',
    SourceNode.fromStringWithSourceMap(pc.code, new SourceMapConsumer(pc.map)),
    ');\n',
  ]).toStringWithSourceMap();
  return {code: out.code, map: out.map.toString()};
};
fork icon0
star icon0
watch icon2

+ 5 other calls in file

80
81
82
83
84
85
86
87
88
89
  })
}

if (transformedModule.map) {
  return SourceNode.fromStringWithSourceMap(
    codeWithTarpRequires, new SourceMapConsumer(transformedModule.map));
} else {
  var sn = new SourceNode(null, null, depModule.path);
  codeWithTarpRequires.split('\n')
    .forEach((line, i) => sn.add(new SourceNode(i + 1, 0, depModule.path, line + '\n')));
fork icon0
star icon0
watch icon2

+ 19 other calls in file

156
157
158
159
160
161
162
163
164
165

var mapFile = getMapFile(file);

if (mapFile) {
  var json = JSON.parse(mapFile.getContent());
  var smc = new SourceMap.SourceMapConsumer(json);

  sourceNode.add(SourceMap.SourceNode.fromStringWithSourceMap(message.content, smc));
  // mapFile.release = false;
} else {
fork icon0
star icon0
watch icon2

+ 3 other calls in file