gulp-include
Advanced tools
Comparing version 1.0.1 to 1.1.0
47
index.js
@@ -62,3 +62,6 @@ var fs = require('fs'), | ||
thisMatchText = "", | ||
files = globMatch(match, filePath); | ||
newMatchText = "", | ||
files = globMatch(match, filePath), | ||
fileName = "", | ||
whitespace = null; | ||
@@ -70,5 +73,22 @@ if (directiveType.indexOf("_tree") !== -1 || directiveType.indexOf("_directory") !== -1) { | ||
for (j = 0; j < files.length; j++) { | ||
var fileName = files[j]; | ||
thisMatchText += expand(String(fs.readFileSync(fileName)), fileName) + "\n"; | ||
if (directiveType.indexOf('require') !== -1 || directiveType.indexOf('include') !== -1) requiredFiles[fileName] = true; | ||
fileName = files[j]; | ||
newMatchText = expand(String(fs.readFileSync(fileName)), fileName); | ||
//Try to retain the same indent level from the original include line | ||
whitespace = original.match(/^\s+/); | ||
if (whitespace) { | ||
//Discard newlines | ||
whitespace = whitespace[0].replace("\n", ""); | ||
//Is there some whitespace left? | ||
if (whitespace) { | ||
newMatchText = addLeadingWhitespace(whitespace, newMatchText); | ||
} | ||
} | ||
thisMatchText += newMatchText + "\n"; | ||
if (directiveType.indexOf('require') !== -1 || directiveType.indexOf('include') !== -1) { | ||
requiredFiles[fileName] = true; | ||
} | ||
} | ||
@@ -108,3 +128,3 @@ | ||
} else { | ||
globs.push(relativeFilePath[i]) | ||
globs.push(relativeFilePath[i]); | ||
} | ||
@@ -164,8 +184,19 @@ } | ||
function addLeadingWhitespace(whitespace, string) { | ||
return string.split("\n").map(function(line) { | ||
return whitespace + line; | ||
}).join("\n"); | ||
} | ||
//We can't use lo-dash's union function because it wouldn't support this: ["*.js", "app.js"], which requires app.js to come last | ||
function union(arr1, arr2) { | ||
if (arr1.length == 0) return arr2; | ||
if (arr1.length == 0) { | ||
return arr2; | ||
} | ||
var index; | ||
for (var i = 0; i < arr2.length; i++) { | ||
if ((index = arr1.indexOf(arr2[i])) !== -1) arr1.splice(index, 1) | ||
if ((index = arr1.indexOf(arr2[i])) !== -1) { | ||
arr1.splice(index, 1); | ||
} | ||
} | ||
@@ -179,3 +210,3 @@ return arr1.concat(arr2); | ||
while ((index = arr1.indexOf(arr2[i])) !== -1) { | ||
arr1.splice(index, 1) | ||
arr1.splice(index, 1); | ||
} | ||
@@ -182,0 +213,0 @@ } |
{ | ||
"name": "gulp-include", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"description": "Makes inclusion of files a breeze. Enables functionality similar to that of snockets / sprockets or other file insertion compilation tools.", | ||
@@ -5,0 +5,0 @@ "homepage": "http://github.com/wiledal/gulp-include", |
@@ -13,3 +13,3 @@ #gulp-include [![NPM version][npm-image]][npm-url] ![Travis build][travis-image] | ||
Then, add your _include-comments_ to your file. | ||
_People who have experience with `sprockets` or `snockets` will feel at home._ | ||
_People who have experience with `sprockets` or `snockets` will feel at home._ | ||
@@ -32,3 +32,3 @@ | ||
(Note: for those of you unfamiliar with the above syntax, check out https://github.com/isaacs/node-glob | ||
or http://gruntjs.com/configuring-tasks#globbing-patterns) | ||
or http://gruntjs.com/configuring-tasks#globbing-patterns) | ||
@@ -47,3 +47,3 @@ You can do all of this in any language, the only requirement is that the first character | ||
``` | ||
//= include included_file.js | ||
//= include included_file.js | ||
``` | ||
@@ -55,3 +55,3 @@ | ||
``` | ||
And so on recursively to an arbitrary depth. | ||
And so on recursively to an arbitrary depth. | ||
@@ -77,3 +77,3 @@ The example below compiles a several coffee-files and js-files into a single js-file: | ||
``` | ||
*Note:* The example above uses backsticks (\`) to allow `gulp-coffee` to compile inline javascript | ||
*Note:* The example above uses backticks (\`) to allow `gulp-coffee` to compile inline javascript | ||
@@ -103,2 +103,5 @@ `gulpfile.js`: | ||
## Release log | ||
#### 1.1.0 | ||
* Merged feature: Keep leading whitespaces by [maxgalbu](https://github.com/maxgalbu) | ||
#### 1.0.1 | ||
@@ -159,2 +162,2 @@ * Fixed issue which caused extensions to be "remembered" if `gulp-include` ran multiple times in a row, resulting in lost includes | ||
[npm-url]: https://npmjs.org/package/gulp-include | ||
[npm-image]: https://badge.fury.io/js/gulp-include.png | ||
[npm-image]: https://badge.fury.io/js/gulp-include.png |
@@ -219,3 +219,24 @@ var gutil = require("gulp-util"), | ||
}) | ||
it("Should retain origin file's leading whitespace", function (done) { | ||
var file = new gutil.File({ | ||
base: "test/fixtures/whitespace/", | ||
path: "test/fixtures/whitespace/a_origin.js", | ||
contents: fs.readFileSync("test/fixtures/whitespace/a_origin.js") | ||
}); | ||
var testInclude = include({ | ||
extensions: 'js' | ||
}) | ||
testInclude.on("data", function (newFile) { | ||
should.exist(newFile) | ||
should.exist(newFile.contents) | ||
String(newFile.contents).should.equal(String(fs.readFileSync('test/expected/whitespace_origin.js'), "utf8")) | ||
done() | ||
}); | ||
testInclude.write(file) | ||
}) | ||
it("Should work with css files", function (done) { | ||
@@ -222,0 +243,0 @@ var file = new gutil.File({ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
28509
46
518
157