middleman / middleman-sprockets

Sprockets support for Middleman
http://middlemanapp.com
MIT License
62 stars 53 forks source link

Helper methods not accessible from erb files in js_dir #87

Closed Aetherus closed 8 years ago

Aetherus commented 8 years ago

I use middleman 4.0.0 with middleman-sprockets 4.0.0.rc.1 to build my static site.

When I try to call helper methods (e.g. url_for) from some *.html.erb file in js_dir, I got error saying that the helper method is not defined.

If I remove the middleman-sprockets from Gemfile, then there is no error when building. But I really want the //= require xxx feature.

Here is the error log:

/home/zhoumh/Desktop/demo/source/javascripts/demo.html.erb:1:in `_evaluate_template': undefined method `url_for' for #<#<Class:0x00000004dc7db0>:0x000000024efe10> (NoMethodError)
    from /home/zhoumh/.rvm/gems/ruby-2.2.3/gems/sprockets-3.4.1/lib/sprockets/erb_processor.rb:26:in `call'
    from /home/zhoumh/.rvm/gems/ruby-2.2.3/gems/sprockets-3.4.1/lib/sprockets/erb_processor.rb:13:in `call'
    from /home/zhoumh/.rvm/gems/ruby-2.2.3/gems/sprockets-3.4.1/lib/sprockets/processor_utils.rb:75:in `call_processor'
    from /home/zhoumh/.rvm/gems/ruby-2.2.3/gems/sprockets-3.4.1/lib/sprockets/processor_utils.rb:57:in `block in call_processors'
    from /home/zhoumh/.rvm/gems/ruby-2.2.3/gems/sprockets-3.4.1/lib/sprockets/processor_utils.rb:56:in `reverse_each'
    from /home/zhoumh/.rvm/gems/ruby-2.2.3/gems/sprockets-3.4.1/lib/sprockets/processor_utils.rb:56:in `call_processors'
    from /home/zhoumh/.rvm/gems/ruby-2.2.3/gems/sprockets-3.4.1/lib/sprockets/loader.rb:134:in `load_from_unloaded'
    from /home/zhoumh/.rvm/gems/ruby-2.2.3/gems/sprockets-3.4.1/lib/sprockets/loader.rb:60:in `block in load'
    from /home/zhoumh/.rvm/gems/ruby-2.2.3/gems/sprockets-3.4.1/lib/sprockets/loader.rb:318:in `fetch_asset_from_dependency_cache'
    from /home/zhoumh/.rvm/gems/ruby-2.2.3/gems/sprockets-3.4.1/lib/sprockets/loader.rb:44:in `load'
    from /home/zhoumh/.rvm/gems/ruby-2.2.3/gems/sprockets-3.4.1/lib/sprockets/cached_environment.rb:20:in `block in initialize'
    from /home/zhoumh/.rvm/gems/ruby-2.2.3/gems/sprockets-3.4.1/lib/sprockets/cached_environment.rb:47:in `yield'
    from /home/zhoumh/.rvm/gems/ruby-2.2.3/gems/sprockets-3.4.1/lib/sprockets/cached_environment.rb:47:in `load'
    from /home/zhoumh/.rvm/gems/ruby-2.2.3/gems/sprockets-3.4.1/lib/sprockets/base.rb:66:in `find_asset'
    from /home/zhoumh/.rvm/gems/ruby-2.2.3/gems/sprockets-3.4.1/lib/sprockets/environment.rb:30:in `find_asset'
    from /home/zhoumh/.rvm/gems/ruby-2.2.3/gems/sprockets-3.4.1/lib/sprockets/base.rb:92:in `[]'
    from /home/zhoumh/.rvm/gems/ruby-2.2.3/gems/middleman-sprockets-4.0.0.rc.1/lib/middleman-sprockets/extension.rb:249:in `sprockets_asset'
    from /home/zhoumh/.rvm/gems/ruby-2.2.3/gems/middleman-sprockets-4.0.0.rc.1/lib/middleman-sprockets/extension.rb:235:in `initialize'
    from /home/zhoumh/.rvm/gems/ruby-2.2.3/gems/middleman-sprockets-4.0.0.rc.1/lib/middleman-sprockets/extension.rb:120:in `new'
    from /home/zhoumh/.rvm/gems/ruby-2.2.3/gems/middleman-sprockets-4.0.0.rc.1/lib/middleman-sprockets/extension.rb:120:in `generate_resource'
    from /home/zhoumh/.rvm/gems/ruby-2.2.3/gems/middleman-sprockets-4.0.0.rc.1/lib/middleman-sprockets/extension.rb:97:in `block in manipulate_resource_list'
    from /home/zhoumh/.rvm/gems/ruby-2.2.3/gems/middleman-sprockets-4.0.0.rc.1/lib/middleman-sprockets/extension.rb:90:in `each'
    from /home/zhoumh/.rvm/gems/ruby-2.2.3/gems/middleman-sprockets-4.0.0.rc.1/lib/middleman-sprockets/extension.rb:90:in `reduce'
    from /home/zhoumh/.rvm/gems/ruby-2.2.3/gems/middleman-sprockets-4.0.0.rc.1/lib/middleman-sprockets/extension.rb:90:in `manipulate_resource_list'
    from /home/zhoumh/.rvm/gems/ruby-2.2.3/gems/middleman-core-4.0.0/lib/middleman-core/sitemap/store.rb:206:in `block (2 levels) in ensure_resource_list_updated!'
    from /home/zhoumh/.rvm/gems/ruby-2.2.3/gems/hamster-2.0.0/lib/hamster/vector.rb:1316:in `each'
    from /home/zhoumh/.rvm/gems/ruby-2.2.3/gems/hamster-2.0.0/lib/hamster/vector.rb:1316:in `traverse_depth_first'
    from /home/zhoumh/.rvm/gems/ruby-2.2.3/gems/hamster-2.0.0/lib/hamster/vector.rb:431:in `each'
    from /home/zhoumh/.rvm/gems/ruby-2.2.3/gems/middleman-core-4.0.0/lib/middleman-core/sitemap/store.rb:204:in `block in ensure_resource_list_updated!'
    from /home/zhoumh/.rvm/rubies/ruby-2.2.3/lib/ruby/2.2.0/monitor.rb:211:in `mon_synchronize'
    from /home/zhoumh/.rvm/gems/ruby-2.2.3/gems/middleman-core-4.0.0/lib/middleman-core/sitemap/store.rb:196:in `ensure_resource_list_updated!'
    from /home/zhoumh/.rvm/gems/ruby-2.2.3/gems/middleman-core-4.0.0/lib/middleman-core/sitemap/extensions/on_disk.rb:21:in `ready'
    from /home/zhoumh/.rvm/gems/ruby-2.2.3/gems/middleman-core-4.0.0/lib/middleman-core/callback_manager.rb:51:in `instance_exec'
    from /home/zhoumh/.rvm/gems/ruby-2.2.3/gems/middleman-core-4.0.0/lib/middleman-core/callback_manager.rb:51:in `block in execute'
    from /home/zhoumh/.rvm/gems/ruby-2.2.3/gems/hamster-2.0.0/lib/hamster/vector.rb:1316:in `each'
    from /home/zhoumh/.rvm/gems/ruby-2.2.3/gems/hamster-2.0.0/lib/hamster/vector.rb:1316:in `traverse_depth_first'
    from /home/zhoumh/.rvm/gems/ruby-2.2.3/gems/hamster-2.0.0/lib/hamster/vector.rb:431:in `each'
    from /home/zhoumh/.rvm/gems/ruby-2.2.3/gems/middleman-core-4.0.0/lib/middleman-core/callback_manager.rb:51:in `execute'
    from /home/zhoumh/.rvm/gems/ruby-2.2.3/gems/middleman-core-4.0.0/lib/middleman-core/callback_manager.rb:28:in `block in install_methods!'
    from /home/zhoumh/.rvm/gems/ruby-2.2.3/gems/middleman-core-4.0.0/lib/middleman-core/application.rb:292:in `start_lifecycle'
    from /home/zhoumh/.rvm/gems/ruby-2.2.3/gems/middleman-core-4.0.0/lib/middleman-core/application.rb:265:in `initialize'
    from /home/zhoumh/.rvm/gems/ruby-2.2.3/gems/middleman-cli-4.0.0/lib/middleman-cli/build.rb:51:in `new'
    from /home/zhoumh/.rvm/gems/ruby-2.2.3/gems/middleman-cli-4.0.0/lib/middleman-cli/build.rb:51:in `build'
    from /home/zhoumh/.rvm/gems/ruby-2.2.3/gems/thor-0.19.1/lib/thor/command.rb:27:in `run'
    from /home/zhoumh/.rvm/gems/ruby-2.2.3/gems/thor-0.19.1/lib/thor/invocation.rb:126:in `invoke_command'
    from /home/zhoumh/.rvm/gems/ruby-2.2.3/gems/thor-0.19.1/lib/thor/invocation.rb:133:in `block in invoke_all'
    from /home/zhoumh/.rvm/gems/ruby-2.2.3/gems/thor-0.19.1/lib/thor/invocation.rb:133:in `each'
    from /home/zhoumh/.rvm/gems/ruby-2.2.3/gems/thor-0.19.1/lib/thor/invocation.rb:133:in `map'
    from /home/zhoumh/.rvm/gems/ruby-2.2.3/gems/thor-0.19.1/lib/thor/invocation.rb:133:in `invoke_all'
    from /home/zhoumh/.rvm/gems/ruby-2.2.3/gems/thor-0.19.1/lib/thor/group.rb:232:in `dispatch'
    from /home/zhoumh/.rvm/gems/ruby-2.2.3/gems/thor-0.19.1/lib/thor/invocation.rb:115:in `invoke'
    from /home/zhoumh/.rvm/gems/ruby-2.2.3/gems/thor-0.19.1/lib/thor.rb:40:in `block in register'
    from /home/zhoumh/.rvm/gems/ruby-2.2.3/gems/thor-0.19.1/lib/thor/command.rb:27:in `run'
    from /home/zhoumh/.rvm/gems/ruby-2.2.3/gems/thor-0.19.1/lib/thor/invocation.rb:126:in `invoke_command'
    from /home/zhoumh/.rvm/gems/ruby-2.2.3/gems/thor-0.19.1/lib/thor.rb:359:in `dispatch'
    from /home/zhoumh/.rvm/gems/ruby-2.2.3/gems/thor-0.19.1/lib/thor/base.rb:440:in `start'
    from /home/zhoumh/.rvm/gems/ruby-2.2.3/gems/middleman-cli-4.0.0/bin/middleman:23:in `<top (required)>'
    from /home/zhoumh/.rvm/gems/ruby-2.2.3/bin/middleman:23:in `load'
    from /home/zhoumh/.rvm/gems/ruby-2.2.3/bin/middleman:23:in `<main>'
    from /home/zhoumh/.rvm/gems/ruby-2.2.3/bin/ruby_executable_hooks:15:in `eval'
    from /home/zhoumh/.rvm/gems/ruby-2.2.3/bin/ruby_executable_hooks:15:in `<main>'

Is there any workaround?

tdreyno commented 8 years ago

Looks like Sprockets is processing ERb instead of Middleman. You should be able to tell it not to like so:

sprockets.engines.delete('.erb')
Aetherus commented 8 years ago

@tdreyno sprockets.engines is a frozen hash.

tdreyno commented 8 years ago

:disappointed: Okay, I'll dig keeping into their API.

annael commented 8 years ago

Same issue with js.erb files.

annael commented 8 years ago

No updates? :(

lightheaded commented 8 years ago

+1

tdreyno commented 8 years ago

There is no maintainer for this repo, currently. Sorry, without that, there won't be much progress.

stevenosloan commented 8 years ago

@annael I think thats a separate, but related issue. The way I'm seeing it, that's the same as https://github.com/middleman/middleman/issues/1801 (just think if your file was js.coffee.erb). It seems like a fairly sticky issue because of expectations on which engine (middleman or sprockets) should render which files -- or in this case if middleman should render, then pass off to sprockets. (think if it was js.coffee.erb). I'm hoping that passing middleman's template context over to sprockets will alleviate most cases this would be an issue.

For the original issue (html.erb), I think we should limit what output file types sprockets should be handling. I'll definitely keep that in mind & report back when I get a chance to work on the context passing.

stevenosloan commented 8 years ago

Sure thing. For starters the .html.erb in js could be solved checking file types here -- probably just looking at the base/output extension. .css & .js being the obvious ones but maybe others are valid for sprockets not middleman to handle?

For context passing, I haven't quite figured out a good way to handle it -- but somewhere in the sprockets context eval the middleman template context needs to be included (or it's helpers at least). It gets kinda tricky though since that's a class -- and sprocket's expectation is to be including modules.

stevenosloan commented 8 years ago

hey @Aetherus , with #93 merged your original issue should be resolved on master. please re-open and let me know if you continue to see issues.