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

Should file.expandMapping support "src" tweaking? #680

Closed
cowboy opened this issue Feb 19, 2013 · 10 comments
Closed

Should file.expandMapping support "src" tweaking? #680

cowboy opened this issue Feb 19, 2013 · 10 comments
Labels
Milestone

Comments

@cowboy
Copy link
Member

cowboy commented Feb 19, 2013

So the rename method allows us to programmatically modify the dest property as src-dest file mappings (via expand) are created. Is there any value in being able to programmatically modify the src property?

I'll post an example where it could be useful: gruntjs/grunt-contrib-concat#9

In order to do what the OP wants, which is to "wrap" every src/main-file-*.js with some before/after content and concat all that together with an additional file prepended to the beginning, we can specify files like so:

files: {
  'build/output.js': [
    'src/wrapper/preamble.js',
    'src/wrapper/before.js',
    'src/main-file-1.js',
    'src/wrapper/after.js',
    'src/wrapper/before.js',
    'src/main-file-2.js',
    'src/wrapper/after.js',
    'src/wrapper/before.js',
    'src/main-file-3.js',
    'src/wrapper/after.js',
  ]
}

Or in an (arguably) more readable but (arguably) harder to maintain format, we can use multiple file objects and tempfiles:

files: [
  {dest: 'temp/main-file-1.js', src: ['src/wrapper/before.js', 'src/main-file-1.js', 'src/wrapper/after.js']},
  {dest: 'temp/main-file-2.js', src: ['src/wrapper/before.js', 'src/main-file-2.js', 'src/wrapper/after.js']},
  {dest: 'temp/main-file-3.js', src: ['src/wrapper/before.js', 'src/main-file-3.js', 'src/wrapper/after.js']},
  {dest: 'build/output.js', src: ['src/wrapper/preamble.js', 'temp/main-file-*.js']},
]

We can currently specify this:

files: [
  {
    expand: true,
    cwd: 'src',
    src: 'main-file-*.js',
    dest: 'temp',
  },
  {dest: 'build/output.js', src: ['src/wrapper/preamble.js', 'temp/main-file-*.js']},
]

But it only gets us halfway there, because the src properties are single files:

files: [
  {dest: 'temp/main-file-1.js', src: ['src/main-file-1.js']},
  {dest: 'temp/main-file-2.js', src: ['src/main-file-2.js']},
  {dest: 'temp/main-file-3.js', src: ['src/main-file-3.js']},
  {dest: 'build/output.js', src: ['src/wrapper/preamble.js', 'temp/main-file-*.js']},
]

If we could somehow modify the src property however, it could allow code like this:

files: [
  {
    expand: true,
    cwd: 'src',
    src: 'main-file-*.js',
    dest: 'temp',
    modifySrc: function(src) {
      return ['src/wrapper/before.js', src, 'src/wrapper/after.js'];
    }
  },
  {dest: 'build/output.js', src: ['src/wrapper/preamble.js', 'temp/main-file-*.js']},
]

To generate the object we want, dynamically:

files: [
  {dest: 'temp/main-file-1.js', src: ['src/wrapper/before.js', 'src/main-file-1.js', 'src/wrapper/after.js']},
  {dest: 'temp/main-file-2.js', src: ['src/wrapper/before.js', 'src/main-file-2.js', 'src/wrapper/after.js']},
  {dest: 'temp/main-file-3.js', src: ['src/wrapper/before.js', 'src/main-file-3.js', 'src/wrapper/after.js']},
  {dest: 'build/output.js', src: ['src/wrapper/preamble.js', 'temp/main-file-*.js']},
]

Thoughts?

@tkellen
Copy link
Member

tkellen commented Feb 19, 2013

Yes! I was just thinking I needed this for that static blog generator hack I made for tkellen.com.

@cowboy
Copy link
Member Author

cowboy commented Feb 19, 2013

How would you use it?

@tkellen
Copy link
Member

tkellen commented Feb 19, 2013

Haha, now I'm drawing a blank. I'll revisit this more later. I definitely think this would make sense in the glob expansion lib, though. I can't wait to start working on that with you. Back to work here.

@dignifiedquire
Copy link

I like it and I could imagine that this has more use cases, but except for the one I brought up I can't think of anything real.

@cesutherland
Copy link

It looks to me like this functionality is similar to the process option here: https://github.com/gruntjs/grunt-contrib-concat/#custom-process-function

The use-case for me is module wrapping, similar to brunch: https://github.com/brunch/brunch/blob/master/docs/config.md#modules

@shama
Copy link
Member

shama commented Jun 24, 2013

@cesutherland It's a bit different. process is for processing the actual content of the files. What is being discussed here is modifying the src array of file paths before being passed to the task.

@cowboy
How about just a helper module that could be called as needed:

{
  'build/output.js': buildSrc([
    'src/wrapper/preamble.js',
    [
      'src/wrapper/before.js',
      ['src/main-file-*.js'],
      'src/wrapper/after.js',
    ],
  ]),
}

Maybe as part of a utility for common config transforms instead?

@cesutherland
Copy link

It is at least sufficient for the task.

@cowboy
Copy link
Member Author

cowboy commented Sep 11, 2013

@jonschlinkert I tried a while back to figure out how to do this declaratively and couldn't figure out anything sane. If it's ok that the values be resolved at init-time (and not when the task runs) you could just do:

{
  assemble: {
    options: {
      // _.load() mixin using resolve-dep
      helpers: _.load("helper-*")
    }
  }
}

Or better yet:

{
  assemble: {
    options: {
      // load helpers using resolve-dep
      helpers: require('resolve-dep').load("helper-*")
    }
  }
}

Also, totally unrelated issue. 😀

@jonschlinkert
Copy link

okay... I moved the topic to 896. feel free to delete these comments if you want to keep this clean.

@vladikoff vladikoff modified the milestones: 0.4.5, 0.4.4 Mar 12, 2014
@vladikoff vladikoff modified the milestones: 0.4.6, 0.4.5 Apr 8, 2014
@cowboy
Copy link
Member Author

cowboy commented Jun 19, 2014

I've actually done this in the "async" branch of node-globule but haven't merged it into master yet. The next major release of Grunt will have this updated behavior.

See examples here: https://github.com/cowboy/node-globule/blob/10fe13c755c34087c3028b615ec7ab7b70ed6d5d/test/mapping_test.js#L174-L217

@cowboy cowboy closed this as completed Jun 19, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

7 participants