Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get coffee-script dependency tree with browserify

I am trying to get the dependency tree for a coffee-script project using browserify.

It seems that the project comes with the option to print the dependency tree from the command line with browserify -t coffeeify --deps ./script.coffee, with

script.coffee

console.log 'hello'
require 'a.coffee'

explodes with the error (edited for concision):

... Unexpected token ILLEGAL
    at parseDeps (/.../module-deps/index.js:172:45)
    at done (/.../browserify/node_modules/module-deps/index.js:152:13)
    at applyTransforms (/.../browserify/node_modules/module-deps/index.js:135:41)
    at /.../browserify/node_modules/module-deps/index.js:112:17
    at fs.js:266:14
    at Object.oncomplete (fs.js:107:15)

I would ideally like to automate the dependency tree creation by using the browserify API, but it is not apparent how one would accomplish this.

It seemed reasonable to begin working backwards from module-deps, but it is not apparent that it was designed to walk the dependency tree for a coffee-script project. I did not a related GitHub pull request, however. In this light, my hope was to inject a browserify transform and read the dependencies for each file after browserify had already transformed it into javascript from coffee-script, however module-deps seems only to take filenames and not javascript source itself. Something like the following:

require('browserify')
  .add('./script.coffee') 
  .transform(require('coffeeify')
  .transform((file) ->
    data = ''
    write = (buf) -> data += buf
    end = ->
      ## parse data for dependencies.
      console.log "File ", file, "dependencies", mdeps(data)
      @queue(data)
      @queue(null)
    return through(write, end)
  )

This doesn't work because the mdeps function does not exist as far as I can tell. I expect it would involve using one of the parsers (esprima, etc.) to look for require calls.

Has anyone accomplished printing a coffee-script dependency tree using browserify? It seems like it'd be very useful, and I'd be surprised if nobody has gotten to it yet.

like image 601
Brian M. Hunt Avatar asked Dec 06 '25 05:12

Brian M. Hunt


1 Answers

There appears to be a bug in browserify that prevents the deps() call from picking up transforms, so deps is trying to parse the raw coffee instead the compiled js.

The fix for your case is to do something like:

var coffeeify = require('coffeeify');
require('browserify')
  .add('./script.coffee') 
  .transform(coffeeify)
  .deps({transform: [coffeeify]});

I.E. pass the transform list to deps() yourself.

like image 86
Jason Walton Avatar answered Dec 08 '25 01:12

Jason Walton



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!