From 80cdee25f3a22e8bbdf821294ac5de8976411618 Mon Sep 17 00:00:00 2001 From: Dirk Elmendorf Date: Sat, 1 Apr 2017 06:46:00 -0500 Subject: [PATCH] fix: outputPath function overriden by useRelativePath (#139) --- index.js | 17 +++++------- test/correct-filename.test.js | 52 ++++++++++++++++++++++++++++++++++- 2 files changed, 58 insertions(+), 11 deletions(-) diff --git a/index.js b/index.js index 54d9861..8d6cc43 100644 --- a/index.js +++ b/index.js @@ -36,14 +36,6 @@ module.exports = function(content) { }); var outputPath = ""; - if (config.outputPath) { - // support functions as outputPath to generate them dynamically - outputPath = ( - typeof config.outputPath === "function" - ? config.outputPath(url) - : config.outputPath - ); - } var filePath = this.resourcePath; if (config.useRelativePath) { @@ -56,8 +48,13 @@ module.exports = function(content) { outputPath = relativePath + url; } url = relativePath + url; - } else if (outputPath) { - outputPath = outputPath + url; + } else if (config.outputPath) { + // support functions as outputPath to generate them dynamically + outputPath = ( + typeof config.outputPath === "function" + ? config.outputPath(url) + : config.outputPath + url + ); url = outputPath; } else { outputPath = url; diff --git a/test/correct-filename.test.js b/test/correct-filename.test.js index 143eb86..129eb00 100644 --- a/test/correct-filename.test.js +++ b/test/correct-filename.test.js @@ -24,6 +24,29 @@ function run(resourcePath, query, content) { result: result } } +function run_with_options(resourcePath,options, content) { + content = content || new Buffer("1234"); + var file = null; + + var context = { + resourcePath: resourcePath, + options: { + "fileLoader": options, + context: "/this/is/the/context" + }, + emitFile: function(url, content2) { + content2.should.be.eql(content); + file = url; + } + }; + + var result = fileLoader.call(context, content) + + return { + file: file, + result: result + } +} function test(excepted, resourcePath, query, content) { run(resourcePath, query, content).file.should.be.eql(excepted); @@ -86,4 +109,31 @@ describe("useRelativePath option", function() { 'module.exports = __webpack_public_path__ + \"this/81dc9bdb52d04dc20036dbd8313ed055.txt\";' ); }); -}); \ No newline at end of file +}); +describe("outputPath function", function() { + it("should be supported", function() { + outputFunc = function(value) { + return("/path/set/by/func"); + + }; + var options = {}; + options.outputPath = outputFunc; + run_with_options("/this/is/the/context/file.txt", options).result.should.be.eql( + 'module.exports = __webpack_public_path__ + \"/path/set/by/func\";' + ); + + }); + it("should be ignored if you set useRelativePath", function() { + outputFunc = function(value) { + return("/path/set/by/func"); + + }; + var options = {}; + options.outputPath = outputFunc; + options.useRelativePath = true; + run_with_options("/this/is/the/context/file.txt", options).result.should.be.eql( + 'module.exports = __webpack_public_path__ + \"./81dc9bdb52d04dc20036dbd8313ed055.txt\";' + ); + + }); +});