jwhitley / requirejs-rails

RequireJS support for your Rails 3 or 4 application
MIT License
592 stars 202 forks source link

requirejs.yml not detected on config.assets.digest is true #222

Closed chenxeed closed 9 years ago

chenxeed commented 9 years ago

Hi there.

I realized that the path described in my requirejs.yml is not shown on my HTML view page until I turn off config.assets.digest. This happened on both version 0.9.5 and 0.9.6. Is there any approach to keep the digest on and requirejs.yml works?

Thanks.

chenxeed commented 9 years ago

This is the require.config that was generated. The shim works, while paths not.

require.config({"baseUrl":"/assets","paths":{"application":"/assets/application-9d77dda6ec63cb19d264d2a7ae37905c"},"shim":{"bootstrap":{"deps":["jquery"]}}}); require(["controller/templates"]);

and this is the requirejs.yml

shim :
  'bootstrap' :
    deps : ['jquery']
paths :
  'bootstrap' : 'lib/bootstrap.min'
  'ajax-interceptor' : 'lib/ajax-interceptor'
  'cookie' : 'common/modules/cookie'
  'analytics' : 'helper/analytics'
  'static-resource' : 'resource/pikto-src'
  'pikto-layout-tools' : 'modules/pikto-layout-tools'
  'jquery' : 'jquery'
  'jquery_ujs' : 'jquery_ujs'
  'jquery.autocomplete' : 'lib/jquery.autocomplete'
  'jquery.lazyload' : 'lib/jquery.lazyload'
modules :
  - name : 'application'
priority : ['bootstrap']
carsomyr commented 9 years ago

@chenxeed I know the problem now. Thanks for the report!

carsomyr commented 9 years ago

@chenxeed What version of Sprockets?

chenxeed commented 9 years ago

Hi @carsomyr my sprockets is

  * sprockets (2.12.3)
  * sprockets-rails (2.2.4)
carsomyr commented 9 years ago

@chenxeed Actually this looks normal. Could you rephrase your question?

chenxeed commented 9 years ago

So basically, what I understand is requirejs.yml will decide how require.config() value shown in view file when we use requirejs_include_tag isn't it?

So this is the requirejs.yml, with some paths inside:

shim :
  'bootstrap' :
    deps : ['jquery']
paths :
  'bootstrap' : 'lib/bootstrap.min'
  'ajax-interceptor' : 'lib/ajax-interceptor'
  'cookie' : 'common/modules/cookie'
  'analytics' : 'helper/analytics'
  'static-resource' : 'resource/pikto-src'
  'pikto-layout-tools' : 'modules/pikto-layout-tools'
  'jquery' : 'jquery'
  'jquery_ujs' : 'jquery_ujs'
  'jquery.autocomplete' : 'lib/jquery.autocomplete'
  'jquery.lazyload' : 'lib/jquery.lazyload'
modules :
  - name : 'application'
priority : ['bootstrap']

But the paths is not shown on require.config()

require.config({"baseUrl":"/assets","paths":{"application":"/assets/application-9d77dda6ec63cb19d264d2a7ae37905c"},"shim":{"bootstrap":{"deps":["jquery"]}}}); require(["controller/templates"]);

Unless I set config.assets.digest to false. Is this normal? If so, means that digest is not supported for requirejs-rails?

carsomyr commented 9 years ago

Ah, not a bug. The power of requirejs-rails is that it integrates RequireJS using Rails conventions. Those conventions say that assets are served as needed in development and are compiled in production. Since everything you list there is inlined into application-*.js, there's no need for extra paths. Note that if you declare additional modules, those modules will be listed in paths, because they are separate compilation units.

chenxeed commented 9 years ago

Thank you for the explanation.

I see that to make it works, I face two conditions:

carsomyr commented 9 years ago

Actually, you may be describing a bug. Let me think about this.

chenxeed commented 9 years ago

There's one thing that I concern.

I need to load a modules asynchronously, for example:

$('.icon-notification').click(function(){
    require(['domman'], function(Domman){
    console.log(Domman);
    });
});

In this case, module 'domman' is not required yet on first initialization. The module 'domman' is only loaded when user click the button.

This approach is still possible in native requirejs or before we compile it. Is there any way to support this too after we precompile?

Thanks.

carsomyr commented 9 years ago

@chenxeed Sorry for the belated reply. Here are my responses to your two points:

  1. When I set config.assets.digest = true, everything still works in development. The system looks like it's serving up digested and undigested versions of assets.
  2. You must declare domman as a module in requirejs.yml. The precompilation procedures scan modules and will pick up domman as something to be explicitly precompiled.

I'll reopen if you're still having trouble with point 1.

chenxeed commented 9 years ago

Okay, thanks! :)

Yes, it works if I put it in modules, and I also have to generate the assets url path manually from rails app.

For example:

when domman is precompiled, it generated with hashname for digest. So, I have to get the domman file location.

var domman_url =  '<%= asset_path('domman') %>';

define([domman_url], function(domman){
...
});

It works this way :)

carsomyr commented 9 years ago

@chenxeed No no, you don't have to do that. Check for the script tag generated by requirejs-rails. You should see a paths entry for domman. Please confirm.

carsomyr commented 9 years ago

@chenxeed In other words, requirejs-rails shouldn't be awkward to use for all but the most exotic scenarios.