Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
trompx opened this issue Jul 13, 2017 · 4 comments
Closed

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

trompx opened this issue Jul 13, 2017 · 4 comments

Comments

@trompx
Copy link

trompx commented Jul 13, 2017

@1egoman

Now (same setup as in #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
Copy link
Owner

1egoman commented Jul 14, 2017

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

@trompx
Copy link
Author

trompx commented Jul 14, 2017

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
Copy link
Owner

1egoman commented Jul 14, 2017

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
Copy link
Author

trompx commented Jul 14, 2017

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.

@trompx trompx closed this as completed Jul 14, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants