简体   繁体   English

阅读 JavaScript-TypeScript 源映射并在另一个中显示代码

[英]Read JavaScript-TypeScript source map and reveal code in another one

I have 2 main questions:我有两个主要问题:

  1. how to read or parse a js/ts source map file in NodeJs如何在NodeJs 中读取或解析 js/ts 源映射文件
  2. how to find the equivalent javascript compiled part of code, from typescript selected code如何从打字稿选定的代码中找到等效的 javascript 编译部分代码

I'm building a typescript compiler and I'm using typescript API for that.我正在构建一个打字稿编译器,为此我正在使用打字稿 API。

after compiling the code I want to let the user to select a part of ts code and then I want to highlight the compiled equivalent code in a WebView.编译代码后,我想让用户选择一部分 ts 代码,然后我想在 WebView 中突出显示编译后的等效代码。

so how to find/locate that piece of code?那么如何找到/定位那段代码呢?

please tell me if you need more information.如果您需要更多信息,请告诉我。 and sorry for poor English抱歉英语不好

source-map is a library that parses source maps and gives you an api to run queries on them. source-map是一个解析源映射的库,并为您提供一个 api 来对它们运行查询。

main.js.map

{
  "version": 3,
  "file": "main.js",
  "sourceRoot": "",
  "sources": [
    "main.ts"
  ],
  "names": [],
  "mappings": "AAKA,IAAM,KAAK,GAAM;IACf,CAAC,EAAE,CAAC;IACJ,CAAC,EAAE,CAAC;CACL,CAAA"
}

Example usage示例用法

await sourcemap.SourceMapConsumer.with(mainSourceMap /* main.js.map */, null, (consumer) => {
  consumer.sources // [ 'main.ts' ]

  consumer.generatedPositionFor({
    source: "main.ts",
    line: 7,
    column: 2,
  }) // { line: 2, column: 4, lastColumn: 4 }

  consumer.originalPositionFor({
    line: 2,
    column: 4,
  })  // { source: 'main.ts', line: 7, column: 2, name: null }
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将一个数组映射到另一个 Typescript - Map one array to another Typescript 来自已编译javascript和源代码映射的打字稿的原始来源 - Original source of typescript from compiled javascript and source map 如何在html页面中阅读JavaScript源代码 - How to read the JavaScript source code in an html page Javascript / Typescript映射较大对象的源对象属性 - Javascript/Typescript map a source object properties from a larger object 从 uglifyjs 源映射中恢复 javascript 源代码 - Recover javascript source code from uglifyjs source map 显示多个图像-javascript - reveal more than one image - javascript 如何将TypeScript代码和JS库合并到一个带源映射的文件中? - How to combine TypeScript code and JS libraries into one file with source maps? 如何通过查看源代码来区分 typescript 和 javascript? - How can I distinguish typescript from javascript by looking at the source code? 在 Typescript 源代码与生成的 JavaScript 上运行测试 - Running tests on Typescript source code Versus the generated JavaScript 优化代码:有没有更好的方法将 map 一个(较短的)数组转换为 Javascript 中的另一个(较长的)对象数组? - Refining code: Is there a better way to map one (shorter) array to another (longer) array of objects in Javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM