Skip to content
Merged
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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ gulp.task("default", ["scripts"]);
* 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.

- `separateInputs` (optional)
* Boolean, `true` by default
* Set this to `false` if you want to process each input file independent, when executing "require" logic.
So, if file required several times inside one file (or inside required by it files), then dublicates will be ignored.
But when another file will begin processing, all information about required files from previuos file will be discarded.

#### Example options usage:
```js
Expand Down
17 changes: 12 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ module.exports = function (params) {
var SourceMapConsumer = require('source-map').SourceMapConsumer;

var extensions = null, // The extension to be searched after
includedFiles = [], // Keeping track of what files have been included
globalIncludedFiles = [], // For track of what files have been included over all files
includePaths = false, // The paths to be searched
hardFail = false; // Throw error when no match
hardFail = false, // Throw error when no match
separateInputs = false; // Process each input file separately when using `require` logic.

// Check for includepaths in the params
if (params.includePaths) {
Expand All @@ -27,6 +28,10 @@ module.exports = function (params) {
includePaths = params.includePaths;
}
}

if (params.separateInputs) {
separateInputs = true;
}

// Toggle error reporting
if (params.hardFail != undefined) {
Expand All @@ -38,6 +43,8 @@ module.exports = function (params) {
}

function include(file, callback) {
var includedFiles = separateInputs ? [] : globalIncludedFiles;

if (file.isNull()) {
return callback(null, file);
}
Expand All @@ -47,7 +54,7 @@ module.exports = function (params) {
}

if (file.isBuffer()) {
var result = processInclude(String(file.contents), file.path, file.sourceMap);
var result = processInclude(String(file.contents), file.path, file.sourceMap, includedFiles);
file.contents = new Buffer(result.content);

if (file.sourceMap && result.map) {
Expand All @@ -68,7 +75,7 @@ module.exports = function (params) {
callback(null, file);
}

function processInclude(content, filePath, sourceMap) {
function processInclude(content, filePath, sourceMap, includedFiles) {
var matches = content.match(/^(\s+)?(\/\/|\/\*|\#|\<\!\-\-)(\s+)?=(\s+)?(include|require)(.+$)/mg);
var relativeBasePath = path.dirname(filePath);

Expand Down Expand Up @@ -172,7 +179,7 @@ module.exports = function (params) {
// 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);
var result = processInclude(fileContents.toString(), globbedFilePath, sourceMap, includedFiles);
var resultContent = result.content;

if (sourceMap) {
Expand Down