1 Star 0 Fork 15

yiming2008/webpack-sourcemap-unpacker

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
webpack-sourcemap-unpacker.js 3.63 KB
一键复制 编辑 原始数据 按行查看 历史
Ninja Lau 提交于 2021-01-24 23:10 . handle filename ends with hash
#!/usr/bin/env node
const path = require('path');
const fse = require('fs-extra');
const glob = require('glob');
const SourceMapConsumer = require('source-map').SourceMapConsumer;
const program = require('commander');
const config = require('./package.json');
const dataUriToBuffer = require('data-uri-to-buffer');
program.version(config.version, '-v, --version').description('Unpack the sourcemaps generated by Webpack to a project folder').usage('[options]').option('-p, --prefix [webpack:///]', 'filter the path with certain prefix').option('-i, --input [glob-pattern]',
'a glob patterns to describe sourcemap files to be unpacked, default to *.map').option('-o, --output [folder]', 'set the output folder, default to current folder');
program.action((options) => {
options.input = options.input || '*.map';
options.output = options.output || '.';
options.prefix = options.prefix || 'webpack:///';
let files = glob.sync(options.input);
if (!files.length) {
console.warn(`There is no ${options.input} in current directory`);
return;
}
console.info(`output src to ${options.output}`);
files.forEach(async function(filepath) {
console.info(`extract sourcemap: ${filepath}`);
const mapSource = fse.readFileSync(path.resolve(filepath));
const consumer = await new SourceMapConsumer(mapSource.toString());
const paths = consumer._absoluteSources;
const sources = consumer.sourcesContent;
paths.forEach((p, idx) => {
const relativePath = path.relative(options.prefix, p).replace(/\\/g, '/');
const parts = relativePath.match(/([0-9a-zA-Z_/.\-@]+?)(\?\w{4})?$/);
if (!parts) {
// ignore special path
console.log(`ignore file: ${p}`);
return;
}
// handle filename ends with hash
const folder = path.resolve(path.join(options.output, path.dirname(parts[1])));
const basename = path.basename(parts[1]);
const extname = path.extname(basename);
const filename = (parts[2] ? path.basename(basename, extname) + parts[2] + extname : basename);
let absPath = path.resolve(path.join(folder, filename));
if (path.relative(path.resolve(), absPath).startsWith('..')) {
console.error(`Do not write files to ancestor folder: ${p}. Please check the prefix option: ${options.prefix}`);
process.exit(1);
}
console.info(`output to file: ${absPath}`);
let source;
switch (extname) {
// plain text files
case '.glsl':
source = evalModule(sources[idx]).exports;
break;
// base64 files
case '.jpg':
case '.gif':
case '.png':
case '.obj':
const exports = evalModule(sources[idx]).exports;
try {
source = dataUriToBuffer(exports);
} catch (e) {
console.info(`It's not a binary file: ${absPath}`);
source = sources[idx];
absPath += '.js';
}
break;
default:
source = sources[idx];
}
fse.ensureDirSync(folder);
fse.writeFileSync(absPath, source);
});
});
});
program.parse(process.argv);
function evalModule(source) {
return eval(`
let module = {};
let __webpack_public_path__ = '';
${source}
module;
`);
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/yiming2008/webpack-sourcemap-unpacker.git
[email protected]:yiming2008/webpack-sourcemap-unpacker.git
yiming2008
webpack-sourcemap-unpacker
webpack-sourcemap-unpacker
master

搜索帮助