How to use source-map - 10 common examples

To help you get started, we’ve selected a few source-map examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github magento / baler / src / __tests__ / minifyWorker.unit.js View on Github external
names: [],
        mappings:
            ';;AAAA,IAAM,IAAI,SAAJ,CAAI,GAAM;AACZ,YAAQ,GAAR,CAAY,UAAK,GAAjB;AACH,CAFD;;;ACAA,IAAM,IAAI,SAAJ,CAAI,GAAM;AACZ,YAAQ,GAAR,CAAY,UAAK,GAAjB;AACH,CAFD',
        file: 'bundle.js',
        sourcesContent: [
            'const b = () => {\n    console.log(this.heh);\n};',
            'const a = () => {\n    console.log(this.lol);\n};',
        ],
    };
    const result = await minifyFromString(js, 'bundle.js', JSON.stringify(map));
    expect(result.code).toMatchInlineSnapshot(`
        "\\"use strict\\";var b=function(){console.log((void 0).heh)},a=function(){console.log((void 0).lol)};
        //# sourceMappingURL=bundle.js.map"
    `);

    const consumer = await SourceMapConsumer.fromSourceMap(result.map);
    // Verify the original code, from before babel, made it to the sourcemap
    expect(consumer.sourceContentFor('file.js')).toMatchInlineSnapshot(`
        "const b = () => {
            console.log(this.heh);
        };"
    `);
    consumer.destroy();
});
github MaayanLab / geo2enrichr / scripts / node_modules / grunt-contrib-concat / tasks / lib / sourcemap.js View on Github external
var sourceMapPath;

      var sourceContent;
      // Browserify, as an example, stores a datauri at sourceMappingURL.
      if (/data:application\/json;base64,([^\s]+)/.test(sourceMapFile)) {
        // Set sourceMapPath to the file that the map is inlined.
        sourceMapPath = filename;
        sourceContent = new Buffer(RegExp.$1, 'base64').toString();
      } else {
        // If sourceMapPath is relative, expand relative to the file
        // refering to it.
        sourceMapPath = path.resolve(path.dirname(filename), sourceMapFile);
        sourceContent = grunt.file.read(sourceMapPath);
      }
      var sourceMap = JSON.parse(sourceContent);
      var sourceMapConsumer = new SourceMapConsumer(sourceMap);
      // Consider the relative path from source files to new sourcemap.
      var sourcePathToSourceMapPath =
        path.relative(path.dirname(this.dest), path.dirname(sourceMapPath));
      // sourceMap path references are URLs, so ensure forward slashes are used for paths passed to sourcemap library
      sourcePathToSourceMapPath = sourcePathToSourceMapPath.replace(/\\/g, '/');
      // Store the sourceMap so that it may later be consumed.
      this.maps.push([
        sourceMapConsumer, relativeFilename, sourcePathToSourceMapPath
      ]);
      // Remove the old sourceMappingURL.
      src = src.replace(/[@#]\s+sourceMappingURL=[^\s]+/, '');
      // Create a node from the source map for the file.
      node = SourceNode.fromStringWithSourceMap(
        src, sourceMapConsumer, sourcePathToSourceMapPath
      );
    } else {
github fossasia / susper.com / node_modules / @angular-devkit / core / src / utils / template.js View on Github external
}),
                    new source_map_1.SourceNode(node.end.line, node.end.column, sourceUrl, ');\n'),
                ];
                break;
            case 'evaluate':
                code = [
                    ...node.expression.split('\n').map((line, i, arr) => {
                        return new source_map_1.SourceNode(node.start.line + i, i == 0 ? node.start.column : 0, sourceUrl, line + ((i == arr.length - 1) ? '' : '\n'));
                    }),
                    new source_map_1.SourceNode(node.end.line, node.end.column, sourceUrl, '\n'),
                ];
                break;
        }
        return chunk.add(new source_map_1.SourceNode(node.start.line, node.start.column, sourceUrl, code));
    }, preamble)
        .add(new source_map_1.SourceNode(end.line, end.column, sourceUrl, [
        '  };\n',
        '\n',
        '  return __p;\n',
        '}\n',
    ]));
    const code = nodes.toStringWithSourceMap({
        file: sourceUrl,
        sourceRoot: options && options.sourceRoot || '.',
    });
    // Set the source content in the source map, otherwise the sourceUrl is not enough
    // to find the content.
    code.map.setSourceContent(sourceUrl, ast.content);
    return code.code
        + '\n//# sourceMappingURL=data:application/json;base64,'
        + new Buffer(code.map.toString()).toString('base64');
}
github vuejs / vueify / lib / compiler.js View on Github external
function generateSourceMap (script, output) {
    // hot-reload source map busting
    var hashedFilename = path.basename(filePath) + '?' + hash(filePath + content)
    var map = new sourceMap.SourceMapGenerator()
    map.setSourceContent(hashedFilename, content)
    // check input source map from babel/coffee etc
    var inMap = resolvedParts.map
    var inMapConsumer = inMap && new sourceMap.SourceMapConsumer(inMap)
    var generatedOffset = (output ? output.split(splitRE).length : 0) + 1
    script.split(splitRE).forEach(function (line, index) {
      var ln = index + 1
      var originalLine = inMapConsumer
        ? inMapConsumer.originalPositionFor({ line: ln, column: 0 }).line
        : ln
      if (originalLine) {
        map.addMapping({
          source: hashedFilename,
          generated: {
            line: ln + generatedOffset,
            column: 0
github erzu / porter / packages / porter / src / package.js View on Github external
async createSourceNode({ source, code, map }) {
    if (map instanceof SourceMapGenerator) {
      map = map.toJSON()
    }

    if (map) {
      const consumer = await new SourceMapConsumer(map)
      return SourceNode.fromStringWithSourceMap(code, consumer)
    } else {
      // Source code need to be mapped line by line for debugging in devtols to work.
      const lines = code.split('\n')
      const node = new SourceNode()
      for (let i = 0; i < lines.length; i++) {
        node.add(new SourceNode(i + 1, 0, source, lines[i]))
      }
      return node.join('\n')
      // return new SourceNode(1, 0, source, code)
    }
  }
github accordproject / concerto / packages / composer-runtime / lib / scriptcompiler.js View on Github external
LOG.entry(method, context, script);

        // Convert the script into a source map.
        let sourceFileName = script.getIdentifier();
        let sourceCode = script.getContents();
        let sourceMap = this.convertScriptToSourceMap(context, script);

        // Allow someone else to post-process the converted script.
        const transformedScript = this.transformScript(sourceFileName, sourceCode, sourceMap);
        sourceFileName = transformedScript.sourceFileName;
        sourceCode = transformedScript.sourceCode;
        sourceMap = transformedScript.sourceMap;

        // Create a new source node from the script contents and source map
        const sourceMapConsumer = new SourceMapConsumer(sourceMap);
        const result = SourceNode.fromStringWithSourceMap(sourceCode, sourceMapConsumer);
        LOG.exit(method, result);
        return result;
    }
github webpack / webpack-sources / lib / applySourceMap.js View on Github external
var middleSourceContents = {};

	var m2rMappingsByLine = {};

	var rightSourceContentsSet = {};
	var rightSourceContentsLines = {};

	// Store all mappings by generated line
	sourceMapConsumer.eachMapping(
		function(mapping) {
			(m2rMappingsByLine[mapping.generatedLine] =
				m2rMappingsByLine[mapping.generatedLine] || []).push(mapping);
		},
		null,
		SourceMapConsumer.GENERATED_ORDER
	);

	// Store all source contents
	sourceNode.walkSourceContents(function(source, content) {
		middleSourceContents["$" + source] = content;
	});

	var middleSource = middleSourceContents["$" + sourceFile];
	var middleSourceLines = middleSource ? middleSource.split("\n") : undefined;

	// Walk all left to middle mappings
	sourceNode.walk(function(chunk, middleMapping) {
		var source;

		// Find a mapping from middle to right
		if(
github jsx / JSX / node_modules / source-map / lib / source-map / source-map-consumer.js View on Github external
while (str.length > 0) {
        if (str.charAt(0) === ';') {
          generatedLine++;
          str = str.slice(1);
          previousGeneratedColumn = 0;
        }
        else if (str.charAt(0) === ',') {
          str = str.slice(1);
        }
        else {
          mapping = {};
          mapping.generatedLine = generatedLine;

          // Generated column.
          temp = base64VLQ.decode(str);
          mapping.generatedColumn = previousGeneratedColumn + temp.value;
          previousGeneratedColumn = mapping.generatedColumn;
          str = temp.rest;

          if (str.length > 0 && !mappingSeparator.test(str.charAt(0))) {
            // Original source.
            temp = base64VLQ.decode(str);
            if (aSourceRoot) {
              mapping.source = util.join(aSourceRoot, this._sources.at(previousSource + temp.value));
            }
            else {
              mapping.source = this._sources.at(previousSource + temp.value);
            }
            previousSource += temp.value;
            str = temp.rest;
            if (str.length === 0 || mappingSeparator.test(str.charAt(0))) {
github jsx / JSX / node_modules / source-map / lib / source-map / source-map-consumer.js View on Github external
// Lines are stored 0-based
            mapping.originalLine += 1;
            str = temp.rest;
            if (str.length === 0 || mappingSeparator.test(str.charAt(0))) {
              throw new Error('Found a source and line, but no column');
            }

            // Original column.
            temp = base64VLQ.decode(str);
            mapping.originalColumn = previousOriginalColumn + temp.value;
            previousOriginalColumn = mapping.originalColumn;
            str = temp.rest;

            if (str.length > 0 && !mappingSeparator.test(str.charAt(0))) {
              // Original name.
              temp = base64VLQ.decode(str);
              mapping.name = this._names.at(previousName + temp.value);
              previousName += temp.value;
              str = temp.rest;
            }
          }

          this._generatedMappings.push(mapping);
          this._originalMappings.push(mapping);
        }
      }

      this._originalMappings.sort(this._compareOriginalPositions);
    };
github jsx / JSX / node_modules / source-map / lib / source-map / source-map-consumer.js View on Github external
}
        else {
          mapping = {};
          mapping.generatedLine = generatedLine;

          // Generated column.
          temp = base64VLQ.decode(str);
          mapping.generatedColumn = previousGeneratedColumn + temp.value;
          previousGeneratedColumn = mapping.generatedColumn;
          str = temp.rest;

          if (str.length > 0 && !mappingSeparator.test(str.charAt(0))) {
            // Original source.
            temp = base64VLQ.decode(str);
            if (aSourceRoot) {
              mapping.source = util.join(aSourceRoot, this._sources.at(previousSource + temp.value));
            }
            else {
              mapping.source = this._sources.at(previousSource + temp.value);
            }
            previousSource += temp.value;
            str = temp.rest;
            if (str.length === 0 || mappingSeparator.test(str.charAt(0))) {
              throw new Error('Found a source, but no line and column');
            }

            // Original line.
            temp = base64VLQ.decode(str);
            mapping.originalLine = previousOriginalLine + temp.value;
            previousOriginalLine = mapping.originalLine;
            // Lines are stored 0-based
            mapping.originalLine += 1;