gruntjs / gruntjs/grunt

`cwd` option should also be available when `expand` is undefined (or false)

Open
#1,307 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
12.2k
Forks
1.5k
PR merge metrics
No merged PRs in 30d

Description

Using the `cwd` option when defining a fileset has no effect if the `expand` option has not been enabled. Under some circumstances, it would be very handful to _prefix_ the current fileset with the defined `cwd` option value.

Here is a common usecase (uglify & concatenate):

```
uglify: {
app: {
files: [{
cwd: '<%= pkg.target %>',
src: '<%= pkg.resources.js %>', // JS resources are dynamic and ordered!
dest: '<%= pkg.target %>js/<%= pkg.name %>.min.js'
}]
}
}
```

When adding the `expand` option, Grunt creates a file mapping for the `uglify` task, which outputs multiple minified resources.

It took me a while to find a workaround for that:

```
uglify: {
app: {
files: [{
src: '{<%= _prefixSrc(pkg.target, pkg.resources.js) %>}', // Note the brackets!
dest: '<%= pkg.target %>min/<%= pkg.name %>.min.js'
}]
}
},
_prefixSrc: function(prefix, files) {
return files.map(function(file){
return prefix + file;
});
},
```

Seems that the current usage is misleading and this feature could be useful to others as well, cf.:
#917

https://github.com/gruntjs/grunt-contrib-copy/pull/169
http://stackoverflow.com/questions/28847471/grunt-how-to-build-the-files-object-dynamically
http://stackoverflow.com/questions/15284556/how-can-i-run-a-grunt-task-from-within-a-grunt-task
http://stackoverflow.com/questions/15927368/gruntjs-use-files-with-dynamic-mapping-in-multitask-config-once-for-all-targets
http://stackoverflow.com/questions/22098528/lazily-evaluate-grunt-task-options

Contributor guide

Open the contributing guide

Research direction

No source file or test is named in the issue. Start by tracing Grunt's fileset handling and comparing the existing `expand` and `cwd` paths; done means `cwd` prefixes sources when `expand` is absent or false while existing expanded mappings continue to work.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
build-system, tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.