gulp-include
Advanced tools
Comparing version 2.2.1 to 2.3.0
48
index.js
@@ -14,3 +14,4 @@ var fs = require('fs'), | ||
includedFiles = [], // Keeping track of what files have been included | ||
includePaths = false; // The paths to be searched | ||
includePaths = false, // The paths to be searched | ||
hardFail = false; // Throw error when no match | ||
@@ -22,3 +23,4 @@ module.exports = function (params) { | ||
includePaths = false; | ||
hardFail = false; | ||
// Check for includepaths in the params | ||
@@ -35,2 +37,7 @@ if (params.includePaths) { | ||
// Toggle error reporting | ||
if (params.hardFail != undefined) { | ||
hardFail = params.hardFail; | ||
} | ||
if (params.extensions) { | ||
@@ -125,3 +132,3 @@ extensions = typeof params.extensions === 'string' ? [params.extensions] : params.extensions; | ||
var split = includeCommand.split(" "); | ||
var currentLine; | ||
@@ -139,3 +146,3 @@ if (sourceMap) { | ||
} | ||
// SEARCHING STARTS HERE | ||
@@ -148,3 +155,3 @@ // Split the directive and the path | ||
var includePath = ""; | ||
if (includePaths != false) { | ||
@@ -154,3 +161,3 @@ // If includepaths are set, search in those folders | ||
includePath = includePaths[y] + "/" + split[1]; | ||
var globResults = glob.sync(includePath, {mark: true}); | ||
@@ -163,22 +170,21 @@ fileMatches = fileMatches.concat(globResults); | ||
var globResults = glob.sync(includePath, {mark: true}); | ||
if (globResults.length < 1) fileNotFoundError(includePath); | ||
fileMatches = globResults; | ||
} | ||
if (fileMatches.length < 1) fileNotFoundError(includePath); | ||
var replaceContent = ''; | ||
for (var y = 0; y < fileMatches.length; y++) { | ||
var globbedFilePath = fileMatches[y]; | ||
// If directive is of type "require" and file already included, skip to next. | ||
if (includeType == "require" && includedFiles.indexOf(globbedFilePath) > -1) continue; | ||
// If not in extensions, skip this file | ||
if (!inExtensions(globbedFilePath)) continue; | ||
if (!inExtensions(globbedFilePath)) continue; | ||
// Get file contents and apply recursive include on result | ||
// Unicode byte order marks are stripped from the start of included files | ||
var fileContents = stripBom(fs.readFileSync(globbedFilePath)); | ||
var result = processInclude(fileContents.toString(), globbedFilePath, sourceMap); | ||
@@ -245,3 +251,3 @@ var resultContent = result.content; | ||
} | ||
if (includedFiles.indexOf(globbedFilePath) == -1) includedFiles.push(globbedFilePath); | ||
@@ -278,3 +284,3 @@ | ||
} | ||
return {content: content, map: map ? map.toString() : null}; | ||
@@ -294,3 +300,11 @@ } | ||
function fileNotFoundError(includePath) { | ||
throw new gutil.PluginError('gulp-include', 'No files found matching ' + includePath); | ||
if (hardFail) { | ||
throw new gutil.PluginError('gulp-include', 'No files found matching ' + includePath); | ||
}else{ | ||
console.warn( | ||
gutil.colors.yellow('WARN: ') + | ||
gutil.colors.cyan('gulp-include') + | ||
' - no files found matching ' + includePath | ||
); | ||
} | ||
} | ||
@@ -297,0 +311,0 @@ |
{ | ||
"name": "gulp-include", | ||
"version": "2.2.1", | ||
"version": "2.3.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", |
@@ -40,15 +40,25 @@ #gulp-include [![NPM version][npm-image]][npm-url] ![Travis build][travis-image] | ||
- `extensions` (optional) | ||
* Takes a `String` or an `Array` of extensions, eg: `"js"` or `["js", "coffee"]` | ||
* Takes a `String` or an `Array` of extensions. | ||
eg: `"js"` or `["js", "coffee"]` | ||
* If set, all directives that does not match the extension(s) will be ignored | ||
- `includePaths` (optional) | ||
* Takes a `String` or an `Array` of paths, | ||
* Takes a `String` or an `Array` of paths. | ||
eg: `__dirname + "/node_modules"` or `[__dirname + "/assets/js", __dirname + "/bower_components"]` | ||
* If set, `gulp-include` will use these folders as base path when searching for files. | ||
- `hardFail` (optional) | ||
* Boolean, `false` by default | ||
* Set this to `true` if you want `gulp-include` to throw errors if a file does not match | ||
an include directive. | ||
* If set to `false` gulp include will not fail, but display warnings in the console. | ||
#### Example options usage: | ||
```js | ||
gulp.src("src/js/main.js") | ||
.pipe(include({ | ||
.pipe(include({ | ||
extensions: "js", | ||
hardFail: true, | ||
includePaths: [ | ||
@@ -64,3 +74,3 @@ __dirname + "/bower_components", | ||
`gulp-include` uses directives similar to `sprockets` or `snockets`. A _directive_ is a comment in your files that `gulp-include` recognizes as a command. | ||
Example directives: | ||
@@ -83,3 +93,3 @@ ```javascript | ||
The contents of the referenced file will replace the file. | ||
### `require` vs. `include` | ||
@@ -94,66 +104,5 @@ A file that is included with `require` will only be included if it has not been included before. Files included with `include` will _always_ be included. | ||
## Release log | ||
#### 2.2.1 | ||
* Now throws an error if glob match is unmet | ||
## Changelog | ||
For release notes see `CHANGELOG.md`. | ||
#### 2.2.0 | ||
* Added `includePaths` option | ||
#### 2.1.1 | ||
* Strip BOMs, by [dhedey](https://github.com/dhedey) | ||
* Improved HTML comment stripping, by [shdwjk](https://github.com/shdwjk) | ||
#### 2.1.0 | ||
* Merged sourcemap support by [vetruvet](https://github.com/vetruvet) | ||
* Merged support for html-comments by [jelmerdemaat](https://github.com/jelmerdemaat) | ||
#### 2.0.3 | ||
* Merged community fix by [shadow1runner](https://github.com/shadow1runner) | ||
#### 2.0.2 | ||
* Updated replace to support specials [Riim](https://github.com/Riim) | ||
#### 2.0.1 | ||
* Fixed an issue with indenting | ||
#### 2.0.0 | ||
* Core rewritten to be slimmer and more comprehensive. | ||
* `require` and `include` no longer work the same. `require` will only include a file that hasn't been included yet. See readme for details. | ||
* Tests have been rewritten based on the old ones, but also to fit the new functionality | ||
* Deprecated `require_tree` and `require_directory` as they serve little purpose. Use globs (`path/to/**/*.xxx`) instead. | ||
#### 1.1.1 | ||
* Merged community fix by [trolev](https://github.com/trolev) | ||
#### 1.1.0 | ||
* Merged feature: Keep leading whitespaces by [maxgalbu](https://github.com/maxgalbu) | ||
#### 1.0.1 | ||
* Fixed issue which caused extensions to be "remembered" if `gulp-include` ran multiple times in a row, resulting in lost includes | ||
#### 1.0.0 | ||
* Merged major refactoring by [scottmas](https://github.com/scottmas) - Many thanks! | ||
* Rewritten core (regex, replacing and file mashing) | ||
* Glob support | ||
* Recursive support | ||
* Respecting indentation of included files | ||
* Upping version to 1.0.0 - seems fitting after such a large refactor | ||
#### 0.2.3 | ||
* Merged community fixes by [platdesign](https://github.com/platdesign) and [cujojp](https://github.com/cujojp) | ||
#### 0.2.2 | ||
* Updated regex directive to not collide with other requireing plugins, like browserify ([cwacek](https://github.com/cwacek)) | ||
#### 0.2.1 | ||
* Changed replace-method to fix issue when requiring a file that contained special characters ([juanghurtado](https://github.com/juanghurtado)) | ||
#### 0.2.0 | ||
* Added `require_tree`/`include_tree` (Thanks to [juanghurtado](https://github.com/juanghurtado)!) | ||
* Method now takes an `extensions` param for controlling what types of files to include | ||
#### 0.1.0 | ||
* Basic include | ||
## Licence | ||
@@ -160,0 +109,0 @@ (MIT License) |
@@ -32,3 +32,3 @@ var gutil = require("gulp-util"), | ||
}); | ||
it("should keep whitespace when including", function (done) { | ||
@@ -51,3 +51,3 @@ var file = new gutil.File({ | ||
}); | ||
it("should include complex folder trees", function (done) { | ||
@@ -71,3 +71,3 @@ var file = new gutil.File({ | ||
}) | ||
it("should not REQUIRE a file twice", function (done) { | ||
@@ -90,3 +90,3 @@ var file = new gutil.File({ | ||
}); | ||
it("should pull files recursively", function (done) { | ||
@@ -109,3 +109,3 @@ var file = new gutil.File({ | ||
}); | ||
it("should only include files with the set extensions, if provided", function (done) { | ||
@@ -130,3 +130,3 @@ var file = new gutil.File({ | ||
}); | ||
it("should work with html-comments", function(done) { | ||
@@ -181,3 +181,3 @@ var file = new gutil.File({ | ||
}) | ||
it("should include from set includePaths", function(done) { | ||
@@ -189,3 +189,3 @@ var file = new gutil.File({ | ||
}); | ||
testInclude = include({ | ||
@@ -201,3 +201,3 @@ includePaths: [ | ||
should.exist(newFile.contents); | ||
String(newFile.contents).should.equal(String(fs.readFileSync("test/expected/js/include-path.js"), "utf8")) | ||
@@ -208,2 +208,37 @@ done(); | ||
}) | ||
it("should throw an error if no match is found with hardFail: true", function(done) { | ||
var file = new gutil.File({ | ||
base: "test/fixtures/", | ||
path: "test/fixtures/js/include-fail.js", | ||
contents: fs.readFileSync("test/fixtures/js/include-fail.js") | ||
}); | ||
testInclude = include({ | ||
hardFail: true | ||
}); | ||
testInclude.on("error", function(err) { | ||
if (err) done(); | ||
}); | ||
testInclude.write(file); | ||
}) | ||
it("should not throw an error if no match is found with hardFail: false", function(done) { | ||
var file = new gutil.File({ | ||
base: "test/fixtures/", | ||
path: "test/fixtures/js/include-fail.js", | ||
contents: fs.readFileSync("test/fixtures/js/include-fail.js") | ||
}); | ||
testInclude = include({ | ||
hardFail: false | ||
}); | ||
testInclude.on("error", function(err) { | ||
done(err); | ||
}); | ||
testInclude.on("data", function(newFile) { | ||
done(); | ||
}); | ||
testInclude.write(file); | ||
}) | ||
}) |
29437
39
554
132