From b13a009eadfd3a1f82a5ab9dc60d5b9a76289b1d Mon Sep 17 00:00:00 2001 From: Akimyou Date: Wed, 7 Mar 2018 14:35:31 +0800 Subject: [PATCH 1/2] added option.alias(dir path) support for define file load path --- lib/preprocess.js | 23 +++++++++++++++++++++-- test/options.spec.js | 9 ++++++++- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/lib/preprocess.js b/lib/preprocess.js index e401d29..cf7bc2b 100644 --- a/lib/preprocess.js +++ b/lib/preprocess.js @@ -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 @@ -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); @@ -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); diff --git a/test/options.spec.js b/test/options.spec.js index ac5aa2e..a1bb16e 100644 --- a/test/options.spec.js +++ b/test/options.spec.js @@ -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"); }); }); -}); \ No newline at end of file + + describe('and support alias(dir path) options', function () { + it('and use alias option', function () { + input = "ac"; + pp.preprocess(input, {}, { alias: { '@': 'test/fixtures/include/' } }).should.equal("a!bazqux!c"); + }); + }); +}); From 6de9b001edea7107f2bacd6bec136951680625d1 Mon Sep 17 00:00:00 2001 From: akimyou Date: Wed, 7 Mar 2018 15:37:39 +0800 Subject: [PATCH 2/2] add test for option.alias --- test/options.spec.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/test/options.spec.js b/test/options.spec.js index a1bb16e..50fdad1 100644 --- a/test/options.spec.js +++ b/test/options.spec.js @@ -1,6 +1,7 @@ 'use strict'; -var chai = require('chai'), +var path = require('path'), + chai = require('chai'), pp = require('../lib/preprocess'); chai.should(); @@ -66,9 +67,13 @@ describe('shall support multiple call signatures', function () { }); describe('and support alias(dir path) options', function () { - it('and use alias option', function () { + it('and use alias option with relative path', function () { input = "ac"; pp.preprocess(input, {}, { alias: { '@': 'test/fixtures/include/' } }).should.equal("a!bazqux!c"); }); + it('and use alias option with absolute path', function () { + input = "ac"; + pp.preprocess(input, {}, { alias: { '@': path.join(__dirname, 'fixtures/include/') } }).should.equal("a!bazqux!c"); + }); }); });