From 4dd3d423c5277369df61d69062c0597fc78a25d6 Mon Sep 17 00:00:00 2001 From: Thomas Berger Date: Fri, 17 Feb 2017 15:24:21 +0100 Subject: [PATCH] add support for autoExtensions this allows us to use gulp-include with some sprockets based projects, like bootstrap-sass --- README.md | 5 +++++ index.js | 18 +++++++++++++++--- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index fd2a31e..46015ed 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,11 @@ gulp.task("default", ["scripts"]); eg: `"js"` or `["js", "coffee"]` * If set, all directives that does not match the extension(s) will be ignored +- `autoExtensions` (optional) + * Takes a `String` or an `Array` of extensions. + eg: `"js"` or `["js", "coffee"]` + * If set, the extensions will be added to the search pattern when looking for files + eg: `//= require ./somescript` will resolve to `./somescript{,.js,.coffee}` - `includePaths` (optional) * Takes a `String` or an `Array` of paths. diff --git a/index.js b/index.js index b82001b..6348c96 100644 --- a/index.js +++ b/index.js @@ -15,7 +15,8 @@ module.exports = function (params) { var extensions = null, // The extension to be searched after includedFiles = [], // Keeping track of what files have been included includePaths = false, // The paths to be searched - hardFail = false; // Throw error when no match + hardFail = false, // Throw error when no match + autoExtensions = false; // additional extensions added to the filename for search // Check for includepaths in the params if (params.includePaths) { @@ -37,6 +38,11 @@ module.exports = function (params) { extensions = typeof params.extensions === 'string' ? [params.extensions] : params.extensions; } + if (params.autoExtensions) { + autoExtensions = typeof params.autoExtensions === 'string' ? [params.autoExtensions] : params.autoExtensions; + autoExtensions = autoExtensions.map(function (ext) { return '.' + ext }) + } + function include(file, callback) { if (file.isNull()) { return callback(null, file); @@ -136,22 +142,28 @@ module.exports = function (params) { // SEARCHING STARTS HERE // Split the directive and the path var includeType = split[0]; + var fileName = split[1]; // Use glob for file searching var fileMatches = []; var includePath = ""; + // add the extensions from autoExtensions if any + if (autoExtensions != false) { + fileName = fileName + '{,' + autoExtensions.join(',') + '}'; + } + if (includePaths != false) { // If includepaths are set, search in those folders for (var y = 0; y < includePaths.length; y++) { - includePath = includePaths[y] + "/" + split[1]; + includePath = includePaths[y] + "/" + fileName; var globResults = glob.sync(includePath, {mark: true}); fileMatches = fileMatches.concat(globResults); } }else{ // Otherwise search relatively - includePath = relativeBasePath + "/" + split[1]; + includePath = relativeBasePath + "/" + fileName; var globResults = glob.sync(includePath, {mark: true}); fileMatches = globResults; }