Socket
Socket
Sign inDemoInstall

@volar/source-map

Package Overview
Dependencies
1
Maintainers
1
Versions
378
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

@volar/source-map


Version published
Maintainers
1
Install size
55.1 kB
Created

Package description

What is @volar/source-map?

@volar/source-map is a package designed to handle source maps, which are used to map the transformed code back to the original source code. This is particularly useful in debugging and development environments where you need to trace errors or understand the flow of the code.

What are @volar/source-map's main functionalities?

Creating Source Maps

This feature allows you to create a new source map. The code sample demonstrates how to generate a source map and add a mapping to it.

const { SourceMapGenerator } = require('@volar/source-map');

const map = new SourceMapGenerator({ file: 'output.js' });
map.addMapping({
  generated: { line: 1, column: 5 },
  source: 'input.js',
  original: { line: 1, column: 5 },
  name: 'example'
});
console.log(map.toString());

Parsing Source Maps

This feature allows you to parse an existing source map. The code sample demonstrates how to use the SourceMapConsumer to read and interpret a raw source map.

const { SourceMapConsumer } = require('@volar/source-map');

const rawSourceMap = {
  version: 3,
  file: 'min.js',
  sources: ['one.js', 'two.js'],
  names: ['foo', 'bar'],
  mappings: 'AAgBC,SAAQ,CAAEA'
};

SourceMapConsumer.with(rawSourceMap, null, consumer => {
  console.log(consumer.sources);
  console.log(consumer.originalPositionFor({ line: 1, column: 5 }));
});

Combining Source Maps

This feature allows you to combine multiple source maps into one. The code sample demonstrates how to create two source maps and then combine them into a single source map.

const { SourceMapGenerator, SourceMapConsumer } = require('@volar/source-map');

const map1 = new SourceMapGenerator({ file: 'output1.js' });
map1.addMapping({
  generated: { line: 1, column: 5 },
  source: 'input1.js',
  original: { line: 1, column: 5 },
  name: 'example1'
});

const map2 = new SourceMapGenerator({ file: 'output2.js' });
map2.addMapping({
  generated: { line: 2, column: 10 },
  source: 'input2.js',
  original: { line: 2, column: 10 },
  name: 'example2'
});

const combinedMap = SourceMapGenerator.fromSourceMap(new SourceMapConsumer(map1.toString()));
combinedMap.applySourceMap(new SourceMapConsumer(map2.toString()));
console.log(combinedMap.toString());

Other packages similar to @volar/source-map

Readme

Source

@volar/source-map

Provides functionality related to source maps.

API

This package exports a SourceMap class with the following methods:

  • getSourceOffset(generatedOffset: number): Returns the source offset for a given generated offset.

  • getGeneratedOffset(sourceOffset: number): Returns the generated offset for a given source offset.

  • getSourceOffsets(generatedOffset: number): Returns all source offsets for a given generated offset.

  • getGeneratedOffsets(sourceOffset: number): Returns all generated offsets for a given source offset.

Data Structures

Mapping

The Mapping is a tuple that represents a mapping in the source map. It consists of the following elements:

  • source: A string representing the source file. This can be undefined.
  • sourceOffsets: Offsets in the source code.
  • generatedOffsets: Offsets in the generated code.
  • data: The data associated with this mapping. The type of this data is generic and can be specified when creating a SourceMap instance.

Here is an example of a Mapping:

let mapping: Mapping<MyDataType> = {
    source: '.../sourceFile.ts',
    sourceOffsets: [10],
    generatedOffsets: [30],
	lengths: [10],
    data: myData,
};

In this example, myData is of type MyDataType, which is the type specified for the SourceMap instance.

Remember to replace MyDataType and myData with actual types and data that are relevant to your project.

License

This project is licensed under the MIT License. See the LICENSE file for more details.

FAQs

Last updated on 06 Jun 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc