-
Notifications
You must be signed in to change notification settings - Fork 20
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
Method for nodes to register dependencies programmatically #26
Comments
I would like this. Of those options, I would lean toward the last one. What about something added to the context of the transform? var html = gobble( 'markdown', { files: [ 'template.html' ] } )
.transform( function ( markdown ) {
return this.files[ 'template.html' ].replace( '__CONTENT__', marked( markdown ) );
});
// 'files' may be better as 'dependencies' or some other appropriate term There's probably some extra benefit to number 4 above though, as the |
There is currently no operation that does this in Plumber, but it should indeed be possible, since all operations are chunks of a pipeline through which a new execution flows when files change. I've been wanting to do that for For your example, it would probably look something like: var template = glob('template.html');
pipelines[''] = [
glob('markdown/**'),
markdown(template),
write('out');
]; (Or else Either way, I guess the point is that it's nice to be able to rely on the standard API for reading out a file (asynchronously) and being notified when it changes. Option 4 above seems the closest as you're just Once you have both the template and the MD files as top-level objects (node, streams, whatever) that emit files initially and on change, you need a way to combine ("merge") them back together into a single node. I'm not familiar enough with Gobble, but it seems like you already have that ability (conceptually at least), albeit just writing all the input node contents into the same output dir. I'm probably being stupid, but would something like this work technically? var template = gobble( 'template.html' );
var files = gobble( 'markdown' );
var html = gobble([template, files]).transform( function ( markdownOrTemplate ) {
if (markdownOrTemplate.filename !== 'template.html') {
var template = fs.readFileSync( 'template.html', 'utf-8' );
return template.replace( '__CONTENT__', marked( markdown ) );
}
}); Now this is of course terrible and inefficient code, but I'm just trying to grasp the basic building blocks :-) If this is not completely misled, it would seem that the issue is that the only way to merge nodes doesn't allow differentiating which node the files came from. Put otherwise, a transform can only really take a single node as input (source, merger or another transform node), but really here we want two. So |
+1 on this. My current usecase is slightly different, though: I have a bunch of independent Sass files (one per "widget" in a library of widgets) which all import a common library. While a directory transformer would handle this, I'd prefer to use a file transformer to avoid recompiling all of the widget files when just one changes, and would then need a way for them to register the library as a dependency (as well as a way to feed the library to Sass, for which it would help if I could register an entire directory as a dependency). |
Yeah, me too, I keep circling back round to this. Occasionally I'll find a spare 45 minutes and start hacking around on it, before realising it's going to take a lot more than a spare 45 minutes... |
You can already return an object with a |
I realise that var html = gobble( 'markdown' ).transform( function ( markdown ) {
var template = fs.readFileSync( 'template.html', 'utf-8' );
return {
code: template.contents().replace( '__CONTENT__', marked( markdown ) ),
deps: ['template.html']
}
}); |
Hmm, I do wonder about @Rich-Harris's comments on caching, though - any time we're doing the reading ourselves, we can't easily cache the file. On the other hand, if we're trying to support external tools that are based around reading from disk instead of accepting arguments as strings (e.g., Webpack), we'd probably want Gobble to just register the dep without reading from the filesystem at all. One fairly crazy approach would be to handle caching down at the filesystem level... something like what https://www.npmjs.com/package/cachedfs does with its |
(Actually, that's arguably not even a module's job, but rather the OS's job... which makes me wonder, how much does manual caching actually matter if the OS is caching in memory anyway? Is there really a significant overhead from the system calls that still makes in-JavaScript caching favorable compared to just calling |
I like Rich's general approach of Gobble optimising for developer sanity. A file transform has a list of watched files which triggers its build and it's up to the plugin how it performs the transform. |
Yeah, I definitely also like the approach... my ramblings were more about This leads to another, hopefully more relevant thought: what if we want to I like Rich's general approach of Gobble optimising for developer sanity. A — |
Gobble build definitions are defined as a sequence of transformations form source to result, rather than (as in JavaScript bundlers like webpack/browserify etc) by taking some final entry point and recursively discovering its dependencies. Generally speaking that's a more appropriate model for the (more general purpose) job Gobble is trying to do.
There are some occasions when it would be useful for a transformer to be able to declare its dependencies (that are outside its
inputdir
) programmatically. For example, if you have a bunch of markdown documents that you want to turn into HTML pages, you probably have an HTML template to wrap around the content:In this example the
html
node would contain the expected result, but if you changed thetemplate.html
file, Gobble wouldn't know that it needed to re-run the transformation.A few quick API ideas:
(A synchronous API is preferable I think, since it means it can be used with both directory and file transformers.)
None of them leap out as being The Answer - would be interested if anyone out there has any feedback. Figuring this out would mean never again having to use static site generators (which in my experience compensate for their lack of intrinsic flexibility with overwhelmingly complex configuration).
/cc @OliverJAsh and @theefer, since this is related to the stuff we were talking about a while back. Have you encountered this problem with Plumber?
The text was updated successfully, but these errors were encountered: