-
Notifications
You must be signed in to change notification settings - Fork 479
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
Refactor template compilation #613
Conversation
1e8f2fc
to
c5b587f
Compare
This will force a 2.7. |
Add "Compiling and Rendering" doc, and add docs for changes from #613
Ugg I actually really really don't want to leave Isn't that the purpose of cutting a 2.7? I kind of want to push back on this one. @jimmyhchan |
Updated to leave all the It's also under |
Meh https://github.com/search?p=4&q=dust.compileFn&ref=simplesearch&type=Code&utf8=%E2%9C%93 Let me see if I can write compat behavior that makes it part of the compiler. |
* Remove dust.load from public API and dust.compileFn completely. * When a template is compiled, dust.compile returns a reference to the compiled template that can be passed to dust.render / dust.stream directly * Compiled template references have a `templateName` property if they were compiled with a name
Set to false to disable caching. Templates will always be loaded again using `dust.onLoad`.
OK I'm happy again. It's part of the compiler now, and it's simpler too thanks to the changes. It should still be removed eventually. |
Compiling a template as a CommonJS module returns a function that must be hydrated with a Dust: var tmpl = require('./my/tmpl.js')(dust); Once you have a handle on that object, you can either invoke it directly: tmpl(context, function(err, out) { ... }) tmpl(context).on('data', ...) Or you can pass it to `dust.render`: dust.render(tmpl, ...) Or you can access the compiled template if you need to do something with it: tmpl.template; // function body_0(chunk, context, bodies, params) { ... }
Added CommonJS tests. |
@rragan would you mind taking a look to see if this supports PayPal's use case? I made many of these changes to specifically try to address how you guys use Dust. You can compile a CommonJS module:
Now, you can require one of those modules, and hydrate it with a dust (this lets you add in the helpers you need first etc)
|
Refactor template compilation * `dust.render` and `dust.stream` now accept a compiled template function in addition to a template name. * `dust.compile` no longer requires a template name, and will compile an anonymous template without one (so `--name` is no longer required for dustc either) * `dust.load` is removed from the public API * `dust.renderSource` is moved to the compiler, so it's only included in dust-full now (Fixes #412) * `dust.compileFn` is moved to the compiler, so it's only included in dust-full now * add `dust.isTemplateFn` * add `dust.config.cache = true`, set to `false` to disable caching and load templates again every time (Fixes #451) * add `dust.config.cjs = false`, set to `true` to compile templates as CommonJS modules * add `--cjs` flag to `dustc` * Move a bunch of exposed compiler stuff under `dust.compiler` (but leave it exposed until 2.8)
Yes, we'll need to work with them to update their code. |
OK, there are quite a few changes in here.
This is the first step towards treating compiled templates as first-class citizens in Dust. We want to be able to pass around compiled templates and render them without having to worry about registering them under a specific name.
dust.render
anddust.stream
now accept a compiled template function in addition to a template name.dust.compile
no longer requires a template name, and will compile an anonymous template without one (so--name
is no longer required for dustc either)dust.load
is removed from the public APIdust.compileFn
is removed completelydust.renderSource
is moved to the compiler, so it's only included in dust-full now (Fixes Remove reliance of functions in dust.js on dust.compile #412)dust.isTemplateFn
dust.config.cache = true
, set tofalse
to disable caching and load templates again every time (Fixes caching is unavoidable #451)dust.config.cjs = false
, set totrue
to compile templates as CommonJS modules for use like:--cjs
flag todustc
dust.compiler
(but leave it exposed until 2.8)Fixes #349 indirectly.
Closes #330 indirectly. require.extensions is deprecated and the Node team recommends it not be used. The supported use case is going to be requiring compiled templates if you want, and hydrating them with a reference to Dust. This also lets you use multiple Dusts if you want, which is cool. Dust is not going to concern itself with how you load a template-- that's what
dust.onLoad
is for.