1egoman / debundle

:card_file_box: A javascript debundler. Takes a Browserify or Webpack bundle and recreates the initial, pre-bundled source.
https://www.npmjs.com/package/debundle
705 stars 145 forks source link

TypeError: Cannot read property '2' of undefined #6

Closed trompx closed 7 years ago

trompx commented 7 years ago

@1egoman

Now (same setup as in https://github.com/1egoman/debundle/issues/5), when I run the command:

debundle -i public/assets/frontend/js/modules/scripts.js -o public/assets/frontend/js/modules/unpacked/ -c public/assets/frontend/js/modules/debundle.config.json

I get:

* Using default AST location for browserify...
* Reading bundle...
* Using auto-discovered browserify entry point...
/usr/local/lib/node_modules/debundle/src/index.js:63
  config.entryPoint = ast.body[0].expression.arguments[2].elements[0].value;
                                                      ^
TypeError: Cannot read property '2' of undefined
    at Object.<anonymous> (/usr/local/lib/node_modules/debundle/src/index.js:63:55)
    at Module._compile (module.js:556:32)
    at Object.Module._extensions..js (module.js:565:10)
    at Module.load (module.js:473:32)
    at tryModuleLoad (module.js:432:12)
    at Function.Module._load (module.js:424:3)
    at Module.runMain (module.js:590:10)
    at run (bootstrap_node.js:394:7)
    at startup (bootstrap_node.js:149:9)
    at bootstrap_node.js:509:3
1egoman commented 7 years ago

Your moduleAST variable is set incorrectly. See https://github.com/1egoman/debundle/blob/master/DOCS.md#moduleast

trompx commented 7 years ago

Thanks, I made some progress. I had to add a console.log(ast.body) to check the structure, maybe considering adding an option to output that data would help people configuring their moduleAst parameter.

console.log(ast.body) output:

Node {
  type: 'ExpressionStatement',
  start: 0,
  end: 2418,
  expression:
   Node {
     type: 'UnaryExpression',
     start: 0,
     end: 2417,
     operator: '!',
     prefix: true,
     argument:
      Node {
        type: 'CallExpression',
        start: 1,
        end: 2417,
        callee: [Object],
        arguments: [Object] } } }

So I ended up with the following config:

{
  "type": "browserify",
  "knownPaths": {},
  "entryPoint": "ast.body[0].expression.argument.arguments[2].elements[0].value",
  "moduleAst": ["body", 0, "expression", "argument", "arguments", 0]
}

With my bundle, I get the following output with still an error:

* Reading bundle...
* Decoding modules...
* Discovered module 1
* Extracted module code for 1
* Extracted module lookup table for 1
* Calculated module lookup table for 1
* Discovered module 2
* Extracted module code for 2
* Extracted module lookup table for 2
* Calculated module lookup table for 2
* Reassembling requires...
* Replacing e with 'module'...
* Replacing i with 'exports'...
* Replacing e with 'module'...
* Replacing i with 'exports'...
* Resolving files...
/usr/local/lib/node_modules/debundle/src/utils/getModulePath.js:8
    throw new Error(`Module ${mod.id} cannot be found in the module array.`);

TypeError: Cannot read property 'id' of undefined
    at makeModuleTree (/usr/local/lib/node_modules/debundle/src/utils/getModulePath.js:8:34)
    at getModuleLocation (/usr/local/lib/node_modules/debundle/src/utils/getModuleLocation.js:17:14)
    at modules.map.i (/usr/local/lib/node_modules/debundle/src/lookupTable.js:14:17)
    at Array.map (native)
    at lookupTableResolver (/usr/local/lib/node_modules/debundle/src/lookupTable.js:10:18)
    at Object.<anonymous> (/usr/local/lib/node_modules/debundle/src/index.js:126:15)
    at Module._compile (module.js:556:32)
    at Object.Module._extensions..js (module.js:565:10)
    at Module.load (module.js:473:32)
    at tryModuleLoad (module.js:432:12)
1egoman commented 7 years ago

Entrypoint should be the id of the module that is at the top of the module tree. See https://github.com/1egoman/debundle/blob/master/DOCS.md#entrypoint-required-for-webpack-bundles

In the bundle here, I believe your entrypoint is the module with the id of 1.

Try something like this:

{
  "type": "browserify",
  "knownPaths": {},
  "entryPoint": 1,
  "moduleAst": ["body", 0, "expression", "argument", "arguments", 0]
}

Also, the docs and error messages for this project could be improved. If you have any suggestions, I'd appreciate if you could either leave an issue or contribute better versions. Unfortunately I don't have time right now to devote to this project. 😦

trompx commented 7 years ago

No problem.

With your config it works correctly. Unfortunatly, the debundle does not return a clean es6 class just like here: https://gist.github.com/SamVerschueren/233d48892f6ba88be4a8768dc4fc5886

Anyway I greatly appreciate your help, thanks again.