做项目时发现一个站点为前后分离的架构,翻了下发现了map文件,采用restore-source-tree还原时报错,记录下解决过程

安装

先从git克隆到本地

git clone https://github.com/laysent/restore-source-tree.git

然后进入文件夹

npm install

这里建议挂个代理、VPN,国内很慢

这里在win下会报错

λ npm cinstall                                                                                                                                                            
npm WARN deprecated [email protected]: � Thanks for using Babel: we recommend using babel-preset-env now: please read https://babeljs.io/env to update!
npm WARN deprecated [email protected]: core-js@<3 is no longer maintained and not recommended for usage due to the number of issues. Please, upgrade your dependencies to the
ctual version of core-js@3.
npm WARN deprecated [email protected]: CircularJSON is in maintenance only, flatted is its successor.

> [email protected] postinstall C:\Users\Administrator\Desktop\qubu\restore-source-tree\node_modules\core-js
> node -e "try{require('./postinstall')}catch(e){}"

Thank you for using core-js ( https://github.com/zloirock/core-js ) for polyfilling JavaScript standard library!

The project needs your help! Please consider supporting of core-js on Open Collective or Patreon:
> https://opencollective.com/core-js
> https://www.patreon.com/zloirock

Also, the author of core-js ( https://github.com/zloirock ) is looking for a good job -)

npm WARN prepublish-on-install As of npm@5, `prepublish` scripts are deprecated.
npm WARN prepublish-on-install Use `prepare` for build steps and `prepublishOnly` for upload-only.
npm WARN prepublish-on-install See the deprecation note in `npm help scripts` for more information.

> [email protected] prepublish C:\Users\Administrator\Desktop\qubu\restore-source-tree
> npm run build


> [email protected] build C:\Users\Administrator\Desktop\qubu\restore-source-tree
> NODE_ENV=production babel index.js --out-file dist.js

'NODE_ENV' is not recognized as an internal or external command,
operable program or batch file.
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] build: `NODE_ENV=production babel index.js --out-file dist.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\Administrator\AppData\Roaming\npm-cache\_logs\2020-03-13T08_30_28_330Z-debug.log
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] prepublish: `npm run build`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] prepublish script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\Administrator\AppData\Roaming\npm-cache\_logs\2020-03-13T08_30_28_406Z-debug.log

'NODE_ENV' is not recognized as an internal or external command,operable program or batch file.

跨平台缘故,NODE_ENV*nix环境变量,安装cross-env包然后修改

npm install -g cross-env

目录下面的package.json中的"build": "NODE_ENV=production babel index.js --out-file dist.js","build": "cross-env NODE_ENV=production babel index.js --out-file dist.js",

修改后完整如下

{
"name": "restore-source-tree",
"version": "0.1.1",
"description": "Restores file structure from source map",
"main": "dist.js",
"bin": {
"restore-source-tree": "bin/restore-source-tree.js"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "cross-env NODE_ENV=production babel index.js --out-file dist.js",
"prepublish": "npm run build"
},
"author": "Alexander <[email protected]> (http://kuzya.org/)",
"license": "MIT",
"dependencies": {
"commander": "^2.9.0",
"glob": "^7.1.3",
"minimist": "^1.2.0",
"mkdirp": "^0.5.1",
"source-map": "^0.5.6"
},
"devDependencies": {
"babel-cli": "^6.11.4",
"babel-eslint": "^6.1.2",
"babel-preset-es2015": "^6.9.0",
"babel-preset-stage-0": "^6.5.0",
"eslint": "^3.1.1"
}
}

然后执行npm install

npm install
npm WARN prepublish-on-install As of npm@5, `prepublish` scripts are deprecated.
npm WARN prepublish-on-install Use `prepare` for build steps and `prepublishOnly` for upload-only.
npm WARN prepublish-on-install See the deprecation note in `npm help scripts` for more information.

> [email protected] prepublish C:\Users\Administrator\Desktop\qubu\restore-source-tree
> npm run build


> [email protected] build C:\Users\Administrator\Desktop\qubu\restore-source-tree
> cross-env NODE_ENV=production babel index.js --out-file dist.js

npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN [email protected] No repository field.
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: [email protected] (node_modules\fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for [email protected]: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"})

audited 4406 packages in 16.868s

3 packages are looking for funding
run `npm fund` for details

found 1 low severity vulnerability
run `npm audit fix` to fix them, or `npm audit` for details

编译成功,restore-source-tree整体拷贝到C:\Users\Administrator\AppData\Roaming\npm\node_modules\restore-source-tree即可使用

image-20200313170912252

备注

Windows平台不允许路径、文件中含有?等特殊字符,修改dist.js中101行前后为


(0, _mkdirp2.default)(dir, function (err) {
if (err) {
console.error('Failed creating directory', dir);
process.exit(1);
} else {
outPath=outPath.replace('?','_');//替换特殊字符
_fs2.default.writeFile(outPath, content, function (err) {
if (err) {
console.error('Failed writing file', outPath);
}
});

修改前

image-20200313171223971

修改后

image-20200313171232939

代码也正常还原了

image-20200313171253553