Skip to content

Commit

Permalink
added option.alias(dir path) support for define file load path
Browse files Browse the repository at this point in the history
  • Loading branch information
Akimyou committed Mar 7, 2018
1 parent 704d289 commit b13a009
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
23 changes: 21 additions & 2 deletions lib/preprocess.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ function preprocess(src, context, typeOrOptions) {
fileNotFoundSilentFail: false,
srcDir: process.cwd(),
srcEol: getEolType(src),
type: delim['html']
type: delim['html'],
alias: {}
};

// needed for backward compatibility with 2.x.x series
Expand All @@ -82,6 +83,7 @@ function preprocess(src, context, typeOrOptions) {
options.fileNotFoundSilentFail = typeOrOptions.fileNotFoundSilentFail || options.fileNotFoundSilentFail;
options.srcEol = typeOrOptions.srcEol || options.srcEol;
options.type = delim[typeOrOptions.type] || options.type;
options.alias = typeOrOptions.alias || options.alias;
}

context = copy(context);
Expand Down Expand Up @@ -327,7 +329,24 @@ function processIncludeDirective(isStatic, context, opts, match, linePrefix, fil
var indent = linePrefix.replace(/\S/g, ' ');
var includedContext = copy(context);
var includedOpts = copy(opts);
includedContext.src = path.join(opts.srcDir,file);

var aliasKeys = Object.keys(opts.alias);
for (var i = 0; i < aliasKeys.length; i++) {
var aliasKey = aliasKeys[i];
var aliasKeyReg = new RegExp(aliasKey);
var aliasVal = opts.alias[aliasKey];

if (file.match(aliasKeyReg)) {
file = file.replace(aliasKeyReg, aliasVal);
break;
}
}

if (file[0] === '/' || file[0] === '\\') {
includedContext.src = file;
} else {
includedContext.src = path.join(opts.srcDir,file);
}
includedOpts.srcDir = path.dirname(includedContext.src);

var fileContents = getFileContents(includedContext.src, opts.fileNotFoundSilentFail, context.src);
Expand Down
9 changes: 8 additions & 1 deletion test/options.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,11 @@ describe('shall support multiple call signatures', function () {
pp.preprocess(input, {}, {srcEol: '\r\n', srcDir: 'test/fixtures/include'}).should.equal("a\r\n!bazqux!\r\nc");
});
});
});

describe('and support alias(dir path) options', function () {
it('and use alias option', function () {
input = "a<!--@include @static.txt-->c";
pp.preprocess(input, {}, { alias: { '@': 'test/fixtures/include/' } }).should.equal("a!bazqux!c");
});
});
});

0 comments on commit b13a009

Please sign in to comment.