Skip to content
This repository has been archived by the owner on Dec 1, 2019. It is now read-only.

add option.exclude #9

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 37 additions & 2 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,41 @@ module.exports = function(grunt) {
src: ['test/fixtures/Project_A/*.js', 'test/fixtures/Project_B/*.js'],
dest: 'tmp/multipackage_partial_simple.js'
},
multipackage_partial_simple_with_only_as_root: {
options: {
name: {
ProjectB: 'test/fixtures/Project_B',
ProjectA: 'test/fixtures/Project_A'
}
},
only: ['ProjectA/ProjectA.Secondary'],
src: ['test/fixtures/Project_A/*.js', 'test/fixtures/Project_B/*.js'],
dest: 'tmp/multipackage_partial_simple_with_only_as_root.js'
},
multipackage_partial_exclude: {
options: {
only: 'ProjectB/*',
exclude: ['ProjectA/*'],
name: {
ProjectB: 'test/fixtures/Project_B',
ProjectA: 'test/fixtures/Project_A'
}
},
src: ['test/fixtures/Project_A/*.js', 'test/fixtures/Project_B/*.js'],
dest: 'tmp/multipackage_partial_exclude.js'
},
multipackage_partial_exclude_with_exclude_as_root: {
options: {
only: 'ProjectB/*',
exclude: ['ProjectA/*'],
name: {
ProjectB: 'test/fixtures/Project_B',
ProjectA: 'test/fixtures/Project_A'
}
},
src: ['test/fixtures/Project_A/*.js', 'test/fixtures/Project_B/*.js'],
dest: 'tmp/multipackage_partial_exclude_with_exclude_as_root.js'
},
multipackage_partial_widcard: {
options: {
only: 'ProjectB/*',
Expand All @@ -70,7 +105,7 @@ module.exports = function(grunt) {
callback: {
options: {
name: 'Test',
callback: function(data){ grunt.file.write('tmp/callbackOutput.js', data);}
callback: function(data){ grunt.file.write('tmp/callbackOutput.js', data);}
},
files: {
'tmp/all.js': 'test/fixtures/*.js',
Expand All @@ -79,7 +114,7 @@ module.exports = function(grunt) {
noOutput: {
options: {
name: 'Test',
noOutput: true
noOutput: true
},
files: {
'tmp/noOutput.js': 'test/fixtures/*.js',
Expand Down
67 changes: 65 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ grunt.initConfig({
],
only: [
'More/Form.Validator'
]
],
dest: 'form.validator.js'
},
// all of the More package and its requirements
Expand All @@ -136,13 +136,55 @@ grunt.initConfig({
],
only: [
'More/*'
]
],
dest: 'more.js'
}
},
});
```

#### options.exclude
Type: `Array`

The specific dependencies packages to exclude from the compilation. This allows you to build a file and exclude some portion of it's dependencies. Example:

```js
grunt.initConfig({
packager: {
options: {
name: {
Core: 'js/mootools-core',
More: 'js/mootools-more'
}
},
// build only Core
all: {
src: [
'js/mootools-core/Source/**/*.js',
],
only: ['Core/*'],
dest: 'mootools-core.js'
},
// the Form.Validator component and its requirements from
// More, but none of the dependencies from Core
formValidator: {
src: [
'js/mootools-core/Source/**/*.js',
'js/mootools-more/Source/**/*.js'
],
only: [
'More/Form.Validator'
],
exclude: [
'Core/*'
],
dest: 'form.validator.js'
}
},
});
```


Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might include an example; the fact that it can take wild cards is non-obvious.

### Other Usage Examples

#### Default Options
Expand Down Expand Up @@ -173,3 +215,24 @@ grunt.initConfig({
},
});
```


#### Build-Specific Options
```js
grunt.initConfig({
packager: {
options: {
name: 'Core'
},
all: {
'dest/mootools.js': 'Source/**.js',
},
allWithoutCompat: {
strip: '.*compat',
src: 'Source/**.js',
dest: 'mootools-no-compat.js'
},
},
});
```

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "grunt-mootools-packager",
"description": "Grunt task for MooTools Packager projects.",
"version": "0.4.0",
"version": "0.5.0",
"homepage": "https://github.com/ibolmo/grunt-mootools-packager",
"author": {
"name": "Olmo Maldonado",
Expand Down
14 changes: 14 additions & 0 deletions tasks/packager.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,12 +159,26 @@ module.exports = function(grunt) {
}
// support global options.only as well as .only per build
var only = options.only || f.only;
// same deal with exclude option
var exclude = options.exclude || f.exclude;

grunt.log.verbose.writeln('compiling', f.dest, 'with dependencies:', only || 'all');

// load each component into the buffer list
(only ? toArray(only) : set).forEach(loadComponent);

// remove unwanted dependencies
if (exclude){
var toExclude = toArray(exclude);
buffer = buffer.filter(function(def){
var shouldKeep = toExclude.filter(function(dependency){
var match = dependency.match(/([^\*]+)/);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not necessary, but I feel like this matcher should be a method reused elsewhere.

return match ? def.key.indexOf(match[1]) != 0 : true;
});
return shouldKeep.length;
});
}

// convert the buffer into the actual source
buffer = buffer.map(function(def){ return def.source; }).join(options.separator);

Expand Down
17 changes: 17 additions & 0 deletions test/expected/multipackage_partial_exclude.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
---

name: B part of ProjectA

description: ProjectB, extending ProjectA

requires: [ProjectA/ProjectA]

provides: ProjectB

...
*/

var projectB = function(){
return [projectA.name].concat('ProjectB');
};
32 changes: 31 additions & 1 deletion test/packager_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var grunt = require('grunt');
exports.packager = {

// simple compat build, one project
default_options: function(test){
default_options: function(test){
test.expect(1);

var actual = grunt.file.read('tmp/all.js');
Expand Down Expand Up @@ -44,6 +44,16 @@ exports.packager = {

test.done();
},
// multi project build with simple option `only: 'package'`
multipackage_partial_simple_with_only_as_root: function(test){
test.expect(1);

var actual = grunt.file.read('tmp/multipackage_partial_simple_with_only_as_root.js');
var expected = grunt.file.read('test/expected/multipackage_partial_simple.js');
test.equal(actual, expected, 'should be exact for multipackage build with specified "only".');

test.done();
},
// multi project build with wildcard option `only: 'package/*'`
multipackage_partial_widcard: function(test){
test.expect(1);
Expand All @@ -54,6 +64,26 @@ exports.packager = {

test.done();
},
// multi project build with wildcard option `exclude: 'package/*'`
multipackage_partial_exclude: function(test){
test.expect(1);

var actual = grunt.file.read('tmp/multipackage_partial_exclude.js');
var expected = grunt.file.read('test/expected/multipackage_partial_exclude.js');
test.equal(actual, expected, 'should be exact for multipackage build without specified excluded components.');

test.done();
},
// multi project build with wildcard option `exclude: 'package/*'`
multipackage_partial_exclude_with_exclude_as_root: function(test){
test.expect(1);

var actual = grunt.file.read('tmp/multipackage_partial_exclude_with_exclude_as_root.js');
var expected = grunt.file.read('test/expected/multipackage_partial_exclude.js');
test.equal(actual, expected, 'should be exact for multipackage build without specified excluded components.');

test.done();
},
// option `callback`
callback: function(test){
test.expect(1);
Expand Down