diff --git a/README.md b/README.md index fd2a31e..09aa095 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/index.js b/index.js index b82001b..162089f 100644 --- a/index.js +++ b/index.js @@ -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) { @@ -27,6 +28,10 @@ module.exports = function (params) { includePaths = params.includePaths; } } + + if (params.separateInputs) { + separateInputs = true; + } // Toggle error reporting if (params.hardFail != undefined) { @@ -38,6 +43,8 @@ module.exports = function (params) { } function include(file, callback) { + var includedFiles = separateInputs ? [] : globalIncludedFiles; + if (file.isNull()) { return callback(null, file); } @@ -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) { @@ -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); @@ -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) {