gruntjs / grunt

Grunt: The JavaScript Task Runner
http://gruntjs.com/
Other
12.27k stars 1.5k forks source link

Use lodash mixins to supply src file paths #896

Open jonschlinkert opened 11 years ago

jonschlinkert commented 11 years ago

(this was moved over from https://github.com/gruntjs/grunt/issues/680#issuecomment-24267002, since it was off-topic)

I created this project, resolve-dep to make it easy to add the resolved paths to node modules (from dependencies) to the src array of file paths. For example, in assemble you could npm install a bunch of handlebars helpers, then add the paths to the helpers to the assemble options, like this:

{
  assemble: {
    options: {
      // _.load() mixin using resolve-dep
      helpers: ['<%= _.load("helper-*") %>']
    }
  }
}

The expanded file paths to the helpers would look something like this:

["node_modules/helper-foo/lib/helper-foo.js", "node_modules/helper-bar/lib/helper-bar.js"]

Currently, I believe Grunt calls toString on the array of file paths returned by the mixin, so the example I gave above doesn't actually work (it works if you use the mixin to specify one file at a time).

I see use cases for this pretty often, any suggestions on how this can be solved?

jonschlinkert commented 11 years ago

@cowboy's comment: https://github.com/gruntjs/grunt/issues/680#issuecomment-24267002

jonschlinkert commented 11 years ago

@cowboy yeah we were thinking the same thing, but then you're limited to the single pattern (or the code gets a little messy), and adding npm and "local" helpers is more challenging. e.g., if this was possible with templates you could do:

{
  assemble: {
    options: {
      // _.load() mixin using resolve-dep
      helpers: helpers: ['<%= _.load("helper-*") %>', 'local-helper-*.js', '!exclude-something']
    }
  }
}

couldn't figure out anything sane.

Same here, we've been trying to come up with something as well. As it relates to this use case at least, it seems that the challenge boils down to how templates should be handled when they follow patterns like:

<%= _.foo("foo-*") %> + <%= _.foo("bar-*") %>

For all intents and purposes, Grunt just sees this as a property (which makes sense) and returns the value of the property instead of passing it to Lo-Dash.

Is there a src file pattern that we could use, or maybe even a different template delimiter that we could implement to tell Grunt how to handle the templates (like [%= _.load("*") %]) ?

IMHO, using mixins like this could solve a boatload of issues and open up all kinds of opportunities for extending Grunt and Grunt plugins.