Skip to content

Commit

Permalink
Add option to specify file name suffix (#31)
Browse files Browse the repository at this point in the history
* Add option to specify cusotm part file name

* Rename 'partLabel' to 'suffix'. Also allow suffix to be a function.

* Depend on individual lodash function instead of the whole lodash library
  • Loading branch information
alvinlin123 authored and adam-lynch committed Oct 9, 2016
1 parent 0a9910b commit d074d8d
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 1 deletion.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,30 @@ gulp.task('watch', function () {
- `imports` - A boolean (which defaults to `true`). Determines whether or not the first chunk / "blessed" file `@import`s the others.
- `cacheBuster` - A boolean (which defaults to `true`). If `imports` is `true`, this will add a random query parameter value to prevent against aggressive caching.
- `log` - A boolean (which defaults to `false`). Logs a small bit of information about the process.
- `suffix` - Either a string or a function (which defaults to `"-blessed"`).
- If it is a string then it will be appended to the original file name before the index. E.g.
```javascript
//Assume you have long.css that is to be splitted into 3 parts, following code will
//produce 3 files: long.css, long-part1.css, long-part2.css
gulp.src('long.css')
.pipe(bless{
suffix: '-part'
})
.pipe(gulp.dest('./'));

```
- If it is a function then whatever returned by the function is appended to the original file name. The function takes in a 1-based index E.g.
```javascript
//Assume you have long.css that is to be splitted into 3 parts, following code will
//produce 3 files: long.css, long-functionpart1.css, long-functionpart2.css
gulp.src('long.css')
.pipe(bless{
suffix: function(index) {
return "-functionpart" + index;
}
})
.pipe(gulp.dest('./'));
```

Example:

Expand Down
21 changes: 20 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
'use strict';

var isString = require("lodash.isstring");
var isFunction = require("lodash.isfunction");
var isUndefined = require("lodash.isundefined");
var through = require('through2');
var path = require('path');
var bless = require('bless');
Expand All @@ -9,12 +12,28 @@ var applySourcemap = require('vinyl-sourcemaps-apply');

var File = gutil.File;
var PluginError = gutil.PluginError;
var createSuffixFunctionFromString = function(configValue) {
var actualSuffix = configValue === undefined? "-blessed" : configValue;
return function(index) {
return actualSuffix + index;
}
}
var createSuffixFunction = function(configValue) {
if(isString(configValue) || isUndefined(configValue)) {
return createSuffixFunctionFromString(configValue);
} else if(isFunction(configValue)) {
return configValue;
} else {
throw new TypeError("suffix is neither a string nor function");
}
}

module.exports = function(options){
var pluginName = 'gulp-bless';
options = options || {};
options.imports = options.imports === undefined ? true : options.imports;
options.cacheBuster = options.cacheBuster === undefined ? true : options.cacheBuster;
options.suffix = createSuffixFunction(options.suffix);

return through.obj(function(file, enc, cb) {
if (file.isNull()) return cb(null, file); // ignore
Expand Down Expand Up @@ -81,7 +100,7 @@ module.exports = function(options){
var outputBasename = path.basename(outputFilePath, outputExtension);

var createBlessedFileName = function(index){
return outputBasename + '-blessed' + index + outputExtension;
return outputBasename + options.suffix(index) + outputExtension;
};

var addImports = function(index, contents){
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
"dependencies": {
"bless": "^4.0.0",
"gulp-util": "*",
"lodash.isfunction": "^3.0.8",
"lodash.isstring": "^4.0.1",
"lodash.isundefined": "^3.0.1",
"merge": "~1.2.0",
"through2": "~0.5.1",
"vinyl-sourcemaps-apply": "^0.2.1"
Expand Down
105 changes: 105 additions & 0 deletions test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,112 @@ describe('gulp-bless', function() {
});
});

it('should use custom suffix if suffix option is set to a string', function(done){
var suffix = "-part"
var stream = bless({
suffix: suffix
});

fs.readFile('./test/css/long.css', function(err, data){
if(err) throw new Error(err);

var longStylesheet = new File({
cwd: "/home/adam/",
base: "/home/adam/test",
path: "/home/adam/test/long-split.css",
contents: new Buffer(data)
});

var actualSplits = []
stream.on("data", function(newFile){
actualSplits.push(newFile);
});

stream.on("end", function(){
actualSplits.should.have.length(2);
var masterPart, subPart;
actualSplits.forEach(function(splittedFile){
should.exist(splittedFile);
should.exist(splittedFile.path);
should.exist(splittedFile.relative);
should.exist(splittedFile.contents);
if (path.basename(splittedFile.path) === "long-split.css") {
masterPart = splittedFile;
} else {
subPart = splittedFile;
}
});
should.exist(masterPart);
should.exist(subPart);
path.basename(subPart.path).should.equal("long-split" + suffix + "1.css");
var importRegex = "@import url\\('long-split" + suffix + "1.css\\?z=[0-9]+'\\);";
masterPart.contents.toString("utf8").should.match(new RegExp(importRegex));
done();
});

stream.write(longStylesheet)
stream.emit("end")
});
});

it('should use custom suffix if suffix option is set to a function', function(done){
var suffix = "-functionpart";
var stream = bless({
suffix: function(index) {
return suffix + "-" + index;
}
});

fs.readFile('./test/css/long.css', function(err, data){
if(err) throw new Error(err);

var longStylesheet = new File({
cwd: "/home/adam/",
base: "/home/adam/test",
path: "/home/adam/test/long-split.css",
contents: new Buffer(data)
});

var actualSplits = []
stream.on("data", function(newFile){
actualSplits.push(newFile);
});

stream.on("end", function(){
actualSplits.should.have.length(2);
var masterPart, subPart;
actualSplits.forEach(function(splittedFile){
should.exist(splittedFile);
should.exist(splittedFile.path);
should.exist(splittedFile.relative);
should.exist(splittedFile.contents);
if (path.basename(splittedFile.path) === "long-split.css") {
masterPart = splittedFile;
} else {
subPart = splittedFile;
}
});
should.exist(masterPart);
should.exist(subPart);
path.basename(subPart.path).should.equal("long-split" + suffix + "-1.css");
var importRegex = "@import url\\('long-split" + suffix + "-1.css\\?z=[0-9]+'\\);";
masterPart.contents.toString("utf8").should.match(new RegExp(importRegex));
done();
});

stream.write(longStylesheet)
stream.emit("end")
});
});

it('should throw error if suffix option is neither string nor function', function(done){

(function callBlessWithInvalidSuffix() {
gulp.src('./test/css/long.css')
.pipe(bless({suffix:{}}))
}).should.throw(/.*suffix.*string.*function.*/);
done();
});

it('should apply sourcemaps', function(done){
var expectedSplits = [
Expand Down

0 comments on commit d074d8d

Please sign in to comment.