Skip to content
This repository has been archived by the owner on Apr 20, 2018. It is now read-only.

Commit

Permalink
fixed configwriter to process post config (incl test and docu)
Browse files Browse the repository at this point in the history
  • Loading branch information
ataube committed Jan 12, 2014
1 parent 158dc95 commit 14afbc5
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 12 deletions.
28 changes: 27 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,37 @@ For example:
options: {
flow: {
steps: {'js' : ['uglifyjs'] },
post: []
post: {}
}
}
}
```

* to customize the generated configuraion via post-processors:

```js
'useminPrepare', {
html: 'index.html',
options: {
flow: {
steps: {'js' : ['uglifyjs'] },
post: {
'js': [{
name: 'uglifyjs',
createConfig: function(context, block) {
var generated = context.options.generated;
generated.options = {
foo: 'bar'
};
}
}]
}
}
}
}
}
```

The given steps or post-processors may be specified as strings (for the default steps and post-processors), or as an object (for the user-defined ones).

#### User-defined steps and post-processors
Expand Down
15 changes: 8 additions & 7 deletions lib/configwriter.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,13 +176,14 @@ ConfigWriter.prototype.process = function(file, config) {
}
context.inFiles = block.src;

self.postprocessors.forEach(function(pp) {
config[pp.name] = config[pp.name] || {};
context.options = config[pp.name];
config[pp.name] = _.extend(config[pp.name], pp.createConfig(context, block));
context.options = null;

});
if(self.postprocessors.hasOwnProperty(block.type)) {
self.postprocessors[block.type].forEach(function(pp) {
config[pp.name] = config[pp.name] || {};
context.options = config[pp.name];
config[pp.name] = _.extend(config[pp.name], pp.createConfig(context, block));
context.options = null;
});
}
});

return config;
Expand Down
47 changes: 43 additions & 4 deletions test/test-usemin.js
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ describe('useminPrepare', function () {
options: {
flow: {
steps: {'js': ['uglifyjs']},
post: []
post: {}
}
}
});
Expand Down Expand Up @@ -469,7 +469,7 @@ describe('useminPrepare', function () {
flow: {
'html': {
steps: {'js': ['uglifyjs']},
post: []
post: {}
}
}
}
Expand Down Expand Up @@ -511,7 +511,7 @@ describe('useminPrepare', function () {
options: {
flow: {
steps: {'js': [copy]},
post: []
post: {}
}
}
});
Expand All @@ -524,5 +524,44 @@ describe('useminPrepare', function () {
assert.ok(copyCfg);
assert.equal(copyCfg.generated.files[0].dest, path.normalize('dist/scripts/plugins.js'));
});
});

it('should allow to post configure generated steps', function() {

var concatPostConfig = {
name: 'concat',
createConfig: function(context) {
var generated = context.options.generated;
generated.options = {
foo: 'bar'
};
}
};

grunt.log.muted = true;
grunt.config.init();
grunt.config('useminPrepare', {
html: 'index.html',
options: {
flow: {
steps: {'js': ['concat']},
post: {
'js': [
concatPostConfig
]
}
}
}
});

grunt.file.copy(path.join(__dirname, 'fixtures/usemin.html'), 'index.html');
grunt.task.run('useminPrepare');
grunt.task.start();

var concatCfg = grunt.config('concat');
var options = concatCfg.generated.options;

assert.ok(options);
assert.equal(options.foo, 'bar');

});
});

0 comments on commit 14afbc5

Please sign in to comment.