Giter Site home page Giter Site logo

source-map-unpack's Introduction

npm version Twitter

CLI to unpack ๐Ÿ› your JS source maps ๐Ÿ—บ to original files and folders.

Usage:

  1. npm install -g source-map-unpack
  2. unpack <project-folder> <path-to-sourcemap>

For example:

unpack egghead js/egghead-bundle.js.map

Note:

The original minified file should be placed according to the path in sources map's file variable.

source-map-unpack's People

Contributors

dependabot[bot] avatar janv avatar pavloko avatar ra80533 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

source-map-unpack's Issues

Error running on node 18

When running this tool under Node 18, the following error is thrown:

Error: You must provide the URL of lib/mappings.wasm by calling SourceMapConsumer.initialize({ 'lib/mappings.wasm': ... }) before using SourceMapConsumer

I've found the explanation here: https://stackoverflow.com/a/77308483/2280444

Workaround: run under node 16 - tested, it works

Solution: upgrade source-map package to 0.7.4 - not tested

Error on unpack

Hi
I want to unpack a sourcemap file and I get this error

image

error TS2345: Argument of type 'string | null' is not assignable to parameter of type

Doesn't work with Node.js 14.8

src/index.ts:49:40 - error TS2345: Argument of type 'string | null' is not assignable to parameter of type 'string | Uint8Array | Uint8ClampedArray | Uint16Array | Uint32Array | Int8Array | Int16Array | In...'.
Type 'null' is not assignable to type 'string | Uint8Array | Uint8ClampedArray | Uint16Array | Uint32Array | Int8Array | Int16Array | In...'.

49             fs.writeFileSync(filePath, content)

This seems to fix it

diff --git a/src/index.ts b/src/index.ts
index e3c3e67..f274fa5 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -46,7 +46,7 @@ try {
const content = consumer.sourceContentFor(source)
const filePath = `${process.cwd()}/${projectNameInput}/${source.substring(WEBPACK_SUBSTRING_INDEX)}`
mkdirp.sync(dirname(filePath))
-            fs.writeFileSync(filePath, content)
+            fs.writeFileSync(filePath, content as string)
})
console.log(chalk.green('๐ŸŽ‰  All done! Enjoy exploring your code ๐Ÿ’ป'))
})

EISDIR: illegal operation on a directory Error

I'm getting the same error as here, I've tried the solutions but they didn't work for me and I'm recreating it because the issue is closed.

#22

Error:

>unpack output C:\Users\windows\Desktop\source\static\js\main.60f996d4.chunk.js.map
Unpacking ๐Ÿ›  your sourceย maps ๐Ÿ—บ
node:fs:590
  handleErrorFromBinding(ctx);
  ^

Error: EISDIR: illegal operation on a directory, open 'C:\Users\alperen.saylar\cikmis\'
    at Object.openSync (node:fs:590:3)
    at Object.writeFileSync (node:fs:2202:35)
    at C:\Users\alperen.saylar\AppData\Roaming\npm\node_modules\source-map-unpack\dist\index.js:47:16
    at Array.forEach (<anonymous>)
    at C:\Users\alperen.saylar\AppData\Roaming\npm\node_modules\source-map-unpack\dist\index.js:42:17
    at C:\Users\alperen.saylar\AppData\Roaming\npm\node_modules\source-map-unpack\node_modules\source-map\lib\source-map-consumer.js:78:16 {
  errno: -4068,
  syscall: 'open',
  code: 'EISDIR',
  path: 'C:\\Users\\alperen.saylar\\cikmis\\'
}

I invested in the arrangement called here #23 but the problem persists:

if (!fs.existsSync(pathToProject)) {
	console.log()
    // Create project dir if it doesn't exist
    try {
        fs.mkdirSync(projectNameInput,'0777', true)
    } catch (err) {
        console.log(chalk.red(`Couldn't create project at: ${pathToProject}, please, check your permissions`))
    }

    console.log(chalk.red(`Project created at: ${pathToProject}`))
    console.log()
    process.exit()
}

The full code is as follows:

#!/usr/bin/env node
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var fs = require("fs");
var path = require("path");
var chalk = require("chalk");
var mkdirp = require("mkdirp");
var minimist = require("minimist");
var source_map_1 = require("source-map");
var argv = minimist(process.argv.slice(2));
var projectNameInput = argv._[0];
var mapInput = argv._[1];
if (!projectNameInput || !mapInput) {
    console.log();
    console.log(chalk.white('Usage: unpack'), chalk.green('<folder-name-to-unpack-files-to)> <path-to-map-file>'));
    console.log();
    console.log(chalk.blue('Note: Minified javascript file should be placed under path specified in .map file.'));
    console.log();
    process.exit();
}
var pathToProject = path.join(process.cwd(), projectNameInput);
var pathToMap = path.isAbsolute(mapInput)
    ? mapInput
    : path.join(process.cwd(), mapInput);
