From dc7ce5cc21bf2c49e913732598225b28a56244e5 Mon Sep 17 00:00:00 2001 From: Artem Arefiev Date: Thu, 20 Dec 2018 11:20:06 +0400 Subject: [PATCH 1/5] Add compiler output options support for hash --- lib/interpolateName.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/interpolateName.js b/lib/interpolateName.js index 30bcef2..dcc4d2e 100644 --- a/lib/interpolateName.js +++ b/lib/interpolateName.js @@ -64,12 +64,18 @@ function interpolateName(loaderContext, name, options) { } } let url = filename; + const compilationOutputOptions = loaderContext._compilation.outputOptions; if(content) { // Match hash template url = url .replace( /\[(?:([^:]+):)?hash(?::([a-z]+\d*))?(?::(\d+))?\]/ig, - (all, hashType, digestType, maxLength) => getHashDigest(content, hashType, digestType, parseInt(maxLength, 10)) + (all, hashType, digestType, maxLength) => { + const resultHashType = hashType || compilationOutputOptions.hashFunction; + const resultDigestType = digestType || compilationOutputOptions.hashDigest; + const resultMaxLength = maxLength && parseInt(maxLength, 10) || compilationOutputOptions.hashDigestLength; + getHashDigest(content, resultHashType, resultDigestType, resultMaxLength); + } ) .replace( /\[emoji(?::(\d+))?\]/ig, From 95e4c948b7118194ea5d667c4cb5a267b4baa7eb Mon Sep 17 00:00:00 2001 From: Artem Arefiev Date: Thu, 20 Dec 2018 11:45:02 +0400 Subject: [PATCH 2/5] Linter cleanup --- test/interpolateName.test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/interpolateName.test.js b/test/interpolateName.test.js index 621e105..dda96c8 100644 --- a/test/interpolateName.test.js +++ b/test/interpolateName.test.js @@ -48,12 +48,12 @@ describe("interpolateName()", () => { assert.throws( () => { const interpolatedName = loaderUtils.interpolateName( - { }, "[" + hashName + ":hash:base64:10]", {content:"a"} + { }, "[" + hashName + ":hash:base64:10]", { content: "a" } ); // if for any reason the system we're running on has a hash // algorithm matching any of our bogus names, at least make sure // the output is not the unmodified name: - assert(interpolatedName[0] !== '['); + assert(interpolatedName[0] !== "["); }, /digest method not supported/i ); From f7a43e015d9864aa14084e5c694be776d8578fba Mon Sep 17 00:00:00 2001 From: Artem Arefiev Date: Thu, 20 Dec 2018 12:01:53 +0400 Subject: [PATCH 3/5] Fix tests --- lib/interpolateName.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/interpolateName.js b/lib/interpolateName.js index dcc4d2e..b468e60 100644 --- a/lib/interpolateName.js +++ b/lib/interpolateName.js @@ -64,7 +64,7 @@ function interpolateName(loaderContext, name, options) { } } let url = filename; - const compilationOutputOptions = loaderContext._compilation.outputOptions; + const compilationOutputOptions = loaderContext._compilation && loaderContext._compilation.outputOptions || {}; if(content) { // Match hash template url = url @@ -74,7 +74,7 @@ function interpolateName(loaderContext, name, options) { const resultHashType = hashType || compilationOutputOptions.hashFunction; const resultDigestType = digestType || compilationOutputOptions.hashDigest; const resultMaxLength = maxLength && parseInt(maxLength, 10) || compilationOutputOptions.hashDigestLength; - getHashDigest(content, resultHashType, resultDigestType, resultMaxLength); + return getHashDigest(content, resultHashType, resultDigestType, resultMaxLength); } ) .replace( From cd7451069cdd1a0c0abc931bd95268696c237ffa Mon Sep 17 00:00:00 2001 From: Artem Arefiev Date: Thu, 20 Dec 2018 12:13:38 +0400 Subject: [PATCH 4/5] Add tests --- test/interpolateName.test.js | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/test/interpolateName.test.js b/test/interpolateName.test.js index dda96c8..c72b1e5 100644 --- a/test/interpolateName.test.js +++ b/test/interpolateName.test.js @@ -23,17 +23,22 @@ describe("interpolateName()", () => { } [ - ["/app/js/javascript.js", "js/[hash].script.[ext]", "test content", "js/9473fdd0d880a43c21b7778d34872157.script.js"], - ["/app/page.html", "html-[hash:6].html", "test content", "html-9473fd.html"], - ["/app/flash.txt", "[hash]", "test content", "9473fdd0d880a43c21b7778d34872157"], - ["/app/img/image.png", "[sha512:hash:base64:7].[ext]", "test content", "2BKDTjl.png"], - ["/app/dir/file.png", "[path][name].[ext]?[hash]", "test content", "/app/dir/file.png?9473fdd0d880a43c21b7778d34872157"], - ["/vendor/test/images/loading.gif", path => path.replace(/\/?vendor\/?/, ""), "test content", "test/images/loading.gif"], - ["/pathWith.period/filename.js", "js/[name].[ext]", "test content", "js/filename.js"], - ["/pathWith.period/filenameWithoutExt", "js/[name].[ext]", "test content", "js/filenameWithoutExt.bin"] + [{ resourcePath: "/app/js/javascript.js" }, "js/[hash].script.[ext]", "test content", "js/9473fdd0d880a43c21b7778d34872157.script.js"], + [{ resourcePath: "/app/page.html" }, "html-[hash:6].html", "test content", "html-9473fd.html"], + [{ resourcePath: "/app/flash.txt" }, "[hash]", "test content", "9473fdd0d880a43c21b7778d34872157"], + [{ resourcePath: "/app/img/image.png" }, "[sha512:hash:base64:7].[ext]", "test content", "2BKDTjl.png"], + [{ resourcePath: "/app/img/image.png", _compilation: { outputOptions: { + hashFunction: "sha512", + hashDigest: "base64", + hashDigestLength: 7 + } } }, "[sha512:hash:base64:7].[ext]", "test content", "2BKDTjl.png"], + [{ resourcePath: "/app/dir/file.png" }, "[path][name].[ext]?[hash]", "test content", "/app/dir/file.png?9473fdd0d880a43c21b7778d34872157"], + [{ resourcePath: "/vendor/test/images/loading.gif" }, path => path.replace(/\/?vendor\/?/, ""), "test content", "test/images/loading.gif"], + [{ resourcePath: "/pathWith.period/filename.js" }, "js/[name].[ext]", "test content", "js/filename.js"], + [{ resourcePath: "/pathWith.period/filenameWithoutExt" }, "js/[name].[ext]", "test content", "js/filenameWithoutExt.bin"] ].forEach(test => { - it("should interpolate " + test[0] + " " + test[1], () => { - const interpolatedName = loaderUtils.interpolateName({ resourcePath: test[0] }, test[1], { content: test[2] }); + it("should interpolate " + test[0].resourcePath + " " + test[1], () => { + const interpolatedName = loaderUtils.interpolateName(test[0], test[1], { content: test[2] }); assert.equal(interpolatedName, test[3]); }); }); From e67219339a5d5f2fa4a55197e41965f3bea4310b Mon Sep 17 00:00:00 2001 From: Artem Arefiev Date: Thu, 20 Dec 2018 18:06:24 +0400 Subject: [PATCH 5/5] Fix added test --- test/interpolateName.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/interpolateName.test.js b/test/interpolateName.test.js index c72b1e5..8ea984a 100644 --- a/test/interpolateName.test.js +++ b/test/interpolateName.test.js @@ -31,7 +31,7 @@ describe("interpolateName()", () => { hashFunction: "sha512", hashDigest: "base64", hashDigestLength: 7 - } } }, "[sha512:hash:base64:7].[ext]", "test content", "2BKDTjl.png"], + } } }, "[hash].[ext]", "test content", "2BKDTjl.png"], [{ resourcePath: "/app/dir/file.png" }, "[path][name].[ext]?[hash]", "test content", "/app/dir/file.png?9473fdd0d880a43c21b7778d34872157"], [{ resourcePath: "/vendor/test/images/loading.gif" }, path => path.replace(/\/?vendor\/?/, ""), "test content", "test/images/loading.gif"], [{ resourcePath: "/pathWith.period/filename.js" }, "js/[name].[ext]", "test content", "js/filename.js"],