browserify / detective

Find all calls to require() no matter how deeply nested using a proper walk of the AST
Other
414 stars 61 forks source link

i compressed this project into 10 lines : #8

Closed weepy closed 11 years ago

weepy commented 12 years ago
var regex = {
    all: /require\s*\('\s*([^'])*'\s*\)|require\s*\("\s*([^"])*"\s*\)/g,
    start: /require\s*\(\s*["']/,
    end: /["']\s*\)$/,
    comments: /\/\*.+?\*\/|\/\/.*(?=[\n\r])/g   // or /(\/\*([\s\S]*?)\*\/)|(\/\/(.*)$)/gm
  }

  function extract_dependencies(text) {
    text = text.replace(regex.comments, '')
    var requires = text.match(regex.all) || []
    for(var i=0; i< requires.length; i++) {
      requires[i] = requires[i].replace(regex.start, "").replace(regex.end, "")
    }
    return requires
  }

It ignores calculated require's which are not string calls such as require(a + b), but catches all others. It's also very fast -

weepy commented 12 years ago

( it also works in the browser)

ghost commented 12 years ago

'what about require("beep") in a string?'

weepy commented 12 years ago

Congrats substack - you win the prize !

I think I had figured this when I wrote it, but it's a veeery edge case.

weepy commented 12 years ago

you could easily remove these as well if you could be bothered. just extract all the strings and throw out the ones with strings inside them

coolaj86 commented 12 years ago

Regex and parsing can work okay, but not nearly as well as an AST.

See: https://github.com/coolaj86/node-pakman/blob/master/lib/get-requires.js And: https://github.com/coolaj86/node-pakman/blob/master/lib/remove-comments.js

I keep having issues with my attempt, so I'm really glad to have found detective. :-D

medikoo commented 12 years ago

100% bulletproof parsing cannot be done safely just with regular expressions, it's impossible if you look closely at JavaScript syntax. Still you can build straightforward code walker, that would be faster than any tool that creates whole AST tree. I've once built such for Webmake -> https://github.com/medikoo/find-requires/blob/master/lib/direct.js It works perfect, the only thing it's missing for now is awareness of HTML comments which are valid in EcmaScript syntax. I haven't seen anyone using them, but still it would good to take them into account I plan fix that ;-)