if (!fs.existsSync(pathToProject)) {
	console.log()
    // Create project dir if it doesn't exist
    try {
        fs.mkdirSync(projectNameInput,'0777', true)
    } catch (err) {
        console.log(chalk.red(`Couldn't create project at: ${pathToProject}, please, check your permissions`))
    }

    console.log(chalk.red(`Project created at: ${pathToProject}`))
    console.log()
    process.exit()
}
if (!fs.existsSync(pathToMap)) {
    console.log();
    console.log(chalk.red("Can't find map file under : " + pathToMap));
    console.log();
    process.exit();
}
try {
    var mapFile = fs.readFileSync(pathToMap, 'utf8');
    source_map_1.SourceMapConsumer.with(mapFile, null, function (consumer) {
        console.log(chalk.green("Unpacking \uD83D\uDECD  your source\u00A0maps \uD83D\uDDFA"));
        var sources = consumer.sources;
        sources.forEach(function (source) {
            var WEBPACK_SUBSTRING_INDEX = 11;
            var content = consumer.sourceContentFor(source);
            var filePath = process.cwd() + "\\" + projectNameInput + "\\" + source.substring(WEBPACK_SUBSTRING_INDEX);
            mkdirp.sync(path.dirname(filePath));
            fs.writeFileSync(filePath, content);
        });
        console.log(chalk.green('๐ŸŽ‰  All done! Enjoy exploring your code ๐Ÿ’ป'));
    });
}
catch (err) {
    console.log(chalk.red('Oops! Something is wrong with the source map'), err);
    console.log(chalk.red('Make sure .min.js is correctly placed under the path specified in .map file'));
    console.log('STDERR: ');
    console.log(err);
}
//# sourceMappingURL=index.js.map

Process does not signal a failure when an error occurs

} catch (err) {
console.log(chalk.red('Oops! Something is wrong with the source map'), err)
console.log(chalk.red('Make sure .min.js is correctly placed under the path specified in .map file'))
console.log('STDERR: ')
console.log(err)
}

Suggestion

  • Cause the process to return a signal code indicative of an error via one of the following:
    1. Set process.exitCode to 1 (recommended; the documentation on process.exit() explains why)
    2. Evaluate process.exit(1)
  • (Optional) Remove one of the two instances of the error being logged

tsconfig.json contains an invalid property

"baseUrl": "./"

baseUrl should reside in the compilerOptions object. The reason why the current set-up is working as expected is that TypeScript defaults baseUrl to the directory in which tsconfig.json resides (which happens to be the same).

Suggestion

Remove baseUrl from tsconfig.json or move it inside compilerOptions.

Allow to process all files in a directory

First, let me thank you for this tool, it is awesome and it is helping us!

Could it be possible to have it go over all the *.map files in a directory and unpack all of them?
Our build outputs a bunch of bundles and chunks.

Thank you!

One character is cut off from the file paths

When I unpacked a .js.map file I get these folders:

asics
omposites

However, they should be "basics" and "composites".

const WEBPACK_SUBSTRING_INDEX = 11;
const content = consumer.sourceContentFor(source) as string;
const filePath = `${process.cwd()}/${projectNameInput}/${source.substring(
WEBPACK_SUBSTRING_INDEX,
)}`;

'webpack://'.length => 10

So the quick fix is probably to change WEBPACK_SUBSTRING_INDEX to 10. However, personally I think I'd prefer if you just did source.replaceAll('://', '/') instead. Not sure if there could ever be another prefix other than webpack://. ๐Ÿคท

EISDIR: illegal operation on a directory

C:\Users\Parthiv>unpack nau Downloads/main.5ff428f5.chunk.js.map
Unpacking ๐Ÿ› your sourceย maps ๐Ÿ—บ

(node:12492) UnhandledPromiseRejectionWarning: Error: EISDIR: illegal operation on a directory, open 'C:\Users\Parthiv/nau/'
    at Object.openSync (fs.js:476:3)
    at Object.writeFileSync (fs.js:1467:35)
    at C:\Users\Parthiv\AppData\Roaming\npm\node_modules\source-map-unpack\dist\index.js:47:16
    at Array.forEach (<anonymous>)
    at C:\Users\Parthiv\AppData\Roaming\npm\node_modules\source-map-unpack\dist\index.js:42:17
    at C:\Users\Parthiv\AppData\Roaming\npm\node_modules\source-map-unpack\node_modules\source-map\lib\source-map-consumer.js:78:16
(Use `node --trace-warnings ...` to show where the warning was created)
(node:12492) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:12492) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Broken in Node.js v18

Since this package is using source-map 0.7.3, it is suffering from mozilla/source-map#349.

$ unpack panel-js panel.js.map
/opt/homebrew/lib/node_modules/source-map-unpack/node_modules/source-map/lib/read-wasm.js:8
      throw new Error("You must provide the URL of lib/mappings.wasm by calling " +
            ^

Error: You must provide the URL of lib/mappings.wasm by calling SourceMapConsumer.initialize({ 'lib/mappings.wasm': ... }) before using SourceMapConsumer
    at readWasm (/opt/homebrew/lib/node_modules/source-map-unpack/node_modules/source-map/lib/read-wasm.js:8:13)
    at wasm (/opt/homebrew/lib/node_modules/source-map-unpack/node_modules/source-map/lib/wasm.js:25:16)
    at /opt/homebrew/lib/node_modules/source-map-unpack/node_modules/source-map/lib/source-map-consumer.js:264:14

Node.js v18.16.1

Installing Node.js v16.20.1 makes it work.

If source-map is upgraded to 0.7.4 then this problem will be fixed.

No need for minified source code files

I don't think this recommendation is necessary. Maybe it was different in older source map versions, but I think these days all of the necessary information is present in the .js.map file. I don't need to have the compiled javascript file in the same directory.

console.log(
chalk.blue(
'*Note: Minified file should be placed under path specified in .map file.',
),
);

Note:
The original minified file should be placed according to the path in sources map's `file` variable.

So this suggestion should probably be removed. If it is required in some special cases then hopefully the person using the program understands this already.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.