Skip to content
This repository has been archived by the owner on Jun 10, 2018. It is now read-only.

Conditional require #90

Closed
coder2000 opened this issue Jun 14, 2011 · 14 comments
Closed

Conditional require #90

coder2000 opened this issue Jun 14, 2011 · 14 comments
Labels

Comments

@coder2000
Copy link

I am working on a rails plugin and it has support for different javascript frameworks. With the new asset pipeline however, I can't select the correct file to use. I would like to be able to determine the framework available and load appropriate javascript.

@josh
Copy link
Contributor

josh commented Jun 14, 2011

You can use .erb to do conditionals.

@josh josh closed this as completed Jun 14, 2011
@coder2000
Copy link
Author

@josh I have tried that however it seems that it isn't parsed because it ends up three lines down the file.

@vhochstein
Copy link

Similar issue here. js.erb are not filtered by erb...
//= require <%=ActiveScaffold.js_framework.to_s%>/my_test

@josh
Copy link
Contributor

josh commented Jun 18, 2011

<% require_asset "#{ActiveScaffold.js_framework}/my_test" %>

@vhochstein
Copy link

If I write the following in my gem manifest file with erb extension:

<% if ActiveScaffold.js_framework == :jquery %>
//= require jquery/active_scaffold
<% else %>
//= require prototype/active_scaffold
<% end %>

erb is evaluated, but unfortuently the code in my manifest file is concatenated to one javascript file, but require statement is nt processed...

@coder2000
Copy link
Author

Thank You. Using require_asset worked. I didn't know you could call it directly or that it existed.

@awendt
Copy link

awendt commented Nov 29, 2011

Thanks @josh for the hint to require_asset

@andyjeffries
Copy link

This seems like a very logical thing to want to do. Is there any explanation of why it works this way round (and you need the require_asset workaround) or plans to "fix" it in a future version?

@MiguelMadero
Copy link

Seems like require statements are processed before the erb processor, that's why it doesn't have any effect. Strangely the require statement is still removed from the file instead of being left as a comment. require_asset did the trick for me.

Thanks

@alex88
Copy link

alex88 commented Apr 2, 2014

Also anything after the erb's require_asset doesn't get parsed, so I think that should be an issue

@coli
Copy link

coli commented May 7, 2014

require_asset is being added to the end of the list, instead of in the order of this file.

eg:

 = require angular
<% require_asset "angular-mocks" if Rails.env.development? %>
 = require common

and the order ended up being:

angular, common, angular-mocks

seanlinsley added a commit to activeadmin/activeadmin that referenced this issue Jul 11, 2014
jquery-ui-rails changes the folder structure, and Sprockets doesn’t
provide a way to conditionally load assets, so the only real solution
is to force ~> 5.0

Closes #3247

Ref: sstephenson/sprockets#90
@tamvm
Copy link

tamvm commented Nov 29, 2014

@coli 's right, any solution to add file conditionally in application.js?

adacosta pushed a commit to adacosta/activeadmin that referenced this issue Dec 4, 2014
jquery-ui-rails changes the folder structure, and Sprockets doesn’t
provide a way to conditionally load assets, so the only real solution
is to force ~> 5.0

Closes activeadmin#3247

Ref: sstephenson/sprockets#90
@meowsus
Copy link

meowsus commented Oct 14, 2015

@coli @tam-vo Did you ever find a solution to this? No matter where I place my ERB conditional it's always added at the end of my file.

kappezoro pushed a commit to kappezoro/sprockets that referenced this issue Dec 3, 2015
This fix is targeted at 4.x.

Right now the cache is using absolute paths to generate cache keys, this doesn't work if you end up running `rake assets:precompile` in different directories. Say you've moved your project or are running on a different server with a different absolute path. Or say, on every build on Heroku ever. This was reported in rails#59 and sstephenson#90

This commit is a introduces UnloadedAsset class that is responsible for among other things generating consistent cache keys.

Previously when running https://gist.github.com/schneems/85f592ba2773761dfcf3 the output would show that the second build took over 38 seconds to build. With this patch applied it takes around 3.38 seconds.

Most of this commit is docs, as the current behavior and use of the cache is not really documented, and extremely difficult to follow in the code. All existing behavior is preserved, all tests pass, an extra test was added to ensure that a project built using a previously built directory will be fast.
kappezoro pushed a commit to kappezoro/sprockets that referenced this issue Dec 3, 2015
…he-paths-4.x

[close sstephenson#90] Use relative path for cache keys
@pjar
Copy link

pjar commented Jul 11, 2017

Having the same issue as @alex88 - everything after an erb tags is ignored and just plain text in the compiled .js file. Any solutions for that?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests