Closed weepy closed 11 years ago
( it also works in the browser)
'what about require("beep") in a string?'
Congrats substack - you win the prize !
I think I had figured this when I wrote it, but it's a veeery edge case.
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
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
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 ;-)
It ignores calculated require's which are not string calls such as
require(a + b)
, but catches all others. It's also very fast -