-
Notifications
You must be signed in to change notification settings - Fork 641
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Provide a way to discover a template's dependencies #1153
Provide a way to discover a template's dependencies #1153
Comments
Related: #1129 |
steal the example from mozilla/nunjucks#1153
steal the example from mozilla/nunjucks#1153
So, for now, we can use |
@andreyvolokitin Yes, but the implementation is hacky as it's tightly coupled to the internals of |
@chocolateboy I use only |
@andreyvolokitin I'm not sure what you're asking, but if you have a question about nunjucks-parser feel free to raise an issue. |
I was wondering if it possible/viable to solve this issue (getting template dependencies) by using the internals of a loader ( |
Not without changing |
I think hooking into {% include template_name %} In the simple case, where includes and extends are only strings, it is possible to parse the dependencies. But in the general case I think you would only be able to determine dependencies at runtime. |
Do you have a proposal for what sort of option would make it easiest to get at runtime template dependencies for parcel? I'd be partial to a callback or an event emitter, so that it would work for async templates as well as synchronous rendering. |
@fdintino Yes, I'd prefer the EventEmitter API. |
@chocolateboy does something like 35e3ad7 work for you? |
@fdintino I haven't had a chance to play with it, but yes, thanks, that looks useful 👍 |
nunjucks doesn't provide a way to discover a template's direct or transitive dependencies i.e. the child and grandchild templates that are loaded (via
extends
,import
orinclude
) when a parent template is rendered. This is needed to integrate (efficiently) with bundlers such as Parcel, which track changes in dependencies and use them to trigger rebuilds.(Note: this is orthogonal to nunjucks' caching support via chokidar, though the implementations may overlap.)
Ideally, this would be scoped to a particular
Environment#render
call, but there doesn't appear to be any space in therender
method's parameters to squeeze in additional options, or to return an additional value. Maybe a sister method with a more flexible interface?This makes dependency tracking optional, to err on the side of efficiency, but they could be included by default as it's a new API, and the overhead is unlikely to be high e.g.:
Another way to do it is via an EventEmitter API (which is already used, lightly, in loaders) e.g.:
This has several advantages:
new Environment(loaders, { trackDependencies: true })
The main disadvantage is that it doesn't directly answer the question "What are this template's dependencies?" i.e. it requires extra work to scope the results to a particular template/render. (This also implies the use of a full EventEmitter implementation rather than the cut-down version that's currently used for loaders, and suggests the addition of some new events e.g.
render:start
andrender:end
.)I'm envisaging each dependency as an object with the following core fields:
Optional fields could include
cached
(a boolean indicating that the template was retrieved from its loader's cache) and maybeasync
(the template was retrieved by an async loader).Example
layout.html
header.html
footer.html
copyright.html
render
result
The text was updated successfully, but these errors were encountered: