hsume2 / browserify-rails

Deprecated https://github.com/browserify-rails/browserify-rails
MIT License
57 stars 11 forks source link

Browserify-rails should attempt to open files relative to asset paths if not present in ./ #19

Closed cymen closed 10 years ago

cymen commented 10 years ago

So say I put a spec in spec/javascripts/a_spec.js then I want to do var a = require('a'); however I can't because a.js is in app/assets/javascripts. Browserify-rails tries to open it relative to the location of the spec file and then bombs out when it doesn't find it. It should instead try to open it relative to the configured asset paths.

cymen commented 10 years ago

So reading: https://github.com/substack/node-resolve

Looks like require takes an option hash as second parameter and we could set opts.path -- would be nicer to have a way to tell node globally "add these three paths to your fallback" so looking for that way to do it.

cymen commented 10 years ago

The answer appears to be NODE_PATH environmental variable: http://nodejs.org/api/modules.html#modules_loading_from_the_global_folders

I'm going to see if I can hook that up to work with the configured asset paths we have already.

cymen commented 10 years ago

Unfortunately, it looks like support for NODE_PATH went away -- it isn't working locally.

Update: might be wrong, exploring.

filaraujo commented 10 years ago

require() is expected to be relative path in node. Its a large caveat, but you can get around this by just having your spec files require relative paths. They tend to be ugly, but it works.

heres an example of a spec file I have

var tracking = require('../../../../app/assets/javascripts/shared/modules/tracking');

describe('tracking.js', function(){
    it('should return an object', function() {
        expect(typeof tracking).toBe('object');
    });
    ...
    });
});
cymen commented 10 years ago

That is what I was doing but it doesn't work with jasmine-rails -- it copies files to another directory breaking the relative path. It works fine with access /specs while running the web server but running the rake task like so bundle exec rake spec:javascripts fails.

Setting NODE_PATH works however browserify --list fails with it although plain browserify works. It seems to be a browserify bug.

cymen commented 10 years ago

I opened a bug report on browserify for this:

https://github.com/substack/node-browserify/issues/726

I am starting to wonder if we're better off wrapping browserify so we can use the function browserify and not the command line interface.

cymen commented 10 years ago

I fixed the bug in node-browserify (actually, module-deps) -- with v3.41.0 we're working again!