Skip to content
Open
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
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
16 changes: 14 additions & 2 deletions test/options.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

var chai = require('chai'),
var path = require('path'),
chai = require('chai'),
pp = require('../lib/preprocess');

chai.should();
Expand Down Expand Up @@ -64,4 +65,15 @@ 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 with relative path', function () {
input = "a<!--@include @static.txt-->c";
pp.preprocess(input, {}, { alias: { '@': 'test/fixtures/include/' } }).should.equal("a!bazqux!c");
});
it('and use alias option with absolute path', function () {
input = "a<!--@include @static.txt-->c";
pp.preprocess(input, {}, { alias: { '@': path.join(__dirname, 'fixtures/include/') } }).should.equal("a!bazqux!c");
});
});
});