Skip to content
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

Use a middleware only once #61

Closed
aymericbeaumet opened this issue Jun 13, 2014 · 4 comments
Closed

Use a middleware only once #61

aymericbeaumet opened this issue Jun 13, 2014 · 4 comments

Comments

@aymericbeaumet
Copy link
Contributor

Hello,

In a classical workflow, every single plugin registered as a middleware is run once. But in a workflow using metalsmith-watch, each registered plugin is meant to be called several times.

The problem is: there is some metalsmith plugins (the ones not directly interacting with the files stream) which makes sense to only run once. Run them twice or more may be a waste of time. This could be the case with:

Therefore, I think it could be great to have the possibility to register a middleware which will only be called once. So the choice would be left to the developer judgment.

A code sample of how I see it:

var metalsmith = require('metalsmith');
var markdown = require('metalsmith-markdown');
var metadata = require('metalsmith-metadata');
var watch = require('metalsmith-watch');

metalsmith(__dirname)
  .once(metadata({ ... }));
  .use(markdown());
  .watch();
  .build();

What do you think?

@ianstormtaylor
Copy link
Contributor

Sorry, but I don't think this is a good idea.

The metalsmith-watch plugin makes assumptions about the builds that shouldn't be made, so it is broken itself, and doesn't mean that other plugins should be edited or core edited because it makes those assumptions. Since Metalsmith is all about transforms, you can't assume that passing different combinations of files through the build process (depending on which ones changed in this case) will result in the full build being correct. That's a core truth to using Metalsmith as far as I can tell, and one which makes it powerful.

You'd have the same problem not only with middleware that should act once, but with any middleware that uses context from other files to produce it's output.

@aymericbeaumet
Copy link
Contributor Author

That's a good abstraction of the problem. But I wonder: if this plugin is broken, why is it present on the official website? It encourages bad practices on Metalsmith development.

Considering your answer, the only feasible solution would be to trigger the whole build again each time a file changes, there is three opinions on this:

  • this is not the Metalsmith job to watch for file changes. It should be handled by an external tool.
  • this can be natively handled by Metalsmith.
  • this can be handled by a Metalsmith plugin.

For the last 2 suggestions, a cache system could be implemented in order to avoid excessive read-access, and only refresh the cache entries concerned by the modifications.

I would really appreciate a functionality like this to faster my development with Metalsmith. I'm ok to do some development but I would like your way of thinking to work in the good direction.

@FWeinb
Copy link

FWeinb commented Jun 13, 2014

Author of metalsmith-watch here. I remember that we had this discussion before (about including a watcher in core) but that was out of scope that time.

It is true that metalsmith-watch makes assumptions that will break with certain plugins but this is due to the fact that it is not feasible to rebuild the whole project for every file change.

Furthermore metalsmith is not exposing a method to re trigger the build, so I had to hack around this to make metalsmith-watch work in the first place.

I would be really glad if we could find a way to make this work.

@aymericbeaumet
Copy link
Contributor Author

Which constraints restrain you from rebuilding the whole project on each modification? Is it for performance reasons?

Doing so would ensure the build integrity.

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

No branches or pull requests

3 participants