tft每日頭條

 > 科技

 > sourcemap的原理

sourcemap的原理

科技 更新时间:2024-12-22 01:06:03

sourcemap的原理?// extract-source-from-source-map.js const fs = require("fs") const path = require("path") const util = require("util") const source = require("source-map") const writeFile = util.promisify(fs.writeFile) const mapFile = process.argv[2] if (!mapFile) { console.error('no input file given') process.exit(1) } // 讀取“mapFile”路徑下的所有.map文件 let dir = fs.readdirSync(mapFile) dir = dir.filter((name) => /\.map$/.test(name)).map(name => `${mapFile}${name}`) for (const valueMap of dir) { console.log("map file start: ", valueMap); const mapFileContent = fs.readFileSync(valueMap, 'utf-8') const outputDir = path.join(__dirname, 'output') fs.mkdirSync(outputDir, { recursive: true }) new source.SourceMapConsumer(mapFileContent).then(consumer => { Promise.all(consumer.sources.map(async (source) => { const content = consumer.sourceContentFor(source) const outputPath = path.join(outputDir, source) fs.mkdirSync(path.dirname(outputPath), { recursive: true }) return writeFile(outputPath, content) })) }).catch(console.error) } ,我來為大家講解一下關于sourcemap的原理?跟着小編一起來看一看吧!

sourcemap的原理(通過source-map恢複源文件)1

sourcemap的原理

通過source-map恢複源文件還原腳本

// extract-source-from-source-map.js const fs = require("fs") const path = require("path") const util = require("util") const source = require("source-map") const writeFile = util.promisify(fs.writeFile) const mapFile = process.argv[2] if (!mapFile) { console.error('no input file given') process.exit(1) } // 讀取“mapFile”路徑下的所有.map文件 let dir = fs.readdirSync(mapFile) dir = dir.filter((name) => /\.map$/.test(name)).map(name => `${mapFile}${name}`) for (const valueMap of dir) { console.log("map file start: ", valueMap); const mapFileContent = fs.readFileSync(valueMap, 'utf-8') const outputDir = path.join(__dirname, 'output') fs.mkdirSync(outputDir, { recursive: true }) new source.SourceMapConsumer(mapFileContent).then(consumer => { Promise.all(consumer.sources.map(async (source) => { const content = consumer.sourceContentFor(source) const outputPath = path.join(outputDir, source) fs.mkdirSync(path.dirname(outputPath), { recursive: true }) return writeFile(outputPath, content) })) }).catch(console.error) }

運行

node extract-source-from-source-map.js [source-map文件目錄]

會在extract-source-from-source-map.js文件所在目錄下的output目錄輸出源代碼

,

更多精彩资讯请关注tft每日頭條,我们将持续为您更新最新资讯!

查看全部

相关科技资讯推荐

热门科技资讯推荐

网友关注

Copyright 2023-2024 - www.tftnews.com All Rights Reserved