Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for option.alias for define file load path. #133

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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");
});
});
});