From 987e8af6e0ca7c003d69b15c6cbecf29ccedf080 Mon Sep 17 00:00:00 2001 From: eliot-akira Date: Wed, 13 Jan 2016 05:20:37 +0100 Subject: [PATCH] add option to use source file extension as default for import/extend --- lib/preprocess.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/preprocess.js b/lib/preprocess.js index 1d10bd7..9b392ad 100644 --- a/lib/preprocess.js +++ b/lib/preprocess.js @@ -59,6 +59,7 @@ function preprocess(src, context, typeOrOptions) { // default values var options = { fileNotFoundSilentFail: false, + defaultSourceExtension: false, srcDir: process.cwd(), srcEol: getEolType(src), type: delim['html'] @@ -80,12 +81,12 @@ function preprocess(src, context, typeOrOptions) { if (typeOrOptions && typeof typeOrOptions === 'object') { options.srcDir = typeOrOptions.srcDir || options.srcDir; options.fileNotFoundSilentFail = typeOrOptions.fileNotFoundSilentFail || options.fileNotFoundSilentFail; + options.defaultSourceExtension = typeOrOptions.defaultSourceExtension || options.defaultSourceExtension; options.srcEol = typeOrOptions.srcEol || options.srcEol; options.type = delim[typeOrOptions.type] || options.type; } context = copy(context); - return preprocessor(src, context, options); } @@ -101,6 +102,9 @@ function preprocessor(src, context, opts, noRestoreEol) { var file = (startMatches[1] || '').trim(); var extendedContext = copy(context); var extendedOpts = copy(opts); + if (file.indexOf('.') < 0 && opts.defaultSourceExtension && extendedContext.src) { + file += '.'+getExtension(extendedContext.src); + } extendedContext.src = path.join(opts.srcDir, file); extendedOpts.srcDir = path.dirname(extendedContext.src); @@ -328,9 +332,12 @@ function processIncludeDirective(isStatic, context, opts, match, linePrefix, fil var indent = linePrefix.replace(/\S/g, ' '); var includedContext = copy(context); var includedOpts = copy(opts); + if (file.indexOf('.') < 0 && opts.defaultSourceExtension && context.src) { + file += '.'+getExtension(context.src); + } + includedContext.src = path.join(opts.srcDir,file); includedOpts.srcDir = path.dirname(includedContext.src); - var fileContents = getFileContents(includedContext.src, opts.fileNotFoundSilentFail, context.src); if (fileContents.error) { return linePrefix + fileContents.contents;