Skip to content
This repository has been archived by the owner. It is now read-only.

Stub causes shared dependency not to be required #467

Closed
derekprior opened this Issue Aug 15, 2013 · 15 comments

Comments

Projects
None yet
6 participants
@derekprior
Copy link

derekprior commented Aug 15, 2013

Consider the following manifests:

application.js:

//= require jquery
//= require_tree .
//= stub active_admin

active_admin.js:

//= require jquery
//= require a_bunch_of_other_stuff

The stub directive in application.js causes all of active_admin.js' dependencies to also be stubbed. I'm seeing that this causes jquery to be stubbed, even in face of the explicit require in application.js It does not seem to matter where I add the stub directive in relation to the jquery require.

Is this behavior intentional? It seems to me that an explicit require at the same level or higher than the stub should override the stub.

@josh

This comment has been minimized.

Copy link
Contributor

josh commented Aug 15, 2013

The stub directive in application.js causes all of active_admin.js' dependencies to also be stubbed.

Yep.

@derekprior

This comment has been minimized.

Copy link
Author

derekprior commented Aug 15, 2013

Right... so the question is, should that be overridden in the face of the explicit require in the very same file? It's not clear to me that was the intent. From you answer, I take it is it?

@josh

This comment has been minimized.

Copy link
Contributor

josh commented Aug 15, 2013

No require can override a stub.

@josh

This comment has been minimized.

Copy link
Contributor

josh commented Aug 15, 2013

If you are stubbing and including active_admin.js on your page, that means jquery is already loaded so it should not be included twice in the application.js file.

@derekprior

This comment has been minimized.

Copy link
Author

derekprior commented Aug 16, 2013

The use case was separate manifests (and compiled files) for different areas of the site.

Most areas get application.js (which should not include the admin javascript). The admin area gets the admin JS. Both need jquery.

I've solved this now by moving the admin JS out to lib/assets. The other option would be to drop require_tree.

@rosenfeld

This comment has been minimized.

Copy link

rosenfeld commented Aug 26, 2013

I have the same issue, and I think this is a common requirement. In my application, I want to precompile 3 bundles:

  • libraries.js
  • client.js
  • internal.js

Libraries usually never change and are big, so bundling them altogether won't invalidate their cache usually after each deployment. Client stuff are intended to be available to all users and shouldn't include duplicate dependencies from libraries.js. Finally internal.js is supposed to contain only code relevant to internal users (admin, editors, etc). Since libraries.js and client.js are always included, internal.js shouldn't contain any duplicates from those files.

Currently I'm finding it a headache to try to accomplish this set-up with sprockets, while it's way easier to use the Grails Resources plugin DSL to get the same effect (take a look at defaultBundle):

http://grails-plugins.github.io/grails-resources/guide/3.%20Declaring%20resources.html#3.1%20The%20resource%20DSL

Right now, just having something like a stub_tree preprocessor would already help a bit, but I'd love to have support for proper bundling from Sprockets. Are there any plans for that in the roadmap?

@rosenfeld

This comment has been minimized.

Copy link

rosenfeld commented Aug 26, 2013

Hummm... it seems stub is already recursive, which is great. I think I can get my set-up working now much easier :)

@jzwolak

This comment has been minimized.

Copy link

jzwolak commented Oct 9, 2013

Back to derekprior's original issue. Stubbing "active_admin" also stubs jquery. I'm having this same problem and cannot figure a good way to stub "active_admin" without stubing jquery. (Note I'm trying to stub "mercury.js" from the Mercury Editor, so I'll call it "mercury" for the rest of this message) "mercury" is loaded on some pages explicitly and application.js is not loaded on those pages because they have a different layout and entirely different functionality. So I need jquery to load with mercury and separately with application. I want to keep require_tree, and exclude mercury from application.js.

Can this be done?

It seems it cannot and stub would make more sense to me if it just excluded what was named, "mercury". And not the requires found in mercury.

It's fine that once a file is stubbed it cannot be included and previous requires/includes are not performed. What's problematic for me is that stub also black lists the requires and includes of the file that was stubbed. Can't it just stub mercury since I wrote "stub mercury.js" instead of also stubbing jquery? In derekprior's case, can't stub just black list "active_admin" since he typed "stub active_admin" instead of also stubbing jquery?

@rosenfeld

This comment has been minimized.

Copy link

rosenfeld commented Oct 9, 2013

I need exactly the current behavior. So maybe you should request a new directive instead of modifying the current one.

@derekprior

This comment has been minimized.

Copy link
Author

derekprior commented Oct 10, 2013

It makes sense that stubbing a file would make sprockets unaware of the original file's dependencies. The contents of the file have been stubbed - it has no requires.

The problem for me comes when I explicitly require something and that required is ignored because a previous stub also happened to require it as a dependency. Stubbing should ignore the listed dependencies in that context only. If the dependencies are required elsewhere then it should work because the dependencies themselves were not explicitly stubbed.

@rosenfeld

This comment has been minimized.

Copy link

rosenfeld commented Oct 10, 2013

If stub's behavior is changed please add some stub_recursive directive with the current behavior as it's quite useful for my application.

@jzwolak

This comment has been minimized.

Copy link

jzwolak commented Oct 11, 2013

I agree with derekprior.

rosenfeld makes a point that it's also useful to recursively stub (black list) all dependencies of the stubbed file. In this case there are two solutions I can imagine:

  1. add a 'stub_recursive' directive with the behavior of the original stub. Or keep the original stub directive as is and add a 'stub_flat' or some other name that implies it will just do the one file (and prevent it's dependencies from loading when it is required, but not prevent the dependencies from loading if another file requires them or if they are explicitly required)

  2. just change the behavior of stub to not recursively blacklist and users that have whole dependency trees that need black listing can put those in a directory and stub the directory. Stubbing a directory would work like 'require'. Optionally, a directive 'stub_tree' could be added to black list recursively the files in the directory (note this is different than recursively black listing the requires and includes of a stubbed file.)

Sounds like there's room to grow with this stub command. I think having it mirror require as in point 2 would be best.

@rosenfeld

This comment has been minimized.

Copy link

rosenfeld commented Oct 11, 2013

Option 2 wouldn't work for me, although 1 is fine.

@rmosolgo

This comment has been minimized.

Copy link

rmosolgo commented Feb 11, 2014

👍 would use stub_tree

@AvaelKross

This comment has been minimized.

Copy link

AvaelKross commented Sep 22, 2017

i can't understand if it's solved somehow or not; stub_flat, stub_recursive, etc. aren't implemented yet

only solution is to remove require_tree and include files manually one by one (so stubbing isn't needed)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
You can’t perform that action at this time.